@loaders.gl/gltf 4.0.0-alpha.22 → 4.0.0-alpha.23

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 (71) hide show
  1. package/dist/dist.min.js +3577 -3025
  2. package/dist/es5/index.js +21 -0
  3. package/dist/es5/index.js.map +1 -1
  4. package/dist/es5/lib/api/gltf-extensions.js +3 -1
  5. package/dist/es5/lib/api/gltf-extensions.js.map +1 -1
  6. package/dist/es5/lib/extensions/EXT_mesh_features.js +111 -0
  7. package/dist/es5/lib/extensions/EXT_mesh_features.js.map +1 -0
  8. package/dist/es5/lib/extensions/EXT_structural_metadata.js +386 -0
  9. package/dist/es5/lib/extensions/EXT_structural_metadata.js.map +1 -0
  10. package/dist/es5/lib/extensions/data-processing.js +181 -0
  11. package/dist/es5/lib/extensions/data-processing.js.map +1 -0
  12. package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js +6 -9
  13. package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -1
  14. package/dist/es5/lib/types/gltf-ext-mesh-features-schema.js +2 -0
  15. package/dist/es5/lib/types/gltf-ext-mesh-features-schema.js.map +1 -0
  16. package/dist/es5/lib/types/gltf-ext-structural-metadata-schema.js +2 -0
  17. package/dist/es5/lib/types/gltf-ext-structural-metadata-schema.js.map +1 -0
  18. package/dist/es5/lib/types/gltf-json-schema.js.map +1 -1
  19. package/dist/es5/lib/utils/version.js +1 -1
  20. package/dist/esm/index.js +3 -0
  21. package/dist/esm/index.js.map +1 -1
  22. package/dist/esm/lib/api/gltf-extensions.js +3 -1
  23. package/dist/esm/lib/api/gltf-extensions.js.map +1 -1
  24. package/dist/esm/lib/extensions/EXT_mesh_features.js +55 -0
  25. package/dist/esm/lib/extensions/EXT_mesh_features.js.map +1 -0
  26. package/dist/esm/lib/extensions/EXT_structural_metadata.js +320 -0
  27. package/dist/esm/lib/extensions/EXT_structural_metadata.js.map +1 -0
  28. package/dist/esm/lib/extensions/data-processing.js +146 -0
  29. package/dist/esm/lib/extensions/data-processing.js.map +1 -0
  30. package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js +4 -7
  31. package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -1
  32. package/dist/esm/lib/types/gltf-ext-mesh-features-schema.js +2 -0
  33. package/dist/esm/lib/types/gltf-ext-mesh-features-schema.js.map +1 -0
  34. package/dist/esm/lib/types/gltf-ext-structural-metadata-schema.js +2 -0
  35. package/dist/esm/lib/types/gltf-ext-structural-metadata-schema.js.map +1 -0
  36. package/dist/esm/lib/types/gltf-json-schema.js.map +1 -1
  37. package/dist/esm/lib/utils/version.js +1 -1
  38. package/dist/index.d.ts +6 -1
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +7 -1
  41. package/dist/lib/api/gltf-extensions.d.ts.map +1 -1
  42. package/dist/lib/api/gltf-extensions.js +5 -0
  43. package/dist/lib/extensions/EXT_mesh_features.d.ts +7 -0
  44. package/dist/lib/extensions/EXT_mesh_features.d.ts.map +1 -0
  45. package/dist/lib/extensions/EXT_mesh_features.js +89 -0
  46. package/dist/lib/extensions/EXT_structural_metadata.d.ts +17 -0
  47. package/dist/lib/extensions/EXT_structural_metadata.d.ts.map +1 -0
  48. package/dist/lib/extensions/EXT_structural_metadata.js +504 -0
  49. package/dist/lib/extensions/data-processing.d.ts +34 -0
  50. package/dist/lib/extensions/data-processing.d.ts.map +1 -0
  51. package/dist/lib/extensions/data-processing.js +212 -0
  52. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts.map +1 -1
  53. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +8 -17
  54. package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts +45 -0
  55. package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts.map +1 -0
  56. package/dist/lib/types/gltf-ext-mesh-features-schema.js +2 -0
  57. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts +310 -0
  58. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts.map +1 -0
  59. package/dist/lib/types/gltf-ext-structural-metadata-schema.js +2 -0
  60. package/dist/lib/types/gltf-json-schema.d.ts +11 -57
  61. package/dist/lib/types/gltf-json-schema.d.ts.map +1 -1
  62. package/package.json +6 -6
  63. package/src/index.ts +11 -3
  64. package/src/lib/api/gltf-extensions.ts +6 -2
  65. package/src/lib/extensions/EXT_mesh_features.ts +117 -0
  66. package/src/lib/extensions/EXT_structural_metadata.ts +730 -0
  67. package/src/lib/extensions/data-processing.ts +264 -0
  68. package/src/lib/extensions/deprecated/EXT_feature_metadata.ts +6 -17
  69. package/src/lib/types/gltf-ext-mesh-features-schema.ts +48 -0
  70. package/src/lib/types/gltf-ext-structural-metadata-schema.ts +357 -0
  71. package/src/lib/types/gltf-json-schema.ts +12 -60
@@ -0,0 +1,730 @@
1
+ /* eslint-disable camelcase */
2
+ import type {GLTF, GLTFTextureInfoMetadata, GLTFMeshPrimitive} from '../types/gltf-json-schema';
3
+ import type {
4
+ GLTF_EXT_structural_metadata_Schema,
5
+ GLTF_EXT_structural_metadata_ClassProperty,
6
+ GLTF_EXT_structural_metadata_Enum,
7
+ GLTF_EXT_structural_metadata_EnumValue,
8
+ GLTF_EXT_structural_metadata_PropertyTable,
9
+ GLTF_EXT_structural_metadata,
10
+ GLTF_EXT_structural_metadata_PropertyTexture,
11
+ GLTF_EXT_structural_metadata_PropertyTable_Property,
12
+ GLTF_EXT_structural_metadata_Primitive
13
+ } from '../types/gltf-ext-structural-metadata-schema';
14
+
15
+ import type {TypedArray} from '@loaders.gl/schema';
16
+ import type {GLTFLoaderOptions} from '../../gltf-loader';
17
+
18
+ import {GLTFScenegraph} from '../api/gltf-scenegraph';
19
+ import {
20
+ convertRawBufferToMetadataArray,
21
+ getPrimitiveTextureData,
22
+ primitivePropertyDataToAttributes,
23
+ getArrayElementByteSize
24
+ } from './data-processing';
25
+
26
+ const EXT_STRUCTURAL_METADATA_NAME = 'EXT_structural_metadata';
27
+ export const name = EXT_STRUCTURAL_METADATA_NAME;
28
+
29
+ export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions): Promise<void> {
30
+ const scenegraph = new GLTFScenegraph(gltfData);
31
+ decodeExtStructuralMetadata(scenegraph, options);
32
+ }
33
+
34
+ /*
35
+ // Example of the extension.
36
+ // See more info at https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_structural_metadata
37
+ const extensions = {
38
+ "extensions": {
39
+ "EXT_structural_metadata": {
40
+ "schema": {
41
+ "classes": {
42
+ "tree": {
43
+ "name": "Tree",
44
+ "description": "Woody, perennial plant.",
45
+ "properties": {
46
+ "species": {
47
+ "description": "Type of tree.",
48
+ "type": "ENUM",
49
+ "enumType": "speciesEnum",
50
+ "required": true
51
+ },
52
+ "age": {
53
+ "description": "The age of the tree, in years",
54
+ "type": "SCALAR",
55
+ "componentType": "UINT8",
56
+ "required": true
57
+ }
58
+ }
59
+ }
60
+ },
61
+ "enums": {
62
+ "speciesEnum": {
63
+ "name": "Species",
64
+ "description": "An example enum for tree species.",
65
+ // valueType is not defined here. Default is "UINT16"
66
+ "values": [
67
+ { "name": "Unspecified", "value": 0 },
68
+ { "name": "Oak", "value": 1 }
69
+ ]
70
+ }
71
+ }
72
+ },
73
+ "propertyTables": [{
74
+ "name": "tree_survey_2021-09-29",
75
+ "class": "tree",
76
+ "count": 10, // The number of elements in each property array (in `species`, in `age`).
77
+ "properties": {
78
+ "species": {
79
+ "values": 0, // It's an index of the buffer view containing property values.
80
+ },
81
+ "age": {
82
+ "values": 1
83
+ }
84
+ }
85
+ }]
86
+ }
87
+ }
88
+ }
89
+ */
90
+
91
+ /**
92
+ * Returns the property table populated with the data taken according to the extension schema.
93
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
94
+ * @param {number} propertyTableIndex - Index of the property table to locate.
95
+ * @returns {GLTF_EXT_structural_metadata_PropertyTable} Property table populated with the data.
96
+ * Throws an exception if no property table was found for propertyTableIndex provided.
97
+ */
98
+ export function getPropertyTablePopulated(
99
+ scenegraph: GLTFScenegraph,
100
+ propertyTableIndex: number
101
+ ): GLTF_EXT_structural_metadata_PropertyTable {
102
+ const extension: GLTF_EXT_structural_metadata | null = scenegraph.getExtension(
103
+ EXT_STRUCTURAL_METADATA_NAME
104
+ );
105
+ const propertyTable = extension?.propertyTables?.[propertyTableIndex];
106
+ if (extension?.schema && propertyTable) {
107
+ processPropertyTable(scenegraph, extension.schema, propertyTable);
108
+ return propertyTable;
109
+ }
110
+ throw new Error(
111
+ `Incorrect data in the EXT_structural_metadata extension: no property table with index ${propertyTableIndex}`
112
+ );
113
+ }
114
+
115
+ /**
116
+ * Decodes feature metadata from extension
117
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
118
+ * @param {GLTFLoaderOptions} options - loader options.
119
+ */
120
+ function decodeExtStructuralMetadata(scenegraph: GLTFScenegraph, options: GLTFLoaderOptions): void {
121
+ const extension: GLTF_EXT_structural_metadata | null = scenegraph.getExtension(
122
+ EXT_STRUCTURAL_METADATA_NAME
123
+ );
124
+ if (!extension?.schema) {
125
+ return;
126
+ }
127
+
128
+ const propertyTextures = extension.propertyTextures;
129
+ const json = scenegraph.gltf.json;
130
+ if (propertyTextures && json.meshes && options?.gltf?.loadImages) {
131
+ // Iterate through all meshes/primitives.
132
+ for (const mesh of json.meshes) {
133
+ for (const primitive of mesh.primitives) {
134
+ processPrimitivePropertyTextures(scenegraph, propertyTextures, primitive, extension);
135
+ }
136
+ }
137
+ }
138
+
139
+ const schemaClasses = extension.schema.classes;
140
+ const propertyTables = extension.propertyTables;
141
+ if (schemaClasses && propertyTables) {
142
+ for (const schemaName in schemaClasses) {
143
+ const propertyTable = findPropertyTableByClass(propertyTables, schemaName);
144
+ if (propertyTable) {
145
+ processPropertyTable(scenegraph, extension.schema, propertyTable);
146
+ }
147
+ }
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Takes data from property textures reffered by the primitive
153
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
154
+ * @param {GLTF_EXT_structural_metadata_PropertyTexture[]} propertyTextures - propertyTexture definition taken from the top-level extention
155
+ * @param {GLTFMeshPrimitive} primitive - Primitive object
156
+ * @param {GLTF_EXT_structural_metadata} extension - top-level extension
157
+ */
158
+ function processPrimitivePropertyTextures(
159
+ scenegraph: GLTFScenegraph,
160
+ propertyTextures: GLTF_EXT_structural_metadata_PropertyTexture[],
161
+ primitive: GLTFMeshPrimitive,
162
+ extension: GLTF_EXT_structural_metadata
163
+ ): void {
164
+ if (!propertyTextures) {
165
+ return;
166
+ }
167
+ const primitiveExtension: GLTF_EXT_structural_metadata_Primitive = primitive.extensions?.[
168
+ EXT_STRUCTURAL_METADATA_NAME
169
+ ] as GLTF_EXT_structural_metadata_Primitive;
170
+ const primitivePropertyTextureIndices = primitiveExtension?.propertyTextures;
171
+ if (!primitivePropertyTextureIndices) {
172
+ return;
173
+ }
174
+
175
+ for (const primitivePropertyTextureIndex of primitivePropertyTextureIndices) {
176
+ const propertyTexture = propertyTextures[primitivePropertyTextureIndex];
177
+ processPrimitivePropertyTexture(scenegraph, propertyTexture, primitive, extension);
178
+ }
179
+ }
180
+
181
+ /**
182
+ * Takes property data from the texture pointed by the primitive and appends them to `exension.data`
183
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
184
+ * @param {GLTF_EXT_structural_metadata_PropertyTexture} propertyTexture - propertyTexture definition taken from the top-level extension.
185
+ * @param {GLTFMeshPrimitive} primitive - Primitive object
186
+ * @param {GLTF_EXT_structural_metadata} extension - top-level extension
187
+ */
188
+ function processPrimitivePropertyTexture(
189
+ scenegraph: GLTFScenegraph,
190
+ propertyTexture: GLTF_EXT_structural_metadata_PropertyTexture,
191
+ primitive: GLTFMeshPrimitive,
192
+ extension: GLTF_EXT_structural_metadata
193
+ ): void {
194
+ if (!propertyTexture.properties) {
195
+ return;
196
+ }
197
+
198
+ if (!extension.dataAttributeNames) {
199
+ extension.dataAttributeNames = [];
200
+ }
201
+
202
+ /* Iterate through all properties defined in propertyTexture, e.g. "speed" and "direction":
203
+ {
204
+ "class": "wind",
205
+ "properties": {
206
+ "speed": {
207
+ "index": 0,
208
+ "texCoord": 0,
209
+ "channels": [0]
210
+ },
211
+ "direction": {
212
+ "index": 0,
213
+ "texCoord": 0,
214
+ "channels": [1, 2]
215
+ }
216
+ }
217
+ }
218
+ */
219
+ const className = propertyTexture.class;
220
+ for (const propName in propertyTexture.properties) {
221
+ // propName has values like "speed", "direction"
222
+ // Make attributeName as a combination of the class name and the propertyName like "wind_speed" or "wind_direction"
223
+ const attributeName = `${className}_${propName}`;
224
+ const textureInfoTopLevel: GLTFTextureInfoMetadata | undefined =
225
+ propertyTexture.properties?.[propName];
226
+ if (!textureInfoTopLevel) {
227
+ // eslint-disable-next-line no-continue
228
+ continue;
229
+ }
230
+
231
+ // The data taken from all meshes/primitives (the same property, e.g. "speed" or "direction") will be combined into one array and saved in textureInfoTopLevel.data
232
+ // Initially textureInfoTopLevel.data will be initialized with an empty array.
233
+ if (!textureInfoTopLevel.data) {
234
+ textureInfoTopLevel.data = [];
235
+ }
236
+ const featureTextureTable: number[] = textureInfoTopLevel.data as number[];
237
+
238
+ const propertyData: number[] | null = getPrimitiveTextureData(
239
+ scenegraph,
240
+ textureInfoTopLevel,
241
+ primitive
242
+ );
243
+ if (propertyData === null) {
244
+ // eslint-disable-next-line no-continue
245
+ continue;
246
+ }
247
+ primitivePropertyDataToAttributes(
248
+ scenegraph,
249
+ attributeName,
250
+ propertyData,
251
+ featureTextureTable,
252
+ primitive
253
+ );
254
+ textureInfoTopLevel.data = featureTextureTable;
255
+ extension.dataAttributeNames.push(attributeName);
256
+ }
257
+ }
258
+
259
+ /**
260
+ * Navigates through all properies in the property table, gets properties data,
261
+ * and put the data to `propertyTable.data` as an array.
262
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
263
+ * @param {GLTF_EXT_structural_metadata_Schema} schema - schema object
264
+ * @param {GLTF_EXT_structural_metadata_PropertyTable} propertyTable - propertyTable definition taken from the top-level extension
265
+ */
266
+ function processPropertyTable(
267
+ scenegraph: GLTFScenegraph,
268
+ schema: GLTF_EXT_structural_metadata_Schema,
269
+ propertyTable: GLTF_EXT_structural_metadata_PropertyTable
270
+ ): void {
271
+ const schemaClass = schema.classes?.[propertyTable.class];
272
+ if (!schemaClass) {
273
+ throw new Error(
274
+ `Incorrect data in the EXT_structural_metadata extension: no schema class with name ${propertyTable.class}`
275
+ );
276
+ }
277
+
278
+ const numberOfElements = propertyTable.count; // `propertyTable.count` is a number of elements in each property array.
279
+
280
+ for (const propertyName in schemaClass.properties) {
281
+ const classProperty = schemaClass.properties[propertyName];
282
+ const propertyTableProperty: GLTF_EXT_structural_metadata_PropertyTable_Property | undefined =
283
+ propertyTable.properties?.[propertyName];
284
+
285
+ if (propertyTableProperty) {
286
+ // Getting all elements (`numberOfElements`) of the array in the `propertyTableProperty`
287
+ const data = getPropertyDataFromBinarySource(
288
+ scenegraph,
289
+ schema,
290
+ classProperty,
291
+ numberOfElements,
292
+ propertyTableProperty
293
+ );
294
+ propertyTableProperty.data = data;
295
+ }
296
+ }
297
+ }
298
+
299
+ /**
300
+ * Decodes properties from binary sourse based on property type.
301
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
302
+ * @param {GLTF_EXT_structural_metadata_Schema} schema - Schema object
303
+ * @param {GLTF_EXT_structural_metadata_ClassProperty} classProperty - class property object
304
+ * @param {GLTF_EXT_structural_metadata_PropertyTable_Property} propertyTableProperty
305
+ * @param {number} numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
306
+ * @returns {string[] | number[] | string[][] | number[][]}
307
+ */
308
+ /* eslint complexity: ["error", 17]*/
309
+ function getPropertyDataFromBinarySource(
310
+ scenegraph: GLTFScenegraph,
311
+ schema: GLTF_EXT_structural_metadata_Schema,
312
+ classProperty: GLTF_EXT_structural_metadata_ClassProperty,
313
+ numberOfElements: number,
314
+ propertyTableProperty: GLTF_EXT_structural_metadata_PropertyTable_Property
315
+ ): string[] | number[] | string[][] | number[][] {
316
+ let data: string[] | number[] | string[][] | number[][] = [];
317
+ const valuesBufferView = propertyTableProperty.values;
318
+ const valuesDataBytes: Uint8Array = scenegraph.getTypedArrayForBufferView(valuesBufferView);
319
+
320
+ let arrayOffsets: TypedArray | null = null;
321
+ if (
322
+ classProperty.array &&
323
+ // `count` is a number of array elements. May only be defined when `array` is true.
324
+ typeof classProperty.count === 'undefined' && // If `count` is NOT defined, it's a VARIABLE-length array
325
+ typeof propertyTableProperty.arrayOffsets !== 'undefined' && // `arrayOffsets` is an index of the buffer view containing offsets for variable-length arrays.
326
+ typeof propertyTableProperty.arrayOffsetType !== 'undefined'
327
+ ) {
328
+ // Data are in a VARIABLE-length array
329
+ arrayOffsets = getOffsetArray(
330
+ scenegraph,
331
+ propertyTableProperty.arrayOffsets,
332
+ propertyTableProperty.arrayOffsetType,
333
+ numberOfElements
334
+ );
335
+ }
336
+
337
+ let stringOffsets: TypedArray | null = null;
338
+ if (
339
+ typeof propertyTableProperty.stringOffsets !== 'undefined' && // `stringOffsets` is an index of the buffer view containing offsets for strings.
340
+ typeof propertyTableProperty.stringOffsetType !== 'undefined'
341
+ ) {
342
+ // Data are in a FIXED-length array
343
+ stringOffsets = getOffsetArray(
344
+ scenegraph,
345
+ propertyTableProperty.stringOffsets,
346
+ propertyTableProperty.stringOffsetType,
347
+ numberOfElements
348
+ );
349
+ }
350
+
351
+ switch (classProperty.type) {
352
+ case 'SCALAR':
353
+ case 'VEC2':
354
+ case 'VEC3':
355
+ case 'VEC4':
356
+ case 'MAT2':
357
+ case 'MAT3':
358
+ case 'MAT4': {
359
+ data = getPropertyDataNumeric(classProperty, numberOfElements, valuesDataBytes, arrayOffsets);
360
+ break;
361
+ }
362
+ case 'BOOLEAN': {
363
+ // TODO: implement it as soon as we have the corresponding tileset
364
+ throw new Error(`Not implemented - classProperty.type=${classProperty.type}`);
365
+ }
366
+ case 'STRING': {
367
+ data = getPropertyDataString(
368
+ classProperty,
369
+ numberOfElements,
370
+ valuesDataBytes,
371
+ arrayOffsets,
372
+ stringOffsets
373
+ );
374
+ break;
375
+ }
376
+ case 'ENUM': {
377
+ data = getPropertyDataENUM(
378
+ schema,
379
+ classProperty,
380
+ numberOfElements,
381
+ valuesDataBytes,
382
+ arrayOffsets
383
+ );
384
+ break;
385
+ }
386
+ default:
387
+ throw new Error(`Unknown classProperty type ${classProperty.type}`);
388
+ }
389
+
390
+ return data;
391
+ }
392
+
393
+ /**
394
+ * Gets offset array from `arrayOffsets` or `stringOffsets`.
395
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
396
+ * @param {number} offsets - Buffer view index
397
+ * @param offsetType - The type of values in `arrayOffsets` or `stringOffsets`.
398
+ * @param {number} numberOfElements - The number of elements in each property array.
399
+ * @returns {TypedArray}
400
+ */
401
+ function getOffsetArray(
402
+ scenegraph: GLTFScenegraph,
403
+ offsets: number,
404
+ offsetType: 'UINT8' | 'UINT16' | 'UINT32' | 'UINT64',
405
+ numberOfElements: number
406
+ ): TypedArray {
407
+ const arrayOffsetsBufferView = offsets;
408
+ const arrayOffsetsBytes = scenegraph.getTypedArrayForBufferView(arrayOffsetsBufferView);
409
+
410
+ const arrayOffsets = convertRawBufferToMetadataArray(
411
+ arrayOffsetsBytes,
412
+ 'SCALAR', // offsets consist of ONE component
413
+ offsetType,
414
+ numberOfElements + 1 // The number of offsets is equal to the property table `count` plus one.
415
+ );
416
+ return arrayOffsets;
417
+ }
418
+
419
+ /**
420
+ * Decodes properties of SCALAR, VEC-N, MAT-N types from binary sourse.
421
+ * @param {GLTF_EXT_structural_metadata_ClassProperty} classProperty - class property object
422
+ * @param {number} numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
423
+ * @param {Uint8Array} valuesDataBytes - data taken from values property of the property table property.
424
+ * @param {TypedArray | null} arrayOffsets - offsets for variable-length arrays. It's null for fixed-length arrays or scalar types.
425
+ * @returns {number[] | number[][]}
426
+ */
427
+ function getPropertyDataNumeric(
428
+ classProperty: GLTF_EXT_structural_metadata_ClassProperty,
429
+ numberOfElements: number,
430
+ valuesDataBytes: Uint8Array,
431
+ arrayOffsets: TypedArray | null
432
+ ): number[] | number[][] {
433
+ const isArray = classProperty.array;
434
+ const arrayCount = classProperty.count;
435
+
436
+ const elementSize = getArrayElementByteSize(classProperty.type, classProperty.componentType);
437
+ const elementCount = valuesDataBytes.byteLength / elementSize;
438
+
439
+ let valuesData: TypedArray;
440
+ if (classProperty.componentType) {
441
+ valuesData = convertRawBufferToMetadataArray(
442
+ valuesDataBytes,
443
+ classProperty.type,
444
+ classProperty.componentType, // The datatype of the element's components. Only applicable to `SCALAR`, `VECN`, and `MATN` types.
445
+ elementCount
446
+ );
447
+ } else {
448
+ // The spec doesn't provide any info what to do if componentType is not set.
449
+ valuesData = valuesDataBytes;
450
+ }
451
+
452
+ if (isArray) {
453
+ if (arrayOffsets) {
454
+ // VARIABLE-length array
455
+ return handleVariableLengthArrayNumeric(
456
+ valuesData,
457
+ numberOfElements,
458
+ arrayOffsets,
459
+ valuesDataBytes.length,
460
+ elementSize
461
+ );
462
+ }
463
+ if (arrayCount) {
464
+ // FIXED-length array
465
+ return handleFixedLengthArrayNumeric(valuesData, numberOfElements, arrayCount);
466
+ }
467
+ return [];
468
+ }
469
+
470
+ // Single value (not an array)
471
+ const attributeValueArray: number[] = [];
472
+ for (let index = 0; index < numberOfElements; index++) {
473
+ const value = valuesData[index];
474
+ attributeValueArray.push(value);
475
+ }
476
+ return attributeValueArray;
477
+ }
478
+
479
+ function handleVariableLengthArrayNumeric(
480
+ valuesData: TypedArray,
481
+ numberOfElements: number,
482
+ arrayOffsets: TypedArray,
483
+ valuesDataBytesLength: number,
484
+ elementSize: number
485
+ ) {
486
+ const attributeValueArray: number[][] = [];
487
+ for (let index = 0; index < numberOfElements; index++) {
488
+ const array: number[] = [];
489
+ const arrayOffset = arrayOffsets[index];
490
+ const arrayByteSize = arrayOffsets[index + 1] - arrayOffsets[index];
491
+ if (arrayByteSize + arrayOffset <= valuesDataBytesLength) {
492
+ const typedArrayOffset = arrayOffset / elementSize;
493
+ const elementCount = arrayByteSize / elementSize;
494
+ for (let i = 0; i < elementCount; i++) {
495
+ const value = valuesData[typedArrayOffset + i];
496
+ array.push(value);
497
+ }
498
+ }
499
+ attributeValueArray.push(array);
500
+ }
501
+ return attributeValueArray;
502
+ }
503
+
504
+ function handleFixedLengthArrayNumeric(
505
+ valuesData: TypedArray,
506
+ numberOfElements: number,
507
+ arrayCount: number
508
+ ) {
509
+ const attributeValueArray: number[][] = [];
510
+ for (let index = 0; index < numberOfElements; index++) {
511
+ const array: number[] = [];
512
+ for (let i = 0; i < arrayCount; i++) {
513
+ const value = valuesData[i];
514
+ array.push(value);
515
+ }
516
+ attributeValueArray.push(array);
517
+ }
518
+ return attributeValueArray;
519
+ }
520
+
521
+ /**
522
+ * Decodes properties of string type from binary sourse.
523
+ * @param {GLTF_EXT_structural_metadata_ClassProperty} classProperty - class property object
524
+ * @param {number} numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
525
+ * @param {Uint8Array} valuesDataBytes - data taken from values property of the property table property.
526
+ * @param {TypedArray | null} arrayOffsets - offsets for variable-length arrays. It's null for fixed-length arrays or scalar types.
527
+ * @param {TypedArray | null} stringOffsets - index of the buffer view containing offsets for strings. It should be available for string type.
528
+ * @returns {string[] | string[][]}
529
+ */
530
+ function getPropertyDataString(
531
+ classProperty: GLTF_EXT_structural_metadata_ClassProperty,
532
+ numberOfElements: number,
533
+ valuesDataBytes: Uint8Array,
534
+ arrayOffsets: TypedArray | null,
535
+ stringOffsets: TypedArray | null
536
+ ): string[] | string[][] {
537
+ if (arrayOffsets) {
538
+ // TODO: implement it as soon as we have the corresponding tileset
539
+ throw new Error(`Not implemented - classProperty.type=${classProperty.type}`);
540
+ }
541
+
542
+ if (stringOffsets) {
543
+ const stringsArray: string[] = [];
544
+ const textDecoder = new TextDecoder('utf8');
545
+
546
+ let stringOffset = 0;
547
+ for (let index = 0; index < numberOfElements; index++) {
548
+ const stringByteSize = stringOffsets[index + 1] - stringOffsets[index];
549
+
550
+ if (stringByteSize + stringOffset <= valuesDataBytes.length) {
551
+ const stringData = valuesDataBytes.subarray(stringOffset, stringByteSize + stringOffset);
552
+ const stringAttribute = textDecoder.decode(stringData);
553
+
554
+ stringsArray.push(stringAttribute);
555
+ stringOffset += stringByteSize;
556
+ }
557
+ }
558
+
559
+ return stringsArray;
560
+ }
561
+ return [];
562
+ }
563
+
564
+ /**
565
+ * Decodes properties of enum type from binary sourse.
566
+ * @param {GLTF_EXT_structural_metadata_Schema} schema - schema object
567
+ * @param {GLTF_EXT_structural_metadata_ClassProperty} classProperty - class property object
568
+ * @param {number} numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
569
+ * @param {Uint8Array} valuesDataBytes - data taken from values property of the property table property.
570
+ * @param {TypedArray | null} arrayOffsets - offsets for variable-length arrays. It's null for fixed-length arrays or scalar types.
571
+ * @returns {string[] | string[][]}
572
+ */
573
+ function getPropertyDataENUM(
574
+ schema: GLTF_EXT_structural_metadata_Schema,
575
+ classProperty: GLTF_EXT_structural_metadata_ClassProperty,
576
+ numberOfElements: number,
577
+ valuesDataBytes: Uint8Array,
578
+ arrayOffsets: TypedArray | null
579
+ ): string[] | string[][] {
580
+ const data: string[] = [];
581
+
582
+ const isArray = classProperty.array;
583
+ const arrayCount = classProperty.count;
584
+
585
+ const enumType = classProperty.enumType;
586
+ // Enum ID as declared in the `enums` dictionary. Required when `type` is `ENUM`.
587
+ if (!enumType) {
588
+ throw new Error(
589
+ 'Incorrect data in the EXT_structural_metadata extension: classProperty.enumType is not set for type ENUM'
590
+ );
591
+ }
592
+
593
+ const enumEntry: GLTF_EXT_structural_metadata_Enum | undefined = schema.enums?.[enumType];
594
+ if (!enumEntry) {
595
+ throw new Error(
596
+ `Incorrect data in the EXT_structural_metadata extension: schema.enums does't contain ${enumType}`
597
+ );
598
+ }
599
+
600
+ const enumValueType = enumEntry.valueType || 'UINT16';
601
+ const elementSize = getArrayElementByteSize(classProperty.type, enumValueType);
602
+ const elementCount = valuesDataBytes.byteLength / elementSize;
603
+ const valuesData: TypedArray = convertRawBufferToMetadataArray(
604
+ valuesDataBytes,
605
+ classProperty.type,
606
+ enumValueType,
607
+ elementCount
608
+ );
609
+
610
+ if (isArray) {
611
+ if (arrayOffsets) {
612
+ // VARIABLE-length array
613
+ return handleVariableLengthArrayENUM(
614
+ valuesData,
615
+ numberOfElements,
616
+ arrayOffsets,
617
+ valuesDataBytes.length,
618
+ elementSize,
619
+ enumEntry
620
+ );
621
+ }
622
+ if (arrayCount) {
623
+ // FIXED-length array
624
+ return handleFixedLengthArrayENUM(valuesData, numberOfElements, arrayCount, enumEntry);
625
+ }
626
+ return [];
627
+ }
628
+
629
+ // Single value (not an array)
630
+ for (let index = 0; index < numberOfElements; index++) {
631
+ const enumValue = valuesData[index];
632
+ const enumObject = getEnumByValue(enumEntry, enumValue);
633
+ if (enumObject) {
634
+ data.push(enumObject.name);
635
+ }
636
+ }
637
+
638
+ return data;
639
+ }
640
+
641
+ /* eslint max-params: ["error", 6]*/
642
+ function handleVariableLengthArrayENUM(
643
+ valuesData: TypedArray,
644
+ numberOfElements: number,
645
+ arrayOffsets: TypedArray,
646
+ valuesDataBytesLength: number,
647
+ elementSize: number,
648
+ enumEntry: GLTF_EXT_structural_metadata_Enum
649
+ ) {
650
+ const attributeValueArray: string[][] = [];
651
+ for (let index = 0; index < numberOfElements; index++) {
652
+ const array: string[] = [];
653
+ const arrayOffset = arrayOffsets[index];
654
+ const arrayByteSize = arrayOffsets[index + 1] - arrayOffsets[index];
655
+ if (arrayByteSize + arrayOffset <= valuesDataBytesLength) {
656
+ const typedArrayOffset = arrayOffset / elementSize;
657
+ const elementCount = arrayByteSize / elementSize;
658
+ for (let i = 0; i < elementCount; i++) {
659
+ const value = valuesData[typedArrayOffset + i];
660
+
661
+ const enumObject = getEnumByValue(enumEntry, value);
662
+ if (enumObject) {
663
+ array.push(enumObject.name);
664
+ }
665
+ }
666
+ }
667
+ attributeValueArray.push(array);
668
+ }
669
+ return attributeValueArray;
670
+ }
671
+
672
+ function handleFixedLengthArrayENUM(
673
+ valuesData: TypedArray,
674
+ numberOfElements: number,
675
+ arrayCount: number,
676
+ enumEntry: GLTF_EXT_structural_metadata_Enum
677
+ ) {
678
+ const attributeValueArray: string[][] = [];
679
+ for (let index = 0; index < numberOfElements; index++) {
680
+ const array: string[] = [];
681
+ for (let i = 0; i < arrayCount; i++) {
682
+ const value = valuesData[i];
683
+
684
+ const enumObject = getEnumByValue(enumEntry, value);
685
+ if (enumObject) {
686
+ array.push(enumObject.name);
687
+ }
688
+ }
689
+ attributeValueArray.push(array);
690
+ }
691
+ return attributeValueArray;
692
+ }
693
+ /**
694
+ * Find the property table by class name.
695
+ * @param {GLTF_EXT_structural_metadata_PropertyTable[]} propertyTables
696
+ * @param {string} schemaClassName
697
+ */
698
+ function findPropertyTableByClass(
699
+ propertyTables: GLTF_EXT_structural_metadata_PropertyTable[],
700
+ schemaClassName: string
701
+ ): GLTF_EXT_structural_metadata_PropertyTable | null {
702
+ for (let i = 0, len = propertyTables.length; i < len; i++) {
703
+ const propertyTable = propertyTables[i];
704
+
705
+ if (propertyTable.class === schemaClassName) {
706
+ return propertyTable;
707
+ }
708
+ }
709
+
710
+ return null;
711
+ }
712
+
713
+ /**
714
+ * Looks up ENUM whose `value` property matches the specified number in the parameter `value`.
715
+ * @param {GLTF_EXT_structural_metadata_Enum} enumEntry - ENUM entry containing the array of possible enums.
716
+ * @param {number} value - the value of the ENUM to locate.
717
+ * @returns {GLTF_EXT_structural_metadata_EnumValue | null} ENUM matcihng the specified value or null of no ENUM object was found.
718
+ */
719
+ function getEnumByValue(
720
+ enumEntry: GLTF_EXT_structural_metadata_Enum,
721
+ value: number
722
+ ): GLTF_EXT_structural_metadata_EnumValue | null {
723
+ for (const enumValue of enumEntry.values) {
724
+ if (enumValue.value === value) {
725
+ return enumValue;
726
+ }
727
+ }
728
+
729
+ return null;
730
+ }