@loaders.gl/gltf 4.2.0-alpha.4 → 4.2.0-alpha.6

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 (149) hide show
  1. package/dist/dist.dev.js +1056 -524
  2. package/dist/dist.min.js +9 -0
  3. package/dist/glb-loader.d.ts +2 -2
  4. package/dist/glb-loader.d.ts.map +1 -1
  5. package/dist/glb-loader.js +22 -21
  6. package/dist/glb-writer.d.ts +2 -2
  7. package/dist/glb-writer.d.ts.map +1 -1
  8. package/dist/glb-writer.js +27 -24
  9. package/dist/gltf-loader.d.ts +3 -3
  10. package/dist/gltf-loader.d.ts.map +1 -1
  11. package/dist/gltf-loader.js +31 -36
  12. package/dist/gltf-writer.js +24 -26
  13. package/dist/index.cjs +138 -330
  14. package/dist/index.cjs.map +7 -0
  15. package/dist/index.d.ts +17 -17
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +3 -1
  18. package/dist/lib/api/gltf-extensions.d.ts +2 -2
  19. package/dist/lib/api/gltf-extensions.d.ts.map +1 -1
  20. package/dist/lib/api/gltf-extensions.js +45 -22
  21. package/dist/lib/api/gltf-scenegraph.d.ts +2 -2
  22. package/dist/lib/api/gltf-scenegraph.d.ts.map +1 -1
  23. package/dist/lib/api/gltf-scenegraph.js +565 -438
  24. package/dist/lib/api/normalize-gltf-v1.js +249 -181
  25. package/dist/lib/api/post-process-gltf.d.ts +3 -3
  26. package/dist/lib/api/post-process-gltf.d.ts.map +1 -1
  27. package/dist/lib/api/post-process-gltf.js +378 -340
  28. package/dist/lib/encoders/encode-glb.js +62 -48
  29. package/dist/lib/encoders/encode-gltf.js +24 -10
  30. package/dist/lib/extensions/EXT_mesh_features.d.ts +2 -2
  31. package/dist/lib/extensions/EXT_mesh_features.d.ts.map +1 -1
  32. package/dist/lib/extensions/EXT_mesh_features.js +55 -33
  33. package/dist/lib/extensions/EXT_meshopt_compression.d.ts +2 -2
  34. package/dist/lib/extensions/EXT_meshopt_compression.d.ts.map +1 -1
  35. package/dist/lib/extensions/EXT_meshopt_compression.js +27 -31
  36. package/dist/lib/extensions/EXT_structural_metadata.d.ts +2 -2
  37. package/dist/lib/extensions/EXT_structural_metadata.d.ts.map +1 -1
  38. package/dist/lib/extensions/EXT_structural_metadata.js +434 -230
  39. package/dist/lib/extensions/EXT_texture_webp.d.ts +2 -2
  40. package/dist/lib/extensions/EXT_texture_webp.d.ts.map +1 -1
  41. package/dist/lib/extensions/EXT_texture_webp.js +24 -17
  42. package/dist/lib/extensions/KHR_binary_gltf.d.ts +1 -1
  43. package/dist/lib/extensions/KHR_binary_gltf.d.ts.map +1 -1
  44. package/dist/lib/extensions/KHR_binary_gltf.js +29 -15
  45. package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts +2 -2
  46. package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts.map +1 -1
  47. package/dist/lib/extensions/KHR_draco_mesh_compression.js +110 -87
  48. package/dist/lib/extensions/KHR_texture_basisu.d.ts +2 -2
  49. package/dist/lib/extensions/KHR_texture_basisu.d.ts.map +1 -1
  50. package/dist/lib/extensions/KHR_texture_basisu.js +19 -12
  51. package/dist/lib/extensions/KHR_texture_transform.d.ts +2 -2
  52. package/dist/lib/extensions/KHR_texture_transform.d.ts.map +1 -1
  53. package/dist/lib/extensions/KHR_texture_transform.js +188 -156
  54. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts +2 -2
  55. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts.map +1 -1
  56. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +263 -143
  57. package/dist/lib/extensions/deprecated/KHR_lights_punctual.d.ts +1 -1
  58. package/dist/lib/extensions/deprecated/KHR_lights_punctual.d.ts.map +1 -1
  59. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +44 -32
  60. package/dist/lib/extensions/deprecated/KHR_materials_unlit.d.ts +1 -1
  61. package/dist/lib/extensions/deprecated/KHR_materials_unlit.d.ts.map +1 -1
  62. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +30 -24
  63. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts +1 -1
  64. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts.map +1 -1
  65. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +65 -52
  66. package/dist/lib/extensions/utils/3d-tiles-utils.d.ts +2 -2
  67. package/dist/lib/extensions/utils/3d-tiles-utils.d.ts.map +1 -1
  68. package/dist/lib/extensions/utils/3d-tiles-utils.js +298 -181
  69. package/dist/lib/gltf-utils/get-typed-array.d.ts +1 -1
  70. package/dist/lib/gltf-utils/get-typed-array.d.ts.map +1 -1
  71. package/dist/lib/gltf-utils/get-typed-array.js +54 -42
  72. package/dist/lib/gltf-utils/gltf-attribute-utils.d.ts +1 -1
  73. package/dist/lib/gltf-utils/gltf-attribute-utils.d.ts.map +1 -1
  74. package/dist/lib/gltf-utils/gltf-attribute-utils.js +58 -52
  75. package/dist/lib/gltf-utils/gltf-constants.js +27 -27
  76. package/dist/lib/gltf-utils/gltf-utils.d.ts +1 -1
  77. package/dist/lib/gltf-utils/gltf-utils.d.ts.map +1 -1
  78. package/dist/lib/gltf-utils/gltf-utils.js +67 -60
  79. package/dist/lib/gltf-utils/resolve-url.js +12 -10
  80. package/dist/lib/parsers/parse-glb.d.ts +1 -1
  81. package/dist/lib/parsers/parse-glb.d.ts.map +1 -1
  82. package/dist/lib/parsers/parse-glb.js +132 -89
  83. package/dist/lib/parsers/parse-gltf.d.ts +3 -3
  84. package/dist/lib/parsers/parse-gltf.d.ts.map +1 -1
  85. package/dist/lib/parsers/parse-gltf.js +155 -126
  86. package/dist/lib/types/glb-types.js +0 -1
  87. package/dist/lib/types/gltf-ext-feature-metadata-schema.d.ts +1 -1
  88. package/dist/lib/types/gltf-ext-feature-metadata-schema.d.ts.map +1 -1
  89. package/dist/lib/types/gltf-ext-feature-metadata-schema.js +0 -1
  90. package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts +1 -1
  91. package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts.map +1 -1
  92. package/dist/lib/types/gltf-ext-mesh-features-schema.js +0 -1
  93. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts +1 -1
  94. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts.map +1 -1
  95. package/dist/lib/types/gltf-ext-structural-metadata-schema.js +0 -1
  96. package/dist/lib/types/gltf-json-schema.js +2 -1
  97. package/dist/lib/types/gltf-postprocessed-schema.js +2 -1
  98. package/dist/lib/types/gltf-types.d.ts +3 -3
  99. package/dist/lib/types/gltf-types.d.ts.map +1 -1
  100. package/dist/lib/types/gltf-types.js +1 -1
  101. package/dist/lib/utils/assert.js +6 -4
  102. package/dist/lib/utils/version.js +4 -2
  103. package/dist/meshopt/meshopt-decoder.js +86 -67
  104. package/dist/webp/webp.js +28 -19
  105. package/package.json +13 -8
  106. package/src/lib/extensions/KHR_texture_transform.ts +18 -24
  107. package/dist/glb-loader.js.map +0 -1
  108. package/dist/glb-writer.js.map +0 -1
  109. package/dist/gltf-loader.js.map +0 -1
  110. package/dist/gltf-writer.js.map +0 -1
  111. package/dist/index.js.map +0 -1
  112. package/dist/lib/api/gltf-extensions.js.map +0 -1
  113. package/dist/lib/api/gltf-scenegraph.js.map +0 -1
  114. package/dist/lib/api/normalize-gltf-v1.js.map +0 -1
  115. package/dist/lib/api/post-process-gltf.js.map +0 -1
  116. package/dist/lib/encoders/encode-glb.js.map +0 -1
  117. package/dist/lib/encoders/encode-gltf.js.map +0 -1
  118. package/dist/lib/extensions/EXT_mesh_features.js.map +0 -1
  119. package/dist/lib/extensions/EXT_meshopt_compression.js.map +0 -1
  120. package/dist/lib/extensions/EXT_structural_metadata.js.map +0 -1
  121. package/dist/lib/extensions/EXT_texture_webp.js.map +0 -1
  122. package/dist/lib/extensions/KHR_binary_gltf.js.map +0 -1
  123. package/dist/lib/extensions/KHR_draco_mesh_compression.js.map +0 -1
  124. package/dist/lib/extensions/KHR_texture_basisu.js.map +0 -1
  125. package/dist/lib/extensions/KHR_texture_transform.js.map +0 -1
  126. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js.map +0 -1
  127. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js.map +0 -1
  128. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js.map +0 -1
  129. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js.map +0 -1
  130. package/dist/lib/extensions/utils/3d-tiles-utils.js.map +0 -1
  131. package/dist/lib/gltf-utils/get-typed-array.js.map +0 -1
  132. package/dist/lib/gltf-utils/gltf-attribute-utils.js.map +0 -1
  133. package/dist/lib/gltf-utils/gltf-constants.js.map +0 -1
  134. package/dist/lib/gltf-utils/gltf-utils.js.map +0 -1
  135. package/dist/lib/gltf-utils/resolve-url.js.map +0 -1
  136. package/dist/lib/parsers/parse-glb.js.map +0 -1
  137. package/dist/lib/parsers/parse-gltf.js.map +0 -1
  138. package/dist/lib/types/glb-types.js.map +0 -1
  139. package/dist/lib/types/gltf-ext-feature-metadata-schema.js.map +0 -1
  140. package/dist/lib/types/gltf-ext-mesh-features-schema.js.map +0 -1
  141. package/dist/lib/types/gltf-ext-structural-metadata-schema.js.map +0 -1
  142. package/dist/lib/types/gltf-json-schema.js.map +0 -1
  143. package/dist/lib/types/gltf-postprocessed-schema.js.map +0 -1
  144. package/dist/lib/types/gltf-types.js.map +0 -1
  145. package/dist/lib/utils/assert.js.map +0 -1
  146. package/dist/lib/utils/version.js.map +0 -1
  147. package/dist/meshopt/meshopt-decoder.js.map +0 -1
  148. package/dist/meshopt/meshopt-encoder.ts.disabled +0 -409
  149. package/dist/webp/webp.js.map +0 -1
@@ -3,280 +3,484 @@ import { convertRawBufferToMetadataArray, getPrimitiveTextureData, primitiveProp
3
3
  const EXT_STRUCTURAL_METADATA_NAME = 'EXT_structural_metadata';
4
4
  export const name = EXT_STRUCTURAL_METADATA_NAME;
5
5
  export async function decode(gltfData, options) {
6
- const scenegraph = new GLTFScenegraph(gltfData);
7
- decodeExtStructuralMetadata(scenegraph, options);
6
+ const scenegraph = new GLTFScenegraph(gltfData);
7
+ decodeExtStructuralMetadata(scenegraph, options);
8
8
  }
9
- function decodeExtStructuralMetadata(scenegraph, options) {
10
- var _options$gltf, _options$gltf2;
11
- if (!((_options$gltf = options.gltf) !== null && _options$gltf !== void 0 && _options$gltf.loadBuffers)) {
12
- return;
13
- }
14
- const extension = scenegraph.getExtension(EXT_STRUCTURAL_METADATA_NAME);
15
- if (!extension) {
16
- return;
17
- }
18
- if ((_options$gltf2 = options.gltf) !== null && _options$gltf2 !== void 0 && _options$gltf2.loadImages) {
19
- decodePropertyTextures(scenegraph, extension);
9
+ /*
10
+ // Example of the extension.
11
+ // See more info at https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_structural_metadata
12
+ const extensions = {
13
+ "extensions": {
14
+ "EXT_structural_metadata": {
15
+ "schema": {
16
+ "classes": {
17
+ "tree": {
18
+ "name": "Tree",
19
+ "description": "Woody, perennial plant.",
20
+ "properties": {
21
+ "species": {
22
+ "description": "Type of tree.",
23
+ "type": "ENUM",
24
+ "enumType": "speciesEnum",
25
+ "required": true
26
+ },
27
+ "age": {
28
+ "description": "The age of the tree, in years",
29
+ "type": "SCALAR",
30
+ "componentType": "UINT8",
31
+ "required": true
32
+ }
33
+ }
34
+ }
35
+ },
36
+ "enums": {
37
+ "speciesEnum": {
38
+ "name": "Species",
39
+ "description": "An example enum for tree species.",
40
+ // valueType is not defined here. Default is "UINT16"
41
+ "values": [
42
+ { "name": "Unspecified", "value": 0 },
43
+ { "name": "Oak", "value": 1 }
44
+ ]
45
+ }
46
+ }
47
+ },
48
+ "propertyTables": [{
49
+ "name": "tree_survey_2021-09-29",
50
+ "class": "tree",
51
+ "count": 10, // The number of elements in each property array (in `species`, in `age`).
52
+ "properties": {
53
+ "species": {
54
+ "values": 0, // It's an index of the buffer view containing property values.
55
+ },
56
+ "age": {
57
+ "values": 1
58
+ }
59
+ }
60
+ }]
61
+ }
20
62
  }
21
- decodePropertyTables(scenegraph, extension);
22
63
  }
64
+ */
65
+ /**
66
+ * Decodes feature metadata from extension.
67
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
68
+ * @param options - GLTFLoader options.
69
+ */
70
+ function decodeExtStructuralMetadata(scenegraph, options) {
71
+ // Decoding metadata involves buffers processing.
72
+ // So, if buffers have not been loaded, there is no reason to process metadata.
73
+ if (!options.gltf?.loadBuffers) {
74
+ return;
75
+ }
76
+ const extension = scenegraph.getExtension(EXT_STRUCTURAL_METADATA_NAME);
77
+ if (!extension) {
78
+ return;
79
+ }
80
+ if (options.gltf?.loadImages) {
81
+ decodePropertyTextures(scenegraph, extension);
82
+ }
83
+ decodePropertyTables(scenegraph, extension);
84
+ }
85
+ /**
86
+ * Processes the data stored in the textures
87
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
88
+ * @param extension - Top-level extension.
89
+ */
23
90
  function decodePropertyTextures(scenegraph, extension) {
24
- const propertyTextures = extension.propertyTextures;
25
- const json = scenegraph.gltf.json;
26
- if (propertyTextures && json.meshes) {
27
- for (const mesh of json.meshes) {
28
- for (const primitive of mesh.primitives) {
29
- processPrimitivePropertyTextures(scenegraph, propertyTextures, primitive, extension);
30
- }
91
+ const propertyTextures = extension.propertyTextures;
92
+ const json = scenegraph.gltf.json;
93
+ if (propertyTextures && json.meshes) {
94
+ // Iterate through all meshes/primitives.
95
+ for (const mesh of json.meshes) {
96
+ for (const primitive of mesh.primitives) {
97
+ processPrimitivePropertyTextures(scenegraph, propertyTextures, primitive, extension);
98
+ }
99
+ }
31
100
  }
32
- }
33
101
  }
102
+ /**
103
+ * Processes the data stored in the property tables.
104
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
105
+ * @param extension - Top-level extension.
106
+ */
34
107
  function decodePropertyTables(scenegraph, extension) {
35
- const schema = extension.schema;
36
- if (!schema) {
37
- return;
38
- }
39
- const schemaClasses = schema.classes;
40
- const propertyTables = extension.propertyTables;
41
- if (schemaClasses && propertyTables) {
42
- for (const schemaName in schemaClasses) {
43
- const propertyTable = findPropertyTableByClass(propertyTables, schemaName);
44
- if (propertyTable) {
45
- processPropertyTable(scenegraph, schema, propertyTable);
46
- }
108
+ const schema = extension.schema;
109
+ if (!schema) {
110
+ return;
111
+ }
112
+ const schemaClasses = schema.classes;
113
+ const propertyTables = extension.propertyTables;
114
+ if (schemaClasses && propertyTables) {
115
+ for (const schemaName in schemaClasses) {
116
+ const propertyTable = findPropertyTableByClass(propertyTables, schemaName);
117
+ if (propertyTable) {
118
+ processPropertyTable(scenegraph, schema, propertyTable);
119
+ }
120
+ }
47
121
  }
48
- }
49
122
  }
123
+ /**
124
+ * Finds the property table by class name.
125
+ * @param propertyTables - propertyTable definition taken from the top-level extension.
126
+ * @param schemaClassName - class name in the extension schema.
127
+ */
50
128
  function findPropertyTableByClass(propertyTables, schemaClassName) {
51
- for (const propertyTable of propertyTables) {
52
- if (propertyTable.class === schemaClassName) {
53
- return propertyTable;
129
+ for (const propertyTable of propertyTables) {
130
+ if (propertyTable.class === schemaClassName) {
131
+ return propertyTable;
132
+ }
54
133
  }
55
- }
56
- return null;
134
+ return null;
57
135
  }
136
+ /**
137
+ * Takes data from property textures reffered by the primitive.
138
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
139
+ * @param propertyTextures - propertyTexture definition taken from the top-level extention.
140
+ * @param primitive - Primitive object.
141
+ * @param extension - Top-level extension.
142
+ */
58
143
  function processPrimitivePropertyTextures(scenegraph, propertyTextures, primitive, extension) {
59
- var _primitive$extensions;
60
- if (!propertyTextures) {
61
- return;
62
- }
63
- const primitiveExtension = (_primitive$extensions = primitive.extensions) === null || _primitive$extensions === void 0 ? void 0 : _primitive$extensions[EXT_STRUCTURAL_METADATA_NAME];
64
- const primitivePropertyTextureIndices = primitiveExtension === null || primitiveExtension === void 0 ? void 0 : primitiveExtension.propertyTextures;
65
- if (!primitivePropertyTextureIndices) {
66
- return;
67
- }
68
- for (const primitivePropertyTextureIndex of primitivePropertyTextureIndices) {
69
- const propertyTexture = propertyTextures[primitivePropertyTextureIndex];
70
- processPrimitivePropertyTexture(scenegraph, propertyTexture, primitive, extension);
71
- }
144
+ if (!propertyTextures) {
145
+ return;
146
+ }
147
+ const primitiveExtension = primitive.extensions?.[EXT_STRUCTURAL_METADATA_NAME];
148
+ const primitivePropertyTextureIndices = primitiveExtension?.propertyTextures;
149
+ if (!primitivePropertyTextureIndices) {
150
+ return;
151
+ }
152
+ for (const primitivePropertyTextureIndex of primitivePropertyTextureIndices) {
153
+ const propertyTexture = propertyTextures[primitivePropertyTextureIndex];
154
+ processPrimitivePropertyTexture(scenegraph, propertyTexture, primitive, extension);
155
+ }
72
156
  }
157
+ /**
158
+ * Takes property data from the texture pointed by the primitive and appends them to `exension.data`.
159
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
160
+ * @param propertyTexture - propertyTexture definition taken from the top-level extension.
161
+ * @param primitive - Primitive object.
162
+ * @param extension - Top-level extension.
163
+ */
73
164
  function processPrimitivePropertyTexture(scenegraph, propertyTexture, primitive, extension) {
74
- if (!propertyTexture.properties) {
75
- return;
76
- }
77
- if (!extension.dataAttributeNames) {
78
- extension.dataAttributeNames = [];
79
- }
80
- const className = propertyTexture.class;
81
- for (const propertyName in propertyTexture.properties) {
82
- var _propertyTexture$prop;
83
- const attributeName = `${className}_${propertyName}`;
84
- const textureInfoTopLevel = (_propertyTexture$prop = propertyTexture.properties) === null || _propertyTexture$prop === void 0 ? void 0 : _propertyTexture$prop[propertyName];
85
- if (!textureInfoTopLevel) {
86
- continue;
165
+ if (!propertyTexture.properties) {
166
+ return;
87
167
  }
88
- if (!textureInfoTopLevel.data) {
89
- textureInfoTopLevel.data = [];
168
+ if (!extension.dataAttributeNames) {
169
+ extension.dataAttributeNames = [];
90
170
  }
91
- const featureTextureTable = textureInfoTopLevel.data;
92
- const propertyData = getPrimitiveTextureData(scenegraph, textureInfoTopLevel, primitive);
93
- if (propertyData === null) {
94
- continue;
171
+ /* Iterate through all properties defined in propertyTexture, e.g. "speed" and "direction":
172
+ {
173
+ "class": "wind",
174
+ "properties": {
175
+ "speed": {
176
+ "index": 0,
177
+ "texCoord": 0,
178
+ "channels": [0]
179
+ },
180
+ "direction": {
181
+ "index": 0,
182
+ "texCoord": 0,
183
+ "channels": [1, 2]
184
+ }
185
+ }
186
+ }
187
+ */
188
+ const className = propertyTexture.class;
189
+ for (const propertyName in propertyTexture.properties) {
190
+ // propertyName has values like "speed", "direction"
191
+ // Make attributeName as a combination of the class name and the propertyName like "wind_speed" or "wind_direction"
192
+ const attributeName = `${className}_${propertyName}`;
193
+ const textureInfoTopLevel = propertyTexture.properties?.[propertyName];
194
+ if (!textureInfoTopLevel) {
195
+ // eslint-disable-next-line no-continue
196
+ continue;
197
+ }
198
+ // 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
199
+ // Initially textureInfoTopLevel.data will be initialized with an empty array.
200
+ if (!textureInfoTopLevel.data) {
201
+ textureInfoTopLevel.data = [];
202
+ }
203
+ const featureTextureTable = textureInfoTopLevel.data;
204
+ const propertyData = getPrimitiveTextureData(scenegraph, textureInfoTopLevel, primitive);
205
+ if (propertyData === null) {
206
+ // eslint-disable-next-line no-continue
207
+ continue;
208
+ }
209
+ primitivePropertyDataToAttributes(scenegraph, attributeName, propertyData, featureTextureTable, primitive);
210
+ textureInfoTopLevel.data = featureTextureTable;
211
+ extension.dataAttributeNames.push(attributeName);
95
212
  }
96
- primitivePropertyDataToAttributes(scenegraph, attributeName, propertyData, featureTextureTable, primitive);
97
- textureInfoTopLevel.data = featureTextureTable;
98
- extension.dataAttributeNames.push(attributeName);
99
- }
100
213
  }
214
+ /**
215
+ * Navigates through all properies in the property table, gets properties data,
216
+ * and put the data to `propertyTable.data` as an array.
217
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
218
+ * @param schema - schema object.
219
+ * @param propertyTable - propertyTable definition taken from the top-level extension.
220
+ */
101
221
  function processPropertyTable(scenegraph, schema, propertyTable) {
102
- var _schema$classes;
103
- const schemaClass = (_schema$classes = schema.classes) === null || _schema$classes === void 0 ? void 0 : _schema$classes[propertyTable.class];
104
- if (!schemaClass) {
105
- throw new Error(`Incorrect data in the EXT_structural_metadata extension: no schema class with name ${propertyTable.class}`);
106
- }
107
- const numberOfElements = propertyTable.count;
108
- for (const propertyName in schemaClass.properties) {
109
- var _propertyTable$proper;
110
- const classProperty = schemaClass.properties[propertyName];
111
- const propertyTableProperty = (_propertyTable$proper = propertyTable.properties) === null || _propertyTable$proper === void 0 ? void 0 : _propertyTable$proper[propertyName];
112
- if (propertyTableProperty) {
113
- const data = getPropertyDataFromBinarySource(scenegraph, schema, classProperty, numberOfElements, propertyTableProperty);
114
- propertyTableProperty.data = data;
222
+ const schemaClass = schema.classes?.[propertyTable.class];
223
+ if (!schemaClass) {
224
+ throw new Error(`Incorrect data in the EXT_structural_metadata extension: no schema class with name ${propertyTable.class}`);
225
+ }
226
+ const numberOfElements = propertyTable.count; // `propertyTable.count` is a number of elements in each property array.
227
+ for (const propertyName in schemaClass.properties) {
228
+ const classProperty = schemaClass.properties[propertyName];
229
+ const propertyTableProperty = propertyTable.properties?.[propertyName];
230
+ if (propertyTableProperty) {
231
+ // Getting all elements (`numberOfElements`) of the array in the `propertyTableProperty`
232
+ const data = getPropertyDataFromBinarySource(scenegraph, schema, classProperty, numberOfElements, propertyTableProperty);
233
+ propertyTableProperty.data = data;
234
+ }
115
235
  }
116
- }
117
236
  }
237
+ /**
238
+ * Decodes a propertyTable column from binary source based on property type.
239
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
240
+ * @param schema - Schema object.
241
+ * @param classProperty - class property object.
242
+ * @param numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
243
+ * @param propertyTableProperty - propertyTable's property metadata.
244
+ * @returns {string[] | number[] | string[][] | number[][]}
245
+ */
118
246
  function getPropertyDataFromBinarySource(scenegraph, schema, classProperty, numberOfElements, propertyTableProperty) {
119
- let data = [];
120
- const valuesBufferView = propertyTableProperty.values;
121
- const valuesDataBytes = scenegraph.getTypedArrayForBufferView(valuesBufferView);
122
- const arrayOffsets = getArrayOffsetsForProperty(scenegraph, classProperty, propertyTableProperty, numberOfElements);
123
- const stringOffsets = getStringOffsetsForProperty(scenegraph, propertyTableProperty, numberOfElements);
124
- switch (classProperty.type) {
125
- case 'SCALAR':
126
- case 'VEC2':
127
- case 'VEC3':
128
- case 'VEC4':
129
- case 'MAT2':
130
- case 'MAT3':
131
- case 'MAT4':
132
- {
133
- data = getPropertyDataNumeric(classProperty, numberOfElements, valuesDataBytes, arrayOffsets);
134
- break;
135
- }
136
- case 'BOOLEAN':
137
- {
138
- throw new Error(`Not implemented - classProperty.type=${classProperty.type}`);
139
- }
140
- case 'STRING':
141
- {
142
- data = getPropertyDataString(numberOfElements, valuesDataBytes, arrayOffsets, stringOffsets);
143
- break;
144
- }
145
- case 'ENUM':
146
- {
147
- data = getPropertyDataENUM(schema, classProperty, numberOfElements, valuesDataBytes, arrayOffsets);
148
- break;
149
- }
150
- default:
151
- throw new Error(`Unknown classProperty type ${classProperty.type}`);
152
- }
153
- return data;
247
+ let data = [];
248
+ const valuesBufferView = propertyTableProperty.values;
249
+ const valuesDataBytes = scenegraph.getTypedArrayForBufferView(valuesBufferView);
250
+ const arrayOffsets = getArrayOffsetsForProperty(scenegraph, classProperty, propertyTableProperty, numberOfElements);
251
+ const stringOffsets = getStringOffsetsForProperty(scenegraph, propertyTableProperty, numberOfElements);
252
+ switch (classProperty.type) {
253
+ case 'SCALAR':
254
+ case 'VEC2':
255
+ case 'VEC3':
256
+ case 'VEC4':
257
+ case 'MAT2':
258
+ case 'MAT3':
259
+ case 'MAT4': {
260
+ data = getPropertyDataNumeric(classProperty, numberOfElements, valuesDataBytes, arrayOffsets);
261
+ break;
262
+ }
263
+ case 'BOOLEAN': {
264
+ // TODO: implement it as soon as we have the corresponding tileset
265
+ throw new Error(`Not implemented - classProperty.type=${classProperty.type}`);
266
+ }
267
+ case 'STRING': {
268
+ data = getPropertyDataString(numberOfElements, valuesDataBytes, arrayOffsets, stringOffsets);
269
+ break;
270
+ }
271
+ case 'ENUM': {
272
+ data = getPropertyDataENUM(schema, classProperty, numberOfElements, valuesDataBytes, arrayOffsets);
273
+ break;
274
+ }
275
+ default:
276
+ throw new Error(`Unknown classProperty type ${classProperty.type}`);
277
+ }
278
+ return data;
154
279
  }
280
+ /**
281
+ * Parses propertyTable.property.arrayOffsets that are offsets of sub-arrays in a flatten array of values.
282
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
283
+ * @param classProperty - class property object.
284
+ * @param propertyTableProperty - propertyTable's property metadata.
285
+ * @param numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
286
+ * @returns Typed array with offset values.
287
+ * @see https://github.com/CesiumGS/glTF/blob/2976f1183343a47a29e4059a70961371cd2fcee8/extensions/2.0/Vendor/EXT_structural_metadata/schema/propertyTable.property.schema.json#L21
288
+ */
155
289
  function getArrayOffsetsForProperty(scenegraph, classProperty, propertyTableProperty, numberOfElements) {
156
- if (classProperty.array && typeof classProperty.count === 'undefined' && typeof propertyTableProperty.arrayOffsets !== 'undefined') {
157
- return getOffsetsForProperty(scenegraph, propertyTableProperty.arrayOffsets, propertyTableProperty.arrayOffsetType || 'UINT32', numberOfElements);
158
- }
159
- return null;
290
+ if (classProperty.array &&
291
+ // `count` is a number of array elements. May only be defined when `array` is true.
292
+ // If `count` is NOT defined, it's a VARIABLE-length array
293
+ typeof classProperty.count === 'undefined' &&
294
+ // `arrayOffsets` is an index of the buffer view containing offsets for variable-length arrays.
295
+ typeof propertyTableProperty.arrayOffsets !== 'undefined') {
296
+ // Data are in a VARIABLE-length array
297
+ return getOffsetsForProperty(scenegraph, propertyTableProperty.arrayOffsets, propertyTableProperty.arrayOffsetType || 'UINT32', numberOfElements);
298
+ }
299
+ return null;
160
300
  }
301
+ /**
302
+ * Parses propertyTable.property.stringOffsets.
303
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
304
+ * @param propertyTableProperty - propertyTable's property metadata.
305
+ * @param numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
306
+ * @returns Typed array with offset values.
307
+ * @see https://github.com/CesiumGS/glTF/blob/2976f1183343a47a29e4059a70961371cd2fcee8/extensions/2.0/Vendor/EXT_structural_metadata/schema/propertyTable.property.schema.json#L29C10-L29C23
308
+ */
161
309
  function getStringOffsetsForProperty(scenegraph, propertyTableProperty, numberOfElements) {
162
- if (typeof propertyTableProperty.stringOffsets !== 'undefined') {
163
- return getOffsetsForProperty(scenegraph, propertyTableProperty.stringOffsets, propertyTableProperty.stringOffsetType || 'UINT32', numberOfElements);
164
- }
165
- return null;
310
+ if (typeof propertyTableProperty.stringOffsets !== 'undefined' // `stringOffsets` is an index of the buffer view containing offsets for strings.
311
+ ) {
312
+ // Data are in a FIXED-length array
313
+ return getOffsetsForProperty(scenegraph, propertyTableProperty.stringOffsets, propertyTableProperty.stringOffsetType || 'UINT32', numberOfElements);
314
+ }
315
+ return null;
166
316
  }
317
+ /**
318
+ * Decodes properties of SCALAR, VEC-N, MAT-N types from binary sourse.
319
+ * @param classProperty - class property object.
320
+ * @param numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
321
+ * @param valuesDataBytes - Data taken from values property of the property table property.
322
+ * @param arrayOffsets - Offsets for variable-length arrays. It's null for fixed-length arrays or scalar types.
323
+ * @returns Property values in a typed array or in an array of typed arrays.
324
+ */
167
325
  function getPropertyDataNumeric(classProperty, numberOfElements, valuesDataBytes, arrayOffsets) {
168
- const isArray = classProperty.array;
169
- const arrayCount = classProperty.count;
170
- const elementSize = getArrayElementByteSize(classProperty.type, classProperty.componentType);
171
- const elementCount = valuesDataBytes.byteLength / elementSize;
172
- let valuesData;
173
- if (classProperty.componentType) {
174
- valuesData = convertRawBufferToMetadataArray(valuesDataBytes, classProperty.type, classProperty.componentType, elementCount);
175
- } else {
176
- valuesData = valuesDataBytes;
177
- }
178
- if (isArray) {
179
- if (arrayOffsets) {
180
- return parseVariableLengthArrayNumeric(valuesData, numberOfElements, arrayOffsets, valuesDataBytes.length, elementSize);
326
+ const isArray = classProperty.array;
327
+ const arrayCount = classProperty.count;
328
+ const elementSize = getArrayElementByteSize(classProperty.type, classProperty.componentType);
329
+ const elementCount = valuesDataBytes.byteLength / elementSize;
330
+ let valuesData;
331
+ if (classProperty.componentType) {
332
+ valuesData = convertRawBufferToMetadataArray(valuesDataBytes, classProperty.type,
333
+ // The datatype of the element's components. Only applicable to `SCALAR`, `VECN`, and `MATN` types.
334
+ classProperty.componentType, elementCount);
181
335
  }
182
- if (arrayCount) {
183
- return parseFixedLengthArrayNumeric(valuesData, numberOfElements, arrayCount);
336
+ else {
337
+ // The spec doesn't provide any info what to do if componentType is not set.
338
+ valuesData = valuesDataBytes;
184
339
  }
185
- return [];
186
- }
187
- return valuesData;
340
+ if (isArray) {
341
+ if (arrayOffsets) {
342
+ // VARIABLE-length array
343
+ return parseVariableLengthArrayNumeric(valuesData, numberOfElements, arrayOffsets, valuesDataBytes.length, elementSize);
344
+ }
345
+ if (arrayCount) {
346
+ // FIXED-length array
347
+ return parseFixedLengthArrayNumeric(valuesData, numberOfElements, arrayCount);
348
+ }
349
+ return [];
350
+ }
351
+ return valuesData;
188
352
  }
353
+ /**
354
+ * Decodes properties of enum type from binary source.
355
+ * @param schema - Schema object.
356
+ * @param classProperty - Class property object.
357
+ * @param numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
358
+ * @param valuesDataBytes - Data taken from values property of the property table property.
359
+ * @param arrayOffsets - Offsets for variable-length arrays. It's null for fixed-length arrays or scalar types.
360
+ * @returns Strings array of nested strings array.
361
+ */
189
362
  function getPropertyDataENUM(schema, classProperty, numberOfElements, valuesDataBytes, arrayOffsets) {
190
- var _schema$enums;
191
- const enumType = classProperty.enumType;
192
- if (!enumType) {
193
- throw new Error('Incorrect data in the EXT_structural_metadata extension: classProperty.enumType is not set for type ENUM');
194
- }
195
- const enumEntry = (_schema$enums = schema.enums) === null || _schema$enums === void 0 ? void 0 : _schema$enums[enumType];
196
- if (!enumEntry) {
197
- throw new Error(`Incorrect data in the EXT_structural_metadata extension: schema.enums does't contain ${enumType}`);
198
- }
199
- const enumValueType = enumEntry.valueType || 'UINT16';
200
- const elementSize = getArrayElementByteSize(classProperty.type, enumValueType);
201
- const elementCount = valuesDataBytes.byteLength / elementSize;
202
- let valuesData = convertRawBufferToMetadataArray(valuesDataBytes, classProperty.type, enumValueType, elementCount);
203
- if (!valuesData) {
204
- valuesData = valuesDataBytes;
205
- }
206
- if (classProperty.array) {
207
- if (arrayOffsets) {
208
- return parseVariableLengthArrayENUM({
209
- valuesData,
210
- numberOfElements,
211
- arrayOffsets,
212
- valuesDataBytesLength: valuesDataBytes.length,
213
- elementSize,
214
- enumEntry
215
- });
363
+ const enumType = classProperty.enumType;
364
+ // Enum ID as declared in the `enums` dictionary. Required when `type` is `ENUM`.
365
+ if (!enumType) {
366
+ throw new Error('Incorrect data in the EXT_structural_metadata extension: classProperty.enumType is not set for type ENUM');
216
367
  }
217
- const arrayCount = classProperty.count;
218
- if (arrayCount) {
219
- return parseFixedLengthArrayENUM(valuesData, numberOfElements, arrayCount, enumEntry);
368
+ const enumEntry = schema.enums?.[enumType];
369
+ if (!enumEntry) {
370
+ throw new Error(`Incorrect data in the EXT_structural_metadata extension: schema.enums does't contain ${enumType}`);
220
371
  }
221
- return [];
222
- }
223
- return getEnumsArray(valuesData, 0, numberOfElements, enumEntry);
372
+ const enumValueType = enumEntry.valueType || 'UINT16';
373
+ const elementSize = getArrayElementByteSize(classProperty.type, enumValueType);
374
+ const elementCount = valuesDataBytes.byteLength / elementSize;
375
+ let valuesData = convertRawBufferToMetadataArray(valuesDataBytes, classProperty.type, enumValueType, elementCount);
376
+ if (!valuesData) {
377
+ valuesData = valuesDataBytes;
378
+ }
379
+ if (classProperty.array) {
380
+ if (arrayOffsets) {
381
+ // VARIABLE-length array
382
+ return parseVariableLengthArrayENUM({
383
+ valuesData,
384
+ numberOfElements,
385
+ arrayOffsets,
386
+ valuesDataBytesLength: valuesDataBytes.length,
387
+ elementSize,
388
+ enumEntry
389
+ });
390
+ }
391
+ const arrayCount = classProperty.count;
392
+ if (arrayCount) {
393
+ // FIXED-length array
394
+ return parseFixedLengthArrayENUM(valuesData, numberOfElements, arrayCount, enumEntry);
395
+ }
396
+ return [];
397
+ }
398
+ // Single value (not an array)
399
+ return getEnumsArray(valuesData, 0, numberOfElements, enumEntry);
224
400
  }
401
+ /**
402
+ * Parses variable length nested ENUM arrays.
403
+ * @param params.valuesData - Values in a flat typed array.
404
+ * @param params.numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
405
+ * @param params.arrayOffsets - Offsets for variable-length arrays. It's null for fixed-length arrays or scalar types.
406
+ * @param params.valuesDataBytesLength - Byte length of values array.
407
+ * @param params.elementSize - Single element byte size.
408
+ * @param params.enumEntry - Enums dictionary.
409
+ * @returns Nested strings array.
410
+ */
225
411
  function parseVariableLengthArrayENUM(params) {
226
- const {
227
- valuesData,
228
- numberOfElements,
229
- arrayOffsets,
230
- valuesDataBytesLength,
231
- elementSize,
232
- enumEntry
233
- } = params;
234
- const attributeValueArray = [];
235
- for (let index = 0; index < numberOfElements; index++) {
236
- const arrayOffset = arrayOffsets[index];
237
- const arrayByteSize = arrayOffsets[index + 1] - arrayOffsets[index];
238
- if (arrayByteSize + arrayOffset > valuesDataBytesLength) {
239
- break;
412
+ const { valuesData, numberOfElements, arrayOffsets, valuesDataBytesLength, elementSize, enumEntry } = params;
413
+ const attributeValueArray = [];
414
+ for (let index = 0; index < numberOfElements; index++) {
415
+ const arrayOffset = arrayOffsets[index];
416
+ const arrayByteSize = arrayOffsets[index + 1] - arrayOffsets[index];
417
+ if (arrayByteSize + arrayOffset > valuesDataBytesLength) {
418
+ break;
419
+ }
420
+ const typedArrayOffset = arrayOffset / elementSize;
421
+ const elementCount = arrayByteSize / elementSize;
422
+ const array = getEnumsArray(valuesData, typedArrayOffset, elementCount, enumEntry);
423
+ attributeValueArray.push(array);
240
424
  }
241
- const typedArrayOffset = arrayOffset / elementSize;
242
- const elementCount = arrayByteSize / elementSize;
243
- const array = getEnumsArray(valuesData, typedArrayOffset, elementCount, enumEntry);
244
- attributeValueArray.push(array);
245
- }
246
- return attributeValueArray;
425
+ return attributeValueArray;
247
426
  }
427
+ /**
428
+ * Parses fixed length ENUM arrays.
429
+ * @param valuesData - Values in a flat typed array.
430
+ * @param numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
431
+ * @param arrayCount - Nested arrays length.
432
+ * @param enumEntry - Enums dictionary.
433
+ * @returns Nested strings array.
434
+ */
248
435
  function parseFixedLengthArrayENUM(valuesData, numberOfElements, arrayCount, enumEntry) {
249
- const attributeValueArray = [];
250
- for (let index = 0; index < numberOfElements; index++) {
251
- const elementOffset = arrayCount * index;
252
- const array = getEnumsArray(valuesData, elementOffset, arrayCount, enumEntry);
253
- attributeValueArray.push(array);
254
- }
255
- return attributeValueArray;
436
+ const attributeValueArray = [];
437
+ for (let index = 0; index < numberOfElements; index++) {
438
+ const elementOffset = arrayCount * index;
439
+ const array = getEnumsArray(valuesData, elementOffset, arrayCount, enumEntry);
440
+ attributeValueArray.push(array);
441
+ }
442
+ return attributeValueArray;
256
443
  }
444
+ /**
445
+ * Parses ENUM values into a string array.
446
+ * @param valuesData - Values in a flat typed array.
447
+ * @param offset - Offset to start parse from.
448
+ * @param count - Values length to parse.
449
+ * @param enumEntry - Enums dictionary.
450
+ * @returns Array of strings with parsed ENUM names.
451
+ */
257
452
  function getEnumsArray(valuesData, offset, count, enumEntry) {
258
- const array = [];
259
- for (let i = 0; i < count; i++) {
260
- if (valuesData instanceof BigInt64Array || valuesData instanceof BigUint64Array) {
261
- array.push('');
262
- } else {
263
- const value = valuesData[offset + i];
264
- const enumObject = getEnumByValue(enumEntry, value);
265
- if (enumObject) {
266
- array.push(enumObject.name);
267
- } else {
268
- array.push('');
269
- }
453
+ const array = [];
454
+ for (let i = 0; i < count; i++) {
455
+ // At the moment we don't support BigInt. It requires additional calculations logic
456
+ // and might be an issue in Safari
457
+ if (valuesData instanceof BigInt64Array || valuesData instanceof BigUint64Array) {
458
+ array.push('');
459
+ }
460
+ else {
461
+ const value = valuesData[offset + i];
462
+ const enumObject = getEnumByValue(enumEntry, value);
463
+ if (enumObject) {
464
+ array.push(enumObject.name);
465
+ }
466
+ else {
467
+ array.push('');
468
+ }
469
+ }
270
470
  }
271
- }
272
- return array;
471
+ return array;
273
472
  }
473
+ /**
474
+ * Looks up ENUM whose `value` property matches the specified number in the parameter `value`.
475
+ * @param {GLTF_EXT_structural_metadata_Enum} enumEntry - ENUM entry containing the array of possible enums.
476
+ * @param {number} value - The value of the ENUM to locate.
477
+ * @returns {GLTF_EXT_structural_metadata_EnumValue | null} ENUM matcihng the specified value or null of no ENUM object was found.
478
+ */
274
479
  function getEnumByValue(enumEntry, value) {
275
- for (const enumValue of enumEntry.values) {
276
- if (enumValue.value === value) {
277
- return enumValue;
480
+ for (const enumValue of enumEntry.values) {
481
+ if (enumValue.value === value) {
482
+ return enumValue;
483
+ }
278
484
  }
279
- }
280
- return null;
485
+ return null;
281
486
  }
282
- //# sourceMappingURL=EXT_structural_metadata.js.map