@loaders.gl/terrain 3.1.0-alpha.5 → 3.1.0-beta.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 (77) hide show
  1. package/dist/bundle.d.ts +1 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +1181 -0
  4. package/dist/es5/lib/decode-quantized-mesh.js.map +1 -1
  5. package/dist/es5/lib/delatin/index.js.map +1 -1
  6. package/dist/es5/lib/helpers/skirt.js +1 -1
  7. package/dist/es5/lib/helpers/skirt.js.map +1 -1
  8. package/dist/es5/lib/parse-quantized-mesh.js.map +1 -1
  9. package/dist/es5/lib/parse-terrain.js.map +1 -1
  10. package/dist/es5/lib/utils/version.js +1 -1
  11. package/dist/es5/lib/utils/version.js.map +1 -1
  12. package/dist/es5/workers/quantized-mesh-worker.js +7 -3
  13. package/dist/es5/workers/quantized-mesh-worker.js.map +1 -0
  14. package/dist/es5/workers/terrain-worker.js +7 -3
  15. package/dist/es5/workers/terrain-worker.js.map +1 -0
  16. package/dist/esm/lib/decode-quantized-mesh.js.map +1 -1
  17. package/dist/esm/lib/delatin/index.js.map +1 -1
  18. package/dist/esm/lib/helpers/skirt.js +1 -1
  19. package/dist/esm/lib/helpers/skirt.js.map +1 -1
  20. package/dist/esm/lib/parse-quantized-mesh.js.map +1 -1
  21. package/dist/esm/lib/parse-terrain.js.map +1 -1
  22. package/dist/esm/lib/utils/version.js +1 -1
  23. package/dist/esm/lib/utils/version.js.map +1 -1
  24. package/dist/esm/workers/quantized-mesh-worker.js +3 -3
  25. package/dist/esm/workers/quantized-mesh-worker.js.map +1 -0
  26. package/dist/esm/workers/terrain-worker.js +3 -3
  27. package/dist/esm/workers/terrain-worker.js.map +1 -0
  28. package/dist/index.d.ts +3 -2
  29. package/dist/index.d.ts.map +1 -0
  30. package/dist/index.js +26 -0
  31. package/dist/lib/decode-quantized-mesh.d.ts +25 -22
  32. package/dist/lib/decode-quantized-mesh.d.ts.map +1 -0
  33. package/dist/lib/decode-quantized-mesh.js +227 -0
  34. package/dist/lib/delatin/index.d.ts +2 -15
  35. package/dist/lib/delatin/index.d.ts.map +1 -0
  36. package/dist/lib/delatin/index.js +418 -0
  37. package/dist/lib/helpers/skirt.d.ts +10 -3
  38. package/dist/lib/helpers/skirt.d.ts.map +1 -0
  39. package/dist/lib/helpers/skirt.js +127 -0
  40. package/dist/lib/parse-quantized-mesh.d.ts +3 -2
  41. package/dist/lib/parse-quantized-mesh.d.ts.map +1 -0
  42. package/dist/lib/parse-quantized-mesh.js +95 -0
  43. package/dist/lib/parse-terrain.d.ts +2 -1
  44. package/dist/lib/parse-terrain.d.ts.map +1 -0
  45. package/dist/lib/parse-terrain.js +162 -0
  46. package/dist/lib/utils/version.d.ts +2 -1
  47. package/dist/lib/utils/version.d.ts.map +1 -0
  48. package/dist/lib/utils/version.js +7 -0
  49. package/dist/quantized-mesh-loader.d.ts +1 -0
  50. package/dist/quantized-mesh-loader.d.ts.map +1 -0
  51. package/dist/quantized-mesh-loader.js +23 -0
  52. package/dist/quantized-mesh-worker.js +1218 -2
  53. package/dist/terrain-loader.d.ts +1 -0
  54. package/dist/terrain-loader.d.ts.map +1 -0
  55. package/dist/terrain-loader.js +34 -0
  56. package/dist/terrain-worker.js +1218 -2
  57. package/dist/workers/quantized-mesh-worker.d.ts +1 -0
  58. package/dist/workers/quantized-mesh-worker.d.ts.map +1 -0
  59. package/dist/workers/quantized-mesh-worker.js +5 -0
  60. package/dist/workers/terrain-worker.d.ts +1 -0
  61. package/dist/workers/terrain-worker.d.ts.map +1 -0
  62. package/dist/workers/terrain-worker.js +5 -0
  63. package/package.json +7 -8
  64. package/src/lib/{decode-quantized-mesh.js → decode-quantized-mesh.ts} +6 -1
  65. package/src/lib/delatin/{index.js → index.ts} +2 -0
  66. package/src/lib/helpers/{skirt.js → skirt.ts} +18 -11
  67. package/src/lib/{parse-quantized-mesh.js → parse-quantized-mesh.ts} +0 -0
  68. package/src/lib/{parse-terrain.js → parse-terrain.ts} +37 -3
  69. package/src/lib/utils/{version.js → version.ts} +0 -0
  70. package/src/workers/quantized-mesh-worker.js +0 -1
  71. package/src/workers/quantized-mesh-worker.ts +4 -0
  72. package/src/workers/terrain-worker.js +0 -1
  73. package/src/workers/terrain-worker.ts +4 -0
  74. package/dist/dist.min.js +0 -2
  75. package/dist/dist.min.js.map +0 -1
  76. package/dist/quantized-mesh-worker.js.map +0 -1
  77. package/dist/terrain-worker.js.map +0 -1
@@ -1,12 +1,19 @@
1
+ export declare type EdgeIndices = {
2
+ westIndices: number[];
3
+ northIndices: number[];
4
+ eastIndices: number[];
5
+ southIndices: number[];
6
+ };
1
7
  /**
2
8
  * Add skirt to existing mesh
3
9
  * @param {object} attributes - POSITION and TEXCOOD_0 attributes data
4
10
  * @param {any} triangles - indices array of the mesh geometry
5
- * @param {number} skirtHeight - height of the skirt geometry
6
- * @param {object} outsideIndices - edge indices from quantized mesh data
11
+ * @param skirtHeight - height of the skirt geometry
12
+ * @param outsideIndices - edge indices from quantized mesh data
7
13
  * @returns - geometry data with added skirt
8
14
  */
9
- export function addSkirt(attributes: object, triangles: any, skirtHeight: number, outsideIndices?: object): {
15
+ export declare function addSkirt(attributes: any, triangles: any, skirtHeight: number, outsideIndices?: EdgeIndices): {
10
16
  attributes: any;
11
17
  triangles: any;
12
18
  };
19
+ //# sourceMappingURL=skirt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"skirt.d.ts","sourceRoot":"","sources":["../../../src/lib/helpers/skirt.ts"],"names":[],"mappings":"AAEA,oBAAY,WAAW,GAAG;IACxB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,YAAY,EAAE,MAAM,EAAE,CAAC;CACxB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,UAAU,KAAA,EAAE,SAAS,KAAA,EAAE,WAAW,EAAE,MAAM,EAAE,cAAc,CAAC,EAAE,WAAW;;;EAqChG"}
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addSkirt = void 0;
4
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
5
+ /**
6
+ * Add skirt to existing mesh
7
+ * @param {object} attributes - POSITION and TEXCOOD_0 attributes data
8
+ * @param {any} triangles - indices array of the mesh geometry
9
+ * @param skirtHeight - height of the skirt geometry
10
+ * @param outsideIndices - edge indices from quantized mesh data
11
+ * @returns - geometry data with added skirt
12
+ */
13
+ function addSkirt(attributes, triangles, skirtHeight, outsideIndices) {
14
+ const outsideEdges = outsideIndices
15
+ ? getOutsideEdgesFromIndices(outsideIndices, attributes.POSITION.value)
16
+ : getOutsideEdgesFromTriangles(triangles);
17
+ // 2 new vertices for each outside edge
18
+ const newPosition = new attributes.POSITION.value.constructor(outsideEdges.length * 6);
19
+ const newTexcoord0 = new attributes.TEXCOORD_0.value.constructor(outsideEdges.length * 4);
20
+ // 2 new triangles for each outside edge
21
+ const newTriangles = new triangles.constructor(outsideEdges.length * 6);
22
+ for (let i = 0; i < outsideEdges.length; i++) {
23
+ const edge = outsideEdges[i];
24
+ updateAttributesForNewEdge({
25
+ edge,
26
+ edgeIndex: i,
27
+ attributes,
28
+ skirtHeight,
29
+ newPosition,
30
+ newTexcoord0,
31
+ newTriangles
32
+ });
33
+ }
34
+ attributes.POSITION.value = (0, loader_utils_1.concatenateTypedArrays)(attributes.POSITION.value, newPosition);
35
+ attributes.TEXCOORD_0.value = (0, loader_utils_1.concatenateTypedArrays)(attributes.TEXCOORD_0.value, newTexcoord0);
36
+ const resultTriangles = triangles instanceof Array
37
+ ? triangles.concat(newTriangles)
38
+ : (0, loader_utils_1.concatenateTypedArrays)(triangles, newTriangles);
39
+ return {
40
+ attributes,
41
+ triangles: resultTriangles
42
+ };
43
+ }
44
+ exports.addSkirt = addSkirt;
45
+ /**
46
+ * Get geometry edges that located on a border of the mesh
47
+ * @param {any} triangles - indices array of the mesh geometry
48
+ * @returns {number[][]} - outside edges data
49
+ */
50
+ function getOutsideEdgesFromTriangles(triangles) {
51
+ const edges = [];
52
+ for (let i = 0; i < triangles.length; i += 3) {
53
+ edges.push([triangles[i], triangles[i + 1]]);
54
+ edges.push([triangles[i + 1], triangles[i + 2]]);
55
+ edges.push([triangles[i + 2], triangles[i]]);
56
+ }
57
+ edges.sort((a, b) => Math.min(...a) - Math.min(...b) || Math.max(...a) - Math.max(...b));
58
+ const outsideEdges = [];
59
+ let index = 1;
60
+ while (index < edges.length) {
61
+ if (edges[index][0] === edges[index - 1][1] && edges[index][1] === edges[index - 1][0]) {
62
+ index += 2;
63
+ }
64
+ else {
65
+ outsideEdges.push(edges[index - 1]);
66
+ index++;
67
+ }
68
+ }
69
+ return outsideEdges;
70
+ }
71
+ /**
72
+ * Get geometry edges that located on a border of the mesh
73
+ * @param {object} indices - edge indices from quantized mesh data
74
+ * @param {TypedArray} position - position attribute geometry data
75
+ * @returns {number[][]} - outside edges data
76
+ */
77
+ function getOutsideEdgesFromIndices(indices, position) {
78
+ // Sort skirt indices to create adjacent triangles
79
+ indices.westIndices.sort((a, b) => position[3 * a + 1] - position[3 * b + 1]);
80
+ // Reverse (b - a) to match triangle winding
81
+ indices.eastIndices.sort((a, b) => position[3 * b + 1] - position[3 * a + 1]);
82
+ indices.southIndices.sort((a, b) => position[3 * b] - position[3 * a]);
83
+ // Reverse (b - a) to match triangle winding
84
+ indices.northIndices.sort((a, b) => position[3 * a] - position[3 * b]);
85
+ const edges = [];
86
+ for (const index in indices) {
87
+ const indexGroup = indices[index];
88
+ for (let i = 0; i < indexGroup.length - 1; i++) {
89
+ edges.push([indexGroup[i], indexGroup[i + 1]]);
90
+ }
91
+ }
92
+ return edges;
93
+ }
94
+ /**
95
+ * Get geometry edges that located on a border of the mesh
96
+ * @param {object} args
97
+ * @param {number[]} args.edge - edge indices in geometry
98
+ * @param {number} args.edgeIndex - edge index in outsideEdges array
99
+ * @param {object} args.attributes - POSITION and TEXCOORD_0 attributes
100
+ * @param {number} args.skirtHeight - height of the skirt geometry
101
+ * @param {TypedArray} args.newPosition - POSITION array for skirt data
102
+ * @param {TypedArray} args.newTexcoord0 - TEXCOORD_0 array for skirt data
103
+ * @param {TypedArray | Array} args.newTriangles - trinagle indices array for skirt data
104
+ * @returns {void}
105
+ */
106
+ function updateAttributesForNewEdge({ edge, edgeIndex, attributes, skirtHeight, newPosition, newTexcoord0, newTriangles }) {
107
+ const positionsLength = attributes.POSITION.value.length;
108
+ const vertex1Offset = edgeIndex * 2;
109
+ const vertex2Offset = edgeIndex * 2 + 1;
110
+ // Define POSITION for new 1st vertex
111
+ newPosition.set(attributes.POSITION.value.subarray(edge[0] * 3, edge[0] * 3 + 3), vertex1Offset * 3);
112
+ newPosition[vertex1Offset * 3 + 2] = newPosition[vertex1Offset * 3 + 2] - skirtHeight; // put down elevation on the skirt height
113
+ // Define POSITION for new 2nd vertex
114
+ newPosition.set(attributes.POSITION.value.subarray(edge[1] * 3, edge[1] * 3 + 3), vertex2Offset * 3);
115
+ newPosition[vertex2Offset * 3 + 2] = newPosition[vertex2Offset * 3 + 2] - skirtHeight; // put down elevation on the skirt height
116
+ // Use same TEXCOORDS for skirt vertices
117
+ newTexcoord0.set(attributes.TEXCOORD_0.value.subarray(edge[0] * 2, edge[0] * 2 + 2), vertex1Offset * 2);
118
+ newTexcoord0.set(attributes.TEXCOORD_0.value.subarray(edge[1] * 2, edge[1] * 2 + 2), vertex2Offset * 2);
119
+ // Define new triangles
120
+ const triangle1Offset = edgeIndex * 2 * 3;
121
+ newTriangles[triangle1Offset] = edge[0];
122
+ newTriangles[triangle1Offset + 1] = edge[1];
123
+ newTriangles[triangle1Offset + 2] = positionsLength / 3 + vertex2Offset;
124
+ newTriangles[triangle1Offset + 3] = positionsLength / 3 + vertex2Offset;
125
+ newTriangles[triangle1Offset + 4] = positionsLength / 3 + vertex1Offset;
126
+ newTriangles[triangle1Offset + 5] = edge[0];
127
+ }
@@ -3,12 +3,12 @@ export default function loadQuantizedMesh(arrayBuffer: any, options: any): {
3
3
  header: {};
4
4
  };
5
5
  header: {
6
- vertexCount: number;
6
+ vertexCount: any;
7
7
  boundingBox: [[number, number, number], [number, number, number]];
8
8
  };
9
9
  mode: number;
10
10
  indices: {
11
- value: Uint16Array | Uint32Array | undefined;
11
+ value: any;
12
12
  size: number;
13
13
  };
14
14
  attributes: {
@@ -22,3 +22,4 @@ export default function loadQuantizedMesh(arrayBuffer: any, options: any): {
22
22
  };
23
23
  };
24
24
  } | null;
25
+ //# sourceMappingURL=parse-quantized-mesh.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-quantized-mesh.d.ts","sourceRoot":"","sources":["../../src/lib/parse-quantized-mesh.ts"],"names":[],"mappings":"AA+FA,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,WAAW,KAAA,EAAE,OAAO,KAAA;;;;;;;;;;;;;;;;;;;;;;;SAE7D"}
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ const schema_1 = require("@loaders.gl/schema");
23
+ const decode_quantized_mesh_1 = __importStar(require("./decode-quantized-mesh"));
24
+ const skirt_1 = require("./helpers/skirt");
25
+ function getMeshAttributes(vertexData, header, bounds) {
26
+ const { minHeight, maxHeight } = header;
27
+ const [minX, minY, maxX, maxY] = bounds || [0, 0, 1, 1];
28
+ const xScale = maxX - minX;
29
+ const yScale = maxY - minY;
30
+ const zScale = maxHeight - minHeight;
31
+ const nCoords = vertexData.length / 3;
32
+ // vec3. x, y defined by bounds, z in meters
33
+ const positions = new Float32Array(nCoords * 3);
34
+ // vec2. 1 to 1 relationship with position. represents the uv on the texture image. 0,0 to 1,1.
35
+ const texCoords = new Float32Array(nCoords * 2);
36
+ // Data is not interleaved; all u, then all v, then all heights
37
+ for (let i = 0; i < nCoords; i++) {
38
+ const x = vertexData[i] / 32767;
39
+ const y = vertexData[i + nCoords] / 32767;
40
+ const z = vertexData[i + nCoords * 2] / 32767;
41
+ positions[3 * i + 0] = x * xScale + minX;
42
+ positions[3 * i + 1] = y * yScale + minY;
43
+ positions[3 * i + 2] = z * zScale + minHeight;
44
+ texCoords[2 * i + 0] = x;
45
+ texCoords[2 * i + 1] = y;
46
+ }
47
+ return {
48
+ POSITION: { value: positions, size: 3 },
49
+ TEXCOORD_0: { value: texCoords, size: 2 }
50
+ // TODO: Parse normals if they exist in the file
51
+ // NORMAL: {}, - optional, but creates the high poly look with lighting
52
+ };
53
+ }
54
+ function getTileMesh(arrayBuffer, options) {
55
+ if (!arrayBuffer) {
56
+ return null;
57
+ }
58
+ const { bounds } = options;
59
+ // Don't parse edge indices or format extensions
60
+ const { header, vertexData, triangleIndices: originalTriangleIndices, westIndices, northIndices, eastIndices, southIndices } = (0, decode_quantized_mesh_1.default)(arrayBuffer, decode_quantized_mesh_1.DECODING_STEPS.triangleIndices);
61
+ let triangleIndices = originalTriangleIndices;
62
+ let attributes = getMeshAttributes(vertexData, header, bounds);
63
+ // Compute bounding box before adding skirt so that z values are not skewed
64
+ // TODO: Find bounding box from header, instead of doing extra pass over
65
+ // vertices.
66
+ const boundingBox = (0, schema_1.getMeshBoundingBox)(attributes);
67
+ if (options.skirtHeight) {
68
+ const { attributes: newAttributes, triangles: newTriangles } = (0, skirt_1.addSkirt)(attributes, triangleIndices, options.skirtHeight, {
69
+ westIndices,
70
+ northIndices,
71
+ eastIndices,
72
+ southIndices
73
+ });
74
+ attributes = newAttributes;
75
+ triangleIndices = newTriangles;
76
+ }
77
+ return {
78
+ // Data return by this loader implementation
79
+ loaderData: {
80
+ header: {}
81
+ },
82
+ header: {
83
+ // @ts-ignore
84
+ vertexCount: triangleIndices.length,
85
+ boundingBox
86
+ },
87
+ mode: 4,
88
+ indices: { value: triangleIndices, size: 1 },
89
+ attributes
90
+ };
91
+ }
92
+ function loadQuantizedMesh(arrayBuffer, options) {
93
+ return getTileMesh(arrayBuffer, options['quantized-mesh']);
94
+ }
95
+ exports.default = loadQuantizedMesh;
@@ -3,7 +3,7 @@ export default function loadTerrain(arrayBuffer: any, options: any, context: any
3
3
  header: {};
4
4
  };
5
5
  header: {
6
- vertexCount: number;
6
+ vertexCount: any;
7
7
  boundingBox: [[number, number, number], [number, number, number]];
8
8
  };
9
9
  mode: number;
@@ -22,3 +22,4 @@ export default function loadTerrain(arrayBuffer: any, options: any, context: any
22
22
  };
23
23
  };
24
24
  } | null>;
25
+ //# sourceMappingURL=parse-terrain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-terrain.d.ts","sourceRoot":"","sources":["../../src/lib/parse-terrain.ts"],"names":[],"mappings":"AA8MA,wBAA8B,WAAW,CAAC,WAAW,KAAA,EAAE,OAAO,KAAA,EAAE,OAAO,KAAA;;;;;;;;;;;;;;;;;;;;;;;UAMtE"}
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const schema_1 = require("@loaders.gl/schema");
7
+ const martini_1 = __importDefault(require("@mapbox/martini"));
8
+ const delatin_1 = __importDefault(require("./delatin"));
9
+ const skirt_1 = require("./helpers/skirt");
10
+ function getTerrain(imageData, width, height, elevationDecoder, tesselator) {
11
+ const { rScaler, bScaler, gScaler, offset } = elevationDecoder;
12
+ // From Martini demo
13
+ // https://observablehq.com/@mourner/martin-real-time-rtin-terrain-mesh
14
+ const terrain = new Float32Array((width + 1) * (height + 1));
15
+ // decode terrain values
16
+ for (let i = 0, y = 0; y < height; y++) {
17
+ for (let x = 0; x < width; x++, i++) {
18
+ const k = i * 4;
19
+ const r = imageData[k + 0];
20
+ const g = imageData[k + 1];
21
+ const b = imageData[k + 2];
22
+ terrain[i + y] = r * rScaler + g * gScaler + b * bScaler + offset;
23
+ }
24
+ }
25
+ if (tesselator === 'martini') {
26
+ // backfill bottom border
27
+ for (let i = (width + 1) * width, x = 0; x < width; x++, i++) {
28
+ terrain[i] = terrain[i - width - 1];
29
+ }
30
+ // backfill right border
31
+ for (let i = height, y = 0; y < height + 1; y++, i += height + 1) {
32
+ terrain[i] = terrain[i - 1];
33
+ }
34
+ }
35
+ return terrain;
36
+ }
37
+ function getMeshAttributes(vertices, terrain, width, height, bounds) {
38
+ const gridSize = width + 1;
39
+ const numOfVerticies = vertices.length / 2;
40
+ // vec3. x, y in pixels, z in meters
41
+ const positions = new Float32Array(numOfVerticies * 3);
42
+ // vec2. 1 to 1 relationship with position. represents the uv on the texture image. 0,0 to 1,1.
43
+ const texCoords = new Float32Array(numOfVerticies * 2);
44
+ const [minX, minY, maxX, maxY] = bounds || [0, 0, width, height];
45
+ const xScale = (maxX - minX) / width;
46
+ const yScale = (maxY - minY) / height;
47
+ for (let i = 0; i < numOfVerticies; i++) {
48
+ const x = vertices[i * 2];
49
+ const y = vertices[i * 2 + 1];
50
+ const pixelIdx = y * gridSize + x;
51
+ positions[3 * i + 0] = x * xScale + minX;
52
+ positions[3 * i + 1] = -y * yScale + maxY;
53
+ positions[3 * i + 2] = terrain[pixelIdx];
54
+ texCoords[2 * i + 0] = x / width;
55
+ texCoords[2 * i + 1] = y / height;
56
+ }
57
+ return {
58
+ POSITION: { value: positions, size: 3 },
59
+ TEXCOORD_0: { value: texCoords, size: 2 }
60
+ // NORMAL: {}, - optional, but creates the high poly look with lighting
61
+ };
62
+ }
63
+ /**
64
+ * Returns generated mesh object from image data
65
+ *
66
+ * @param {object} terrainImage terrain image data
67
+ * @param {object} terrainOptions terrain options
68
+ * @returns mesh object
69
+ */
70
+ function getMesh(terrainImage, terrainOptions) {
71
+ if (terrainImage === null) {
72
+ return null;
73
+ }
74
+ const { meshMaxError, bounds, elevationDecoder } = terrainOptions;
75
+ const { data, width, height } = terrainImage;
76
+ let terrain;
77
+ let mesh;
78
+ switch (terrainOptions.tesselator) {
79
+ case 'martini':
80
+ terrain = getTerrain(data, width, height, elevationDecoder, terrainOptions.tesselator);
81
+ mesh = getMartiniTileMesh(meshMaxError, width, terrain);
82
+ break;
83
+ case 'delatin':
84
+ terrain = getTerrain(data, width, height, elevationDecoder, terrainOptions.tesselator);
85
+ mesh = getDelatinTileMesh(meshMaxError, width, height, terrain);
86
+ break;
87
+ // auto
88
+ default:
89
+ if (width === height && !(height & (width - 1))) {
90
+ terrain = getTerrain(data, width, height, elevationDecoder, 'martini');
91
+ mesh = getMartiniTileMesh(meshMaxError, width, terrain);
92
+ }
93
+ else {
94
+ terrain = getTerrain(data, width, height, elevationDecoder, 'delatin');
95
+ mesh = getDelatinTileMesh(meshMaxError, width, height, terrain);
96
+ }
97
+ break;
98
+ }
99
+ const { vertices } = mesh;
100
+ let { triangles } = mesh;
101
+ let attributes = getMeshAttributes(vertices, terrain, width, height, bounds);
102
+ // Compute bounding box before adding skirt so that z values are not skewed
103
+ const boundingBox = (0, schema_1.getMeshBoundingBox)(attributes);
104
+ if (terrainOptions.skirtHeight) {
105
+ const { attributes: newAttributes, triangles: newTriangles } = (0, skirt_1.addSkirt)(attributes, triangles, terrainOptions.skirtHeight);
106
+ attributes = newAttributes;
107
+ triangles = newTriangles;
108
+ }
109
+ return {
110
+ // Data return by this loader implementation
111
+ loaderData: {
112
+ header: {}
113
+ },
114
+ header: {
115
+ vertexCount: triangles.length,
116
+ boundingBox
117
+ },
118
+ mode: 4,
119
+ indices: { value: Uint32Array.from(triangles), size: 1 },
120
+ attributes
121
+ };
122
+ }
123
+ /**
124
+ * Get Martini generated vertices and triangles
125
+ *
126
+ * @param {number} meshMaxError threshold for simplifying mesh
127
+ * @param {number} width width of the input data
128
+ * @param {number[] | Float32Array} terrain elevation data
129
+ * @returns {{vertices: Uint16Array, triangles: Uint32Array}} vertices and triangles data
130
+ */
131
+ function getMartiniTileMesh(meshMaxError, width, terrain) {
132
+ const gridSize = width + 1;
133
+ const martini = new martini_1.default(gridSize);
134
+ const tile = martini.createTile(terrain);
135
+ const { vertices, triangles } = tile.getMesh(meshMaxError);
136
+ return { vertices, triangles };
137
+ }
138
+ /**
139
+ * Get Delatin generated vertices and triangles
140
+ *
141
+ * @param {number} meshMaxError threshold for simplifying mesh
142
+ * @param {number} width width of the input data array
143
+ * @param {number} height height of the input data array
144
+ * @param {number[] | Float32Array} terrain elevation data
145
+ * @returns {{vertices: number[], triangles: number[]}} vertices and triangles data
146
+ */
147
+ function getDelatinTileMesh(meshMaxError, width, height, terrain) {
148
+ const tin = new delatin_1.default(terrain, width + 1, height + 1);
149
+ tin.run(meshMaxError);
150
+ // @ts-expect-error
151
+ const { coords, triangles } = tin;
152
+ const vertices = coords;
153
+ return { vertices, triangles };
154
+ }
155
+ async function loadTerrain(arrayBuffer, options, context) {
156
+ options.image = options.image || {};
157
+ options.image.type = 'data';
158
+ const image = await context.parse(arrayBuffer, options, options.baseUri);
159
+ // Extend function to support additional mesh generation options (square grid or delatin)
160
+ return getMesh(image, options.terrain);
161
+ }
162
+ exports.default = loadTerrain;
@@ -1 +1,2 @@
1
- export const VERSION: any;
1
+ export declare const VERSION: any;
2
+ //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/version.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,OAAO,KAA8D,CAAC"}
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VERSION = void 0;
4
+ // Version constant cannot be imported, it needs to correspond to the build version of **this** module.
5
+ // __VERSION__ is injected by babel-plugin-version-inline
6
+ // @ts-ignore TS2304: Cannot find name '__VERSION__'.
7
+ exports.VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
@@ -18,3 +18,4 @@ export declare const QuantizedMeshLoader: {
18
18
  };
19
19
  };
20
20
  export declare const _typecheckQuantizedMeshLoader: Loader;
21
+ //# sourceMappingURL=quantized-mesh-loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quantized-mesh-loader.d.ts","sourceRoot":"","sources":["../src/quantized-mesh-loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,0BAA0B,CAAC;AAGrD;;GAEG;AACH,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;CAc/B,CAAC;AAEF,eAAO,MAAM,6BAA6B,EAAE,MAA4B,CAAC"}
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports._typecheckQuantizedMeshLoader = exports.QuantizedMeshLoader = void 0;
4
+ const version_1 = require("./lib/utils/version");
5
+ /**
6
+ * Worker loader for quantized meshes
7
+ */
8
+ exports.QuantizedMeshLoader = {
9
+ name: 'Quantized Mesh',
10
+ id: 'quantized-mesh',
11
+ module: 'terrain',
12
+ version: version_1.VERSION,
13
+ worker: true,
14
+ extensions: ['terrain'],
15
+ mimeTypes: ['application/vnd.quantized-mesh'],
16
+ options: {
17
+ 'quantized-mesh': {
18
+ bounds: [0, 0, 1, 1],
19
+ skirtHeight: null
20
+ }
21
+ }
22
+ };
23
+ exports._typecheckQuantizedMeshLoader = exports.QuantizedMeshLoader;