@loaders.gl/terrain 4.0.0-alpha.4 → 4.0.0-alpha.5

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