@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,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decode = exports.name = void 0;
4
+ const gltf_scenegraph_1 = require("../api/gltf-scenegraph");
5
+ const data_processing_1 = require("./data-processing");
6
+ const EXT_structural_metadata_1 = require("./EXT_structural_metadata");
7
+ const EXT_MESH_FEATURES_NAME = 'EXT_mesh_features';
8
+ exports.name = EXT_MESH_FEATURES_NAME;
9
+ async function decode(gltfData, options) {
10
+ const scenegraph = new gltf_scenegraph_1.GLTFScenegraph(gltfData);
11
+ decodeExtMeshFeatures(scenegraph, options);
12
+ }
13
+ exports.decode = decode;
14
+ /**
15
+ * Decodes feature metadata from extension
16
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
17
+ * @param {GLTFLoaderOptions} options - loader options.
18
+ */
19
+ function decodeExtMeshFeatures(scenegraph, options) {
20
+ const json = scenegraph.gltf.json;
21
+ if (!json.meshes) {
22
+ return;
23
+ }
24
+ // Iterate through all meshes/primitives.
25
+ for (const mesh of json.meshes) {
26
+ for (const primitive of mesh.primitives) {
27
+ processMeshPrimitiveFeatures(scenegraph, primitive, options);
28
+ }
29
+ }
30
+ }
31
+ /**
32
+ * Takes data from EXT_mesh_features and store it in 'data' property of featureIds.
33
+ * If combined with EXT_structural_metadata, corresponding data are taken from the property tables of that extension.
34
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
35
+ * @param {GLTFMeshPrimitive} primitive - primitive that contains extensions.
36
+ * @param {GLTFLoaderOptions} options - loader options.
37
+ */
38
+ function processMeshPrimitiveFeatures(scenegraph, primitive, options) {
39
+ const extension = primitive.extensions?.[EXT_MESH_FEATURES_NAME];
40
+ const featureIds = extension?.featureIds;
41
+ if (!featureIds)
42
+ return;
43
+ if (!extension.dataAttributeNames) {
44
+ extension.dataAttributeNames = [];
45
+ }
46
+ let featureIdCount = 0; // It can be used to name the feature if neither label nor property table name is provided.
47
+ for (const featureId of featureIds) {
48
+ /*
49
+ When combined with the EXT_structural_metadata extension, feature ID sets can be associated with property tables.
50
+ A property table maps each feature ID to a set of values that are associated with the respective feature.
51
+ The feature ID in this case serves as an index for the row of the table.
52
+ The index of the property table that a certain set of feature IDs is associated with is stored in the propertyTable of the feature ID set definition.
53
+ */
54
+ let propertyTable = null;
55
+ if (typeof featureId.propertyTable === 'number') {
56
+ propertyTable = (0, EXT_structural_metadata_1.getPropertyTablePopulated)(scenegraph, featureId.propertyTable);
57
+ }
58
+ let propertyData = null;
59
+ // Process "Feature ID by Vertex"
60
+ if (typeof featureId.attribute !== 'undefined') {
61
+ const accessorKey = `_FEATURE_ID_${featureId.attribute}`;
62
+ const accessorIndex = primitive.attributes[accessorKey];
63
+ const propertyDataTypedArray = scenegraph.getTypedArrayForAccessor(accessorIndex);
64
+ propertyData = Array.prototype.slice.call(propertyDataTypedArray);
65
+ }
66
+ // Process "Feature ID by Texture Coordinates"
67
+ else if (typeof featureId.texture !== 'undefined' && options?.gltf?.loadImages) {
68
+ propertyData = (0, data_processing_1.getPrimitiveTextureData)(scenegraph, featureId.texture, primitive);
69
+ }
70
+ // Process "Feature ID by Index"
71
+ else {
72
+ /*
73
+ When both featureId.attribute and featureId.texture are undefined,
74
+ then the feature ID value for each vertex is given implicitly, via the index of the vertex.
75
+ In this case, the featureCount must match the number of vertices of the mesh primitive.
76
+ */
77
+ // TODO: At the moment of writing we don't have a tileset with the data of that kind. Implement it later.
78
+ }
79
+ const attributeName = featureId.label || propertyTable?.name || `featureAttribute${featureIdCount}`;
80
+ // featureTable - an array where unique data from the property data are being stored
81
+ const featureTable = [];
82
+ if (propertyData) {
83
+ (0, data_processing_1.primitivePropertyDataToAttributes)(scenegraph, attributeName, propertyData, featureTable, primitive);
84
+ }
85
+ extension.dataAttributeNames.push(attributeName);
86
+ featureId.data = featureTable;
87
+ featureIdCount++;
88
+ }
89
+ }
@@ -0,0 +1,17 @@
1
+ import type { GLTF } from '../types/gltf-json-schema';
2
+ import type { GLTF_EXT_structural_metadata_PropertyTable } from '../types/gltf-ext-structural-metadata-schema';
3
+ import type { GLTFLoaderOptions } from '../../gltf-loader';
4
+ import { GLTFScenegraph } from '../api/gltf-scenegraph';
5
+ export declare const name = "EXT_structural_metadata";
6
+ export declare function decode(gltfData: {
7
+ json: GLTF;
8
+ }, options: GLTFLoaderOptions): Promise<void>;
9
+ /**
10
+ * Returns the property table populated with the data taken according to the extension schema.
11
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
12
+ * @param {number} propertyTableIndex - Index of the property table to locate.
13
+ * @returns {GLTF_EXT_structural_metadata_PropertyTable} Property table populated with the data.
14
+ * Throws an exception if no property table was found for propertyTableIndex provided.
15
+ */
16
+ export declare function getPropertyTablePopulated(scenegraph: GLTFScenegraph, propertyTableIndex: number): GLTF_EXT_structural_metadata_PropertyTable;
17
+ //# sourceMappingURL=EXT_structural_metadata.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EXT_structural_metadata.d.ts","sourceRoot":"","sources":["../../../src/lib/extensions/EXT_structural_metadata.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,IAAI,EAA6C,MAAM,2BAA2B,CAAC;AAChG,OAAO,KAAK,EAKV,0CAA0C,EAK3C,MAAM,8CAA8C,CAAC;AAGtD,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,mBAAmB,CAAC;AAEzD,OAAO,EAAC,cAAc,EAAC,MAAM,wBAAwB,CAAC;AAStD,eAAO,MAAM,IAAI,4BAA+B,CAAC;AAEjD,wBAAsB,MAAM,CAAC,QAAQ,EAAE;IAAC,IAAI,EAAE,IAAI,CAAA;CAAC,EAAE,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC,CAG9F;AA2DD;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CACvC,UAAU,EAAE,cAAc,EAC1B,kBAAkB,EAAE,MAAM,GACzB,0CAA0C,CAY5C"}
@@ -0,0 +1,504 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPropertyTablePopulated = exports.decode = exports.name = void 0;
4
+ const gltf_scenegraph_1 = require("../api/gltf-scenegraph");
5
+ const data_processing_1 = require("./data-processing");
6
+ const EXT_STRUCTURAL_METADATA_NAME = 'EXT_structural_metadata';
7
+ exports.name = EXT_STRUCTURAL_METADATA_NAME;
8
+ async function decode(gltfData, options) {
9
+ const scenegraph = new gltf_scenegraph_1.GLTFScenegraph(gltfData);
10
+ decodeExtStructuralMetadata(scenegraph, options);
11
+ }
12
+ exports.decode = decode;
13
+ /*
14
+ // Example of the extension.
15
+ // See more info at https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_structural_metadata
16
+ const extensions = {
17
+ "extensions": {
18
+ "EXT_structural_metadata": {
19
+ "schema": {
20
+ "classes": {
21
+ "tree": {
22
+ "name": "Tree",
23
+ "description": "Woody, perennial plant.",
24
+ "properties": {
25
+ "species": {
26
+ "description": "Type of tree.",
27
+ "type": "ENUM",
28
+ "enumType": "speciesEnum",
29
+ "required": true
30
+ },
31
+ "age": {
32
+ "description": "The age of the tree, in years",
33
+ "type": "SCALAR",
34
+ "componentType": "UINT8",
35
+ "required": true
36
+ }
37
+ }
38
+ }
39
+ },
40
+ "enums": {
41
+ "speciesEnum": {
42
+ "name": "Species",
43
+ "description": "An example enum for tree species.",
44
+ // valueType is not defined here. Default is "UINT16"
45
+ "values": [
46
+ { "name": "Unspecified", "value": 0 },
47
+ { "name": "Oak", "value": 1 }
48
+ ]
49
+ }
50
+ }
51
+ },
52
+ "propertyTables": [{
53
+ "name": "tree_survey_2021-09-29",
54
+ "class": "tree",
55
+ "count": 10, // The number of elements in each property array (in `species`, in `age`).
56
+ "properties": {
57
+ "species": {
58
+ "values": 0, // It's an index of the buffer view containing property values.
59
+ },
60
+ "age": {
61
+ "values": 1
62
+ }
63
+ }
64
+ }]
65
+ }
66
+ }
67
+ }
68
+ */
69
+ /**
70
+ * Returns the property table populated with the data taken according to the extension schema.
71
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
72
+ * @param {number} propertyTableIndex - Index of the property table to locate.
73
+ * @returns {GLTF_EXT_structural_metadata_PropertyTable} Property table populated with the data.
74
+ * Throws an exception if no property table was found for propertyTableIndex provided.
75
+ */
76
+ function getPropertyTablePopulated(scenegraph, propertyTableIndex) {
77
+ const extension = scenegraph.getExtension(EXT_STRUCTURAL_METADATA_NAME);
78
+ const propertyTable = extension?.propertyTables?.[propertyTableIndex];
79
+ if (extension?.schema && propertyTable) {
80
+ processPropertyTable(scenegraph, extension.schema, propertyTable);
81
+ return propertyTable;
82
+ }
83
+ throw new Error(`Incorrect data in the EXT_structural_metadata extension: no property table with index ${propertyTableIndex}`);
84
+ }
85
+ exports.getPropertyTablePopulated = getPropertyTablePopulated;
86
+ /**
87
+ * Decodes feature metadata from extension
88
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
89
+ * @param {GLTFLoaderOptions} options - loader options.
90
+ */
91
+ function decodeExtStructuralMetadata(scenegraph, options) {
92
+ const extension = scenegraph.getExtension(EXT_STRUCTURAL_METADATA_NAME);
93
+ if (!extension?.schema) {
94
+ return;
95
+ }
96
+ const propertyTextures = extension.propertyTextures;
97
+ const json = scenegraph.gltf.json;
98
+ if (propertyTextures && json.meshes && options?.gltf?.loadImages) {
99
+ // Iterate through all meshes/primitives.
100
+ for (const mesh of json.meshes) {
101
+ for (const primitive of mesh.primitives) {
102
+ processPrimitivePropertyTextures(scenegraph, propertyTextures, primitive, extension);
103
+ }
104
+ }
105
+ }
106
+ const schemaClasses = extension.schema.classes;
107
+ const propertyTables = extension.propertyTables;
108
+ if (schemaClasses && propertyTables) {
109
+ for (const schemaName in schemaClasses) {
110
+ const propertyTable = findPropertyTableByClass(propertyTables, schemaName);
111
+ if (propertyTable) {
112
+ processPropertyTable(scenegraph, extension.schema, propertyTable);
113
+ }
114
+ }
115
+ }
116
+ }
117
+ /**
118
+ * Takes data from property textures reffered by the primitive
119
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
120
+ * @param {GLTF_EXT_structural_metadata_PropertyTexture[]} propertyTextures - propertyTexture definition taken from the top-level extention
121
+ * @param {GLTFMeshPrimitive} primitive - Primitive object
122
+ * @param {GLTF_EXT_structural_metadata} extension - top-level extension
123
+ */
124
+ function processPrimitivePropertyTextures(scenegraph, propertyTextures, primitive, extension) {
125
+ if (!propertyTextures) {
126
+ return;
127
+ }
128
+ const primitiveExtension = primitive.extensions?.[EXT_STRUCTURAL_METADATA_NAME];
129
+ const primitivePropertyTextureIndices = primitiveExtension?.propertyTextures;
130
+ if (!primitivePropertyTextureIndices) {
131
+ return;
132
+ }
133
+ for (const primitivePropertyTextureIndex of primitivePropertyTextureIndices) {
134
+ const propertyTexture = propertyTextures[primitivePropertyTextureIndex];
135
+ processPrimitivePropertyTexture(scenegraph, propertyTexture, primitive, extension);
136
+ }
137
+ }
138
+ /**
139
+ * Takes property data from the texture pointed by the primitive and appends them to `exension.data`
140
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
141
+ * @param {GLTF_EXT_structural_metadata_PropertyTexture} propertyTexture - propertyTexture definition taken from the top-level extension.
142
+ * @param {GLTFMeshPrimitive} primitive - Primitive object
143
+ * @param {GLTF_EXT_structural_metadata} extension - top-level extension
144
+ */
145
+ function processPrimitivePropertyTexture(scenegraph, propertyTexture, primitive, extension) {
146
+ if (!propertyTexture.properties) {
147
+ return;
148
+ }
149
+ if (!extension.dataAttributeNames) {
150
+ extension.dataAttributeNames = [];
151
+ }
152
+ /* Iterate through all properties defined in propertyTexture, e.g. "speed" and "direction":
153
+ {
154
+ "class": "wind",
155
+ "properties": {
156
+ "speed": {
157
+ "index": 0,
158
+ "texCoord": 0,
159
+ "channels": [0]
160
+ },
161
+ "direction": {
162
+ "index": 0,
163
+ "texCoord": 0,
164
+ "channels": [1, 2]
165
+ }
166
+ }
167
+ }
168
+ */
169
+ const className = propertyTexture.class;
170
+ for (const propName in propertyTexture.properties) {
171
+ // propName has values like "speed", "direction"
172
+ // Make attributeName as a combination of the class name and the propertyName like "wind_speed" or "wind_direction"
173
+ const attributeName = `${className}_${propName}`;
174
+ const textureInfoTopLevel = propertyTexture.properties?.[propName];
175
+ if (!textureInfoTopLevel) {
176
+ // eslint-disable-next-line no-continue
177
+ continue;
178
+ }
179
+ // 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
180
+ // Initially textureInfoTopLevel.data will be initialized with an empty array.
181
+ if (!textureInfoTopLevel.data) {
182
+ textureInfoTopLevel.data = [];
183
+ }
184
+ const featureTextureTable = textureInfoTopLevel.data;
185
+ const propertyData = (0, data_processing_1.getPrimitiveTextureData)(scenegraph, textureInfoTopLevel, primitive);
186
+ if (propertyData === null) {
187
+ // eslint-disable-next-line no-continue
188
+ continue;
189
+ }
190
+ (0, data_processing_1.primitivePropertyDataToAttributes)(scenegraph, attributeName, propertyData, featureTextureTable, primitive);
191
+ textureInfoTopLevel.data = featureTextureTable;
192
+ extension.dataAttributeNames.push(attributeName);
193
+ }
194
+ }
195
+ /**
196
+ * Navigates through all properies in the property table, gets properties data,
197
+ * and put the data to `propertyTable.data` as an array.
198
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
199
+ * @param {GLTF_EXT_structural_metadata_Schema} schema - schema object
200
+ * @param {GLTF_EXT_structural_metadata_PropertyTable} propertyTable - propertyTable definition taken from the top-level extension
201
+ */
202
+ function processPropertyTable(scenegraph, schema, propertyTable) {
203
+ const schemaClass = schema.classes?.[propertyTable.class];
204
+ if (!schemaClass) {
205
+ throw new Error(`Incorrect data in the EXT_structural_metadata extension: no schema class with name ${propertyTable.class}`);
206
+ }
207
+ const numberOfElements = propertyTable.count; // `propertyTable.count` is a number of elements in each property array.
208
+ for (const propertyName in schemaClass.properties) {
209
+ const classProperty = schemaClass.properties[propertyName];
210
+ const propertyTableProperty = propertyTable.properties?.[propertyName];
211
+ if (propertyTableProperty) {
212
+ // Getting all elements (`numberOfElements`) of the array in the `propertyTableProperty`
213
+ const data = getPropertyDataFromBinarySource(scenegraph, schema, classProperty, numberOfElements, propertyTableProperty);
214
+ propertyTableProperty.data = data;
215
+ }
216
+ }
217
+ }
218
+ /**
219
+ * Decodes properties from binary sourse based on property type.
220
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
221
+ * @param {GLTF_EXT_structural_metadata_Schema} schema - Schema object
222
+ * @param {GLTF_EXT_structural_metadata_ClassProperty} classProperty - class property object
223
+ * @param {GLTF_EXT_structural_metadata_PropertyTable_Property} propertyTableProperty
224
+ * @param {number} numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
225
+ * @returns {string[] | number[] | string[][] | number[][]}
226
+ */
227
+ /* eslint complexity: ["error", 17]*/
228
+ function getPropertyDataFromBinarySource(scenegraph, schema, classProperty, numberOfElements, propertyTableProperty) {
229
+ let data = [];
230
+ const valuesBufferView = propertyTableProperty.values;
231
+ const valuesDataBytes = scenegraph.getTypedArrayForBufferView(valuesBufferView);
232
+ let arrayOffsets = null;
233
+ if (classProperty.array &&
234
+ // `count` is a number of array elements. May only be defined when `array` is true.
235
+ typeof classProperty.count === 'undefined' && // If `count` is NOT defined, it's a VARIABLE-length array
236
+ typeof propertyTableProperty.arrayOffsets !== 'undefined' && // `arrayOffsets` is an index of the buffer view containing offsets for variable-length arrays.
237
+ typeof propertyTableProperty.arrayOffsetType !== 'undefined') {
238
+ // Data are in a VARIABLE-length array
239
+ arrayOffsets = getOffsetArray(scenegraph, propertyTableProperty.arrayOffsets, propertyTableProperty.arrayOffsetType, numberOfElements);
240
+ }
241
+ let stringOffsets = null;
242
+ if (typeof propertyTableProperty.stringOffsets !== 'undefined' && // `stringOffsets` is an index of the buffer view containing offsets for strings.
243
+ typeof propertyTableProperty.stringOffsetType !== 'undefined') {
244
+ // Data are in a FIXED-length array
245
+ stringOffsets = getOffsetArray(scenegraph, propertyTableProperty.stringOffsets, propertyTableProperty.stringOffsetType, numberOfElements);
246
+ }
247
+ switch (classProperty.type) {
248
+ case 'SCALAR':
249
+ case 'VEC2':
250
+ case 'VEC3':
251
+ case 'VEC4':
252
+ case 'MAT2':
253
+ case 'MAT3':
254
+ case 'MAT4': {
255
+ data = getPropertyDataNumeric(classProperty, numberOfElements, valuesDataBytes, arrayOffsets);
256
+ break;
257
+ }
258
+ case 'BOOLEAN': {
259
+ // TODO: implement it as soon as we have the corresponding tileset
260
+ throw new Error(`Not implemented - classProperty.type=${classProperty.type}`);
261
+ }
262
+ case 'STRING': {
263
+ data = getPropertyDataString(classProperty, numberOfElements, valuesDataBytes, arrayOffsets, stringOffsets);
264
+ break;
265
+ }
266
+ case 'ENUM': {
267
+ data = getPropertyDataENUM(schema, classProperty, numberOfElements, valuesDataBytes, arrayOffsets);
268
+ break;
269
+ }
270
+ default:
271
+ throw new Error(`Unknown classProperty type ${classProperty.type}`);
272
+ }
273
+ return data;
274
+ }
275
+ /**
276
+ * Gets offset array from `arrayOffsets` or `stringOffsets`.
277
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
278
+ * @param {number} offsets - Buffer view index
279
+ * @param offsetType - The type of values in `arrayOffsets` or `stringOffsets`.
280
+ * @param {number} numberOfElements - The number of elements in each property array.
281
+ * @returns {TypedArray}
282
+ */
283
+ function getOffsetArray(scenegraph, offsets, offsetType, numberOfElements) {
284
+ const arrayOffsetsBufferView = offsets;
285
+ const arrayOffsetsBytes = scenegraph.getTypedArrayForBufferView(arrayOffsetsBufferView);
286
+ const arrayOffsets = (0, data_processing_1.convertRawBufferToMetadataArray)(arrayOffsetsBytes, 'SCALAR', // offsets consist of ONE component
287
+ offsetType, numberOfElements + 1 // The number of offsets is equal to the property table `count` plus one.
288
+ );
289
+ return arrayOffsets;
290
+ }
291
+ /**
292
+ * Decodes properties of SCALAR, VEC-N, MAT-N types from binary sourse.
293
+ * @param {GLTF_EXT_structural_metadata_ClassProperty} classProperty - class property object
294
+ * @param {number} numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
295
+ * @param {Uint8Array} valuesDataBytes - data taken from values property of the property table property.
296
+ * @param {TypedArray | null} arrayOffsets - offsets for variable-length arrays. It's null for fixed-length arrays or scalar types.
297
+ * @returns {number[] | number[][]}
298
+ */
299
+ function getPropertyDataNumeric(classProperty, numberOfElements, valuesDataBytes, arrayOffsets) {
300
+ const isArray = classProperty.array;
301
+ const arrayCount = classProperty.count;
302
+ const elementSize = (0, data_processing_1.getArrayElementByteSize)(classProperty.type, classProperty.componentType);
303
+ const elementCount = valuesDataBytes.byteLength / elementSize;
304
+ let valuesData;
305
+ if (classProperty.componentType) {
306
+ valuesData = (0, data_processing_1.convertRawBufferToMetadataArray)(valuesDataBytes, classProperty.type, classProperty.componentType, // The datatype of the element's components. Only applicable to `SCALAR`, `VECN`, and `MATN` types.
307
+ elementCount);
308
+ }
309
+ else {
310
+ // The spec doesn't provide any info what to do if componentType is not set.
311
+ valuesData = valuesDataBytes;
312
+ }
313
+ if (isArray) {
314
+ if (arrayOffsets) {
315
+ // VARIABLE-length array
316
+ return handleVariableLengthArrayNumeric(valuesData, numberOfElements, arrayOffsets, valuesDataBytes.length, elementSize);
317
+ }
318
+ if (arrayCount) {
319
+ // FIXED-length array
320
+ return handleFixedLengthArrayNumeric(valuesData, numberOfElements, arrayCount);
321
+ }
322
+ return [];
323
+ }
324
+ // Single value (not an array)
325
+ const attributeValueArray = [];
326
+ for (let index = 0; index < numberOfElements; index++) {
327
+ const value = valuesData[index];
328
+ attributeValueArray.push(value);
329
+ }
330
+ return attributeValueArray;
331
+ }
332
+ function handleVariableLengthArrayNumeric(valuesData, numberOfElements, arrayOffsets, valuesDataBytesLength, elementSize) {
333
+ const attributeValueArray = [];
334
+ for (let index = 0; index < numberOfElements; index++) {
335
+ const array = [];
336
+ const arrayOffset = arrayOffsets[index];
337
+ const arrayByteSize = arrayOffsets[index + 1] - arrayOffsets[index];
338
+ if (arrayByteSize + arrayOffset <= valuesDataBytesLength) {
339
+ const typedArrayOffset = arrayOffset / elementSize;
340
+ const elementCount = arrayByteSize / elementSize;
341
+ for (let i = 0; i < elementCount; i++) {
342
+ const value = valuesData[typedArrayOffset + i];
343
+ array.push(value);
344
+ }
345
+ }
346
+ attributeValueArray.push(array);
347
+ }
348
+ return attributeValueArray;
349
+ }
350
+ function handleFixedLengthArrayNumeric(valuesData, numberOfElements, arrayCount) {
351
+ const attributeValueArray = [];
352
+ for (let index = 0; index < numberOfElements; index++) {
353
+ const array = [];
354
+ for (let i = 0; i < arrayCount; i++) {
355
+ const value = valuesData[i];
356
+ array.push(value);
357
+ }
358
+ attributeValueArray.push(array);
359
+ }
360
+ return attributeValueArray;
361
+ }
362
+ /**
363
+ * Decodes properties of string type from binary sourse.
364
+ * @param {GLTF_EXT_structural_metadata_ClassProperty} classProperty - class property object
365
+ * @param {number} numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
366
+ * @param {Uint8Array} valuesDataBytes - data taken from values property of the property table property.
367
+ * @param {TypedArray | null} arrayOffsets - offsets for variable-length arrays. It's null for fixed-length arrays or scalar types.
368
+ * @param {TypedArray | null} stringOffsets - index of the buffer view containing offsets for strings. It should be available for string type.
369
+ * @returns {string[] | string[][]}
370
+ */
371
+ function getPropertyDataString(classProperty, numberOfElements, valuesDataBytes, arrayOffsets, stringOffsets) {
372
+ if (arrayOffsets) {
373
+ // TODO: implement it as soon as we have the corresponding tileset
374
+ throw new Error(`Not implemented - classProperty.type=${classProperty.type}`);
375
+ }
376
+ if (stringOffsets) {
377
+ const stringsArray = [];
378
+ const textDecoder = new TextDecoder('utf8');
379
+ let stringOffset = 0;
380
+ for (let index = 0; index < numberOfElements; index++) {
381
+ const stringByteSize = stringOffsets[index + 1] - stringOffsets[index];
382
+ if (stringByteSize + stringOffset <= valuesDataBytes.length) {
383
+ const stringData = valuesDataBytes.subarray(stringOffset, stringByteSize + stringOffset);
384
+ const stringAttribute = textDecoder.decode(stringData);
385
+ stringsArray.push(stringAttribute);
386
+ stringOffset += stringByteSize;
387
+ }
388
+ }
389
+ return stringsArray;
390
+ }
391
+ return [];
392
+ }
393
+ /**
394
+ * Decodes properties of enum type from binary sourse.
395
+ * @param {GLTF_EXT_structural_metadata_Schema} schema - schema object
396
+ * @param {GLTF_EXT_structural_metadata_ClassProperty} classProperty - class property object
397
+ * @param {number} numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
398
+ * @param {Uint8Array} valuesDataBytes - data taken from values property of the property table property.
399
+ * @param {TypedArray | null} arrayOffsets - offsets for variable-length arrays. It's null for fixed-length arrays or scalar types.
400
+ * @returns {string[] | string[][]}
401
+ */
402
+ function getPropertyDataENUM(schema, classProperty, numberOfElements, valuesDataBytes, arrayOffsets) {
403
+ const data = [];
404
+ const isArray = classProperty.array;
405
+ const arrayCount = classProperty.count;
406
+ const enumType = classProperty.enumType;
407
+ // Enum ID as declared in the `enums` dictionary. Required when `type` is `ENUM`.
408
+ if (!enumType) {
409
+ throw new Error('Incorrect data in the EXT_structural_metadata extension: classProperty.enumType is not set for type ENUM');
410
+ }
411
+ const enumEntry = schema.enums?.[enumType];
412
+ if (!enumEntry) {
413
+ throw new Error(`Incorrect data in the EXT_structural_metadata extension: schema.enums does't contain ${enumType}`);
414
+ }
415
+ const enumValueType = enumEntry.valueType || 'UINT16';
416
+ const elementSize = (0, data_processing_1.getArrayElementByteSize)(classProperty.type, enumValueType);
417
+ const elementCount = valuesDataBytes.byteLength / elementSize;
418
+ const valuesData = (0, data_processing_1.convertRawBufferToMetadataArray)(valuesDataBytes, classProperty.type, enumValueType, elementCount);
419
+ if (isArray) {
420
+ if (arrayOffsets) {
421
+ // VARIABLE-length array
422
+ return handleVariableLengthArrayENUM(valuesData, numberOfElements, arrayOffsets, valuesDataBytes.length, elementSize, enumEntry);
423
+ }
424
+ if (arrayCount) {
425
+ // FIXED-length array
426
+ return handleFixedLengthArrayENUM(valuesData, numberOfElements, arrayCount, enumEntry);
427
+ }
428
+ return [];
429
+ }
430
+ // Single value (not an array)
431
+ for (let index = 0; index < numberOfElements; index++) {
432
+ const enumValue = valuesData[index];
433
+ const enumObject = getEnumByValue(enumEntry, enumValue);
434
+ if (enumObject) {
435
+ data.push(enumObject.name);
436
+ }
437
+ }
438
+ return data;
439
+ }
440
+ /* eslint max-params: ["error", 6]*/
441
+ function handleVariableLengthArrayENUM(valuesData, numberOfElements, arrayOffsets, valuesDataBytesLength, elementSize, enumEntry) {
442
+ const attributeValueArray = [];
443
+ for (let index = 0; index < numberOfElements; index++) {
444
+ const array = [];
445
+ const arrayOffset = arrayOffsets[index];
446
+ const arrayByteSize = arrayOffsets[index + 1] - arrayOffsets[index];
447
+ if (arrayByteSize + arrayOffset <= valuesDataBytesLength) {
448
+ const typedArrayOffset = arrayOffset / elementSize;
449
+ const elementCount = arrayByteSize / elementSize;
450
+ for (let i = 0; i < elementCount; i++) {
451
+ const value = valuesData[typedArrayOffset + i];
452
+ const enumObject = getEnumByValue(enumEntry, value);
453
+ if (enumObject) {
454
+ array.push(enumObject.name);
455
+ }
456
+ }
457
+ }
458
+ attributeValueArray.push(array);
459
+ }
460
+ return attributeValueArray;
461
+ }
462
+ function handleFixedLengthArrayENUM(valuesData, numberOfElements, arrayCount, enumEntry) {
463
+ const attributeValueArray = [];
464
+ for (let index = 0; index < numberOfElements; index++) {
465
+ const array = [];
466
+ for (let i = 0; i < arrayCount; i++) {
467
+ const value = valuesData[i];
468
+ const enumObject = getEnumByValue(enumEntry, value);
469
+ if (enumObject) {
470
+ array.push(enumObject.name);
471
+ }
472
+ }
473
+ attributeValueArray.push(array);
474
+ }
475
+ return attributeValueArray;
476
+ }
477
+ /**
478
+ * Find the property table by class name.
479
+ * @param {GLTF_EXT_structural_metadata_PropertyTable[]} propertyTables
480
+ * @param {string} schemaClassName
481
+ */
482
+ function findPropertyTableByClass(propertyTables, schemaClassName) {
483
+ for (let i = 0, len = propertyTables.length; i < len; i++) {
484
+ const propertyTable = propertyTables[i];
485
+ if (propertyTable.class === schemaClassName) {
486
+ return propertyTable;
487
+ }
488
+ }
489
+ return null;
490
+ }
491
+ /**
492
+ * Looks up ENUM whose `value` property matches the specified number in the parameter `value`.
493
+ * @param {GLTF_EXT_structural_metadata_Enum} enumEntry - ENUM entry containing the array of possible enums.
494
+ * @param {number} value - the value of the ENUM to locate.
495
+ * @returns {GLTF_EXT_structural_metadata_EnumValue | null} ENUM matcihng the specified value or null of no ENUM object was found.
496
+ */
497
+ function getEnumByValue(enumEntry, value) {
498
+ for (const enumValue of enumEntry.values) {
499
+ if (enumValue.value === value) {
500
+ return enumValue;
501
+ }
502
+ }
503
+ return null;
504
+ }
@@ -0,0 +1,34 @@
1
+ import type { GLTFTextureInfoMetadata, GLTFMeshPrimitive } from '../types/gltf-json-schema';
2
+ import type { TypedArray } from '@loaders.gl/schema';
3
+ import { GLTFScenegraph } from '../api/gltf-scenegraph';
4
+ export declare function getArrayElementByteSize(attributeType: any, componentType: any): number;
5
+ /**
6
+ * Converts raw bytes that are in the buffer to an array of the type defined by the schema.
7
+ * @param {Uint8Array} typedArray - raw bytes in the buffer
8
+ * @param {string} attributeType - SCALAR, VECN, MATN
9
+ * @param {string} componentType - type of the component in elements, e.g. 'UINT8' or 'FLOAT32'
10
+ * @param {number} elementCount - number of elements in the array. Default value is 1.
11
+ * @returns {TypedArray} Data array
12
+ */
13
+ export declare function convertRawBufferToMetadataArray(typedArray: Uint8Array, attributeType: string, componentType: string, elementCount?: number): TypedArray;
14
+ /**
15
+ * Processes data encoded in the texture associated with the primitive.
16
+ * If Ext_mesh_featues is combined with the Ext_structural_metadata, propertyTable will also be processed.
17
+ * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
18
+ * @param {GLTFTextureInfoMetadata} textureInfo - reference to the texture where extension data are stored.
19
+ * @param {GLTFMeshPrimitive} primitive - primitive object in the mesh
20
+ * @returns {number[] | null} Array of data taken. Null if data can't be taken from the texture.
21
+ */
22
+ export declare function getPrimitiveTextureData(scenegraph: GLTFScenegraph, textureInfo: GLTFTextureInfoMetadata, primitive: GLTFMeshPrimitive): number[] | null;
23
+ /**
24
+ * Puts property data to attributes.
25
+ * It creates corresponding buffer, bufferView and accessor
26
+ * so the data can be accessed like regular data stored in buffers.
27
+ * @param {GLTFScenegraph} scenegraph - scenegraph object
28
+ * @param {string} attributeName - name of the attribute
29
+ * @param {number[]} propertyData - property data to store
30
+ * @param {number[]} featureTable - an array where unique data from the property data are being stored
31
+ * @param {GLTFMeshPrimitive} primitive - primitive object
32
+ */
33
+ export declare function primitivePropertyDataToAttributes(scenegraph: GLTFScenegraph, attributeName: string, propertyData: number[], featureTable: number[], primitive: GLTFMeshPrimitive): void;
34
+ //# sourceMappingURL=data-processing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data-processing.d.ts","sourceRoot":"","sources":["../../../src/lib/extensions/data-processing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,uBAAuB,EAAE,iBAAiB,EAAC,MAAM,2BAA2B,CAAC;AAC1F,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,oBAAoB,CAAC;AAGnD,OAAO,EAAC,cAAc,EAAC,MAAM,wBAAwB,CAAC;AA4CtD,wBAAgB,uBAAuB,CAAC,aAAa,KAAA,EAAE,aAAa,KAAA,GAAG,MAAM,CAK5E;AAED;;;;;;;GAOG;AACH,wBAAgB,+BAA+B,CAC7C,UAAU,EAAE,UAAU,EACtB,aAAa,EAAE,MAAM,EACrB,aAAa,EAAE,MAAM,EACrB,YAAY,GAAE,MAAU,GACvB,UAAU,CAWZ;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,UAAU,EAAE,cAAc,EAC1B,WAAW,EAAE,uBAAuB,EACpC,SAAS,EAAE,iBAAiB,GAC3B,MAAM,EAAE,GAAG,IAAI,CAkDjB;AAED;;;;;;;;;GASG;AACH,wBAAgB,iCAAiC,CAC/C,UAAU,EAAE,cAAc,EAC1B,aAAa,EAAE,MAAM,EACrB,YAAY,EAAE,MAAM,EAAE,EACtB,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,iBAAiB,GAC3B,IAAI,CA+BN"}