@loaders.gl/terrain 3.1.0-alpha.1 → 4.0.0-alpha.1
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.
- package/dist/bundle.js +2 -4
- package/dist/bundle.js.map +1 -1
- package/dist/lib/decode-quantized-mesh.js.map +1 -1
- package/dist/lib/delatin/index.js.map +1 -1
- package/dist/lib/helpers/skirt.js +1 -1
- package/dist/lib/helpers/skirt.js.map +1 -1
- package/dist/lib/parse-quantized-mesh.js.map +1 -1
- package/dist/lib/parse-terrain.js.map +1 -1
- package/dist/lib/utils/version.js +1 -1
- package/dist/quantized-mesh-worker.js +1218 -2
- package/dist/terrain-worker.js +1218 -2
- package/dist/workers/quantized-mesh-worker.js +3 -3
- package/dist/workers/quantized-mesh-worker.js.map +1 -0
- package/dist/workers/terrain-worker.js +3 -3
- package/dist/workers/terrain-worker.js.map +1 -0
- package/package.json +7 -7
- package/src/bundle.ts +2 -3
- package/src/lib/decode-quantized-mesh.ts +325 -0
- package/src/lib/delatin/index.js +2 -0
- package/src/lib/helpers/skirt.ts +168 -0
- package/src/lib/parse-quantized-mesh.ts +98 -0
- package/src/lib/parse-terrain.ts +212 -0
- package/src/workers/quantized-mesh-worker.ts +4 -0
- package/src/workers/terrain-worker.ts +4 -0
- package/dist/dist.min.js +0 -2
- package/dist/dist.min.js.map +0 -1
- package/dist/quantized-mesh-worker.js.map +0 -1
- package/dist/terrain-worker.js.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {createLoaderWorker} from '@loaders.gl/loader-utils';
|
|
2
|
-
import {QuantizedMeshLoader} from '../index';
|
|
3
|
-
|
|
1
|
+
import { createLoaderWorker } from '@loaders.gl/loader-utils';
|
|
2
|
+
import { QuantizedMeshLoader } from '../index';
|
|
4
3
|
createLoaderWorker(QuantizedMeshLoader);
|
|
4
|
+
//# sourceMappingURL=quantized-mesh-worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/workers/quantized-mesh-worker.ts"],"names":["createLoaderWorker","QuantizedMeshLoader"],"mappings":"AAAA,SAAQA,kBAAR,QAAiC,0BAAjC;AACA,SAAQC,mBAAR,QAAkC,UAAlC;AAEAD,kBAAkB,CAACC,mBAAD,CAAlB","sourcesContent":["import {createLoaderWorker} from '@loaders.gl/loader-utils';\nimport {QuantizedMeshLoader} from '../index';\n\ncreateLoaderWorker(QuantizedMeshLoader);\n"],"file":"quantized-mesh-worker.js"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {createLoaderWorker} from '@loaders.gl/loader-utils';
|
|
2
|
-
import {TerrainLoader} from '../index';
|
|
3
|
-
|
|
1
|
+
import { createLoaderWorker } from '@loaders.gl/loader-utils';
|
|
2
|
+
import { TerrainLoader } from '../index';
|
|
4
3
|
createLoaderWorker(TerrainLoader);
|
|
4
|
+
//# sourceMappingURL=terrain-worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/workers/terrain-worker.ts"],"names":["createLoaderWorker","TerrainLoader"],"mappings":"AAAA,SAAQA,kBAAR,QAAiC,0BAAjC;AACA,SAAQC,aAAR,QAA4B,UAA5B;AAEAD,kBAAkB,CAACC,aAAD,CAAlB","sourcesContent":["import {createLoaderWorker} from '@loaders.gl/loader-utils';\nimport {TerrainLoader} from '../index';\n\ncreateLoaderWorker(TerrainLoader);\n"],"file":"terrain-worker.js"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loaders.gl/terrain",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-alpha.1",
|
|
4
4
|
"description": "Framework-independent loader for terrain raster formats",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -29,15 +29,15 @@
|
|
|
29
29
|
],
|
|
30
30
|
"scripts": {
|
|
31
31
|
"pre-build": "npm run build-worker && npm run build-worker2 && npm run build-bundle",
|
|
32
|
-
"build-bundle": "
|
|
33
|
-
"build-worker": "
|
|
34
|
-
"build-worker2": "
|
|
32
|
+
"build-bundle": "esbuild src/bundle.ts --bundle --outfile=dist/bundle.js",
|
|
33
|
+
"build-worker": "esbuild src/workers/terrain-worker.js --bundle --outfile=dist/terrain-worker.js",
|
|
34
|
+
"build-worker2": "esbuild src/workers/quantized-mesh-worker.js --bundle --outfile=dist/quantized-mesh-worker.js"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@babel/runtime": "^7.3.1",
|
|
38
|
-
"@loaders.gl/loader-utils": "
|
|
39
|
-
"@loaders.gl/schema": "
|
|
38
|
+
"@loaders.gl/loader-utils": "4.0.0-alpha.1",
|
|
39
|
+
"@loaders.gl/schema": "4.0.0-alpha.1",
|
|
40
40
|
"@mapbox/martini": "^0.2.0"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "e48f5534fe7188c810f23f965a440c144b3cf6d0"
|
|
43
43
|
}
|
package/src/bundle.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
2
|
const moduleExports = require('./index');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
module.exports = Object.assign(_global.loaders, moduleExports);
|
|
3
|
+
globalThis.loaders = globalThis.loaders || {};
|
|
4
|
+
module.exports = Object.assign(globalThis.loaders, moduleExports);
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
// Copyright (C) 2018-2019 HERE Europe B.V.
|
|
2
|
+
//
|
|
3
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
// in the Software without restriction, including without limitation the rights
|
|
6
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
// furnished to do so, subject to the following conditions:
|
|
9
|
+
//
|
|
10
|
+
// The above copyright notice and this permission notice shall be included in
|
|
11
|
+
// all copies or substantial portions of the Software.
|
|
12
|
+
//
|
|
13
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
// SOFTWARE.
|
|
20
|
+
|
|
21
|
+
const QUANTIZED_MESH_HEADER = new Map([
|
|
22
|
+
['centerX', Float64Array.BYTES_PER_ELEMENT],
|
|
23
|
+
['centerY', Float64Array.BYTES_PER_ELEMENT],
|
|
24
|
+
['centerZ', Float64Array.BYTES_PER_ELEMENT],
|
|
25
|
+
|
|
26
|
+
['minHeight', Float32Array.BYTES_PER_ELEMENT],
|
|
27
|
+
['maxHeight', Float32Array.BYTES_PER_ELEMENT],
|
|
28
|
+
|
|
29
|
+
['boundingSphereCenterX', Float64Array.BYTES_PER_ELEMENT],
|
|
30
|
+
['boundingSphereCenterY', Float64Array.BYTES_PER_ELEMENT],
|
|
31
|
+
['boundingSphereCenterZ', Float64Array.BYTES_PER_ELEMENT],
|
|
32
|
+
['boundingSphereRadius', Float64Array.BYTES_PER_ELEMENT],
|
|
33
|
+
|
|
34
|
+
['horizonOcclusionPointX', Float64Array.BYTES_PER_ELEMENT],
|
|
35
|
+
['horizonOcclusionPointY', Float64Array.BYTES_PER_ELEMENT],
|
|
36
|
+
['horizonOcclusionPointZ', Float64Array.BYTES_PER_ELEMENT]
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
function decodeZigZag(value) {
|
|
40
|
+
return (value >> 1) ^ -(value & 1);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function decodeHeader(dataView) {
|
|
44
|
+
let position = 0;
|
|
45
|
+
const header = {};
|
|
46
|
+
|
|
47
|
+
for (const [key, bytesCount] of QUANTIZED_MESH_HEADER) {
|
|
48
|
+
const getter = bytesCount === 8 ? dataView.getFloat64 : dataView.getFloat32;
|
|
49
|
+
|
|
50
|
+
header[key] = getter.call(dataView, position, true);
|
|
51
|
+
position += bytesCount;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {header, headerEndPosition: position};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function decodeVertexData(dataView, headerEndPosition) {
|
|
58
|
+
let position = headerEndPosition;
|
|
59
|
+
const elementsPerVertex = 3;
|
|
60
|
+
const vertexCount = dataView.getUint32(position, true);
|
|
61
|
+
const vertexData = new Uint16Array(vertexCount * elementsPerVertex);
|
|
62
|
+
|
|
63
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
64
|
+
|
|
65
|
+
const bytesPerArrayElement = Uint16Array.BYTES_PER_ELEMENT;
|
|
66
|
+
const elementArrayLength = vertexCount * bytesPerArrayElement;
|
|
67
|
+
const uArrayStartPosition = position;
|
|
68
|
+
const vArrayStartPosition = uArrayStartPosition + elementArrayLength;
|
|
69
|
+
const heightArrayStartPosition = vArrayStartPosition + elementArrayLength;
|
|
70
|
+
|
|
71
|
+
let u = 0;
|
|
72
|
+
let v = 0;
|
|
73
|
+
let height = 0;
|
|
74
|
+
|
|
75
|
+
for (let i = 0; i < vertexCount; i++) {
|
|
76
|
+
u += decodeZigZag(dataView.getUint16(uArrayStartPosition + bytesPerArrayElement * i, true));
|
|
77
|
+
v += decodeZigZag(dataView.getUint16(vArrayStartPosition + bytesPerArrayElement * i, true));
|
|
78
|
+
height += decodeZigZag(
|
|
79
|
+
dataView.getUint16(heightArrayStartPosition + bytesPerArrayElement * i, true)
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
vertexData[i] = u;
|
|
83
|
+
vertexData[i + vertexCount] = v;
|
|
84
|
+
vertexData[i + vertexCount * 2] = height;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
position += elementArrayLength * 3;
|
|
88
|
+
|
|
89
|
+
return {vertexData, vertexDataEndPosition: position};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function decodeIndex(buffer, position, indicesCount, bytesPerIndex, encoded = true) {
|
|
93
|
+
let indices;
|
|
94
|
+
|
|
95
|
+
if (bytesPerIndex === 2) {
|
|
96
|
+
indices = new Uint16Array(buffer, position, indicesCount);
|
|
97
|
+
} else {
|
|
98
|
+
indices = new Uint32Array(buffer, position, indicesCount);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!encoded) {
|
|
102
|
+
return indices;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let highest = 0;
|
|
106
|
+
|
|
107
|
+
for (let i = 0; i < indices.length; ++i) {
|
|
108
|
+
const code = indices[i];
|
|
109
|
+
|
|
110
|
+
indices[i] = highest - code;
|
|
111
|
+
|
|
112
|
+
if (code === 0) {
|
|
113
|
+
++highest;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return indices;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function decodeTriangleIndices(dataView, vertexData, vertexDataEndPosition) {
|
|
121
|
+
let position = vertexDataEndPosition;
|
|
122
|
+
const elementsPerVertex = 3;
|
|
123
|
+
const vertexCount = vertexData.length / elementsPerVertex;
|
|
124
|
+
const bytesPerIndex =
|
|
125
|
+
vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;
|
|
126
|
+
|
|
127
|
+
if (position % bytesPerIndex !== 0) {
|
|
128
|
+
position += bytesPerIndex - (position % bytesPerIndex);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const triangleCount = dataView.getUint32(position, true);
|
|
132
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
133
|
+
|
|
134
|
+
const triangleIndicesCount = triangleCount * 3;
|
|
135
|
+
const triangleIndices = decodeIndex(
|
|
136
|
+
dataView.buffer,
|
|
137
|
+
position,
|
|
138
|
+
triangleIndicesCount,
|
|
139
|
+
bytesPerIndex
|
|
140
|
+
);
|
|
141
|
+
position += triangleIndicesCount * bytesPerIndex;
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
triangleIndicesEndPosition: position,
|
|
145
|
+
triangleIndices
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function decodeEdgeIndices(dataView, vertexData, triangleIndicesEndPosition) {
|
|
150
|
+
let position = triangleIndicesEndPosition;
|
|
151
|
+
const elementsPerVertex = 3;
|
|
152
|
+
const vertexCount = vertexData.length / elementsPerVertex;
|
|
153
|
+
const bytesPerIndex =
|
|
154
|
+
vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;
|
|
155
|
+
|
|
156
|
+
const westVertexCount = dataView.getUint32(position, true);
|
|
157
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
158
|
+
|
|
159
|
+
const westIndices = decodeIndex(dataView.buffer, position, westVertexCount, bytesPerIndex, false);
|
|
160
|
+
position += westVertexCount * bytesPerIndex;
|
|
161
|
+
|
|
162
|
+
const southVertexCount = dataView.getUint32(position, true);
|
|
163
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
164
|
+
|
|
165
|
+
const southIndices = decodeIndex(
|
|
166
|
+
dataView.buffer,
|
|
167
|
+
position,
|
|
168
|
+
southVertexCount,
|
|
169
|
+
bytesPerIndex,
|
|
170
|
+
false
|
|
171
|
+
);
|
|
172
|
+
position += southVertexCount * bytesPerIndex;
|
|
173
|
+
|
|
174
|
+
const eastVertexCount = dataView.getUint32(position, true);
|
|
175
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
176
|
+
|
|
177
|
+
const eastIndices = decodeIndex(dataView.buffer, position, eastVertexCount, bytesPerIndex, false);
|
|
178
|
+
position += eastVertexCount * bytesPerIndex;
|
|
179
|
+
|
|
180
|
+
const northVertexCount = dataView.getUint32(position, true);
|
|
181
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
182
|
+
|
|
183
|
+
const northIndices = decodeIndex(
|
|
184
|
+
dataView.buffer,
|
|
185
|
+
position,
|
|
186
|
+
northVertexCount,
|
|
187
|
+
bytesPerIndex,
|
|
188
|
+
false
|
|
189
|
+
);
|
|
190
|
+
position += northVertexCount * bytesPerIndex;
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
edgeIndicesEndPosition: position,
|
|
194
|
+
westIndices,
|
|
195
|
+
southIndices,
|
|
196
|
+
eastIndices,
|
|
197
|
+
northIndices
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function decodeVertexNormalsExtension(extensionDataView) {
|
|
202
|
+
return new Uint8Array(
|
|
203
|
+
extensionDataView.buffer,
|
|
204
|
+
extensionDataView.byteOffset,
|
|
205
|
+
extensionDataView.byteLength
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function decodeWaterMaskExtension(extensionDataView) {
|
|
210
|
+
return extensionDataView.buffer.slice(
|
|
211
|
+
extensionDataView.byteOffset,
|
|
212
|
+
extensionDataView.byteOffset + extensionDataView.byteLength
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
type Extensions = {
|
|
217
|
+
vertexNormals?: any;
|
|
218
|
+
waterMask?: any;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
function decodeExtensions(dataView, indicesEndPosition) {
|
|
222
|
+
const extensions: Extensions = {};
|
|
223
|
+
|
|
224
|
+
if (dataView.byteLength <= indicesEndPosition) {
|
|
225
|
+
return {extensions, extensionsEndPosition: indicesEndPosition};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
let position = indicesEndPosition;
|
|
229
|
+
|
|
230
|
+
while (position < dataView.byteLength) {
|
|
231
|
+
const extensionId = dataView.getUint8(position, true);
|
|
232
|
+
position += Uint8Array.BYTES_PER_ELEMENT;
|
|
233
|
+
|
|
234
|
+
const extensionLength = dataView.getUint32(position, true);
|
|
235
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
236
|
+
|
|
237
|
+
const extensionView = new DataView(dataView.buffer, position, extensionLength);
|
|
238
|
+
|
|
239
|
+
switch (extensionId) {
|
|
240
|
+
case 1: {
|
|
241
|
+
extensions.vertexNormals = decodeVertexNormalsExtension(extensionView);
|
|
242
|
+
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
case 2: {
|
|
246
|
+
extensions.waterMask = decodeWaterMaskExtension(extensionView);
|
|
247
|
+
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
default: {
|
|
251
|
+
// console.warn(`Unknown extension with id ${extensionId}`)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
position += extensionLength;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return {extensions, extensionsEndPosition: position};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export const DECODING_STEPS = {
|
|
262
|
+
header: 0,
|
|
263
|
+
vertices: 1,
|
|
264
|
+
triangleIndices: 2,
|
|
265
|
+
edgeIndices: 3,
|
|
266
|
+
extensions: 4
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const DEFAULT_OPTIONS = {
|
|
270
|
+
maxDecodingStep: DECODING_STEPS.extensions
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
export default function decode(data, userOptions) {
|
|
274
|
+
const options = Object.assign({}, DEFAULT_OPTIONS, userOptions);
|
|
275
|
+
const view = new DataView(data);
|
|
276
|
+
const {header, headerEndPosition} = decodeHeader(view);
|
|
277
|
+
|
|
278
|
+
if (options.maxDecodingStep < DECODING_STEPS.vertices) {
|
|
279
|
+
return {header};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const {vertexData, vertexDataEndPosition} = decodeVertexData(view, headerEndPosition);
|
|
283
|
+
|
|
284
|
+
if (options.maxDecodingStep < DECODING_STEPS.triangleIndices) {
|
|
285
|
+
return {header, vertexData};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const {triangleIndices, triangleIndicesEndPosition} = decodeTriangleIndices(
|
|
289
|
+
view,
|
|
290
|
+
vertexData,
|
|
291
|
+
vertexDataEndPosition
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
if (options.maxDecodingStep < DECODING_STEPS.edgeIndices) {
|
|
295
|
+
return {header, vertexData, triangleIndices};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const {westIndices, southIndices, eastIndices, northIndices, edgeIndicesEndPosition} =
|
|
299
|
+
decodeEdgeIndices(view, vertexData, triangleIndicesEndPosition);
|
|
300
|
+
|
|
301
|
+
if (options.maxDecodingStep < DECODING_STEPS.extensions) {
|
|
302
|
+
return {
|
|
303
|
+
header,
|
|
304
|
+
vertexData,
|
|
305
|
+
triangleIndices,
|
|
306
|
+
westIndices,
|
|
307
|
+
northIndices,
|
|
308
|
+
eastIndices,
|
|
309
|
+
southIndices
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const {extensions} = decodeExtensions(view, edgeIndicesEndPosition);
|
|
314
|
+
|
|
315
|
+
return {
|
|
316
|
+
header,
|
|
317
|
+
vertexData,
|
|
318
|
+
triangleIndices,
|
|
319
|
+
westIndices,
|
|
320
|
+
northIndices,
|
|
321
|
+
eastIndices,
|
|
322
|
+
southIndices,
|
|
323
|
+
extensions
|
|
324
|
+
};
|
|
325
|
+
}
|
package/src/lib/delatin/index.js
CHANGED
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
15
15
|
// THIS SOFTWARE.
|
|
16
16
|
|
|
17
|
+
// @ts-nocheck
|
|
18
|
+
|
|
17
19
|
/* eslint-disable complexity, max-params, max-statements, max-depth, no-constant-condition */
|
|
18
20
|
export default class Delatin {
|
|
19
21
|
constructor(data, width, height = width) {
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import {concatenateTypedArrays} from '@loaders.gl/loader-utils';
|
|
2
|
+
|
|
3
|
+
export type EdgeIndices = {
|
|
4
|
+
westIndices: number[];
|
|
5
|
+
northIndices: number[];
|
|
6
|
+
eastIndices: number[];
|
|
7
|
+
southIndices: number[];
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Add skirt to existing mesh
|
|
12
|
+
* @param {object} attributes - POSITION and TEXCOOD_0 attributes data
|
|
13
|
+
* @param {any} triangles - indices array of the mesh geometry
|
|
14
|
+
* @param skirtHeight - height of the skirt geometry
|
|
15
|
+
* @param outsideIndices - edge indices from quantized mesh data
|
|
16
|
+
* @returns - geometry data with added skirt
|
|
17
|
+
*/
|
|
18
|
+
export function addSkirt(attributes, triangles, skirtHeight: number, outsideIndices?: EdgeIndices) {
|
|
19
|
+
const outsideEdges = outsideIndices
|
|
20
|
+
? getOutsideEdgesFromIndices(outsideIndices, attributes.POSITION.value)
|
|
21
|
+
: getOutsideEdgesFromTriangles(triangles);
|
|
22
|
+
|
|
23
|
+
// 2 new vertices for each outside edge
|
|
24
|
+
const newPosition = new attributes.POSITION.value.constructor(outsideEdges.length * 6);
|
|
25
|
+
const newTexcoord0 = new attributes.TEXCOORD_0.value.constructor(outsideEdges.length * 4);
|
|
26
|
+
|
|
27
|
+
// 2 new triangles for each outside edge
|
|
28
|
+
const newTriangles = new triangles.constructor(outsideEdges.length * 6);
|
|
29
|
+
|
|
30
|
+
for (let i = 0; i < outsideEdges.length; i++) {
|
|
31
|
+
const edge = outsideEdges[i];
|
|
32
|
+
|
|
33
|
+
updateAttributesForNewEdge({
|
|
34
|
+
edge,
|
|
35
|
+
edgeIndex: i,
|
|
36
|
+
attributes,
|
|
37
|
+
skirtHeight,
|
|
38
|
+
newPosition,
|
|
39
|
+
newTexcoord0,
|
|
40
|
+
newTriangles
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
attributes.POSITION.value = concatenateTypedArrays(attributes.POSITION.value, newPosition);
|
|
45
|
+
attributes.TEXCOORD_0.value = concatenateTypedArrays(attributes.TEXCOORD_0.value, newTexcoord0);
|
|
46
|
+
const resultTriangles =
|
|
47
|
+
triangles instanceof Array
|
|
48
|
+
? triangles.concat(newTriangles)
|
|
49
|
+
: concatenateTypedArrays(triangles, newTriangles);
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
attributes,
|
|
53
|
+
triangles: resultTriangles
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Get geometry edges that located on a border of the mesh
|
|
59
|
+
* @param {any} triangles - indices array of the mesh geometry
|
|
60
|
+
* @returns {number[][]} - outside edges data
|
|
61
|
+
*/
|
|
62
|
+
function getOutsideEdgesFromTriangles(triangles) {
|
|
63
|
+
const edges: number[][] = [];
|
|
64
|
+
for (let i = 0; i < triangles.length; i += 3) {
|
|
65
|
+
edges.push([triangles[i], triangles[i + 1]]);
|
|
66
|
+
edges.push([triangles[i + 1], triangles[i + 2]]);
|
|
67
|
+
edges.push([triangles[i + 2], triangles[i]]);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
edges.sort((a, b) => Math.min(...a) - Math.min(...b) || Math.max(...a) - Math.max(...b));
|
|
71
|
+
|
|
72
|
+
const outsideEdges: number[][] = [];
|
|
73
|
+
let index = 1;
|
|
74
|
+
while (index < edges.length) {
|
|
75
|
+
if (edges[index][0] === edges[index - 1][1] && edges[index][1] === edges[index - 1][0]) {
|
|
76
|
+
index += 2;
|
|
77
|
+
} else {
|
|
78
|
+
outsideEdges.push(edges[index - 1]);
|
|
79
|
+
index++;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return outsideEdges;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Get geometry edges that located on a border of the mesh
|
|
87
|
+
* @param {object} indices - edge indices from quantized mesh data
|
|
88
|
+
* @param {TypedArray} position - position attribute geometry data
|
|
89
|
+
* @returns {number[][]} - outside edges data
|
|
90
|
+
*/
|
|
91
|
+
function getOutsideEdgesFromIndices(indices: EdgeIndices, position) {
|
|
92
|
+
// Sort skirt indices to create adjacent triangles
|
|
93
|
+
indices.westIndices.sort((a, b) => position[3 * a + 1] - position[3 * b + 1]);
|
|
94
|
+
// Reverse (b - a) to match triangle winding
|
|
95
|
+
indices.eastIndices.sort((a, b) => position[3 * b + 1] - position[3 * a + 1]);
|
|
96
|
+
indices.southIndices.sort((a, b) => position[3 * b] - position[3 * a]);
|
|
97
|
+
// Reverse (b - a) to match triangle winding
|
|
98
|
+
indices.northIndices.sort((a, b) => position[3 * a] - position[3 * b]);
|
|
99
|
+
|
|
100
|
+
const edges: number[][] = [];
|
|
101
|
+
for (const index in indices) {
|
|
102
|
+
const indexGroup = indices[index];
|
|
103
|
+
for (let i = 0; i < indexGroup.length - 1; i++) {
|
|
104
|
+
edges.push([indexGroup[i], indexGroup[i + 1]]);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return edges;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Get geometry edges that located on a border of the mesh
|
|
112
|
+
* @param {object} args
|
|
113
|
+
* @param {number[]} args.edge - edge indices in geometry
|
|
114
|
+
* @param {number} args.edgeIndex - edge index in outsideEdges array
|
|
115
|
+
* @param {object} args.attributes - POSITION and TEXCOORD_0 attributes
|
|
116
|
+
* @param {number} args.skirtHeight - height of the skirt geometry
|
|
117
|
+
* @param {TypedArray} args.newPosition - POSITION array for skirt data
|
|
118
|
+
* @param {TypedArray} args.newTexcoord0 - TEXCOORD_0 array for skirt data
|
|
119
|
+
* @param {TypedArray | Array} args.newTriangles - trinagle indices array for skirt data
|
|
120
|
+
* @returns {void}
|
|
121
|
+
*/
|
|
122
|
+
function updateAttributesForNewEdge({
|
|
123
|
+
edge,
|
|
124
|
+
edgeIndex,
|
|
125
|
+
attributes,
|
|
126
|
+
skirtHeight,
|
|
127
|
+
newPosition,
|
|
128
|
+
newTexcoord0,
|
|
129
|
+
newTriangles
|
|
130
|
+
}) {
|
|
131
|
+
const positionsLength = attributes.POSITION.value.length;
|
|
132
|
+
const vertex1Offset = edgeIndex * 2;
|
|
133
|
+
const vertex2Offset = edgeIndex * 2 + 1;
|
|
134
|
+
|
|
135
|
+
// Define POSITION for new 1st vertex
|
|
136
|
+
newPosition.set(
|
|
137
|
+
attributes.POSITION.value.subarray(edge[0] * 3, edge[0] * 3 + 3),
|
|
138
|
+
vertex1Offset * 3
|
|
139
|
+
);
|
|
140
|
+
newPosition[vertex1Offset * 3 + 2] = newPosition[vertex1Offset * 3 + 2] - skirtHeight; // put down elevation on the skirt height
|
|
141
|
+
|
|
142
|
+
// Define POSITION for new 2nd vertex
|
|
143
|
+
newPosition.set(
|
|
144
|
+
attributes.POSITION.value.subarray(edge[1] * 3, edge[1] * 3 + 3),
|
|
145
|
+
vertex2Offset * 3
|
|
146
|
+
);
|
|
147
|
+
newPosition[vertex2Offset * 3 + 2] = newPosition[vertex2Offset * 3 + 2] - skirtHeight; // put down elevation on the skirt height
|
|
148
|
+
|
|
149
|
+
// Use same TEXCOORDS for skirt vertices
|
|
150
|
+
newTexcoord0.set(
|
|
151
|
+
attributes.TEXCOORD_0.value.subarray(edge[0] * 2, edge[0] * 2 + 2),
|
|
152
|
+
vertex1Offset * 2
|
|
153
|
+
);
|
|
154
|
+
newTexcoord0.set(
|
|
155
|
+
attributes.TEXCOORD_0.value.subarray(edge[1] * 2, edge[1] * 2 + 2),
|
|
156
|
+
vertex2Offset * 2
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
// Define new triangles
|
|
160
|
+
const triangle1Offset = edgeIndex * 2 * 3;
|
|
161
|
+
newTriangles[triangle1Offset] = edge[0];
|
|
162
|
+
newTriangles[triangle1Offset + 1] = edge[1];
|
|
163
|
+
newTriangles[triangle1Offset + 2] = positionsLength / 3 + vertex2Offset;
|
|
164
|
+
|
|
165
|
+
newTriangles[triangle1Offset + 3] = positionsLength / 3 + vertex2Offset;
|
|
166
|
+
newTriangles[triangle1Offset + 4] = positionsLength / 3 + vertex1Offset;
|
|
167
|
+
newTriangles[triangle1Offset + 5] = edge[0];
|
|
168
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {getMeshBoundingBox} from '@loaders.gl/schema';
|
|
2
|
+
import decode, {DECODING_STEPS} from './decode-quantized-mesh';
|
|
3
|
+
import {addSkirt} from './helpers/skirt';
|
|
4
|
+
|
|
5
|
+
function getMeshAttributes(vertexData, header, bounds) {
|
|
6
|
+
const {minHeight, maxHeight} = header;
|
|
7
|
+
const [minX, minY, maxX, maxY] = bounds || [0, 0, 1, 1];
|
|
8
|
+
const xScale = maxX - minX;
|
|
9
|
+
const yScale = maxY - minY;
|
|
10
|
+
const zScale = maxHeight - minHeight;
|
|
11
|
+
|
|
12
|
+
const nCoords = vertexData.length / 3;
|
|
13
|
+
// vec3. x, y defined by bounds, z in meters
|
|
14
|
+
const positions = new Float32Array(nCoords * 3);
|
|
15
|
+
|
|
16
|
+
// vec2. 1 to 1 relationship with position. represents the uv on the texture image. 0,0 to 1,1.
|
|
17
|
+
const texCoords = new Float32Array(nCoords * 2);
|
|
18
|
+
|
|
19
|
+
// Data is not interleaved; all u, then all v, then all heights
|
|
20
|
+
for (let i = 0; i < nCoords; i++) {
|
|
21
|
+
const x = vertexData[i] / 32767;
|
|
22
|
+
const y = vertexData[i + nCoords] / 32767;
|
|
23
|
+
const z = vertexData[i + nCoords * 2] / 32767;
|
|
24
|
+
|
|
25
|
+
positions[3 * i + 0] = x * xScale + minX;
|
|
26
|
+
positions[3 * i + 1] = y * yScale + minY;
|
|
27
|
+
positions[3 * i + 2] = z * zScale + minHeight;
|
|
28
|
+
|
|
29
|
+
texCoords[2 * i + 0] = x;
|
|
30
|
+
texCoords[2 * i + 1] = y;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
POSITION: {value: positions, size: 3},
|
|
35
|
+
TEXCOORD_0: {value: texCoords, size: 2}
|
|
36
|
+
// TODO: Parse normals if they exist in the file
|
|
37
|
+
// NORMAL: {}, - optional, but creates the high poly look with lighting
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function getTileMesh(arrayBuffer, options) {
|
|
42
|
+
if (!arrayBuffer) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const {bounds} = options;
|
|
46
|
+
// Don't parse edge indices or format extensions
|
|
47
|
+
const {
|
|
48
|
+
header,
|
|
49
|
+
vertexData,
|
|
50
|
+
triangleIndices: originalTriangleIndices,
|
|
51
|
+
westIndices,
|
|
52
|
+
northIndices,
|
|
53
|
+
eastIndices,
|
|
54
|
+
southIndices
|
|
55
|
+
} = decode(arrayBuffer, DECODING_STEPS.triangleIndices);
|
|
56
|
+
let triangleIndices = originalTriangleIndices;
|
|
57
|
+
let attributes = getMeshAttributes(vertexData, header, bounds);
|
|
58
|
+
|
|
59
|
+
// Compute bounding box before adding skirt so that z values are not skewed
|
|
60
|
+
// TODO: Find bounding box from header, instead of doing extra pass over
|
|
61
|
+
// vertices.
|
|
62
|
+
const boundingBox = getMeshBoundingBox(attributes);
|
|
63
|
+
|
|
64
|
+
if (options.skirtHeight) {
|
|
65
|
+
const {attributes: newAttributes, triangles: newTriangles} = addSkirt(
|
|
66
|
+
attributes,
|
|
67
|
+
triangleIndices,
|
|
68
|
+
options.skirtHeight,
|
|
69
|
+
{
|
|
70
|
+
westIndices,
|
|
71
|
+
northIndices,
|
|
72
|
+
eastIndices,
|
|
73
|
+
southIndices
|
|
74
|
+
}
|
|
75
|
+
);
|
|
76
|
+
attributes = newAttributes;
|
|
77
|
+
triangleIndices = newTriangles;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
// Data return by this loader implementation
|
|
82
|
+
loaderData: {
|
|
83
|
+
header: {}
|
|
84
|
+
},
|
|
85
|
+
header: {
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
vertexCount: triangleIndices.length,
|
|
88
|
+
boundingBox
|
|
89
|
+
},
|
|
90
|
+
mode: 4, // TRIANGLES
|
|
91
|
+
indices: {value: triangleIndices, size: 1},
|
|
92
|
+
attributes
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default function loadQuantizedMesh(arrayBuffer, options) {
|
|
97
|
+
return getTileMesh(arrayBuffer, options['quantized-mesh']);
|
|
98
|
+
}
|