@loaders.gl/gltf 4.0.0-alpha.7 → 4.0.0-alpha.8

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 (35) hide show
  1. package/dist/dist.min.js +149 -23
  2. package/dist/es5/lib/api/gltf-scenegraph.js +6 -3
  3. package/dist/es5/lib/api/gltf-scenegraph.js.map +1 -1
  4. package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js +193 -15
  5. package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -1
  6. package/dist/es5/lib/parsers/parse-gltf.js +13 -16
  7. package/dist/es5/lib/parsers/parse-gltf.js.map +1 -1
  8. package/dist/es5/lib/types/gltf-json-schema.js.map +1 -1
  9. package/dist/es5/lib/utils/version.js +1 -1
  10. package/dist/esm/lib/api/gltf-scenegraph.js +6 -3
  11. package/dist/esm/lib/api/gltf-scenegraph.js.map +1 -1
  12. package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js +160 -14
  13. package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -1
  14. package/dist/esm/lib/parsers/parse-gltf.js +2 -6
  15. package/dist/esm/lib/parsers/parse-gltf.js.map +1 -1
  16. package/dist/esm/lib/types/gltf-json-schema.js.map +1 -1
  17. package/dist/esm/lib/utils/version.js +1 -1
  18. package/dist/index.js +4 -4
  19. package/dist/lib/api/gltf-extensions.js +1 -1
  20. package/dist/lib/api/gltf-scenegraph.d.ts +2 -1
  21. package/dist/lib/api/gltf-scenegraph.d.ts.map +1 -1
  22. package/dist/lib/api/gltf-scenegraph.js +5 -4
  23. package/dist/lib/encoders/encode-glb.js +1 -1
  24. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts +2 -1
  25. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts.map +1 -1
  26. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +193 -18
  27. package/dist/lib/parsers/parse-gltf.d.ts.map +1 -1
  28. package/dist/lib/parsers/parse-gltf.js +4 -7
  29. package/dist/lib/types/gltf-json-schema.d.ts +2 -1
  30. package/dist/lib/types/gltf-json-schema.d.ts.map +1 -1
  31. package/package.json +6 -6
  32. package/src/lib/api/gltf-scenegraph.ts +6 -5
  33. package/src/lib/extensions/deprecated/EXT_feature_metadata.ts +262 -21
  34. package/src/lib/parsers/parse-gltf.ts +4 -9
  35. package/src/lib/types/gltf-json-schema.ts +3 -0
@@ -1,44 +1,41 @@
1
1
  /* eslint-disable camelcase */
2
2
  import type {GLTF} from '../../types/gltf-json-schema';
3
-
4
3
  import {GLTFScenegraph} from '../../api/gltf-scenegraph';
4
+ import {getImageData} from '@loaders.gl/images';
5
5
  import {
6
6
  ClassProperty,
7
7
  EXT_feature_metadata_class_object,
8
8
  EXT_feature_metadata_feature_table,
9
9
  FeatureTableProperty,
10
- GLTF_EXT_feature_metadata
10
+ GLTF_EXT_feature_metadata,
11
+ EXT_feature_metadata_feature_texture,
12
+ FeatureTextureProperty,
13
+ GLTFMeshPrimitive
11
14
  } from '../../types/gltf-json-schema';
15
+ import {getComponentTypeFromArray} from '../../gltf-utils/gltf-utils';
16
+ import {GLTFLoaderOptions} from '../../../gltf-loader';
12
17
 
13
18
  /** Extension name */
14
19
  const EXT_FEATURE_METADATA = 'EXT_feature_metadata';
15
20
 
16
21
  export const name = EXT_FEATURE_METADATA;
17
22
 
18
- export async function decode(gltfData: {json: GLTF}): Promise<void> {
23
+ export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions): Promise<void> {
19
24
  const scenegraph = new GLTFScenegraph(gltfData);
20
- decodeExtFeatureMetadata(scenegraph);
25
+ decodeExtFeatureMetadata(scenegraph, options);
21
26
  }
22
27
 
23
28
  /**
24
29
  * Decodes feature metadata from extension
25
30
  * @param scenegraph
26
31
  */
27
- function decodeExtFeatureMetadata(scenegraph: GLTFScenegraph): void {
32
+ function decodeExtFeatureMetadata(scenegraph: GLTFScenegraph, options: GLTFLoaderOptions): void {
28
33
  const extension: GLTF_EXT_feature_metadata | null = scenegraph.getExtension(EXT_FEATURE_METADATA);
29
- const schemaClasses = extension?.schema?.classes;
30
- const featureTables = extension?.featureTables;
31
- const featureTextures = extension?.featureTextures;
32
-
33
- if (featureTextures) {
34
- /*
35
- * TODO add support for featureTextures
36
- * Spec - https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_feature_metadata#feature-textures
37
- */
38
- // eslint-disable-next-line no-console
39
- console.warn('featureTextures is not yet supported in the "EXT_feature_metadata" extension.');
40
- }
34
+ if (!extension) return;
35
+
36
+ const schemaClasses = extension.schema?.classes;
41
37
 
38
+ const {featureTables} = extension;
42
39
  if (schemaClasses && featureTables) {
43
40
  for (const schemaName in schemaClasses) {
44
41
  const schemaClass = schemaClasses[schemaName];
@@ -49,6 +46,18 @@ function decodeExtFeatureMetadata(scenegraph: GLTFScenegraph): void {
49
46
  }
50
47
  }
51
48
  }
49
+
50
+ const {featureTextures} = extension;
51
+ if (schemaClasses && featureTextures && options.gltf?.loadImages) {
52
+ for (const schemaName in schemaClasses) {
53
+ const schemaClass = schemaClasses[schemaName];
54
+ const featureTexture = findFeatureTextureByName(featureTextures, schemaName);
55
+
56
+ if (featureTexture) {
57
+ handleFeatureTextureProperties(scenegraph, featureTexture, schemaClass);
58
+ }
59
+ }
60
+ }
52
61
  }
53
62
 
54
63
  /**
@@ -79,6 +88,30 @@ function handleFeatureTableProperties(
79
88
  }
80
89
  }
81
90
 
91
+ /**
92
+ * Navigate throw all properies in feature texture and gets properties data.
93
+ * Data will be stored in featureTexture.properties[propertyName].data
94
+ * @param scenegraph
95
+ * @param featureTexture
96
+ * @param schemaClass
97
+ */
98
+ function handleFeatureTextureProperties(
99
+ scenegraph: GLTFScenegraph,
100
+ featureTexture: EXT_feature_metadata_feature_texture,
101
+ schemaClass: EXT_feature_metadata_class_object
102
+ ): void {
103
+ const attributeName = featureTexture.class;
104
+
105
+ for (const propertyName in schemaClass.properties) {
106
+ const featureTextureProperty = featureTexture?.properties?.[propertyName];
107
+
108
+ if (featureTextureProperty) {
109
+ const data = getPropertyDataFromTexture(scenegraph, featureTextureProperty, attributeName);
110
+ featureTextureProperty.data = data;
111
+ }
112
+ }
113
+ }
114
+
82
115
  /**
83
116
  * Decode properties from binary sourse based on property type.
84
117
  * @param scenegraph
@@ -94,20 +127,213 @@ function getPropertyDataFromBinarySource(
94
127
  ): Uint8Array | string[] {
95
128
  const bufferView = featureTableProperty.bufferView;
96
129
  // TODO think maybe we shouldn't get data only in Uint8Array format.
97
- let data: Uint8Array | string[] = scenegraph.getTypedArrayForBufferView(bufferView);
130
+ const dataArray: Uint8Array = scenegraph.getTypedArrayForBufferView(bufferView);
98
131
 
99
132
  switch (schemaProperty.type) {
100
133
  case 'STRING': {
101
134
  // stringOffsetBufferView should be available for string type.
102
135
  const stringOffsetBufferView = featureTableProperty.stringOffsetBufferView!;
103
136
  const offsetsData = scenegraph.getTypedArrayForBufferView(stringOffsetBufferView);
104
- data = getStringAttributes(data, offsetsData, numberOfFeatures);
105
- break;
137
+ return getStringAttributes(dataArray, offsetsData, numberOfFeatures);
106
138
  }
107
139
  default:
108
140
  }
109
141
 
110
- return data;
142
+ return dataArray;
143
+ }
144
+
145
+ /**
146
+ * Get properties from texture associated with all mesh primitives.
147
+ * @param scenegraph
148
+ * @param featureTextureProperty
149
+ * @param attributeName
150
+ * @returns Feature texture data
151
+ */
152
+ function getPropertyDataFromTexture(
153
+ scenegraph: GLTFScenegraph,
154
+ featureTextureProperty: FeatureTextureProperty,
155
+ attributeName: string
156
+ ): number[] {
157
+ const json = scenegraph.gltf.json;
158
+ if (!json.meshes) {
159
+ return [];
160
+ }
161
+ const featureTextureTable: number[] = [];
162
+ for (const mesh of json.meshes) {
163
+ for (const primitive of mesh.primitives) {
164
+ processPrimitiveTextures(
165
+ scenegraph,
166
+ attributeName,
167
+ featureTextureProperty,
168
+ featureTextureTable,
169
+ primitive
170
+ );
171
+ }
172
+ }
173
+ return featureTextureTable;
174
+ }
175
+
176
+ // eslint-disable-next-line max-statements
177
+ /**
178
+ * Processes data encoded in the texture associated with the primitive. This data will be accessible through the attributes.
179
+ * @param scenegraph
180
+ * @param attributeName
181
+ * @param featureTextureProperty
182
+ * @param featureTextureTable
183
+ * @param primitive
184
+ */
185
+ function processPrimitiveTextures(
186
+ scenegraph: GLTFScenegraph,
187
+ attributeName: string,
188
+ featureTextureProperty: FeatureTextureProperty,
189
+ featureTextureTable: number[],
190
+ primitive: GLTFMeshPrimitive
191
+ ): void {
192
+ /*
193
+ texture.index is an index for the "textures" array.
194
+ The texture object referenced by this index looks like this:
195
+ {
196
+ "sampler": 0,
197
+ "source": 0
198
+ }
199
+ "sampler" is an index for the "samplers" array
200
+ "source" is an index for the "images" array that contains data. These data are stored in rgba channels of the image.
201
+
202
+ texture.texCoord is a number-suffix (like 1) for an attribute like "TEXCOORD_1" in meshes.primitives
203
+ The value of "TEXCOORD_1" is an accessor that is used to get coordinates. These coordinates ared used to get data from the image.
204
+ */
205
+ const json = scenegraph.gltf.json;
206
+ const textureData: number[] = [];
207
+ const texCoordAccessorKey = `TEXCOORD_${featureTextureProperty.texture.texCoord}`;
208
+ const texCoordAccessorIndex = primitive.attributes[texCoordAccessorKey];
209
+ const texCoordBufferView = scenegraph.getBufferView(texCoordAccessorIndex);
210
+ const texCoordArray: Uint8Array = scenegraph.getTypedArrayForBufferView(texCoordBufferView);
211
+
212
+ const textureCoordinates: Float32Array = new Float32Array(
213
+ texCoordArray.buffer,
214
+ texCoordArray.byteOffset,
215
+ texCoordArray.length / 4
216
+ );
217
+ // textureCoordinates contains UV coordinates of the actual data stored in the texture
218
+ // accessor.count is a number of UV pairs (they are stored as VEC2)
219
+
220
+ const textureIndex = featureTextureProperty.texture.index;
221
+ const texture = json.textures?.[textureIndex];
222
+ const imageIndex = texture?.source;
223
+ if (typeof imageIndex !== 'undefined') {
224
+ const image = json.images?.[imageIndex];
225
+ const mimeType = image?.mimeType;
226
+ const parsedImage = scenegraph.gltf.images?.[imageIndex];
227
+ if (parsedImage) {
228
+ for (let index = 0; index < textureCoordinates.length; index += 2) {
229
+ const value = getImageValueByCoordinates(
230
+ parsedImage,
231
+ mimeType,
232
+ textureCoordinates,
233
+ index,
234
+ featureTextureProperty.channels
235
+ );
236
+ textureData.push(value);
237
+ }
238
+ }
239
+ }
240
+ /*
241
+ featureTextureTable will contain unique values, e.g.
242
+ textureData = [24, 35, 28, 24]
243
+ featureTextureTable = [24, 35, 28]
244
+ featureIndices will contain indices hat refer featureTextureTable, e.g.
245
+ featureIndices = [0, 1, 2, 0]
246
+ */
247
+ const featureIndices: number[] = [];
248
+ for (const texelData of textureData) {
249
+ let index = featureTextureTable.findIndex((item) => item === texelData);
250
+ if (index === -1) {
251
+ index = featureTextureTable.push(texelData) - 1;
252
+ }
253
+ featureIndices.push(index);
254
+ }
255
+ const typedArray = new Uint32Array(featureIndices);
256
+ const bufferIndex =
257
+ scenegraph.gltf.buffers.push({
258
+ arrayBuffer: typedArray.buffer,
259
+ byteOffset: 0,
260
+ byteLength: typedArray.byteLength
261
+ }) - 1;
262
+ const bufferViewIndex = scenegraph.addBufferView(typedArray, bufferIndex, 0);
263
+ const accessorIndex = scenegraph.addAccessor(bufferViewIndex, {
264
+ size: 1,
265
+ componentType: getComponentTypeFromArray(typedArray),
266
+ count: typedArray.length
267
+ });
268
+ primitive.attributes[attributeName] = accessorIndex;
269
+ }
270
+
271
+ function getImageValueByCoordinates(
272
+ parsedImage: any,
273
+ mimeType: string | undefined,
274
+ textureCoordinates: Float32Array,
275
+ index: number,
276
+ channels: string
277
+ ) {
278
+ const CHANNELS_MAP = {
279
+ r: {offset: 0, shift: 0},
280
+ g: {offset: 1, shift: 8},
281
+ b: {offset: 2, shift: 16},
282
+ a: {offset: 3, shift: 24}
283
+ };
284
+
285
+ const u = textureCoordinates[index];
286
+ const v = textureCoordinates[index + 1];
287
+
288
+ let components = 1;
289
+ if (mimeType && (mimeType.indexOf('image/jpeg') !== -1 || mimeType.indexOf('image/png') !== -1))
290
+ components = 4;
291
+ const offset = coordinatesToOffset(u, v, parsedImage, components);
292
+ let value = 0;
293
+ for (const c of channels) {
294
+ const map = CHANNELS_MAP[c];
295
+ const val = getVal(parsedImage, offset + map.offset);
296
+ value |= val << map.shift;
297
+ }
298
+ return value;
299
+ }
300
+
301
+ function getVal(parsedImage: any, offset: number): number {
302
+ const imageData = getImageData(parsedImage);
303
+ if (imageData.data.length <= offset) {
304
+ throw new Error(`${imageData.data.length} <= ${offset}`);
305
+ }
306
+ return imageData.data[offset];
307
+ }
308
+
309
+ function coordinatesToOffset(
310
+ u: number,
311
+ v: number,
312
+ parsedImage: any,
313
+ componentsCount: number = 1
314
+ ): number {
315
+ const w = parsedImage.width;
316
+ const iX = emod(u) * (w - 1);
317
+ const indX = Math.round(iX);
318
+
319
+ const h = parsedImage.height;
320
+ const iY = emod(v) * (h - 1);
321
+ const indY = Math.round(iY);
322
+ const components = parsedImage.components ? parsedImage.components : componentsCount;
323
+ // components is a number of channels in the image
324
+ const offset = (indY * w + indX) * components;
325
+ return offset;
326
+ }
327
+
328
+ // The following is taken from tile-converter\src\i3s-converter\helpers\batch-ids-extensions.ts
329
+ /**
330
+ * Handle UVs if they are out of range [0,1].
331
+ * @param n
332
+ * @param m
333
+ */
334
+ function emod(n: number): number {
335
+ const a = ((n % 1) + 1) % 1;
336
+ return a;
111
337
  }
112
338
 
113
339
  /**
@@ -130,6 +356,21 @@ function findFeatureTableByName(
130
356
  return null;
131
357
  }
132
358
 
359
+ function findFeatureTextureByName(
360
+ featureTextures: {[key: string]: EXT_feature_metadata_feature_texture},
361
+ schemaClassName: string
362
+ ): EXT_feature_metadata_feature_texture | null {
363
+ for (const featureTexturesName in featureTextures) {
364
+ const featureTable = featureTextures[featureTexturesName];
365
+
366
+ if (featureTable.class === schemaClassName) {
367
+ return featureTable;
368
+ }
369
+ }
370
+
371
+ return null;
372
+ }
373
+
133
374
  /**
134
375
  * Getting string attributes from binary data.
135
376
  * Spec - https://github.com/CesiumGS/3d-tiles/tree/main/specification/Metadata#strings
@@ -47,23 +47,18 @@ export async function parseGLTF(
47
47
 
48
48
  preprocessExtensions(gltf, options, context);
49
49
 
50
- const promises: Promise<any>[] = [];
51
-
52
50
  // Load linked buffers asynchronously and decodes base64 buffers in parallel
53
51
  if (options?.gltf?.loadBuffers && gltf.json.buffers) {
54
52
  await loadBuffers(gltf, options, context);
55
53
  }
56
54
 
55
+ // loadImages and decodeExtensions should not be running in parallel, because
56
+ // decodeExtensions uses data from images taken during the loadImages call.
57
57
  if (options?.gltf?.loadImages) {
58
- const promise = loadImages(gltf, options, context);
59
- promises.push(promise);
58
+ await loadImages(gltf, options, context);
60
59
  }
61
60
 
62
- const promise = decodeExtensions(gltf, options, context);
63
- promises.push(promise);
64
-
65
- // Parallelize image loading and buffer loading/extension decoding
66
- await Promise.all(promises);
61
+ await decodeExtensions(gltf, options, context);
67
62
 
68
63
  return gltf;
69
64
  }
@@ -1038,6 +1038,7 @@ export type FeatureTableProperty = {
1038
1038
  * Otherwise it is measured relative to the beginning of the buffer.
1039
1039
  */
1040
1040
  bufferView: number;
1041
+
1041
1042
  /** The type of values in arrayOffsetBufferView and stringOffsetBufferView. */
1042
1043
  offsetType?: string; // default: "UINT32"
1043
1044
  /**
@@ -1083,6 +1084,8 @@ type FeatureTexture = {
1083
1084
  extras?: any;
1084
1085
  [key: string]: any;
1085
1086
  };
1087
+ export type {FeatureTexture as EXT_feature_metadata_feature_texture};
1088
+ export type {TextureAccessor as FeatureTextureProperty};
1086
1089
 
1087
1090
  /**
1088
1091
  * Spec - https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_feature_metadata#texture-accessor