@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.
- package/dist/bundle.d.ts +2 -0
- package/dist/bundle.d.ts.map +1 -0
- package/dist/dist.min.js +1181 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/lib/decode-quantized-mesh.d.ts +59 -0
- package/dist/lib/decode-quantized-mesh.d.ts.map +1 -0
- package/dist/lib/delatin/index.d.ts +24 -0
- package/dist/lib/delatin/index.d.ts.map +1 -0
- package/dist/lib/delatin/index.js.map +1 -1
- package/dist/lib/helpers/skirt.d.ts +19 -0
- package/dist/lib/helpers/skirt.d.ts.map +1 -0
- package/dist/lib/parse-quantized-mesh.d.ts +25 -0
- package/dist/lib/parse-quantized-mesh.d.ts.map +1 -0
- package/dist/lib/parse-terrain.d.ts +25 -0
- package/dist/lib/parse-terrain.d.ts.map +1 -0
- package/dist/lib/parse-terrain.js.map +1 -1
- package/dist/lib/utils/version.d.ts +2 -0
- package/dist/lib/utils/version.d.ts.map +1 -0
- package/dist/lib/utils/version.js +1 -1
- package/dist/lib/utils/version.js.map +1 -1
- package/dist/quantized-mesh-loader.d.ts +21 -0
- package/dist/quantized-mesh-loader.d.ts.map +1 -0
- package/dist/quantized-mesh-worker.js +3 -3
- package/dist/terrain-loader.d.ts +32 -0
- package/dist/terrain-loader.d.ts.map +1 -0
- package/dist/terrain-worker.js +3 -3
- package/dist/workers/quantized-mesh-worker.d.ts +2 -0
- package/dist/workers/quantized-mesh-worker.d.ts.map +1 -0
- package/dist/workers/terrain-worker.d.ts +2 -0
- package/dist/workers/terrain-worker.d.ts.map +1 -0
- package/package.json +8 -8
- package/src/lib/delatin/{index.js → index.ts} +0 -0
- package/src/lib/parse-terrain.ts +1 -0
- package/src/lib/utils/{version.js → version.ts} +0 -0
- package/src/workers/quantized-mesh-worker.js +0 -1
- package/src/workers/terrain-worker.js +0 -1
- package/src/lib/decode-quantized-mesh.js +0 -320
- package/src/lib/helpers/skirt.js +0 -161
- package/src/lib/parse-quantized-mesh.js +0 -98
- package/src/lib/parse-terrain.js +0 -179
package/src/lib/parse-terrain.js
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import {getMeshBoundingBox} from '@loaders.gl/schema';
|
|
2
|
-
import Martini from '@mapbox/martini';
|
|
3
|
-
import Delatin from './delatin';
|
|
4
|
-
import {addSkirt} from './helpers/skirt';
|
|
5
|
-
|
|
6
|
-
function getTerrain(imageData, width, height, elevationDecoder, tesselator) {
|
|
7
|
-
const {rScaler, bScaler, gScaler, offset} = elevationDecoder;
|
|
8
|
-
|
|
9
|
-
// From Martini demo
|
|
10
|
-
// https://observablehq.com/@mourner/martin-real-time-rtin-terrain-mesh
|
|
11
|
-
const terrain = new Float32Array((width + 1) * (height + 1));
|
|
12
|
-
// decode terrain values
|
|
13
|
-
for (let i = 0, y = 0; y < height; y++) {
|
|
14
|
-
for (let x = 0; x < width; x++, i++) {
|
|
15
|
-
const k = i * 4;
|
|
16
|
-
const r = imageData[k + 0];
|
|
17
|
-
const g = imageData[k + 1];
|
|
18
|
-
const b = imageData[k + 2];
|
|
19
|
-
terrain[i + y] = r * rScaler + g * gScaler + b * bScaler + offset;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (tesselator === 'martini') {
|
|
24
|
-
// backfill bottom border
|
|
25
|
-
for (let i = (width + 1) * width, x = 0; x < width; x++, i++) {
|
|
26
|
-
terrain[i] = terrain[i - width - 1];
|
|
27
|
-
}
|
|
28
|
-
// backfill right border
|
|
29
|
-
for (let i = height, y = 0; y < height + 1; y++, i += height + 1) {
|
|
30
|
-
terrain[i] = terrain[i - 1];
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return terrain;
|
|
35
|
-
}
|
|
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
|
-
|
|
45
|
-
const [minX, minY, maxX, maxY] = bounds || [0, 0, width, height];
|
|
46
|
-
const xScale = (maxX - minX) / width;
|
|
47
|
-
const yScale = (maxY - minY) / height;
|
|
48
|
-
|
|
49
|
-
for (let i = 0; i < numOfVerticies; i++) {
|
|
50
|
-
const x = vertices[i * 2];
|
|
51
|
-
const y = vertices[i * 2 + 1];
|
|
52
|
-
const pixelIdx = y * gridSize + x;
|
|
53
|
-
|
|
54
|
-
positions[3 * i + 0] = x * xScale + minX;
|
|
55
|
-
positions[3 * i + 1] = -y * yScale + maxY;
|
|
56
|
-
positions[3 * i + 2] = terrain[pixelIdx];
|
|
57
|
-
|
|
58
|
-
texCoords[2 * i + 0] = x / width;
|
|
59
|
-
texCoords[2 * i + 1] = y / height;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return {
|
|
63
|
-
POSITION: {value: positions, size: 3},
|
|
64
|
-
TEXCOORD_0: {value: texCoords, size: 2}
|
|
65
|
-
// NORMAL: {}, - optional, but creates the high poly look with lighting
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Returns generated mesh object from image data
|
|
71
|
-
*
|
|
72
|
-
* @param {object} terrainImage terrain image data
|
|
73
|
-
* @param {object} terrainOptions terrain options
|
|
74
|
-
* @returns mesh object
|
|
75
|
-
*/
|
|
76
|
-
function getMesh(terrainImage, terrainOptions) {
|
|
77
|
-
if (terrainImage === null) {
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
const {meshMaxError, bounds, elevationDecoder} = terrainOptions;
|
|
81
|
-
|
|
82
|
-
const {data, width, height} = terrainImage;
|
|
83
|
-
|
|
84
|
-
let terrain;
|
|
85
|
-
let mesh;
|
|
86
|
-
switch (terrainOptions.tesselator) {
|
|
87
|
-
case 'martini':
|
|
88
|
-
terrain = getTerrain(data, width, height, elevationDecoder, terrainOptions.tesselator);
|
|
89
|
-
mesh = getMartiniTileMesh(meshMaxError, width, terrain);
|
|
90
|
-
break;
|
|
91
|
-
case 'delatin':
|
|
92
|
-
terrain = getTerrain(data, width, height, elevationDecoder, terrainOptions.tesselator);
|
|
93
|
-
mesh = getDelatinTileMesh(meshMaxError, width, height, terrain);
|
|
94
|
-
break;
|
|
95
|
-
// auto
|
|
96
|
-
default:
|
|
97
|
-
if (width === height && !(height & (width - 1))) {
|
|
98
|
-
terrain = getTerrain(data, width, height, elevationDecoder, 'martini');
|
|
99
|
-
mesh = getMartiniTileMesh(meshMaxError, width, terrain);
|
|
100
|
-
} else {
|
|
101
|
-
terrain = getTerrain(data, width, height, elevationDecoder, 'delatin');
|
|
102
|
-
mesh = getDelatinTileMesh(meshMaxError, width, height, terrain);
|
|
103
|
-
}
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const {vertices} = mesh;
|
|
108
|
-
let {triangles} = mesh;
|
|
109
|
-
let attributes = getMeshAttributes(vertices, terrain, width, height, bounds);
|
|
110
|
-
|
|
111
|
-
// Compute bounding box before adding skirt so that z values are not skewed
|
|
112
|
-
const boundingBox = getMeshBoundingBox(attributes);
|
|
113
|
-
|
|
114
|
-
if (terrainOptions.skirtHeight) {
|
|
115
|
-
const {attributes: newAttributes, triangles: newTriangles} = addSkirt(
|
|
116
|
-
attributes,
|
|
117
|
-
triangles,
|
|
118
|
-
terrainOptions.skirtHeight
|
|
119
|
-
);
|
|
120
|
-
attributes = newAttributes;
|
|
121
|
-
triangles = newTriangles;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return {
|
|
125
|
-
// Data return by this loader implementation
|
|
126
|
-
loaderData: {
|
|
127
|
-
header: {}
|
|
128
|
-
},
|
|
129
|
-
header: {
|
|
130
|
-
vertexCount: triangles.length,
|
|
131
|
-
boundingBox
|
|
132
|
-
},
|
|
133
|
-
mode: 4, // TRIANGLES
|
|
134
|
-
indices: {value: Uint32Array.from(triangles), size: 1},
|
|
135
|
-
attributes
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Get Martini generated vertices and triangles
|
|
141
|
-
*
|
|
142
|
-
* @param {number} meshMaxError threshold for simplifying mesh
|
|
143
|
-
* @param {number} width width of the input data
|
|
144
|
-
* @param {number[] | Float32Array} terrain elevation data
|
|
145
|
-
* @returns {{vertices: Uint16Array, triangles: Uint32Array}} vertices and triangles data
|
|
146
|
-
*/
|
|
147
|
-
function getMartiniTileMesh(meshMaxError, width, terrain) {
|
|
148
|
-
const gridSize = width + 1;
|
|
149
|
-
const martini = new Martini(gridSize);
|
|
150
|
-
const tile = martini.createTile(terrain);
|
|
151
|
-
const {vertices, triangles} = tile.getMesh(meshMaxError);
|
|
152
|
-
|
|
153
|
-
return {vertices, triangles};
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Get Delatin generated vertices and triangles
|
|
158
|
-
*
|
|
159
|
-
* @param {number} meshMaxError threshold for simplifying mesh
|
|
160
|
-
* @param {number} width width of the input data array
|
|
161
|
-
* @param {number} height height of the input data array
|
|
162
|
-
* @param {number[] | Float32Array} terrain elevation data
|
|
163
|
-
* @returns {{vertices: number[], triangles: number[]}} vertices and triangles data
|
|
164
|
-
*/
|
|
165
|
-
function getDelatinTileMesh(meshMaxError, width, height, terrain) {
|
|
166
|
-
const tin = new Delatin(terrain, width + 1, height + 1);
|
|
167
|
-
tin.run(meshMaxError);
|
|
168
|
-
const {coords, triangles} = tin;
|
|
169
|
-
const vertices = coords;
|
|
170
|
-
return {vertices, triangles};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
export default async function loadTerrain(arrayBuffer, options, context) {
|
|
174
|
-
options.image = options.image || {};
|
|
175
|
-
options.image.type = 'data';
|
|
176
|
-
const image = await context.parse(arrayBuffer, options, options.baseUri);
|
|
177
|
-
// Extend function to support additional mesh generation options (square grid or delatin)
|
|
178
|
-
return getMesh(image, options.terrain);
|
|
179
|
-
}
|