@loaders.gl/terrain 4.0.0-beta.2 → 4.0.0-beta.4

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 (69) hide show
  1. package/dist/dist.dev.js +1193 -0
  2. package/dist/index.cjs +1020 -0
  3. package/dist/{esm/index.js → index.js} +4 -4
  4. package/dist/index.js.map +1 -0
  5. package/dist/lib/decode-quantized-mesh.js.map +1 -0
  6. package/dist/lib/delatin/index.js.map +1 -0
  7. package/dist/lib/helpers/skirt.js.map +1 -0
  8. package/dist/{esm/lib → lib}/parse-quantized-mesh.js +2 -2
  9. package/dist/lib/parse-quantized-mesh.js.map +1 -0
  10. package/dist/{esm/lib → lib}/parse-terrain.js +2 -2
  11. package/dist/lib/parse-terrain.js.map +1 -0
  12. package/dist/lib/utils/version.js +2 -0
  13. package/dist/lib/utils/version.js.map +1 -0
  14. package/dist/{esm/quantized-mesh-loader.js → quantized-mesh-loader.js} +1 -1
  15. package/dist/quantized-mesh-loader.js.map +1 -0
  16. package/dist/quantized-mesh-worker.js +133 -27
  17. package/dist/{esm/terrain-loader.js → terrain-loader.js} +1 -1
  18. package/dist/terrain-loader.js.map +1 -0
  19. package/dist/terrain-worker.js +133 -27
  20. package/dist/{esm/workers → workers}/quantized-mesh-worker.js +1 -1
  21. package/dist/workers/quantized-mesh-worker.js.map +1 -0
  22. package/dist/{esm/workers → workers}/terrain-worker.js +1 -1
  23. package/dist/workers/terrain-worker.js.map +1 -0
  24. package/package.json +17 -9
  25. package/dist/bundle.d.ts +0 -2
  26. package/dist/bundle.d.ts.map +0 -1
  27. package/dist/dist.min.js +0 -1186
  28. package/dist/es5/bundle.js +0 -6
  29. package/dist/es5/bundle.js.map +0 -1
  30. package/dist/es5/index.js +0 -88
  31. package/dist/es5/index.js.map +0 -1
  32. package/dist/es5/lib/decode-quantized-mesh.js +0 -250
  33. package/dist/es5/lib/decode-quantized-mesh.js.map +0 -1
  34. package/dist/es5/lib/delatin/index.js +0 -408
  35. package/dist/es5/lib/delatin/index.js.map +0 -1
  36. package/dist/es5/lib/helpers/skirt.js +0 -105
  37. package/dist/es5/lib/helpers/skirt.js.map +0 -1
  38. package/dist/es5/lib/parse-quantized-mesh.js +0 -95
  39. package/dist/es5/lib/parse-quantized-mesh.js.map +0 -1
  40. package/dist/es5/lib/parse-terrain.js +0 -152
  41. package/dist/es5/lib/parse-terrain.js.map +0 -1
  42. package/dist/es5/lib/utils/version.js +0 -9
  43. package/dist/es5/lib/utils/version.js.map +0 -1
  44. package/dist/es5/quantized-mesh-loader.js +0 -24
  45. package/dist/es5/quantized-mesh-loader.js.map +0 -1
  46. package/dist/es5/terrain-loader.js +0 -32
  47. package/dist/es5/terrain-loader.js.map +0 -1
  48. package/dist/es5/workers/quantized-mesh-worker.js +0 -6
  49. package/dist/es5/workers/quantized-mesh-worker.js.map +0 -1
  50. package/dist/es5/workers/terrain-worker.js +0 -6
  51. package/dist/es5/workers/terrain-worker.js.map +0 -1
  52. package/dist/esm/bundle.js +0 -4
  53. package/dist/esm/bundle.js.map +0 -1
  54. package/dist/esm/index.js.map +0 -1
  55. package/dist/esm/lib/decode-quantized-mesh.js.map +0 -1
  56. package/dist/esm/lib/delatin/index.js.map +0 -1
  57. package/dist/esm/lib/helpers/skirt.js.map +0 -1
  58. package/dist/esm/lib/parse-quantized-mesh.js.map +0 -1
  59. package/dist/esm/lib/parse-terrain.js.map +0 -1
  60. package/dist/esm/lib/utils/version.js +0 -2
  61. package/dist/esm/lib/utils/version.js.map +0 -1
  62. package/dist/esm/quantized-mesh-loader.js.map +0 -1
  63. package/dist/esm/terrain-loader.js.map +0 -1
  64. package/dist/esm/workers/quantized-mesh-worker.js.map +0 -1
  65. package/dist/esm/workers/terrain-worker.js.map +0 -1
  66. package/src/bundle.ts +0 -4
  67. /package/dist/{esm/lib → lib}/decode-quantized-mesh.js +0 -0
  68. /package/dist/{esm/lib → lib}/delatin/index.js +0 -0
  69. /package/dist/{esm/lib → lib}/helpers/skirt.js +0 -0
@@ -0,0 +1,1193 @@
1
+ (function webpackUniversalModuleDefinition(root, factory) {
2
+ if (typeof exports === 'object' && typeof module === 'object')
3
+ module.exports = factory();
4
+ else if (typeof define === 'function' && define.amd) define([], factory);
5
+ else if (typeof exports === 'object') exports['loader'] = factory();
6
+ else root['loader'] = factory();})(globalThis, function () {
7
+ "use strict";
8
+ var __exports__ = (() => {
9
+ var __defProp = Object.defineProperty;
10
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
11
+ var __getOwnPropNames = Object.getOwnPropertyNames;
12
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
13
+ var __export = (target, all) => {
14
+ for (var name in all)
15
+ __defProp(target, name, { get: all[name], enumerable: true });
16
+ };
17
+ var __copyProps = (to, from, except, desc) => {
18
+ if (from && typeof from === "object" || typeof from === "function") {
19
+ for (let key of __getOwnPropNames(from))
20
+ if (!__hasOwnProp.call(to, key) && key !== except)
21
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
22
+ }
23
+ return to;
24
+ };
25
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
26
+
27
+ // src/index.ts
28
+ var src_exports = {};
29
+ __export(src_exports, {
30
+ QuantizedMeshLoader: () => QuantizedMeshLoader2,
31
+ QuantizedMeshWorkerLoader: () => QuantizedMeshLoader,
32
+ TerrainLoader: () => TerrainLoader2,
33
+ TerrainWorkerLoader: () => TerrainLoader,
34
+ parseTerrain: () => parseTerrain
35
+ });
36
+
37
+ // ../loader-utils/src/loader-types.ts
38
+ async function parseFromContext(data, loaders, options, context) {
39
+ return context._parse(data, loaders, options, context);
40
+ }
41
+
42
+ // ../loader-utils/src/lib/binary-utils/array-buffer-utils.ts
43
+ function concatenateTypedArrays(...typedArrays) {
44
+ const arrays = typedArrays;
45
+ const TypedArrayConstructor = arrays && arrays.length > 1 && arrays[0].constructor || null;
46
+ if (!TypedArrayConstructor) {
47
+ throw new Error('"concatenateTypedArrays" - incorrect quantity of arguments or arguments have incompatible data types');
48
+ }
49
+ const sumLength = arrays.reduce((acc, value) => acc + value.length, 0);
50
+ const result = new TypedArrayConstructor(sumLength);
51
+ let offset = 0;
52
+ for (const array of arrays) {
53
+ result.set(array, offset);
54
+ offset += array.length;
55
+ }
56
+ return result;
57
+ }
58
+
59
+ // ../schema/src/lib/mesh/mesh-utils.ts
60
+ function getMeshBoundingBox(attributes) {
61
+ let minX = Infinity;
62
+ let minY = Infinity;
63
+ let minZ = Infinity;
64
+ let maxX = -Infinity;
65
+ let maxY = -Infinity;
66
+ let maxZ = -Infinity;
67
+ const positions = attributes.POSITION ? attributes.POSITION.value : [];
68
+ const len = positions && positions.length;
69
+ for (let i = 0; i < len; i += 3) {
70
+ const x = positions[i];
71
+ const y = positions[i + 1];
72
+ const z = positions[i + 2];
73
+ minX = x < minX ? x : minX;
74
+ minY = y < minY ? y : minY;
75
+ minZ = z < minZ ? z : minZ;
76
+ maxX = x > maxX ? x : maxX;
77
+ maxY = y > maxY ? y : maxY;
78
+ maxZ = z > maxZ ? z : maxZ;
79
+ }
80
+ return [[minX, minY, minZ], [maxX, maxY, maxZ]];
81
+ }
82
+
83
+ // src/lib/decode-quantized-mesh.ts
84
+ var QUANTIZED_MESH_HEADER = /* @__PURE__ */ new Map([["centerX", Float64Array.BYTES_PER_ELEMENT], ["centerY", Float64Array.BYTES_PER_ELEMENT], ["centerZ", Float64Array.BYTES_PER_ELEMENT], ["minHeight", Float32Array.BYTES_PER_ELEMENT], ["maxHeight", Float32Array.BYTES_PER_ELEMENT], ["boundingSphereCenterX", Float64Array.BYTES_PER_ELEMENT], ["boundingSphereCenterY", Float64Array.BYTES_PER_ELEMENT], ["boundingSphereCenterZ", Float64Array.BYTES_PER_ELEMENT], ["boundingSphereRadius", Float64Array.BYTES_PER_ELEMENT], ["horizonOcclusionPointX", Float64Array.BYTES_PER_ELEMENT], ["horizonOcclusionPointY", Float64Array.BYTES_PER_ELEMENT], ["horizonOcclusionPointZ", Float64Array.BYTES_PER_ELEMENT]]);
85
+ function decodeZigZag(value) {
86
+ return value >> 1 ^ -(value & 1);
87
+ }
88
+ function decodeHeader(dataView) {
89
+ let position = 0;
90
+ const header = {};
91
+ for (const [key, bytesCount] of QUANTIZED_MESH_HEADER) {
92
+ const getter = bytesCount === 8 ? dataView.getFloat64 : dataView.getFloat32;
93
+ header[key] = getter.call(dataView, position, true);
94
+ position += bytesCount;
95
+ }
96
+ return {
97
+ header,
98
+ headerEndPosition: position
99
+ };
100
+ }
101
+ function decodeVertexData(dataView, headerEndPosition) {
102
+ let position = headerEndPosition;
103
+ const elementsPerVertex = 3;
104
+ const vertexCount = dataView.getUint32(position, true);
105
+ const vertexData = new Uint16Array(vertexCount * elementsPerVertex);
106
+ position += Uint32Array.BYTES_PER_ELEMENT;
107
+ const bytesPerArrayElement = Uint16Array.BYTES_PER_ELEMENT;
108
+ const elementArrayLength = vertexCount * bytesPerArrayElement;
109
+ const uArrayStartPosition = position;
110
+ const vArrayStartPosition = uArrayStartPosition + elementArrayLength;
111
+ const heightArrayStartPosition = vArrayStartPosition + elementArrayLength;
112
+ let u = 0;
113
+ let v = 0;
114
+ let height = 0;
115
+ for (let i = 0; i < vertexCount; i++) {
116
+ u += decodeZigZag(dataView.getUint16(uArrayStartPosition + bytesPerArrayElement * i, true));
117
+ v += decodeZigZag(dataView.getUint16(vArrayStartPosition + bytesPerArrayElement * i, true));
118
+ height += decodeZigZag(dataView.getUint16(heightArrayStartPosition + bytesPerArrayElement * i, true));
119
+ vertexData[i] = u;
120
+ vertexData[i + vertexCount] = v;
121
+ vertexData[i + vertexCount * 2] = height;
122
+ }
123
+ position += elementArrayLength * 3;
124
+ return {
125
+ vertexData,
126
+ vertexDataEndPosition: position
127
+ };
128
+ }
129
+ function decodeIndex(buffer, position, indicesCount, bytesPerIndex, encoded = true) {
130
+ let indices;
131
+ if (bytesPerIndex === 2) {
132
+ indices = new Uint16Array(buffer, position, indicesCount);
133
+ } else {
134
+ indices = new Uint32Array(buffer, position, indicesCount);
135
+ }
136
+ if (!encoded) {
137
+ return indices;
138
+ }
139
+ let highest = 0;
140
+ for (let i = 0; i < indices.length; ++i) {
141
+ const code = indices[i];
142
+ indices[i] = highest - code;
143
+ if (code === 0) {
144
+ ++highest;
145
+ }
146
+ }
147
+ return indices;
148
+ }
149
+ function decodeTriangleIndices(dataView, vertexData, vertexDataEndPosition) {
150
+ let position = vertexDataEndPosition;
151
+ const elementsPerVertex = 3;
152
+ const vertexCount = vertexData.length / elementsPerVertex;
153
+ const bytesPerIndex = vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;
154
+ if (position % bytesPerIndex !== 0) {
155
+ position += bytesPerIndex - position % bytesPerIndex;
156
+ }
157
+ const triangleCount = dataView.getUint32(position, true);
158
+ position += Uint32Array.BYTES_PER_ELEMENT;
159
+ const triangleIndicesCount = triangleCount * 3;
160
+ const triangleIndices = decodeIndex(dataView.buffer, position, triangleIndicesCount, bytesPerIndex);
161
+ position += triangleIndicesCount * bytesPerIndex;
162
+ return {
163
+ triangleIndicesEndPosition: position,
164
+ triangleIndices
165
+ };
166
+ }
167
+ function decodeEdgeIndices(dataView, vertexData, triangleIndicesEndPosition) {
168
+ let position = triangleIndicesEndPosition;
169
+ const elementsPerVertex = 3;
170
+ const vertexCount = vertexData.length / elementsPerVertex;
171
+ const bytesPerIndex = vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;
172
+ const westVertexCount = dataView.getUint32(position, true);
173
+ position += Uint32Array.BYTES_PER_ELEMENT;
174
+ const westIndices = decodeIndex(dataView.buffer, position, westVertexCount, bytesPerIndex, false);
175
+ position += westVertexCount * bytesPerIndex;
176
+ const southVertexCount = dataView.getUint32(position, true);
177
+ position += Uint32Array.BYTES_PER_ELEMENT;
178
+ const southIndices = decodeIndex(dataView.buffer, position, southVertexCount, bytesPerIndex, false);
179
+ position += southVertexCount * bytesPerIndex;
180
+ const eastVertexCount = dataView.getUint32(position, true);
181
+ position += Uint32Array.BYTES_PER_ELEMENT;
182
+ const eastIndices = decodeIndex(dataView.buffer, position, eastVertexCount, bytesPerIndex, false);
183
+ position += eastVertexCount * bytesPerIndex;
184
+ const northVertexCount = dataView.getUint32(position, true);
185
+ position += Uint32Array.BYTES_PER_ELEMENT;
186
+ const northIndices = decodeIndex(dataView.buffer, position, northVertexCount, bytesPerIndex, false);
187
+ position += northVertexCount * bytesPerIndex;
188
+ return {
189
+ edgeIndicesEndPosition: position,
190
+ westIndices,
191
+ southIndices,
192
+ eastIndices,
193
+ northIndices
194
+ };
195
+ }
196
+ function decodeVertexNormalsExtension(extensionDataView) {
197
+ return new Uint8Array(extensionDataView.buffer, extensionDataView.byteOffset, extensionDataView.byteLength);
198
+ }
199
+ function decodeWaterMaskExtension(extensionDataView) {
200
+ return extensionDataView.buffer.slice(extensionDataView.byteOffset, extensionDataView.byteOffset + extensionDataView.byteLength);
201
+ }
202
+ function decodeExtensions(dataView, indicesEndPosition) {
203
+ const extensions = {};
204
+ if (dataView.byteLength <= indicesEndPosition) {
205
+ return {
206
+ extensions,
207
+ extensionsEndPosition: indicesEndPosition
208
+ };
209
+ }
210
+ let position = indicesEndPosition;
211
+ while (position < dataView.byteLength) {
212
+ const extensionId = dataView.getUint8(position, true);
213
+ position += Uint8Array.BYTES_PER_ELEMENT;
214
+ const extensionLength = dataView.getUint32(position, true);
215
+ position += Uint32Array.BYTES_PER_ELEMENT;
216
+ const extensionView = new DataView(dataView.buffer, position, extensionLength);
217
+ switch (extensionId) {
218
+ case 1: {
219
+ extensions.vertexNormals = decodeVertexNormalsExtension(extensionView);
220
+ break;
221
+ }
222
+ case 2: {
223
+ extensions.waterMask = decodeWaterMaskExtension(extensionView);
224
+ break;
225
+ }
226
+ default: {
227
+ }
228
+ }
229
+ position += extensionLength;
230
+ }
231
+ return {
232
+ extensions,
233
+ extensionsEndPosition: position
234
+ };
235
+ }
236
+ var DECODING_STEPS = {
237
+ header: 0,
238
+ vertices: 1,
239
+ triangleIndices: 2,
240
+ edgeIndices: 3,
241
+ extensions: 4
242
+ };
243
+ var DEFAULT_OPTIONS = {
244
+ maxDecodingStep: DECODING_STEPS.extensions
245
+ };
246
+ function decode(data, userOptions) {
247
+ const options = Object.assign({}, DEFAULT_OPTIONS, userOptions);
248
+ const view = new DataView(data);
249
+ const {
250
+ header,
251
+ headerEndPosition
252
+ } = decodeHeader(view);
253
+ if (options.maxDecodingStep < DECODING_STEPS.vertices) {
254
+ return {
255
+ header
256
+ };
257
+ }
258
+ const {
259
+ vertexData,
260
+ vertexDataEndPosition
261
+ } = decodeVertexData(view, headerEndPosition);
262
+ if (options.maxDecodingStep < DECODING_STEPS.triangleIndices) {
263
+ return {
264
+ header,
265
+ vertexData
266
+ };
267
+ }
268
+ const {
269
+ triangleIndices,
270
+ triangleIndicesEndPosition
271
+ } = decodeTriangleIndices(view, vertexData, vertexDataEndPosition);
272
+ if (options.maxDecodingStep < DECODING_STEPS.edgeIndices) {
273
+ return {
274
+ header,
275
+ vertexData,
276
+ triangleIndices
277
+ };
278
+ }
279
+ const {
280
+ westIndices,
281
+ southIndices,
282
+ eastIndices,
283
+ northIndices,
284
+ edgeIndicesEndPosition
285
+ } = decodeEdgeIndices(view, vertexData, triangleIndicesEndPosition);
286
+ if (options.maxDecodingStep < DECODING_STEPS.extensions) {
287
+ return {
288
+ header,
289
+ vertexData,
290
+ triangleIndices,
291
+ westIndices,
292
+ northIndices,
293
+ eastIndices,
294
+ southIndices
295
+ };
296
+ }
297
+ const {
298
+ extensions
299
+ } = decodeExtensions(view, edgeIndicesEndPosition);
300
+ return {
301
+ header,
302
+ vertexData,
303
+ triangleIndices,
304
+ westIndices,
305
+ northIndices,
306
+ eastIndices,
307
+ southIndices,
308
+ extensions
309
+ };
310
+ }
311
+
312
+ // src/lib/helpers/skirt.ts
313
+ function addSkirt(attributes, triangles, skirtHeight, outsideIndices) {
314
+ const outsideEdges = outsideIndices ? getOutsideEdgesFromIndices(outsideIndices, attributes.POSITION.value) : getOutsideEdgesFromTriangles(triangles);
315
+ const newPosition = new attributes.POSITION.value.constructor(outsideEdges.length * 6);
316
+ const newTexcoord0 = new attributes.TEXCOORD_0.value.constructor(outsideEdges.length * 4);
317
+ const newTriangles = new triangles.constructor(outsideEdges.length * 6);
318
+ for (let i = 0; i < outsideEdges.length; i++) {
319
+ const edge = outsideEdges[i];
320
+ updateAttributesForNewEdge({
321
+ edge,
322
+ edgeIndex: i,
323
+ attributes,
324
+ skirtHeight,
325
+ newPosition,
326
+ newTexcoord0,
327
+ newTriangles
328
+ });
329
+ }
330
+ attributes.POSITION.value = concatenateTypedArrays(attributes.POSITION.value, newPosition);
331
+ attributes.TEXCOORD_0.value = concatenateTypedArrays(attributes.TEXCOORD_0.value, newTexcoord0);
332
+ const resultTriangles = triangles instanceof Array ? triangles.concat(newTriangles) : concatenateTypedArrays(triangles, newTriangles);
333
+ return {
334
+ attributes,
335
+ triangles: resultTriangles
336
+ };
337
+ }
338
+ function getOutsideEdgesFromTriangles(triangles) {
339
+ const edges = [];
340
+ for (let i = 0; i < triangles.length; i += 3) {
341
+ edges.push([triangles[i], triangles[i + 1]]);
342
+ edges.push([triangles[i + 1], triangles[i + 2]]);
343
+ edges.push([triangles[i + 2], triangles[i]]);
344
+ }
345
+ edges.sort((a, b) => Math.min(...a) - Math.min(...b) || Math.max(...a) - Math.max(...b));
346
+ const outsideEdges = [];
347
+ let index = 0;
348
+ while (index < edges.length) {
349
+ if (edges[index][0] === edges[index + 1]?.[1] && edges[index][1] === edges[index + 1]?.[0]) {
350
+ index += 2;
351
+ } else {
352
+ outsideEdges.push(edges[index]);
353
+ index++;
354
+ }
355
+ }
356
+ return outsideEdges;
357
+ }
358
+ function getOutsideEdgesFromIndices(indices, position) {
359
+ indices.westIndices.sort((a, b) => position[3 * a + 1] - position[3 * b + 1]);
360
+ indices.eastIndices.sort((a, b) => position[3 * b + 1] - position[3 * a + 1]);
361
+ indices.southIndices.sort((a, b) => position[3 * b] - position[3 * a]);
362
+ indices.northIndices.sort((a, b) => position[3 * a] - position[3 * b]);
363
+ const edges = [];
364
+ for (const index in indices) {
365
+ const indexGroup = indices[index];
366
+ for (let i = 0; i < indexGroup.length - 1; i++) {
367
+ edges.push([indexGroup[i], indexGroup[i + 1]]);
368
+ }
369
+ }
370
+ return edges;
371
+ }
372
+ function updateAttributesForNewEdge({
373
+ edge,
374
+ edgeIndex,
375
+ attributes,
376
+ skirtHeight,
377
+ newPosition,
378
+ newTexcoord0,
379
+ newTriangles
380
+ }) {
381
+ const positionsLength = attributes.POSITION.value.length;
382
+ const vertex1Offset = edgeIndex * 2;
383
+ const vertex2Offset = edgeIndex * 2 + 1;
384
+ newPosition.set(attributes.POSITION.value.subarray(edge[0] * 3, edge[0] * 3 + 3), vertex1Offset * 3);
385
+ newPosition[vertex1Offset * 3 + 2] = newPosition[vertex1Offset * 3 + 2] - skirtHeight;
386
+ newPosition.set(attributes.POSITION.value.subarray(edge[1] * 3, edge[1] * 3 + 3), vertex2Offset * 3);
387
+ newPosition[vertex2Offset * 3 + 2] = newPosition[vertex2Offset * 3 + 2] - skirtHeight;
388
+ newTexcoord0.set(attributes.TEXCOORD_0.value.subarray(edge[0] * 2, edge[0] * 2 + 2), vertex1Offset * 2);
389
+ newTexcoord0.set(attributes.TEXCOORD_0.value.subarray(edge[1] * 2, edge[1] * 2 + 2), vertex2Offset * 2);
390
+ const triangle1Offset = edgeIndex * 2 * 3;
391
+ newTriangles[triangle1Offset] = edge[0];
392
+ newTriangles[triangle1Offset + 1] = positionsLength / 3 + vertex2Offset;
393
+ newTriangles[triangle1Offset + 2] = edge[1];
394
+ newTriangles[triangle1Offset + 3] = positionsLength / 3 + vertex2Offset;
395
+ newTriangles[triangle1Offset + 4] = edge[0];
396
+ newTriangles[triangle1Offset + 5] = positionsLength / 3 + vertex1Offset;
397
+ }
398
+
399
+ // src/lib/parse-quantized-mesh.ts
400
+ function parseQuantizedMesh(arrayBuffer, options = {}) {
401
+ const {
402
+ bounds
403
+ } = options;
404
+ const {
405
+ header,
406
+ vertexData,
407
+ triangleIndices: originalTriangleIndices,
408
+ westIndices,
409
+ northIndices,
410
+ eastIndices,
411
+ southIndices
412
+ } = decode(arrayBuffer, DECODING_STEPS.triangleIndices);
413
+ let triangleIndices = originalTriangleIndices;
414
+ let attributes = getMeshAttributes(vertexData, header, bounds);
415
+ const boundingBox = getMeshBoundingBox(attributes);
416
+ if (options?.skirtHeight) {
417
+ const {
418
+ attributes: newAttributes,
419
+ triangles: newTriangles
420
+ } = addSkirt(attributes, triangleIndices, options.skirtHeight, {
421
+ westIndices,
422
+ northIndices,
423
+ eastIndices,
424
+ southIndices
425
+ });
426
+ attributes = newAttributes;
427
+ triangleIndices = newTriangles;
428
+ }
429
+ return {
430
+ loaderData: {
431
+ header: {}
432
+ },
433
+ header: {
434
+ vertexCount: triangleIndices.length,
435
+ boundingBox
436
+ },
437
+ schema: void 0,
438
+ topology: "triangle-list",
439
+ mode: 4,
440
+ indices: {
441
+ value: triangleIndices,
442
+ size: 1
443
+ },
444
+ attributes
445
+ };
446
+ }
447
+ function getMeshAttributes(vertexData, header, bounds) {
448
+ const {
449
+ minHeight,
450
+ maxHeight
451
+ } = header;
452
+ const [minX, minY, maxX, maxY] = bounds || [0, 0, 1, 1];
453
+ const xScale = maxX - minX;
454
+ const yScale = maxY - minY;
455
+ const zScale = maxHeight - minHeight;
456
+ const nCoords = vertexData.length / 3;
457
+ const positions = new Float32Array(nCoords * 3);
458
+ const texCoords = new Float32Array(nCoords * 2);
459
+ for (let i = 0; i < nCoords; i++) {
460
+ const x = vertexData[i] / 32767;
461
+ const y = vertexData[i + nCoords] / 32767;
462
+ const z = vertexData[i + nCoords * 2] / 32767;
463
+ positions[3 * i + 0] = x * xScale + minX;
464
+ positions[3 * i + 1] = y * yScale + minY;
465
+ positions[3 * i + 2] = z * zScale + minHeight;
466
+ texCoords[2 * i + 0] = x;
467
+ texCoords[2 * i + 1] = y;
468
+ }
469
+ return {
470
+ POSITION: {
471
+ value: positions,
472
+ size: 3
473
+ },
474
+ TEXCOORD_0: {
475
+ value: texCoords,
476
+ size: 2
477
+ }
478
+ };
479
+ }
480
+
481
+ // ../../node_modules/@mapbox/martini/index.js
482
+ var Martini = class {
483
+ constructor(gridSize = 257) {
484
+ this.gridSize = gridSize;
485
+ const tileSize = gridSize - 1;
486
+ if (tileSize & tileSize - 1)
487
+ throw new Error(
488
+ `Expected grid size to be 2^n+1, got ${gridSize}.`
489
+ );
490
+ this.numTriangles = tileSize * tileSize * 2 - 2;
491
+ this.numParentTriangles = this.numTriangles - tileSize * tileSize;
492
+ this.indices = new Uint32Array(this.gridSize * this.gridSize);
493
+ this.coords = new Uint16Array(this.numTriangles * 4);
494
+ for (let i = 0; i < this.numTriangles; i++) {
495
+ let id = i + 2;
496
+ let ax = 0, ay = 0, bx = 0, by = 0, cx = 0, cy = 0;
497
+ if (id & 1) {
498
+ bx = by = cx = tileSize;
499
+ } else {
500
+ ax = ay = cy = tileSize;
501
+ }
502
+ while ((id >>= 1) > 1) {
503
+ const mx = ax + bx >> 1;
504
+ const my = ay + by >> 1;
505
+ if (id & 1) {
506
+ bx = ax;
507
+ by = ay;
508
+ ax = cx;
509
+ ay = cy;
510
+ } else {
511
+ ax = bx;
512
+ ay = by;
513
+ bx = cx;
514
+ by = cy;
515
+ }
516
+ cx = mx;
517
+ cy = my;
518
+ }
519
+ const k = i * 4;
520
+ this.coords[k + 0] = ax;
521
+ this.coords[k + 1] = ay;
522
+ this.coords[k + 2] = bx;
523
+ this.coords[k + 3] = by;
524
+ }
525
+ }
526
+ createTile(terrain) {
527
+ return new Tile(terrain, this);
528
+ }
529
+ };
530
+ var Tile = class {
531
+ constructor(terrain, martini) {
532
+ const size = martini.gridSize;
533
+ if (terrain.length !== size * size)
534
+ throw new Error(
535
+ `Expected terrain data of length ${size * size} (${size} x ${size}), got ${terrain.length}.`
536
+ );
537
+ this.terrain = terrain;
538
+ this.martini = martini;
539
+ this.errors = new Float32Array(terrain.length);
540
+ this.update();
541
+ }
542
+ update() {
543
+ const { numTriangles, numParentTriangles, coords, gridSize: size } = this.martini;
544
+ const { terrain, errors } = this;
545
+ for (let i = numTriangles - 1; i >= 0; i--) {
546
+ const k = i * 4;
547
+ const ax = coords[k + 0];
548
+ const ay = coords[k + 1];
549
+ const bx = coords[k + 2];
550
+ const by = coords[k + 3];
551
+ const mx = ax + bx >> 1;
552
+ const my = ay + by >> 1;
553
+ const cx = mx + my - ay;
554
+ const cy = my + ax - mx;
555
+ const interpolatedHeight = (terrain[ay * size + ax] + terrain[by * size + bx]) / 2;
556
+ const middleIndex = my * size + mx;
557
+ const middleError = Math.abs(interpolatedHeight - terrain[middleIndex]);
558
+ errors[middleIndex] = Math.max(errors[middleIndex], middleError);
559
+ if (i < numParentTriangles) {
560
+ const leftChildIndex = (ay + cy >> 1) * size + (ax + cx >> 1);
561
+ const rightChildIndex = (by + cy >> 1) * size + (bx + cx >> 1);
562
+ errors[middleIndex] = Math.max(errors[middleIndex], errors[leftChildIndex], errors[rightChildIndex]);
563
+ }
564
+ }
565
+ }
566
+ getMesh(maxError = 0) {
567
+ const { gridSize: size, indices } = this.martini;
568
+ const { errors } = this;
569
+ let numVertices = 0;
570
+ let numTriangles = 0;
571
+ const max = size - 1;
572
+ indices.fill(0);
573
+ function countElements(ax, ay, bx, by, cx, cy) {
574
+ const mx = ax + bx >> 1;
575
+ const my = ay + by >> 1;
576
+ if (Math.abs(ax - cx) + Math.abs(ay - cy) > 1 && errors[my * size + mx] > maxError) {
577
+ countElements(cx, cy, ax, ay, mx, my);
578
+ countElements(bx, by, cx, cy, mx, my);
579
+ } else {
580
+ indices[ay * size + ax] = indices[ay * size + ax] || ++numVertices;
581
+ indices[by * size + bx] = indices[by * size + bx] || ++numVertices;
582
+ indices[cy * size + cx] = indices[cy * size + cx] || ++numVertices;
583
+ numTriangles++;
584
+ }
585
+ }
586
+ countElements(0, 0, max, max, max, 0);
587
+ countElements(max, max, 0, 0, 0, max);
588
+ const vertices = new Uint16Array(numVertices * 2);
589
+ const triangles = new Uint32Array(numTriangles * 3);
590
+ let triIndex = 0;
591
+ function processTriangle(ax, ay, bx, by, cx, cy) {
592
+ const mx = ax + bx >> 1;
593
+ const my = ay + by >> 1;
594
+ if (Math.abs(ax - cx) + Math.abs(ay - cy) > 1 && errors[my * size + mx] > maxError) {
595
+ processTriangle(cx, cy, ax, ay, mx, my);
596
+ processTriangle(bx, by, cx, cy, mx, my);
597
+ } else {
598
+ const a = indices[ay * size + ax] - 1;
599
+ const b = indices[by * size + bx] - 1;
600
+ const c = indices[cy * size + cx] - 1;
601
+ vertices[2 * a] = ax;
602
+ vertices[2 * a + 1] = ay;
603
+ vertices[2 * b] = bx;
604
+ vertices[2 * b + 1] = by;
605
+ vertices[2 * c] = cx;
606
+ vertices[2 * c + 1] = cy;
607
+ triangles[triIndex++] = a;
608
+ triangles[triIndex++] = b;
609
+ triangles[triIndex++] = c;
610
+ }
611
+ }
612
+ processTriangle(0, 0, max, max, max, 0);
613
+ processTriangle(max, max, 0, 0, 0, max);
614
+ return { vertices, triangles };
615
+ }
616
+ };
617
+
618
+ // src/lib/delatin/index.ts
619
+ var Delatin = class {
620
+ constructor(data, width, height = width) {
621
+ this.data = data;
622
+ this.width = width;
623
+ this.height = height;
624
+ this.coords = [];
625
+ this.triangles = [];
626
+ this._halfedges = [];
627
+ this._candidates = [];
628
+ this._queueIndices = [];
629
+ this._queue = [];
630
+ this._errors = [];
631
+ this._rms = [];
632
+ this._pending = [];
633
+ this._pendingLen = 0;
634
+ this._rmsSum = 0;
635
+ const x1 = width - 1;
636
+ const y1 = height - 1;
637
+ const p0 = this._addPoint(0, 0);
638
+ const p1 = this._addPoint(x1, 0);
639
+ const p2 = this._addPoint(0, y1);
640
+ const p3 = this._addPoint(x1, y1);
641
+ const t0 = this._addTriangle(p3, p0, p2, -1, -1, -1);
642
+ this._addTriangle(p0, p3, p1, t0, -1, -1);
643
+ this._flush();
644
+ }
645
+ run(maxError = 1) {
646
+ while (this.getMaxError() > maxError) {
647
+ this.refine();
648
+ }
649
+ }
650
+ refine() {
651
+ this._step();
652
+ this._flush();
653
+ }
654
+ getMaxError() {
655
+ return this._errors[0];
656
+ }
657
+ getRMSD() {
658
+ return this._rmsSum > 0 ? Math.sqrt(this._rmsSum / (this.width * this.height)) : 0;
659
+ }
660
+ heightAt(x, y) {
661
+ return this.data[this.width * y + x];
662
+ }
663
+ _flush() {
664
+ const coords = this.coords;
665
+ for (let i = 0; i < this._pendingLen; i++) {
666
+ const t = this._pending[i];
667
+ const a = 2 * this.triangles[t * 3 + 0];
668
+ const b = 2 * this.triangles[t * 3 + 1];
669
+ const c = 2 * this.triangles[t * 3 + 2];
670
+ this._findCandidate(coords[a], coords[a + 1], coords[b], coords[b + 1], coords[c], coords[c + 1], t);
671
+ }
672
+ this._pendingLen = 0;
673
+ }
674
+ _findCandidate(p0x, p0y, p1x, p1y, p2x, p2y, t) {
675
+ const minX = Math.min(p0x, p1x, p2x);
676
+ const minY = Math.min(p0y, p1y, p2y);
677
+ const maxX = Math.max(p0x, p1x, p2x);
678
+ const maxY = Math.max(p0y, p1y, p2y);
679
+ let w00 = orient(p1x, p1y, p2x, p2y, minX, minY);
680
+ let w01 = orient(p2x, p2y, p0x, p0y, minX, minY);
681
+ let w02 = orient(p0x, p0y, p1x, p1y, minX, minY);
682
+ const a01 = p1y - p0y;
683
+ const b01 = p0x - p1x;
684
+ const a12 = p2y - p1y;
685
+ const b12 = p1x - p2x;
686
+ const a20 = p0y - p2y;
687
+ const b20 = p2x - p0x;
688
+ const a = orient(p0x, p0y, p1x, p1y, p2x, p2y);
689
+ const z0 = this.heightAt(p0x, p0y) / a;
690
+ const z1 = this.heightAt(p1x, p1y) / a;
691
+ const z2 = this.heightAt(p2x, p2y) / a;
692
+ let maxError = 0;
693
+ let mx = 0;
694
+ let my = 0;
695
+ let rms = 0;
696
+ for (let y = minY; y <= maxY; y++) {
697
+ let dx = 0;
698
+ if (w00 < 0 && a12 !== 0) {
699
+ dx = Math.max(dx, Math.floor(-w00 / a12));
700
+ }
701
+ if (w01 < 0 && a20 !== 0) {
702
+ dx = Math.max(dx, Math.floor(-w01 / a20));
703
+ }
704
+ if (w02 < 0 && a01 !== 0) {
705
+ dx = Math.max(dx, Math.floor(-w02 / a01));
706
+ }
707
+ let w0 = w00 + a12 * dx;
708
+ let w1 = w01 + a20 * dx;
709
+ let w2 = w02 + a01 * dx;
710
+ let wasInside = false;
711
+ for (let x = minX + dx; x <= maxX; x++) {
712
+ if (w0 >= 0 && w1 >= 0 && w2 >= 0) {
713
+ wasInside = true;
714
+ const z = z0 * w0 + z1 * w1 + z2 * w2;
715
+ const dz = Math.abs(z - this.heightAt(x, y));
716
+ rms += dz * dz;
717
+ if (dz > maxError) {
718
+ maxError = dz;
719
+ mx = x;
720
+ my = y;
721
+ }
722
+ } else if (wasInside) {
723
+ break;
724
+ }
725
+ w0 += a12;
726
+ w1 += a20;
727
+ w2 += a01;
728
+ }
729
+ w00 += b12;
730
+ w01 += b20;
731
+ w02 += b01;
732
+ }
733
+ if (mx === p0x && my === p0y || mx === p1x && my === p1y || mx === p2x && my === p2y) {
734
+ maxError = 0;
735
+ }
736
+ this._candidates[2 * t] = mx;
737
+ this._candidates[2 * t + 1] = my;
738
+ this._rms[t] = rms;
739
+ this._queuePush(t, maxError, rms);
740
+ }
741
+ _step() {
742
+ const t = this._queuePop();
743
+ const e0 = t * 3 + 0;
744
+ const e1 = t * 3 + 1;
745
+ const e2 = t * 3 + 2;
746
+ const p0 = this.triangles[e0];
747
+ const p1 = this.triangles[e1];
748
+ const p2 = this.triangles[e2];
749
+ const ax = this.coords[2 * p0];
750
+ const ay = this.coords[2 * p0 + 1];
751
+ const bx = this.coords[2 * p1];
752
+ const by = this.coords[2 * p1 + 1];
753
+ const cx = this.coords[2 * p2];
754
+ const cy = this.coords[2 * p2 + 1];
755
+ const px = this._candidates[2 * t];
756
+ const py = this._candidates[2 * t + 1];
757
+ const pn = this._addPoint(px, py);
758
+ if (orient(ax, ay, bx, by, px, py) === 0) {
759
+ this._handleCollinear(pn, e0);
760
+ } else if (orient(bx, by, cx, cy, px, py) === 0) {
761
+ this._handleCollinear(pn, e1);
762
+ } else if (orient(cx, cy, ax, ay, px, py) === 0) {
763
+ this._handleCollinear(pn, e2);
764
+ } else {
765
+ const h0 = this._halfedges[e0];
766
+ const h1 = this._halfedges[e1];
767
+ const h2 = this._halfedges[e2];
768
+ const t0 = this._addTriangle(p0, p1, pn, h0, -1, -1, e0);
769
+ const t1 = this._addTriangle(p1, p2, pn, h1, -1, t0 + 1);
770
+ const t2 = this._addTriangle(p2, p0, pn, h2, t0 + 2, t1 + 1);
771
+ this._legalize(t0);
772
+ this._legalize(t1);
773
+ this._legalize(t2);
774
+ }
775
+ }
776
+ _addPoint(x, y) {
777
+ const i = this.coords.length >> 1;
778
+ this.coords.push(x, y);
779
+ return i;
780
+ }
781
+ _addTriangle(a, b, c, ab, bc, ca, e = this.triangles.length) {
782
+ const t = e / 3;
783
+ this.triangles[e + 0] = a;
784
+ this.triangles[e + 1] = b;
785
+ this.triangles[e + 2] = c;
786
+ this._halfedges[e + 0] = ab;
787
+ this._halfedges[e + 1] = bc;
788
+ this._halfedges[e + 2] = ca;
789
+ if (ab >= 0) {
790
+ this._halfedges[ab] = e + 0;
791
+ }
792
+ if (bc >= 0) {
793
+ this._halfedges[bc] = e + 1;
794
+ }
795
+ if (ca >= 0) {
796
+ this._halfedges[ca] = e + 2;
797
+ }
798
+ this._candidates[2 * t + 0] = 0;
799
+ this._candidates[2 * t + 1] = 0;
800
+ this._queueIndices[t] = -1;
801
+ this._rms[t] = 0;
802
+ this._pending[this._pendingLen++] = t;
803
+ return e;
804
+ }
805
+ _legalize(a) {
806
+ const b = this._halfedges[a];
807
+ if (b < 0) {
808
+ return;
809
+ }
810
+ const a0 = a - a % 3;
811
+ const b0 = b - b % 3;
812
+ const al = a0 + (a + 1) % 3;
813
+ const ar = a0 + (a + 2) % 3;
814
+ const bl = b0 + (b + 2) % 3;
815
+ const br = b0 + (b + 1) % 3;
816
+ const p0 = this.triangles[ar];
817
+ const pr = this.triangles[a];
818
+ const pl = this.triangles[al];
819
+ const p1 = this.triangles[bl];
820
+ const coords = this.coords;
821
+ if (!inCircle(coords[2 * p0], coords[2 * p0 + 1], coords[2 * pr], coords[2 * pr + 1], coords[2 * pl], coords[2 * pl + 1], coords[2 * p1], coords[2 * p1 + 1])) {
822
+ return;
823
+ }
824
+ const hal = this._halfedges[al];
825
+ const har = this._halfedges[ar];
826
+ const hbl = this._halfedges[bl];
827
+ const hbr = this._halfedges[br];
828
+ this._queueRemove(a0 / 3);
829
+ this._queueRemove(b0 / 3);
830
+ const t0 = this._addTriangle(p0, p1, pl, -1, hbl, hal, a0);
831
+ const t1 = this._addTriangle(p1, p0, pr, t0, har, hbr, b0);
832
+ this._legalize(t0 + 1);
833
+ this._legalize(t1 + 2);
834
+ }
835
+ _handleCollinear(pn, a) {
836
+ const a0 = a - a % 3;
837
+ const al = a0 + (a + 1) % 3;
838
+ const ar = a0 + (a + 2) % 3;
839
+ const p0 = this.triangles[ar];
840
+ const pr = this.triangles[a];
841
+ const pl = this.triangles[al];
842
+ const hal = this._halfedges[al];
843
+ const har = this._halfedges[ar];
844
+ const b = this._halfedges[a];
845
+ if (b < 0) {
846
+ const t02 = this._addTriangle(pn, p0, pr, -1, har, -1, a0);
847
+ const t12 = this._addTriangle(p0, pn, pl, t02, -1, hal);
848
+ this._legalize(t02 + 1);
849
+ this._legalize(t12 + 2);
850
+ return;
851
+ }
852
+ const b0 = b - b % 3;
853
+ const bl = b0 + (b + 2) % 3;
854
+ const br = b0 + (b + 1) % 3;
855
+ const p1 = this.triangles[bl];
856
+ const hbl = this._halfedges[bl];
857
+ const hbr = this._halfedges[br];
858
+ this._queueRemove(b0 / 3);
859
+ const t0 = this._addTriangle(p0, pr, pn, har, -1, -1, a0);
860
+ const t1 = this._addTriangle(pr, p1, pn, hbr, -1, t0 + 1, b0);
861
+ const t2 = this._addTriangle(p1, pl, pn, hbl, -1, t1 + 1);
862
+ const t3 = this._addTriangle(pl, p0, pn, hal, t0 + 2, t2 + 1);
863
+ this._legalize(t0);
864
+ this._legalize(t1);
865
+ this._legalize(t2);
866
+ this._legalize(t3);
867
+ }
868
+ _queuePush(t, error, rms) {
869
+ const i = this._queue.length;
870
+ this._queueIndices[t] = i;
871
+ this._queue.push(t);
872
+ this._errors.push(error);
873
+ this._rmsSum += rms;
874
+ this._queueUp(i);
875
+ }
876
+ _queuePop() {
877
+ const n = this._queue.length - 1;
878
+ this._queueSwap(0, n);
879
+ this._queueDown(0, n);
880
+ return this._queuePopBack();
881
+ }
882
+ _queuePopBack() {
883
+ const t = this._queue.pop();
884
+ this._errors.pop();
885
+ this._rmsSum -= this._rms[t];
886
+ this._queueIndices[t] = -1;
887
+ return t;
888
+ }
889
+ _queueRemove(t) {
890
+ const i = this._queueIndices[t];
891
+ if (i < 0) {
892
+ const it = this._pending.indexOf(t);
893
+ if (it !== -1) {
894
+ this._pending[it] = this._pending[--this._pendingLen];
895
+ } else {
896
+ throw new Error("Broken triangulation (something went wrong).");
897
+ }
898
+ return;
899
+ }
900
+ const n = this._queue.length - 1;
901
+ if (n !== i) {
902
+ this._queueSwap(i, n);
903
+ if (!this._queueDown(i, n)) {
904
+ this._queueUp(i);
905
+ }
906
+ }
907
+ this._queuePopBack();
908
+ }
909
+ _queueLess(i, j) {
910
+ return this._errors[i] > this._errors[j];
911
+ }
912
+ _queueSwap(i, j) {
913
+ const pi = this._queue[i];
914
+ const pj = this._queue[j];
915
+ this._queue[i] = pj;
916
+ this._queue[j] = pi;
917
+ this._queueIndices[pi] = j;
918
+ this._queueIndices[pj] = i;
919
+ const e = this._errors[i];
920
+ this._errors[i] = this._errors[j];
921
+ this._errors[j] = e;
922
+ }
923
+ _queueUp(j0) {
924
+ let j = j0;
925
+ while (true) {
926
+ const i = j - 1 >> 1;
927
+ if (i === j || !this._queueLess(j, i)) {
928
+ break;
929
+ }
930
+ this._queueSwap(i, j);
931
+ j = i;
932
+ }
933
+ }
934
+ _queueDown(i0, n) {
935
+ let i = i0;
936
+ while (true) {
937
+ const j1 = 2 * i + 1;
938
+ if (j1 >= n || j1 < 0) {
939
+ break;
940
+ }
941
+ const j2 = j1 + 1;
942
+ let j = j1;
943
+ if (j2 < n && this._queueLess(j2, j1)) {
944
+ j = j2;
945
+ }
946
+ if (!this._queueLess(j, i)) {
947
+ break;
948
+ }
949
+ this._queueSwap(i, j);
950
+ i = j;
951
+ }
952
+ return i > i0;
953
+ }
954
+ };
955
+ function orient(ax, ay, bx, by, cx, cy) {
956
+ return (bx - cx) * (ay - cy) - (by - cy) * (ax - cx);
957
+ }
958
+ function inCircle(ax, ay, bx, by, cx, cy, px, py) {
959
+ const dx = ax - px;
960
+ const dy = ay - py;
961
+ const ex = bx - px;
962
+ const ey = by - py;
963
+ const fx = cx - px;
964
+ const fy = cy - py;
965
+ const ap = dx * dx + dy * dy;
966
+ const bp = ex * ex + ey * ey;
967
+ const cp = fx * fx + fy * fy;
968
+ return dx * (ey * cp - bp * fy) - dy * (ex * cp - bp * fx) + ap * (ex * fy - ey * fx) < 0;
969
+ }
970
+
971
+ // src/lib/parse-terrain.ts
972
+ function makeTerrainMeshFromImage(terrainImage, terrainOptions) {
973
+ const {
974
+ meshMaxError,
975
+ bounds,
976
+ elevationDecoder
977
+ } = terrainOptions;
978
+ const {
979
+ data,
980
+ width,
981
+ height
982
+ } = terrainImage;
983
+ let terrain;
984
+ let mesh;
985
+ switch (terrainOptions.tesselator) {
986
+ case "martini":
987
+ terrain = getTerrain(data, width, height, elevationDecoder, terrainOptions.tesselator);
988
+ mesh = getMartiniTileMesh(meshMaxError, width, terrain);
989
+ break;
990
+ case "delatin":
991
+ terrain = getTerrain(data, width, height, elevationDecoder, terrainOptions.tesselator);
992
+ mesh = getDelatinTileMesh(meshMaxError, width, height, terrain);
993
+ break;
994
+ default:
995
+ if (width === height && !(height & width - 1)) {
996
+ terrain = getTerrain(data, width, height, elevationDecoder, "martini");
997
+ mesh = getMartiniTileMesh(meshMaxError, width, terrain);
998
+ } else {
999
+ terrain = getTerrain(data, width, height, elevationDecoder, "delatin");
1000
+ mesh = getDelatinTileMesh(meshMaxError, width, height, terrain);
1001
+ }
1002
+ break;
1003
+ }
1004
+ const {
1005
+ vertices
1006
+ } = mesh;
1007
+ let {
1008
+ triangles
1009
+ } = mesh;
1010
+ let attributes = getMeshAttributes2(vertices, terrain, width, height, bounds);
1011
+ const boundingBox = getMeshBoundingBox(attributes);
1012
+ if (terrainOptions.skirtHeight) {
1013
+ const {
1014
+ attributes: newAttributes,
1015
+ triangles: newTriangles
1016
+ } = addSkirt(attributes, triangles, terrainOptions.skirtHeight);
1017
+ attributes = newAttributes;
1018
+ triangles = newTriangles;
1019
+ }
1020
+ return {
1021
+ loaderData: {
1022
+ header: {}
1023
+ },
1024
+ header: {
1025
+ vertexCount: triangles.length,
1026
+ boundingBox
1027
+ },
1028
+ mode: 4,
1029
+ indices: {
1030
+ value: Uint32Array.from(triangles),
1031
+ size: 1
1032
+ },
1033
+ attributes
1034
+ };
1035
+ }
1036
+ function getMartiniTileMesh(meshMaxError, width, terrain) {
1037
+ const gridSize = width + 1;
1038
+ const martini = new Martini(gridSize);
1039
+ const tile = martini.createTile(terrain);
1040
+ const {
1041
+ vertices,
1042
+ triangles
1043
+ } = tile.getMesh(meshMaxError);
1044
+ return {
1045
+ vertices,
1046
+ triangles
1047
+ };
1048
+ }
1049
+ function getDelatinTileMesh(meshMaxError, width, height, terrain) {
1050
+ const tin = new Delatin(terrain, width + 1, height + 1);
1051
+ tin.run(meshMaxError);
1052
+ const {
1053
+ coords,
1054
+ triangles
1055
+ } = tin;
1056
+ const vertices = coords;
1057
+ return {
1058
+ vertices,
1059
+ triangles
1060
+ };
1061
+ }
1062
+ function getTerrain(imageData, width, height, elevationDecoder, tesselator) {
1063
+ const {
1064
+ rScaler,
1065
+ bScaler,
1066
+ gScaler,
1067
+ offset
1068
+ } = elevationDecoder;
1069
+ const terrain = new Float32Array((width + 1) * (height + 1));
1070
+ for (let i = 0, y = 0; y < height; y++) {
1071
+ for (let x = 0; x < width; x++, i++) {
1072
+ const k = i * 4;
1073
+ const r = imageData[k + 0];
1074
+ const g = imageData[k + 1];
1075
+ const b = imageData[k + 2];
1076
+ terrain[i + y] = r * rScaler + g * gScaler + b * bScaler + offset;
1077
+ }
1078
+ }
1079
+ if (tesselator === "martini") {
1080
+ for (let i = (width + 1) * width, x = 0; x < width; x++, i++) {
1081
+ terrain[i] = terrain[i - width - 1];
1082
+ }
1083
+ for (let i = height, y = 0; y < height + 1; y++, i += height + 1) {
1084
+ terrain[i] = terrain[i - 1];
1085
+ }
1086
+ }
1087
+ return terrain;
1088
+ }
1089
+ function getMeshAttributes2(vertices, terrain, width, height, bounds) {
1090
+ const gridSize = width + 1;
1091
+ const numOfVerticies = vertices.length / 2;
1092
+ const positions = new Float32Array(numOfVerticies * 3);
1093
+ const texCoords = new Float32Array(numOfVerticies * 2);
1094
+ const [minX, minY, maxX, maxY] = bounds || [0, 0, width, height];
1095
+ const xScale = (maxX - minX) / width;
1096
+ const yScale = (maxY - minY) / height;
1097
+ for (let i = 0; i < numOfVerticies; i++) {
1098
+ const x = vertices[i * 2];
1099
+ const y = vertices[i * 2 + 1];
1100
+ const pixelIdx = y * gridSize + x;
1101
+ positions[3 * i + 0] = x * xScale + minX;
1102
+ positions[3 * i + 1] = -y * yScale + maxY;
1103
+ positions[3 * i + 2] = terrain[pixelIdx];
1104
+ texCoords[2 * i + 0] = x / width;
1105
+ texCoords[2 * i + 1] = y / height;
1106
+ }
1107
+ return {
1108
+ POSITION: {
1109
+ value: positions,
1110
+ size: 3
1111
+ },
1112
+ TEXCOORD_0: {
1113
+ value: texCoords,
1114
+ size: 2
1115
+ }
1116
+ };
1117
+ }
1118
+
1119
+ // src/lib/utils/version.ts
1120
+ var VERSION = typeof __VERSION__ !== "undefined" ? __VERSION__ : "latest";
1121
+
1122
+ // src/terrain-loader.ts
1123
+ var TerrainLoader = {
1124
+ name: "Terrain",
1125
+ id: "terrain",
1126
+ module: "terrain",
1127
+ version: VERSION,
1128
+ worker: true,
1129
+ extensions: ["png", "pngraw", "jpg", "jpeg", "gif", "webp", "bmp"],
1130
+ mimeTypes: ["image/png", "image/jpeg", "image/gif", "image/webp", "image/bmp"],
1131
+ options: {
1132
+ terrain: {
1133
+ tesselator: "auto",
1134
+ bounds: void 0,
1135
+ meshMaxError: 10,
1136
+ elevationDecoder: {
1137
+ rScaler: 1,
1138
+ gScaler: 0,
1139
+ bScaler: 0,
1140
+ offset: 0
1141
+ },
1142
+ skirtHeight: void 0
1143
+ }
1144
+ }
1145
+ };
1146
+
1147
+ // src/quantized-mesh-loader.ts
1148
+ var QuantizedMeshLoader = {
1149
+ name: "Quantized Mesh",
1150
+ id: "quantized-mesh",
1151
+ module: "terrain",
1152
+ version: VERSION,
1153
+ worker: true,
1154
+ extensions: ["terrain"],
1155
+ mimeTypes: ["application/vnd.quantized-mesh"],
1156
+ options: {
1157
+ "quantized-mesh": {
1158
+ bounds: [0, 0, 1, 1],
1159
+ skirtHeight: null
1160
+ }
1161
+ }
1162
+ };
1163
+
1164
+ // src/index.ts
1165
+ var TerrainLoader2 = {
1166
+ ...TerrainLoader,
1167
+ parse: parseTerrain
1168
+ };
1169
+ async function parseTerrain(arrayBuffer, options, context) {
1170
+ const loadImageOptions = {
1171
+ ...options,
1172
+ mimeType: "application/x.image",
1173
+ image: {
1174
+ ...options?.image,
1175
+ type: "data"
1176
+ }
1177
+ };
1178
+ const image = await parseFromContext(arrayBuffer, [], loadImageOptions, context);
1179
+ const terrainOptions = {
1180
+ ...TerrainLoader2.options.terrain,
1181
+ ...options?.terrain
1182
+ };
1183
+ return makeTerrainMeshFromImage(image, terrainOptions);
1184
+ }
1185
+ var QuantizedMeshLoader2 = {
1186
+ ...QuantizedMeshLoader,
1187
+ parseSync: (arrayBuffer, options) => parseQuantizedMesh(arrayBuffer, options?.["quantized-mesh"]),
1188
+ parse: async (arrayBuffer, options) => parseQuantizedMesh(arrayBuffer, options?.["quantized-mesh"])
1189
+ };
1190
+ return __toCommonJS(src_exports);
1191
+ })();
1192
+ return __exports__;
1193
+ });