@openglobus/og 0.13.8 → 0.13.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/README.md +1 -1
  2. package/css/og.css +388 -129
  3. package/dist/@openglobus/og.css +1 -1
  4. package/dist/@openglobus/og.esm.js +2 -2
  5. package/dist/@openglobus/og.esm.js.map +1 -1
  6. package/dist/@openglobus/og.umd.js +2 -2
  7. package/dist/@openglobus/og.umd.js.map +1 -1
  8. package/package.json +20 -19
  9. package/src/og/Globe.js +15 -16
  10. package/src/og/Object3d.js +33 -0
  11. package/src/og/control/GeoImageDragControl.js +102 -75
  12. package/src/og/control/LayerAnimation.js +306 -38
  13. package/src/og/control/LayerSwitcher.js +506 -159
  14. package/src/og/control/Lighting.js +27 -27
  15. package/src/og/control/RulerSwitcher.js +69 -0
  16. package/src/og/control/UIhelpers.js +146 -0
  17. package/src/og/control/index.js +3 -1
  18. package/src/og/control/ruler/RulerScene.js +6 -2
  19. package/src/og/control/visibleExtent/Segment.js +2 -1
  20. package/src/og/ellipsoid/wgs84.js +1 -1
  21. package/src/og/entity/LabelHandler.js +2 -2
  22. package/src/og/entity/LabelWorker.js +36 -54
  23. package/src/og/layer/BaseGeoImage.js +347 -312
  24. package/src/og/layer/CanvasTiles.js +312 -290
  25. package/src/og/layer/GeoImage.js +222 -214
  26. package/src/og/layer/GeoVideo.js +291 -291
  27. package/src/og/layer/Layer.js +71 -30
  28. package/src/og/layer/Material.js +109 -109
  29. package/src/og/layer/Vector.js +1 -0
  30. package/src/og/layer/XYZ.js +20 -12
  31. package/src/og/light/LightSource.js +364 -363
  32. package/src/og/math.js +2 -2
  33. package/src/og/quadTree/Node.js +7 -5
  34. package/src/og/renderer/Renderer.js +1 -1
  35. package/src/og/scene/Planet.js +103 -42
  36. package/src/og/segment/Segment.js +34 -56
  37. package/src/og/segment/Slice.js +1 -1
  38. package/src/og/shaders/drawnode.js +1 -1
  39. package/src/og/terrain/GlobusTerrain.js +31 -20
  40. package/src/og/terrain/MapboxTerrain.js +1 -1
  41. package/src/og/utils/BaseWorker.js +46 -0
  42. package/src/og/utils/GeoImageCreator.js +164 -161
  43. package/src/og/utils/Loader.js +60 -33
  44. package/src/og/utils/NormalMapCreator.js +403 -412
  45. package/src/og/utils/PlainSegmentWorker.js +29 -55
  46. package/src/og/utils/TerrainWorker.js +572 -589
  47. package/src/og/utils/functionComposition.js +13 -0
  48. package/src/og/webgl/Handler.js +42 -41
  49. package/types/Object3d.d.ts +1 -0
  50. package/types/camera/PlanetCamera.d.ts +4 -0
  51. package/types/control/GeoImageDragControl.d.ts +2 -0
  52. package/types/control/LayerAnimation.d.ts +73 -0
  53. package/types/control/LayerSwitcher.d.ts +55 -11
  54. package/types/control/MouseNavigation.d.ts +1 -0
  55. package/types/control/MouseWheelZoomControl.d.ts +1 -0
  56. package/types/control/Sun.d.ts +1 -0
  57. package/types/control/TouchNavigation.d.ts +1 -0
  58. package/types/control/UIhelpers.d.ts +13 -0
  59. package/types/control/index.d.ts +2 -1
  60. package/types/entity/LabelHandler.d.ts +2 -0
  61. package/types/entity/LabelWorker.d.ts +5 -7
  62. package/types/layer/BaseGeoImage.d.ts +9 -1
  63. package/types/layer/CanvasTiles.d.ts +5 -1
  64. package/types/layer/GeoImage.d.ts +1 -0
  65. package/types/layer/GeoTexture2d.d.ts +1 -0
  66. package/types/layer/Layer.d.ts +5 -1
  67. package/types/layer/Vector.d.ts +7 -0
  68. package/types/layer/WMS.d.ts +1 -0
  69. package/types/layer/XYZ.d.ts +8 -6
  70. package/types/math.d.ts +1 -1
  71. package/types/quadTree/EntityCollectionNode.d.ts +7 -0
  72. package/types/quadTree/Node.d.ts +1 -2
  73. package/types/renderer/Renderer.d.ts +1 -1
  74. package/types/renderer/RendererEvents.d.ts +16 -0
  75. package/types/scene/Planet.d.ts +19 -4
  76. package/types/segment/SegmentLonLat.d.ts +2 -0
  77. package/types/terrain/BilTerrain.d.ts +6 -0
  78. package/types/terrain/GlobusTerrain.d.ts +7 -0
  79. package/types/terrain/MapboxTerrain.d.ts +8 -0
  80. package/types/utils/BaseWorker.d.ts +14 -0
  81. package/types/utils/Loader.d.ts +3 -1
  82. package/types/utils/PlainSegmentWorker.d.ts +4 -6
  83. package/types/utils/TerrainWorker.d.ts +4 -6
  84. package/types/utils/functionComposition.d.ts +5 -0
  85. package/types/webgl/Handler.d.ts +4 -1
@@ -1,590 +1,573 @@
1
- 'use strict';
2
-
3
- // import { QueueArray } from '../QueueArray.js';
4
-
5
- class TerrainWorker {
6
- constructor(numWorkers = 2) {
7
- this._id = 0;
8
- this._segments = {};
9
-
10
- this._workerQueue = [];//new QueueArray(numWorkers);
11
- var elevationProgramm = new Blob([_programm], { type: 'application/javascript' });
12
-
13
- var that = this;
14
-
15
- for (let i = 0; i < numWorkers; i++) {
16
- var w = new Worker(URL.createObjectURL(elevationProgramm));
17
- w.onmessage = function (e) {
18
-
19
- that._segments[e.data.id]._terrainWorkerCallback(e.data);
20
- that._segments[e.data.id] = null;
21
-
22
- e.data.normalMapNormals = null;
23
- e.data.normalMapVertices = null;
24
- e.data.normalMapVerticesHigh = null;
25
- e.data.normalMapVerticesLow = null;
26
- e.data.terrainVertices = null;
27
- e.data.terrainVerticesHigh = null;
28
- e.data.terrainVerticesLow = null;
29
-
30
- delete that._segments[e.data.id];
31
-
32
- that._workerQueue.unshift(this);
33
- that.check();
34
- };
35
-
36
- this._workerQueue.push(w);
37
- }
38
-
39
- this._pendingQueue = [];//new QueueArray(512);
40
- }
41
-
42
- check() {
43
- if (this._pendingQueue.length) {
44
- var p = this._pendingQueue.pop();
45
- this.make(p.segment, p.elevations);
46
- }
47
- }
48
-
49
- make(segment, elevations) {
50
-
51
- if (segment.plainReady && segment.terrainIsLoading) {
52
-
53
- var _elevations = new Float32Array(elevations.length);
54
- _elevations.set(elevations);
55
-
56
- if (this._workerQueue.length) {
57
-
58
- var w = this._workerQueue.pop();
59
-
60
- this._segments[this._id] = segment;
61
-
62
- w.postMessage({
63
- 'elevations': _elevations,
64
- 'this_plainVertices': segment.plainVertices,
65
- 'this_plainNormals': segment.plainNormals,
66
- 'this_normalMapVertices': segment.normalMapVertices,
67
- 'this_normalMapNormals': segment.normalMapNormals,
68
- 'heightFactor': segment.planet._heightFactor,
69
- 'gridSize': segment.planet.terrain.gridSizeByZoom[segment.tileZoom],
70
- 'noDataValues': segment.planet.terrain.noDataValues,
71
- 'id': this._id++
72
- }, [
73
- _elevations.buffer,
74
- segment.plainVertices.buffer,
75
- segment.plainNormals.buffer,
76
- segment.normalMapVertices.buffer,
77
- segment.normalMapNormals.buffer
78
- ]);
79
-
80
- } else {
81
- this._pendingQueue.push({ 'segment': segment, 'elevations': _elevations });
82
- }
83
- } else {
84
- this.check();
85
- }
86
- }
87
- }
88
-
89
- const _programm =
90
- `'use strict';
91
- //
92
- //Terrain worker
93
- //
94
-
95
- function binarySearchFast(arr, x) {
96
- let start = 0,
97
- end = arr.length - 1;
98
- while (start <= end) {
99
- let k = Math.floor((start + end) * 0.5);
100
- if (arr[k] === x)
101
- return k;
102
- else if (arr[k] < x)
103
- start = k + 1;
104
- else
105
- end = k - 1;
106
- }
107
- return -1;
108
- };
109
-
110
- function checkNoDataValue(noDataValues, value) {
111
- return binarySearchFast(noDataValues, value) !== -1;
112
- };
113
-
114
-
115
- var Vec3 = function(x, y, z) {
116
- this.x = x;
117
- this.y = y;
118
- this.z = z;
119
- };
120
-
121
- var doubleToTwoFloats = function(v, high, low) {
122
-
123
- let x = v.x, y = v.y, z = v.z;
124
-
125
- if (x >= 0.0) {
126
- var doubleHigh = Math.floor(x / 65536.0) * 65536.0;
127
- high.x = Math.fround(doubleHigh);
128
- low.x = Math.fround(x - doubleHigh);
129
- } else {
130
- var doubleHigh = Math.floor(-x / 65536.0) * 65536.0;
131
- high.x = Math.fround(-doubleHigh);
132
- low.x = Math.fround(x + doubleHigh);
133
- }
134
-
135
- if (y >= 0.0) {
136
- var doubleHigh = Math.floor(y / 65536.0) * 65536.0;
137
- high.y = Math.fround(doubleHigh);
138
- low.y = Math.fround(y - doubleHigh);
139
- } else {
140
- var doubleHigh = Math.floor(-y / 65536.0) * 65536.0;
141
- high.y = Math.fround(-doubleHigh);
142
- low.y = Math.fround(y + doubleHigh);
143
- }
144
-
145
- if (z >= 0.0) {
146
- var doubleHigh = Math.floor(z / 65536.0) * 65536.0;
147
- high.z = Math.fround(doubleHigh);
148
- low.z = Math.fround(z - doubleHigh);
149
- } else {
150
- var doubleHigh = Math.floor(-z / 65536.0) * 65536.0;
151
- high.z = Math.fround(-doubleHigh);
152
- low.z = Math.fround(z + doubleHigh);
153
- }
154
- };
155
-
156
- Vec3.prototype.sub = function(v) {
157
- return new Vec3(this.x - v.x, this.y - v.y, this.z - v.z);
158
- };
159
-
160
- Vec3.prototype.add = function(v) {
161
- return new Vec3(this.x + v.x, this.y + v.y, this.z + v.z);
162
- };
163
-
164
- Vec3.prototype.cross = function(v) {
165
- return new Vec3(
166
- this.y * v.z - this.z * v.y,
167
- this.z * v.x - this.x * v.z,
168
- this.x * v.y - this.y * v.x
169
- );
170
- };
171
-
172
- Vec3.prototype.normalize = function(v) {
173
- var x = this.x, y = this.y, z = this.z;
174
- var length = 1.0 / Math.sqrt(x * x + y * y + z * z);
175
- this.x = x * length;
176
- this.y = y * length;
177
- this.z = z * length;
178
- return this;
179
- };
180
-
181
- Vec3.prototype.distance = function(v) {
182
- return this.sub(v).length();
183
- };
184
-
185
- Vec3.prototype.length = function () {
186
- return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
187
- };
188
-
189
- var blerp = function(x, y, fQ11, fQ21, fQ12, fQ22) {
190
- return (fQ11 * (1.0 - x) * (1.0 - y) + fQ21 * x * (1.0 - y) + fQ12 * (1.0 - x) * y + fQ22 * x * y);
191
- };
192
-
193
- var slice = function (t, h1, h0) {
194
- return t * (h1 - h0);
195
- };
196
-
197
- var _tempVec = new Vec3(0.0, 0.0, 0.0);
198
-
199
- var _tempHigh = new Vec3(0.0, 0.0, 0.0),
200
- _tempLow = new Vec3(0.0, 0.0, 0.0);
201
-
202
- self.onmessage = function (e) {
203
- var elevations = e.data.elevations,
204
- this_plainVertices = e.data.this_plainVertices,
205
- this_plainNormals = e.data.this_plainNormals,
206
- this_normalMapVertices = e.data.this_normalMapVertices,
207
- this_normalMapNormals = e.data.this_normalMapNormals,
208
- heightFactor = e.data.heightFactor,
209
- gridSize = e.data.gridSize,
210
- noDataValues = e.data.noDataValues,
211
- id = e.data.id;
212
-
213
- var xmin = 549755748352.0, xmax = -549755748352.0,
214
- ymin = 549755748352.0, ymax = -549755748352.0,
215
- zmin = 549755748352.0, zmax = -549755748352.0;
216
-
217
- const fileGridSize = Math.sqrt(elevations.length) - 1;
218
-
219
- const fileGridSize_one = fileGridSize + 1;
220
- const fileGridSize_one_x2 = fileGridSize_one * fileGridSize_one;
221
- const tgs = gridSize;
222
- const dg = fileGridSize / tgs;
223
- const gs = tgs + 1;
224
- const hf = heightFactor;
225
-
226
- var nmvInd = 0,
227
- vInd = 0,
228
- noDataInd = 0;
229
-
230
- var gsgs3 = gs * gs * 3;
231
-
232
- var terrainVertices = new Float64Array(gsgs3),
233
- terrainVerticesHigh = new Float32Array(gsgs3),
234
- terrainVerticesLow = new Float32Array(gsgs3),
235
- noDataVertices = new Uint8Array(gs * gs);
236
-
237
- var normalMapNormals,
238
- normalMapVertices,
239
- normalMapVerticesHigh,
240
- normalMapVerticesLow;
241
-
242
- var nv = this_normalMapVertices,
243
- nn = this_normalMapNormals;
244
-
245
- if (fileGridSize >= tgs) {
246
-
247
- normalMapNormals = new Float32Array(fileGridSize_one_x2 * 3);
248
- normalMapVertices = new Float64Array(fileGridSize_one_x2 * 3);
249
- normalMapVerticesHigh = new Float32Array(fileGridSize_one_x2 * 3);
250
- normalMapVerticesLow = new Float32Array(fileGridSize_one_x2 * 3);
251
-
252
- for (var k = 0; k < fileGridSize_one_x2; k++) {
253
-
254
- var j = k % fileGridSize_one,
255
- i = ~~(k / fileGridSize_one);
256
-
257
- //
258
- // V0
259
- //
260
- var hInd0 = k;
261
- var vInd0 = hInd0 * 3;
262
- var currElv = elevations[hInd0];
263
- if(checkNoDataValue(noDataValues, currElv)) {
264
- currElv = 0.0;
265
- }
266
- var h0 = hf * currElv;
267
- var v0 = new Vec3(nv[vInd0] + h0 * nn[vInd0], nv[vInd0 + 1] + h0 * nn[vInd0 + 1], nv[vInd0 + 2] + h0 * nn[vInd0 + 2]);
268
-
269
- doubleToTwoFloats(v0, _tempHigh, _tempLow);
270
-
271
- normalMapVertices[vInd0] = v0.x;
272
- normalMapVertices[vInd0 + 1] = v0.y;
273
- normalMapVertices[vInd0 + 2] = v0.z;
274
-
275
- normalMapVerticesHigh[vInd0] = _tempHigh.x;
276
- normalMapVerticesHigh[vInd0 + 1] = _tempHigh.y;
277
- normalMapVerticesHigh[vInd0 + 2] = _tempHigh.z;
278
-
279
- normalMapVerticesLow[vInd0] = _tempLow.x;
280
- normalMapVerticesLow[vInd0 + 1] = _tempLow.y;
281
- normalMapVerticesLow[vInd0 + 2] = _tempLow.z;
282
-
283
- //
284
- // The vertex goes into screen buffer
285
- if (i % dg === 0 && j % dg === 0) {
286
-
287
- let currVert = new Vec3(nv[vInd0], nv[vInd0 + 1], nv[vInd0 + 2]);
288
- let nextVert = new Vec3(nv[vInd0 + 3], nv[vInd0 + 4], nv[vInd0 + 5]);
289
-
290
- let nextElv = elevations[hInd0 + 1];
291
- if(checkNoDataValue(noDataValues, nextElv)) {
292
- nextElv = 0.0;
293
- }
294
-
295
- let eps = false;
296
- if(noDataValues.length === 0){
297
- let step = currVert.distance(nextVert);
298
- let deltaElv = Math.abs(currElv - nextElv);
299
- eps = ((deltaElv / step) > 10.0) || (currElv < -5000);
300
- }
301
-
302
- if(eps){
303
- noDataVertices[noDataInd] = 1;
304
- } else {
305
- noDataVertices[noDataInd] = 0;
306
- if (v0.x < xmin) xmin = v0.x; if (v0.x > xmax) xmax = v0.x;
307
- if (v0.y < ymin) ymin = v0.y; if (v0.y > ymax) ymax = v0.y;
308
- if (v0.z < zmin) zmin = v0.z; if (v0.z > zmax) zmax = v0.z;
309
- }
310
-
311
- terrainVerticesHigh[vInd] = _tempHigh.x;
312
- terrainVerticesLow[vInd] = _tempLow.x;
313
- terrainVertices[vInd++] = v0.x;
314
-
315
- terrainVerticesHigh[vInd] = _tempHigh.y;
316
- terrainVerticesLow[vInd] = _tempLow.y;
317
- terrainVertices[vInd++] = v0.y;
318
-
319
- terrainVerticesHigh[vInd] = _tempHigh.z;
320
- terrainVerticesLow[vInd] = _tempLow.z;
321
- terrainVertices[vInd++] = v0.z;
322
-
323
- noDataInd++;
324
- }
325
-
326
- if (i !== fileGridSize && j !== fileGridSize) {
327
-
328
- //
329
- // V1
330
- //
331
- var hInd1 = k + 1;
332
- var vInd1 = hInd1 * 3;
333
- var elv = elevations[hInd1];
334
- if(checkNoDataValue(noDataValues, elv)) {
335
- elv = 0.0;
336
- }
337
- var h1 = hf * elv;
338
- var v1 = new Vec3(nv[vInd1] + h1 * nn[vInd1], nv[vInd1 + 1] + h1 * nn[vInd1 + 1], nv[vInd1 + 2] + h1 * nn[vInd1 + 2]);
339
-
340
- doubleToTwoFloats(v1, _tempHigh, _tempLow);
341
-
342
- normalMapVertices[vInd1] = v1.x;
343
- normalMapVertices[vInd1 + 1] = v1.y;
344
- normalMapVertices[vInd1 + 2] = v1.z;
345
-
346
- normalMapVerticesHigh[vInd1] = _tempHigh.x;
347
- normalMapVerticesHigh[vInd1 + 1] = _tempHigh.y;
348
- normalMapVerticesHigh[vInd1 + 2] = _tempHigh.z;
349
-
350
- normalMapVerticesLow[vInd1] = _tempLow.x;
351
- normalMapVerticesLow[vInd1 + 1] = _tempLow.y;
352
- normalMapVerticesLow[vInd1 + 2] = _tempLow.z;
353
-
354
- //
355
- // V2
356
- //
357
- var hInd2 = k + fileGridSize_one;
358
- var vInd2 = hInd2 * 3;
359
- var elv = elevations[hInd2];
360
- if(checkNoDataValue(noDataValues, elv)) {
361
- elv = 0.0;
362
- }
363
- var h2 = hf * elv;
364
- var v2 = new Vec3(nv[vInd2] + h2 * nn[vInd2], nv[vInd2 + 1] + h2 * nn[vInd2 + 1], nv[vInd2 + 2] + h2 * nn[vInd2 + 2]);
365
-
366
- doubleToTwoFloats(v2, _tempHigh, _tempLow);
367
-
368
- normalMapVertices[vInd2] = v2.x;
369
- normalMapVertices[vInd2 + 1] = v2.y;
370
- normalMapVertices[vInd2 + 2] = v2.z;
371
-
372
- normalMapVerticesHigh[vInd2] = _tempHigh.x;
373
- normalMapVerticesHigh[vInd2 + 1] = _tempHigh.y;
374
- normalMapVerticesHigh[vInd2 + 2] = _tempHigh.z;
375
-
376
- normalMapVerticesLow[vInd2] = _tempLow.x;
377
- normalMapVerticesLow[vInd2 + 1] = _tempLow.y;
378
- normalMapVerticesLow[vInd2 + 2] = _tempLow.z;
379
-
380
- //
381
- // V3
382
- //
383
- var hInd3 = k + fileGridSize_one + 1;
384
- var vInd3 = hInd3 * 3;
385
- var elv = elevations[hInd3];
386
- if(checkNoDataValue(noDataValues, elv)) {
387
- elv = 0.0;
388
- }
389
- var h3 = hf * elv;
390
- var v3 = new Vec3(nv[vInd3] + h3 * nn[vInd3], nv[vInd3 + 1] + h3 * nn[vInd3 + 1], nv[vInd3 + 2] + h3 * nn[vInd3 + 2]);
391
-
392
- doubleToTwoFloats(v3, _tempHigh, _tempLow);
393
-
394
- normalMapVertices[vInd3] = v3.x;
395
- normalMapVertices[vInd3 + 1] = v3.y;
396
- normalMapVertices[vInd3 + 2] = v3.z;
397
-
398
- normalMapVerticesHigh[vInd3] = _tempHigh.x;
399
- normalMapVerticesHigh[vInd3 + 1] = _tempHigh.y;
400
- normalMapVerticesHigh[vInd3 + 2] = _tempHigh.z;
401
-
402
- normalMapVerticesLow[vInd3] = _tempLow.x;
403
- normalMapVerticesLow[vInd3 + 1] = _tempLow.y;
404
- normalMapVerticesLow[vInd3 + 2] = _tempLow.z;
405
-
406
- //
407
- // Normal
408
- //
409
- var e10 = v1.sub(v0),
410
- e20 = v2.sub(v0),
411
- e30 = v3.sub(v0);
412
- var sw = e20.cross(e30).normalize();
413
- var ne = e30.cross(e10).normalize();
414
- var n0 = ne.add(sw).normalize();
415
-
416
- normalMapNormals[vInd0] += n0.x;
417
- normalMapNormals[vInd0 + 1] += n0.y;
418
- normalMapNormals[vInd0 + 2] += n0.z;
419
-
420
- normalMapNormals[vInd1] += ne.x;
421
- normalMapNormals[vInd1 + 1] += ne.y;
422
- normalMapNormals[vInd1 + 2] += ne.z;
423
-
424
- normalMapNormals[vInd2] += sw.x;
425
- normalMapNormals[vInd2 + 1] += sw.y;
426
- normalMapNormals[vInd2 + 2] += sw.z;
427
-
428
- normalMapNormals[vInd3] += n0.x;
429
- normalMapNormals[vInd3 + 1] += n0.y;
430
- normalMapNormals[vInd3 + 2] += n0.z;
431
- }
432
- }
433
-
434
- } else {
435
-
436
- normalMapNormals = new Float32Array(gsgs3);
437
- normalMapVertices = new Float64Array(gsgs3);
438
- normalMapVerticesHigh = new Float32Array(gsgs3);
439
- normalMapVerticesLow = new Float32Array(gsgs3);
440
- normalMapNormals = new Float32Array(gsgs3);
441
-
442
- var oneSize = tgs / fileGridSize;
443
- var h, inside_i, inside_j, v_i, v_j;
444
- var gsgs = gsgs3 / 3;
445
- var fgsOne = fileGridSize + 1;
446
-
447
- for(let i = 0; i < gsgs; i++) {
448
- let ii = Math.floor(i / gs),
449
- ij = i % gs;
450
-
451
- let qii = ii % oneSize,
452
- qij = ij % oneSize;
453
-
454
- let hlt_ind = Math.floor(ii / oneSize) * fgsOne + Math.floor(ij / oneSize);
455
-
456
- if (ij === tgs) {
457
- hlt_ind -= 1;
458
- qij = oneSize;
459
- }
460
-
461
- if (ii === tgs) {
462
- hlt_ind -= fgsOne;
463
- qii = oneSize;
464
- }
465
-
466
- let hrt_ind = hlt_ind + 1,
467
- hlb_ind = hlt_ind + fgsOne,
468
- hrb_ind = hlb_ind + 1;
469
-
470
- let h_lt = elevations[hlt_ind],
471
- h_rt = elevations[hrt_ind],
472
- h_lb = elevations[hlb_ind],
473
- h_rb = elevations[hrb_ind];
474
-
475
- if(checkNoDataValue(noDataValues, h_lt)) {
476
- h_lt = 0.0;
477
- }
478
-
479
- if(checkNoDataValue(noDataValues, h_rt)) {
480
- h_rt = 0.0;
481
- }
482
-
483
- if(checkNoDataValue(noDataValues, h_lb)) {
484
- h_lb = 0.0;
485
- }
486
-
487
- if(checkNoDataValue(noDataValues, h_rb)) {
488
- h_rb = 0.0;
489
- }
490
-
491
- let hi = blerp(qij / oneSize, qii / oneSize, h_lt, h_rt, h_lb, h_rb);
492
-
493
- let i3 = i * 3;
494
-
495
- _tempVec.x = this_plainVertices[i3] + hi * this_plainNormals[i3],
496
- _tempVec.y = this_plainVertices[i3 + 1] + hi * this_plainNormals[i3 + 1],
497
- _tempVec.z = this_plainVertices[i3 + 2] + hi * this_plainNormals[i3 + 2];
498
-
499
- doubleToTwoFloats(_tempVec, _tempHigh, _tempLow);
500
-
501
- terrainVertices[i3] = _tempVec.x;
502
- terrainVertices[i3 + 1] = _tempVec.y;
503
- terrainVertices[i3 + 2] = _tempVec.z;
504
-
505
- terrainVerticesHigh[i3] = _tempHigh.x;
506
- terrainVerticesHigh[i3 + 1] = _tempHigh.y;
507
- terrainVerticesHigh[i3 + 2] = _tempHigh.z;
508
-
509
- terrainVerticesLow[i3] = _tempLow.x;
510
- terrainVerticesLow[i3 + 1] = _tempLow.y;
511
- terrainVerticesLow[i3 + 2] = _tempLow.z;
512
-
513
- if (_tempVec.x < xmin) xmin = _tempVec.x; if (_tempVec.x > xmax) xmax = _tempVec.x;
514
- if (_tempVec.y < ymin) ymin = _tempVec.y; if (_tempVec.y > ymax) ymax = _tempVec.y;
515
- if (_tempVec.z < zmin) zmin = _tempVec.z; if (_tempVec.z > zmax) zmax = _tempVec.z;
516
- }
517
-
518
- normalMapVertices.set(terrainVertices);
519
- normalMapVerticesHigh.set(terrainVerticesHigh);
520
- normalMapVerticesLow.set(terrainVerticesLow);
521
-
522
- for(var k=0;k < gsgs; k++) {
523
-
524
- var j = k % gs,
525
- i = ~~(k / gs);
526
-
527
- if (i !== tgs && j !== tgs) {
528
- var v0ind = k * 3,
529
- v1ind = v0ind + 3,
530
- v2ind = v0ind + gs * 3,
531
- v3ind = v2ind + 3;
532
-
533
-
534
- var v0 = new Vec3(terrainVertices[v0ind], terrainVertices[v0ind + 1], terrainVertices[v0ind + 2]),
535
- v1 = new Vec3(terrainVertices[v1ind], terrainVertices[v1ind + 1], terrainVertices[v1ind + 2]),
536
- v2 = new Vec3(terrainVertices[v2ind], terrainVertices[v2ind + 1], terrainVertices[v2ind + 2]),
537
- v3 = new Vec3(terrainVertices[v3ind], terrainVertices[v3ind + 1], terrainVertices[v3ind + 2]);
538
-
539
- var e10 = v1.sub(v0).normalize(),
540
- e20 = v2.sub(v0).normalize(),
541
- e30 = v3.sub(v0).normalize();
542
-
543
- var sw = e20.cross(e30).normalize();
544
- var ne = e30.cross(e10).normalize();
545
- var n0 = ne.add(sw).normalize();
546
-
547
- normalMapNormals[v0ind] += n0.x;
548
- normalMapNormals[v0ind + 1] += n0.y;
549
- normalMapNormals[v0ind + 2] += n0.z;
550
-
551
- normalMapNormals[v1ind] += ne.x;
552
- normalMapNormals[v1ind + 1] += ne.y;
553
- normalMapNormals[v1ind + 2] += ne.z;
554
-
555
- normalMapNormals[v2ind] += sw.x;
556
- normalMapNormals[v2ind + 1] += sw.y;
557
- normalMapNormals[v2ind + 2] += sw.z;
558
-
559
- normalMapNormals[v3ind] += n0.x;
560
- normalMapNormals[v3ind + 1] += n0.y;
561
- normalMapNormals[v3ind + 2] += n0.z;
562
- }
563
- }
564
- }
565
-
566
- self.postMessage({
567
- id: id,
568
- normalMapNormals: normalMapNormals,
569
- normalMapVertices: normalMapVertices,
570
- normalMapVerticesHigh: normalMapVerticesHigh,
571
- normalMapVerticesLow: normalMapVerticesLow,
572
- terrainVertices: terrainVertices,
573
- terrainVerticesHigh: terrainVerticesHigh,
574
- terrainVerticesLow: terrainVerticesLow,
575
- noDataVertices: noDataVertices,
576
- //bounds: [xmin, xmax, ymin, ymax, zmin, zmax]
577
- bounds: [xmin, ymin, zmin, xmax, ymax, zmax]
578
- }, [
579
- normalMapNormals.buffer,
580
- normalMapVertices.buffer,
581
- normalMapVerticesHigh.buffer,
582
- normalMapVerticesLow.buffer,
583
- terrainVertices.buffer,
584
- terrainVerticesHigh.buffer,
585
- terrainVerticesLow.buffer,
586
- noDataVertices.buffer
587
- ]);
588
- }`;
589
-
1
+ 'use strict';
2
+
3
+ // import { QueueArray } from '../QueueArray.js';
4
+
5
+ import { BaseWorker } from "./BaseWorker.js";
6
+
7
+ class TerrainWorker extends BaseWorker {
8
+ constructor(numWorkers = 2) {
9
+ super(numWorkers, _programm);
10
+ this._segments = new Map();
11
+ }
12
+
13
+ _onMessage(e) {
14
+ this._segments.get(e.data.id)._terrainWorkerCallback(e.data);
15
+ this._segments.delete(e.data.id);
16
+
17
+ e.data.normalMapNormals = null;
18
+ e.data.normalMapVertices = null;
19
+ e.data.normalMapVerticesHigh = null;
20
+ e.data.normalMapVerticesLow = null;
21
+ e.data.terrainVertices = null;
22
+ e.data.terrainVerticesHigh = null;
23
+ e.data.terrainVerticesLow = null;
24
+ }
25
+
26
+ check() {
27
+ if (this._pendingQueue.length) {
28
+ var p = this._pendingQueue.pop();
29
+ this.make(p.segment, p.elevations);
30
+ }
31
+ }
32
+
33
+ make(segment, elevations) {
34
+ if (segment.plainReady && segment.terrainIsLoading) {
35
+
36
+ var _elevations = new Float32Array(elevations.length);
37
+ _elevations.set(elevations);
38
+
39
+ if (this._workerQueue.length) {
40
+
41
+ var w = this._workerQueue.pop();
42
+
43
+ this._segments.set(this._id, segment);
44
+
45
+ w.postMessage({
46
+ 'elevations': _elevations,
47
+ 'this_plainVertices': segment.plainVertices,
48
+ 'this_plainNormals': segment.plainNormals,
49
+ 'this_normalMapVertices': segment.normalMapVertices,
50
+ 'this_normalMapNormals': segment.normalMapNormals,
51
+ 'heightFactor': segment.planet._heightFactor,
52
+ 'gridSize': segment.planet.terrain.gridSizeByZoom[segment.tileZoom],
53
+ 'noDataValues': segment.planet.terrain.noDataValues,
54
+ 'id': this._id++
55
+ }, [
56
+ _elevations.buffer,
57
+ segment.plainVertices.buffer,
58
+ segment.plainNormals.buffer,
59
+ segment.normalMapVertices.buffer,
60
+ segment.normalMapNormals.buffer
61
+ ]);
62
+
63
+ } else {
64
+ this._pendingQueue.push({ segment: segment, elevations: _elevations });
65
+ }
66
+ } else {
67
+ this.check();
68
+ }
69
+ }
70
+ }
71
+
72
+ const _programm =
73
+ `'use strict';
74
+ //
75
+ //Terrain worker
76
+ //
77
+
78
+ function binarySearchFast(arr, x) {
79
+ let start = 0,
80
+ end = arr.length - 1;
81
+ while (start <= end) {
82
+ let k = Math.floor((start + end) * 0.5);
83
+ if (arr[k] === x)
84
+ return k;
85
+ else if (arr[k] < x)
86
+ start = k + 1;
87
+ else
88
+ end = k - 1;
89
+ }
90
+ return -1;
91
+ };
92
+
93
+ function checkNoDataValue(noDataValues, value) {
94
+ return binarySearchFast(noDataValues, value) !== -1;
95
+ };
96
+
97
+
98
+ var Vec3 = function(x, y, z) {
99
+ this.x = x;
100
+ this.y = y;
101
+ this.z = z;
102
+ };
103
+
104
+ var doubleToTwoFloats = function(v, high, low) {
105
+
106
+ let x = v.x, y = v.y, z = v.z;
107
+
108
+ if (x >= 0.0) {
109
+ var doubleHigh = Math.floor(x / 65536.0) * 65536.0;
110
+ high.x = Math.fround(doubleHigh);
111
+ low.x = Math.fround(x - doubleHigh);
112
+ } else {
113
+ var doubleHigh = Math.floor(-x / 65536.0) * 65536.0;
114
+ high.x = Math.fround(-doubleHigh);
115
+ low.x = Math.fround(x + doubleHigh);
116
+ }
117
+
118
+ if (y >= 0.0) {
119
+ var doubleHigh = Math.floor(y / 65536.0) * 65536.0;
120
+ high.y = Math.fround(doubleHigh);
121
+ low.y = Math.fround(y - doubleHigh);
122
+ } else {
123
+ var doubleHigh = Math.floor(-y / 65536.0) * 65536.0;
124
+ high.y = Math.fround(-doubleHigh);
125
+ low.y = Math.fround(y + doubleHigh);
126
+ }
127
+
128
+ if (z >= 0.0) {
129
+ var doubleHigh = Math.floor(z / 65536.0) * 65536.0;
130
+ high.z = Math.fround(doubleHigh);
131
+ low.z = Math.fround(z - doubleHigh);
132
+ } else {
133
+ var doubleHigh = Math.floor(-z / 65536.0) * 65536.0;
134
+ high.z = Math.fround(-doubleHigh);
135
+ low.z = Math.fround(z + doubleHigh);
136
+ }
137
+ };
138
+
139
+ Vec3.prototype.sub = function(v) {
140
+ return new Vec3(this.x - v.x, this.y - v.y, this.z - v.z);
141
+ };
142
+
143
+ Vec3.prototype.add = function(v) {
144
+ return new Vec3(this.x + v.x, this.y + v.y, this.z + v.z);
145
+ };
146
+
147
+ Vec3.prototype.cross = function(v) {
148
+ return new Vec3(
149
+ this.y * v.z - this.z * v.y,
150
+ this.z * v.x - this.x * v.z,
151
+ this.x * v.y - this.y * v.x
152
+ );
153
+ };
154
+
155
+ Vec3.prototype.normalize = function(v) {
156
+ var x = this.x, y = this.y, z = this.z;
157
+ var length = 1.0 / Math.sqrt(x * x + y * y + z * z);
158
+ this.x = x * length;
159
+ this.y = y * length;
160
+ this.z = z * length;
161
+ return this;
162
+ };
163
+
164
+ Vec3.prototype.distance = function(v) {
165
+ return this.sub(v).length();
166
+ };
167
+
168
+ Vec3.prototype.length = function () {
169
+ return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
170
+ };
171
+
172
+ var blerp = function(x, y, fQ11, fQ21, fQ12, fQ22) {
173
+ return (fQ11 * (1.0 - x) * (1.0 - y) + fQ21 * x * (1.0 - y) + fQ12 * (1.0 - x) * y + fQ22 * x * y);
174
+ };
175
+
176
+ var slice = function (t, h1, h0) {
177
+ return t * (h1 - h0);
178
+ };
179
+
180
+ var _tempVec = new Vec3(0.0, 0.0, 0.0);
181
+
182
+ var _tempHigh = new Vec3(0.0, 0.0, 0.0),
183
+ _tempLow = new Vec3(0.0, 0.0, 0.0);
184
+
185
+ self.onmessage = function (e) {
186
+ var elevations = e.data.elevations,
187
+ this_plainVertices = e.data.this_plainVertices,
188
+ this_plainNormals = e.data.this_plainNormals,
189
+ this_normalMapVertices = e.data.this_normalMapVertices,
190
+ this_normalMapNormals = e.data.this_normalMapNormals,
191
+ heightFactor = e.data.heightFactor,
192
+ gridSize = e.data.gridSize,
193
+ noDataValues = e.data.noDataValues,
194
+ id = e.data.id;
195
+
196
+ var xmin = 549755748352.0, xmax = -549755748352.0,
197
+ ymin = 549755748352.0, ymax = -549755748352.0,
198
+ zmin = 549755748352.0, zmax = -549755748352.0;
199
+
200
+ const fileGridSize = Math.sqrt(elevations.length) - 1;
201
+
202
+ const fileGridSize_one = fileGridSize + 1;
203
+ const fileGridSize_one_x2 = fileGridSize_one * fileGridSize_one;
204
+ const tgs = gridSize;
205
+ const dg = fileGridSize / tgs;
206
+ const gs = tgs + 1;
207
+ const hf = heightFactor;
208
+
209
+ var nmvInd = 0,
210
+ vInd = 0,
211
+ noDataInd = 0;
212
+
213
+ var gsgs3 = gs * gs * 3;
214
+
215
+ var terrainVertices = new Float64Array(gsgs3),
216
+ terrainVerticesHigh = new Float32Array(gsgs3),
217
+ terrainVerticesLow = new Float32Array(gsgs3),
218
+ noDataVertices = new Uint8Array(gs * gs);
219
+
220
+ var normalMapNormals,
221
+ normalMapVertices,
222
+ normalMapVerticesHigh,
223
+ normalMapVerticesLow;
224
+
225
+ var nv = this_normalMapVertices,
226
+ nn = this_normalMapNormals;
227
+
228
+ if (fileGridSize >= tgs) {
229
+
230
+ normalMapNormals = new Float32Array(fileGridSize_one_x2 * 3);
231
+ normalMapVertices = new Float64Array(fileGridSize_one_x2 * 3);
232
+ normalMapVerticesHigh = new Float32Array(fileGridSize_one_x2 * 3);
233
+ normalMapVerticesLow = new Float32Array(fileGridSize_one_x2 * 3);
234
+
235
+ for (var k = 0; k < fileGridSize_one_x2; k++) {
236
+
237
+ var j = k % fileGridSize_one,
238
+ i = ~~(k / fileGridSize_one);
239
+
240
+ //
241
+ // V0
242
+ //
243
+ var hInd0 = k;
244
+ var vInd0 = hInd0 * 3;
245
+ var currElv = elevations[hInd0];
246
+ if(checkNoDataValue(noDataValues, currElv)) {
247
+ currElv = 0.0;
248
+ }
249
+ var h0 = hf * currElv;
250
+ var v0 = new Vec3(nv[vInd0] + h0 * nn[vInd0], nv[vInd0 + 1] + h0 * nn[vInd0 + 1], nv[vInd0 + 2] + h0 * nn[vInd0 + 2]);
251
+
252
+ doubleToTwoFloats(v0, _tempHigh, _tempLow);
253
+
254
+ normalMapVertices[vInd0] = v0.x;
255
+ normalMapVertices[vInd0 + 1] = v0.y;
256
+ normalMapVertices[vInd0 + 2] = v0.z;
257
+
258
+ normalMapVerticesHigh[vInd0] = _tempHigh.x;
259
+ normalMapVerticesHigh[vInd0 + 1] = _tempHigh.y;
260
+ normalMapVerticesHigh[vInd0 + 2] = _tempHigh.z;
261
+
262
+ normalMapVerticesLow[vInd0] = _tempLow.x;
263
+ normalMapVerticesLow[vInd0 + 1] = _tempLow.y;
264
+ normalMapVerticesLow[vInd0 + 2] = _tempLow.z;
265
+
266
+ //
267
+ // The vertex goes into screen buffer
268
+ if (i % dg === 0 && j % dg === 0) {
269
+
270
+ let currVert = new Vec3(nv[vInd0], nv[vInd0 + 1], nv[vInd0 + 2]);
271
+ let nextVert = new Vec3(nv[vInd0 + 3], nv[vInd0 + 4], nv[vInd0 + 5]);
272
+
273
+ let nextElv = elevations[hInd0 + 1];
274
+ if(checkNoDataValue(noDataValues, nextElv)) {
275
+ nextElv = 0.0;
276
+ }
277
+
278
+ let eps = false;
279
+ if(noDataValues.length === 0){
280
+ let step = currVert.distance(nextVert);
281
+ let deltaElv = Math.abs(currElv - nextElv);
282
+ eps = ((deltaElv / step) > 10.0) || (currElv < -5000);
283
+ }
284
+
285
+ if(eps){
286
+ noDataVertices[noDataInd] = 1;
287
+ } else {
288
+ noDataVertices[noDataInd] = 0;
289
+ if (v0.x < xmin) xmin = v0.x; if (v0.x > xmax) xmax = v0.x;
290
+ if (v0.y < ymin) ymin = v0.y; if (v0.y > ymax) ymax = v0.y;
291
+ if (v0.z < zmin) zmin = v0.z; if (v0.z > zmax) zmax = v0.z;
292
+ }
293
+
294
+ terrainVerticesHigh[vInd] = _tempHigh.x;
295
+ terrainVerticesLow[vInd] = _tempLow.x;
296
+ terrainVertices[vInd++] = v0.x;
297
+
298
+ terrainVerticesHigh[vInd] = _tempHigh.y;
299
+ terrainVerticesLow[vInd] = _tempLow.y;
300
+ terrainVertices[vInd++] = v0.y;
301
+
302
+ terrainVerticesHigh[vInd] = _tempHigh.z;
303
+ terrainVerticesLow[vInd] = _tempLow.z;
304
+ terrainVertices[vInd++] = v0.z;
305
+
306
+ noDataInd++;
307
+ }
308
+
309
+ if (i !== fileGridSize && j !== fileGridSize) {
310
+
311
+ //
312
+ // V1
313
+ //
314
+ var hInd1 = k + 1;
315
+ var vInd1 = hInd1 * 3;
316
+ var elv = elevations[hInd1];
317
+ if(checkNoDataValue(noDataValues, elv)) {
318
+ elv = 0.0;
319
+ }
320
+ var h1 = hf * elv;
321
+ var v1 = new Vec3(nv[vInd1] + h1 * nn[vInd1], nv[vInd1 + 1] + h1 * nn[vInd1 + 1], nv[vInd1 + 2] + h1 * nn[vInd1 + 2]);
322
+
323
+ doubleToTwoFloats(v1, _tempHigh, _tempLow);
324
+
325
+ normalMapVertices[vInd1] = v1.x;
326
+ normalMapVertices[vInd1 + 1] = v1.y;
327
+ normalMapVertices[vInd1 + 2] = v1.z;
328
+
329
+ normalMapVerticesHigh[vInd1] = _tempHigh.x;
330
+ normalMapVerticesHigh[vInd1 + 1] = _tempHigh.y;
331
+ normalMapVerticesHigh[vInd1 + 2] = _tempHigh.z;
332
+
333
+ normalMapVerticesLow[vInd1] = _tempLow.x;
334
+ normalMapVerticesLow[vInd1 + 1] = _tempLow.y;
335
+ normalMapVerticesLow[vInd1 + 2] = _tempLow.z;
336
+
337
+ //
338
+ // V2
339
+ //
340
+ var hInd2 = k + fileGridSize_one;
341
+ var vInd2 = hInd2 * 3;
342
+ var elv = elevations[hInd2];
343
+ if(checkNoDataValue(noDataValues, elv)) {
344
+ elv = 0.0;
345
+ }
346
+ var h2 = hf * elv;
347
+ var v2 = new Vec3(nv[vInd2] + h2 * nn[vInd2], nv[vInd2 + 1] + h2 * nn[vInd2 + 1], nv[vInd2 + 2] + h2 * nn[vInd2 + 2]);
348
+
349
+ doubleToTwoFloats(v2, _tempHigh, _tempLow);
350
+
351
+ normalMapVertices[vInd2] = v2.x;
352
+ normalMapVertices[vInd2 + 1] = v2.y;
353
+ normalMapVertices[vInd2 + 2] = v2.z;
354
+
355
+ normalMapVerticesHigh[vInd2] = _tempHigh.x;
356
+ normalMapVerticesHigh[vInd2 + 1] = _tempHigh.y;
357
+ normalMapVerticesHigh[vInd2 + 2] = _tempHigh.z;
358
+
359
+ normalMapVerticesLow[vInd2] = _tempLow.x;
360
+ normalMapVerticesLow[vInd2 + 1] = _tempLow.y;
361
+ normalMapVerticesLow[vInd2 + 2] = _tempLow.z;
362
+
363
+ //
364
+ // V3
365
+ //
366
+ var hInd3 = k + fileGridSize_one + 1;
367
+ var vInd3 = hInd3 * 3;
368
+ var elv = elevations[hInd3];
369
+ if(checkNoDataValue(noDataValues, elv)) {
370
+ elv = 0.0;
371
+ }
372
+ var h3 = hf * elv;
373
+ var v3 = new Vec3(nv[vInd3] + h3 * nn[vInd3], nv[vInd3 + 1] + h3 * nn[vInd3 + 1], nv[vInd3 + 2] + h3 * nn[vInd3 + 2]);
374
+
375
+ doubleToTwoFloats(v3, _tempHigh, _tempLow);
376
+
377
+ normalMapVertices[vInd3] = v3.x;
378
+ normalMapVertices[vInd3 + 1] = v3.y;
379
+ normalMapVertices[vInd3 + 2] = v3.z;
380
+
381
+ normalMapVerticesHigh[vInd3] = _tempHigh.x;
382
+ normalMapVerticesHigh[vInd3 + 1] = _tempHigh.y;
383
+ normalMapVerticesHigh[vInd3 + 2] = _tempHigh.z;
384
+
385
+ normalMapVerticesLow[vInd3] = _tempLow.x;
386
+ normalMapVerticesLow[vInd3 + 1] = _tempLow.y;
387
+ normalMapVerticesLow[vInd3 + 2] = _tempLow.z;
388
+
389
+ //
390
+ // Normal
391
+ //
392
+ var e10 = v1.sub(v0),
393
+ e20 = v2.sub(v0),
394
+ e30 = v3.sub(v0);
395
+ var sw = e20.cross(e30).normalize();
396
+ var ne = e30.cross(e10).normalize();
397
+ var n0 = ne.add(sw).normalize();
398
+
399
+ normalMapNormals[vInd0] += n0.x;
400
+ normalMapNormals[vInd0 + 1] += n0.y;
401
+ normalMapNormals[vInd0 + 2] += n0.z;
402
+
403
+ normalMapNormals[vInd1] += ne.x;
404
+ normalMapNormals[vInd1 + 1] += ne.y;
405
+ normalMapNormals[vInd1 + 2] += ne.z;
406
+
407
+ normalMapNormals[vInd2] += sw.x;
408
+ normalMapNormals[vInd2 + 1] += sw.y;
409
+ normalMapNormals[vInd2 + 2] += sw.z;
410
+
411
+ normalMapNormals[vInd3] += n0.x;
412
+ normalMapNormals[vInd3 + 1] += n0.y;
413
+ normalMapNormals[vInd3 + 2] += n0.z;
414
+ }
415
+ }
416
+
417
+ } else {
418
+
419
+ normalMapNormals = new Float32Array(gsgs3);
420
+ normalMapVertices = new Float64Array(gsgs3);
421
+ normalMapVerticesHigh = new Float32Array(gsgs3);
422
+ normalMapVerticesLow = new Float32Array(gsgs3);
423
+ normalMapNormals = new Float32Array(gsgs3);
424
+
425
+ var oneSize = tgs / fileGridSize;
426
+ var h, inside_i, inside_j, v_i, v_j;
427
+ var gsgs = gsgs3 / 3;
428
+ var fgsOne = fileGridSize + 1;
429
+
430
+ for(let i = 0; i < gsgs; i++) {
431
+ let ii = Math.floor(i / gs),
432
+ ij = i % gs;
433
+
434
+ let qii = ii % oneSize,
435
+ qij = ij % oneSize;
436
+
437
+ let hlt_ind = Math.floor(ii / oneSize) * fgsOne + Math.floor(ij / oneSize);
438
+
439
+ if (ij === tgs) {
440
+ hlt_ind -= 1;
441
+ qij = oneSize;
442
+ }
443
+
444
+ if (ii === tgs) {
445
+ hlt_ind -= fgsOne;
446
+ qii = oneSize;
447
+ }
448
+
449
+ let hrt_ind = hlt_ind + 1,
450
+ hlb_ind = hlt_ind + fgsOne,
451
+ hrb_ind = hlb_ind + 1;
452
+
453
+ let h_lt = elevations[hlt_ind],
454
+ h_rt = elevations[hrt_ind],
455
+ h_lb = elevations[hlb_ind],
456
+ h_rb = elevations[hrb_ind];
457
+
458
+ if(checkNoDataValue(noDataValues, h_lt)) {
459
+ h_lt = 0.0;
460
+ }
461
+
462
+ if(checkNoDataValue(noDataValues, h_rt)) {
463
+ h_rt = 0.0;
464
+ }
465
+
466
+ if(checkNoDataValue(noDataValues, h_lb)) {
467
+ h_lb = 0.0;
468
+ }
469
+
470
+ if(checkNoDataValue(noDataValues, h_rb)) {
471
+ h_rb = 0.0;
472
+ }
473
+
474
+ let hi = blerp(qij / oneSize, qii / oneSize, h_lt, h_rt, h_lb, h_rb);
475
+
476
+ let i3 = i * 3;
477
+
478
+ _tempVec.x = this_plainVertices[i3] + hi * this_plainNormals[i3],
479
+ _tempVec.y = this_plainVertices[i3 + 1] + hi * this_plainNormals[i3 + 1],
480
+ _tempVec.z = this_plainVertices[i3 + 2] + hi * this_plainNormals[i3 + 2];
481
+
482
+ doubleToTwoFloats(_tempVec, _tempHigh, _tempLow);
483
+
484
+ terrainVertices[i3] = _tempVec.x;
485
+ terrainVertices[i3 + 1] = _tempVec.y;
486
+ terrainVertices[i3 + 2] = _tempVec.z;
487
+
488
+ terrainVerticesHigh[i3] = _tempHigh.x;
489
+ terrainVerticesHigh[i3 + 1] = _tempHigh.y;
490
+ terrainVerticesHigh[i3 + 2] = _tempHigh.z;
491
+
492
+ terrainVerticesLow[i3] = _tempLow.x;
493
+ terrainVerticesLow[i3 + 1] = _tempLow.y;
494
+ terrainVerticesLow[i3 + 2] = _tempLow.z;
495
+
496
+ if (_tempVec.x < xmin) xmin = _tempVec.x; if (_tempVec.x > xmax) xmax = _tempVec.x;
497
+ if (_tempVec.y < ymin) ymin = _tempVec.y; if (_tempVec.y > ymax) ymax = _tempVec.y;
498
+ if (_tempVec.z < zmin) zmin = _tempVec.z; if (_tempVec.z > zmax) zmax = _tempVec.z;
499
+ }
500
+
501
+ normalMapVertices.set(terrainVertices);
502
+ normalMapVerticesHigh.set(terrainVerticesHigh);
503
+ normalMapVerticesLow.set(terrainVerticesLow);
504
+
505
+ for(var k=0;k < gsgs; k++) {
506
+
507
+ var j = k % gs,
508
+ i = ~~(k / gs);
509
+
510
+ if (i !== tgs && j !== tgs) {
511
+ var v0ind = k * 3,
512
+ v1ind = v0ind + 3,
513
+ v2ind = v0ind + gs * 3,
514
+ v3ind = v2ind + 3;
515
+
516
+
517
+ var v0 = new Vec3(terrainVertices[v0ind], terrainVertices[v0ind + 1], terrainVertices[v0ind + 2]),
518
+ v1 = new Vec3(terrainVertices[v1ind], terrainVertices[v1ind + 1], terrainVertices[v1ind + 2]),
519
+ v2 = new Vec3(terrainVertices[v2ind], terrainVertices[v2ind + 1], terrainVertices[v2ind + 2]),
520
+ v3 = new Vec3(terrainVertices[v3ind], terrainVertices[v3ind + 1], terrainVertices[v3ind + 2]);
521
+
522
+ var e10 = v1.sub(v0).normalize(),
523
+ e20 = v2.sub(v0).normalize(),
524
+ e30 = v3.sub(v0).normalize();
525
+
526
+ var sw = e20.cross(e30).normalize();
527
+ var ne = e30.cross(e10).normalize();
528
+ var n0 = ne.add(sw).normalize();
529
+
530
+ normalMapNormals[v0ind] += n0.x;
531
+ normalMapNormals[v0ind + 1] += n0.y;
532
+ normalMapNormals[v0ind + 2] += n0.z;
533
+
534
+ normalMapNormals[v1ind] += ne.x;
535
+ normalMapNormals[v1ind + 1] += ne.y;
536
+ normalMapNormals[v1ind + 2] += ne.z;
537
+
538
+ normalMapNormals[v2ind] += sw.x;
539
+ normalMapNormals[v2ind + 1] += sw.y;
540
+ normalMapNormals[v2ind + 2] += sw.z;
541
+
542
+ normalMapNormals[v3ind] += n0.x;
543
+ normalMapNormals[v3ind + 1] += n0.y;
544
+ normalMapNormals[v3ind + 2] += n0.z;
545
+ }
546
+ }
547
+ }
548
+
549
+ self.postMessage({
550
+ id: id,
551
+ normalMapNormals: normalMapNormals,
552
+ normalMapVertices: normalMapVertices,
553
+ normalMapVerticesHigh: normalMapVerticesHigh,
554
+ normalMapVerticesLow: normalMapVerticesLow,
555
+ terrainVertices: terrainVertices,
556
+ terrainVerticesHigh: terrainVerticesHigh,
557
+ terrainVerticesLow: terrainVerticesLow,
558
+ noDataVertices: noDataVertices,
559
+ //bounds: [xmin, xmax, ymin, ymax, zmin, zmax]
560
+ bounds: [xmin, ymin, zmin, xmax, ymax, zmax]
561
+ }, [
562
+ normalMapNormals.buffer,
563
+ normalMapVertices.buffer,
564
+ normalMapVerticesHigh.buffer,
565
+ normalMapVerticesLow.buffer,
566
+ terrainVertices.buffer,
567
+ terrainVerticesHigh.buffer,
568
+ terrainVerticesLow.buffer,
569
+ noDataVertices.buffer
570
+ ]);
571
+ }`;
572
+
590
573
  export { TerrainWorker };