@loaders.gl/gltf 4.0.0-alpha.9 → 4.0.0-beta.1

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 (152) hide show
  1. package/dist/dist.min.js +3777 -3105
  2. package/dist/es5/glb-loader.js +1 -3
  3. package/dist/es5/glb-loader.js.map +1 -1
  4. package/dist/es5/index.js +33 -0
  5. package/dist/es5/index.js.map +1 -1
  6. package/dist/es5/lib/api/gltf-extensions.js +3 -1
  7. package/dist/es5/lib/api/gltf-extensions.js.map +1 -1
  8. package/dist/es5/lib/extensions/EXT_mesh_features.js +99 -0
  9. package/dist/es5/lib/extensions/EXT_mesh_features.js.map +1 -0
  10. package/dist/es5/lib/extensions/EXT_meshopt_compression.js +2 -2
  11. package/dist/es5/lib/extensions/EXT_meshopt_compression.js.map +1 -1
  12. package/dist/es5/lib/extensions/EXT_structural_metadata.js +375 -0
  13. package/dist/es5/lib/extensions/EXT_structural_metadata.js.map +1 -0
  14. package/dist/es5/lib/extensions/KHR_draco_mesh_compression.js +6 -7
  15. package/dist/es5/lib/extensions/KHR_draco_mesh_compression.js.map +1 -1
  16. package/dist/es5/lib/extensions/KHR_texture_transform.js +2 -1
  17. package/dist/es5/lib/extensions/KHR_texture_transform.js.map +1 -1
  18. package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js +162 -183
  19. package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -1
  20. package/dist/es5/lib/extensions/utils/3d-tiles-utils.js +254 -0
  21. package/dist/es5/lib/extensions/utils/3d-tiles-utils.js.map +1 -0
  22. package/dist/es5/lib/gltf-utils/gltf-utils.js +29 -0
  23. package/dist/es5/lib/gltf-utils/gltf-utils.js.map +1 -1
  24. package/dist/es5/lib/parsers/parse-gltf.js +7 -4
  25. package/dist/es5/lib/parsers/parse-gltf.js.map +1 -1
  26. package/dist/es5/lib/types/gltf-ext-feature-metadata-schema.js +2 -0
  27. package/dist/es5/lib/types/gltf-ext-feature-metadata-schema.js.map +1 -0
  28. package/dist/es5/lib/types/gltf-ext-mesh-features-schema.js +2 -0
  29. package/dist/es5/lib/types/gltf-ext-mesh-features-schema.js.map +1 -0
  30. package/dist/es5/lib/types/gltf-ext-structural-metadata-schema.js +2 -0
  31. package/dist/es5/lib/types/gltf-ext-structural-metadata-schema.js.map +1 -0
  32. package/dist/es5/lib/types/gltf-json-schema.js.map +1 -1
  33. package/dist/es5/lib/types/gltf-types.js.map +1 -1
  34. package/dist/es5/lib/utils/version.js +1 -1
  35. package/dist/es5/lib/utils/version.js.map +1 -1
  36. package/dist/esm/glb-loader.js +0 -1
  37. package/dist/esm/glb-loader.js.map +1 -1
  38. package/dist/esm/index.js +5 -0
  39. package/dist/esm/index.js.map +1 -1
  40. package/dist/esm/lib/api/gltf-extensions.js +3 -1
  41. package/dist/esm/lib/api/gltf-extensions.js.map +1 -1
  42. package/dist/esm/lib/extensions/EXT_mesh_features.js +43 -0
  43. package/dist/esm/lib/extensions/EXT_mesh_features.js.map +1 -0
  44. package/dist/esm/lib/extensions/EXT_meshopt_compression.js +2 -2
  45. package/dist/esm/lib/extensions/EXT_meshopt_compression.js.map +1 -1
  46. package/dist/esm/lib/extensions/EXT_structural_metadata.js +302 -0
  47. package/dist/esm/lib/extensions/EXT_structural_metadata.js.map +1 -0
  48. package/dist/esm/lib/extensions/KHR_draco_mesh_compression.js +2 -5
  49. package/dist/esm/lib/extensions/KHR_draco_mesh_compression.js.map +1 -1
  50. package/dist/esm/lib/extensions/KHR_texture_transform.js +2 -1
  51. package/dist/esm/lib/extensions/KHR_texture_transform.js.map +1 -1
  52. package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js +156 -167
  53. package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -1
  54. package/dist/esm/lib/extensions/utils/3d-tiles-utils.js +215 -0
  55. package/dist/esm/lib/extensions/utils/3d-tiles-utils.js.map +1 -0
  56. package/dist/esm/lib/gltf-utils/gltf-utils.js +30 -0
  57. package/dist/esm/lib/gltf-utils/gltf-utils.js.map +1 -1
  58. package/dist/esm/lib/parsers/parse-gltf.js +6 -6
  59. package/dist/esm/lib/parsers/parse-gltf.js.map +1 -1
  60. package/dist/esm/lib/types/gltf-ext-feature-metadata-schema.js +2 -0
  61. package/dist/esm/lib/types/gltf-ext-feature-metadata-schema.js.map +1 -0
  62. package/dist/esm/lib/types/gltf-ext-mesh-features-schema.js +2 -0
  63. package/dist/esm/lib/types/gltf-ext-mesh-features-schema.js.map +1 -0
  64. package/dist/esm/lib/types/gltf-ext-structural-metadata-schema.js +2 -0
  65. package/dist/esm/lib/types/gltf-ext-structural-metadata-schema.js.map +1 -0
  66. package/dist/esm/lib/types/gltf-json-schema.js.map +1 -1
  67. package/dist/esm/lib/types/gltf-types.js.map +1 -1
  68. package/dist/esm/lib/utils/version.js +1 -1
  69. package/dist/esm/lib/utils/version.js.map +1 -1
  70. package/dist/glb-loader.d.ts +3 -1
  71. package/dist/glb-loader.d.ts.map +1 -1
  72. package/dist/index.d.ts +10 -2
  73. package/dist/index.d.ts.map +1 -1
  74. package/dist/lib/api/gltf-extensions.d.ts.map +1 -1
  75. package/dist/lib/extensions/EXT_mesh_features.d.ts +7 -0
  76. package/dist/lib/extensions/EXT_mesh_features.d.ts.map +1 -0
  77. package/dist/lib/extensions/EXT_structural_metadata.d.ts +16 -0
  78. package/dist/lib/extensions/EXT_structural_metadata.d.ts.map +1 -0
  79. package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts +1 -1
  80. package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts.map +1 -1
  81. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts +9 -0
  82. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts.map +1 -1
  83. package/dist/lib/extensions/utils/3d-tiles-utils.d.ts +82 -0
  84. package/dist/lib/extensions/utils/3d-tiles-utils.d.ts.map +1 -0
  85. package/dist/lib/gltf-utils/gltf-utils.d.ts +2 -0
  86. package/dist/lib/gltf-utils/gltf-utils.d.ts.map +1 -1
  87. package/dist/lib/parsers/parse-gltf.d.ts.map +1 -1
  88. package/dist/lib/types/gltf-ext-feature-metadata-schema.d.ts +421 -0
  89. package/dist/lib/types/gltf-ext-feature-metadata-schema.d.ts.map +1 -0
  90. package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts +43 -0
  91. package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts.map +1 -0
  92. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts +329 -0
  93. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts.map +1 -0
  94. package/dist/lib/types/gltf-json-schema.d.ts +12 -404
  95. package/dist/lib/types/gltf-json-schema.d.ts.map +1 -1
  96. package/dist/lib/types/gltf-types.d.ts +4 -1
  97. package/dist/lib/types/gltf-types.d.ts.map +1 -1
  98. package/package.json +6 -6
  99. package/src/glb-loader.ts +3 -3
  100. package/src/index.ts +37 -6
  101. package/src/lib/api/gltf-extensions.ts +6 -2
  102. package/src/lib/extensions/EXT_mesh_features.ts +91 -0
  103. package/src/lib/extensions/EXT_meshopt_compression.ts +1 -1
  104. package/src/lib/extensions/EXT_structural_metadata.ts +750 -0
  105. package/src/lib/extensions/KHR_draco_mesh_compression.ts +7 -7
  106. package/src/lib/extensions/KHR_texture_transform.ts +1 -1
  107. package/src/lib/extensions/deprecated/EXT_feature_metadata.ts +407 -281
  108. package/src/lib/extensions/utils/3d-tiles-utils.ts +430 -0
  109. package/src/lib/gltf-utils/gltf-utils.ts +38 -0
  110. package/src/lib/parsers/parse-gltf.ts +14 -6
  111. package/src/lib/types/gltf-ext-feature-metadata-schema.ts +470 -0
  112. package/src/lib/types/gltf-ext-mesh-features-schema.ts +46 -0
  113. package/src/lib/types/gltf-ext-structural-metadata-schema.ts +378 -0
  114. package/src/lib/types/gltf-json-schema.ts +26 -465
  115. package/src/lib/types/gltf-types.ts +5 -3
  116. package/dist/bundle.js +0 -5
  117. package/dist/glb-loader.js +0 -36
  118. package/dist/glb-writer.js +0 -35
  119. package/dist/gltf-loader.js +0 -50
  120. package/dist/gltf-writer.js +0 -32
  121. package/dist/index.js +0 -28
  122. package/dist/lib/api/gltf-extensions.js +0 -83
  123. package/dist/lib/api/gltf-scenegraph.js +0 -580
  124. package/dist/lib/api/normalize-gltf-v1.js +0 -299
  125. package/dist/lib/api/post-process-gltf.js +0 -433
  126. package/dist/lib/encoders/encode-glb.js +0 -72
  127. package/dist/lib/encoders/encode-gltf.js +0 -32
  128. package/dist/lib/extensions/EXT_meshopt_compression.js +0 -41
  129. package/dist/lib/extensions/EXT_texture_webp.js +0 -36
  130. package/dist/lib/extensions/KHR_binary_gltf.js +0 -39
  131. package/dist/lib/extensions/KHR_draco_mesh_compression.js +0 -138
  132. package/dist/lib/extensions/KHR_texture_basisu.js +0 -29
  133. package/dist/lib/extensions/KHR_texture_transform.js +0 -227
  134. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +0 -290
  135. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +0 -59
  136. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +0 -44
  137. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +0 -79
  138. package/dist/lib/gltf-utils/get-typed-array.js +0 -41
  139. package/dist/lib/gltf-utils/gltf-attribute-utils.js +0 -73
  140. package/dist/lib/gltf-utils/gltf-constants.js +0 -43
  141. package/dist/lib/gltf-utils/gltf-utils.js +0 -90
  142. package/dist/lib/gltf-utils/resolve-url.js +0 -18
  143. package/dist/lib/parsers/parse-glb.js +0 -166
  144. package/dist/lib/parsers/parse-gltf.js +0 -179
  145. package/dist/lib/types/glb-types.js +0 -2
  146. package/dist/lib/types/gltf-json-schema.js +0 -4
  147. package/dist/lib/types/gltf-postprocessed-schema.js +0 -4
  148. package/dist/lib/types/gltf-types.js +0 -3
  149. package/dist/lib/utils/assert.js +0 -12
  150. package/dist/lib/utils/version.js +0 -7
  151. package/dist/meshopt/meshopt-decoder.js +0 -118
  152. package/dist/webp/webp.js +0 -38
@@ -1,24 +1,35 @@
1
1
  /* eslint-disable camelcase */
2
- import type {GLTF} from '../../types/gltf-json-schema';
2
+ import type {GLTF, GLTFTextureInfoMetadata} from '../../types/gltf-json-schema';
3
+ import type {
4
+ GLTF_EXT_feature_metadata_Class,
5
+ GLTF_EXT_feature_metadata_ClassProperty,
6
+ GLTF_EXT_feature_metadata_FeatureTable,
7
+ GLTF_EXT_feature_metadata_FeatureTableProperty,
8
+ GLTF_EXT_feature_metadata_FeatureTexture,
9
+ GLTF_EXT_feature_metadata_GLTF,
10
+ GLTF_EXT_feature_metadata_TextureAccessor,
11
+ GLTF_EXT_feature_metadata_Schema
12
+ } from '../../types/gltf-ext-feature-metadata-schema';
13
+ import type {BigTypedArray, TypedArray} from '@loaders.gl/schema';
14
+ import type {FeatureTableJson} from '../../types/gltf-types';
3
15
  import {GLTFScenegraph} from '../../api/gltf-scenegraph';
4
- import {getImageData} from '@loaders.gl/images';
5
- import {
6
- ClassProperty,
7
- EXT_feature_metadata_class_object,
8
- EXT_feature_metadata_feature_table,
9
- FeatureTableProperty,
10
- GLTF_EXT_feature_metadata,
11
- EXT_feature_metadata_feature_texture,
12
- FeatureTextureProperty,
13
- GLTFMeshPrimitive
14
- } from '../../types/gltf-json-schema';
15
- import {getComponentTypeFromArray} from '../../gltf-utils/gltf-utils';
16
+ import {GLTFMeshPrimitive} from '../../types/gltf-json-schema';
16
17
  import {GLTFLoaderOptions} from '../../../gltf-loader';
18
+ import {
19
+ convertRawBufferToMetadataArray,
20
+ getPrimitiveTextureData,
21
+ primitivePropertyDataToAttributes,
22
+ getArrayElementByteSize,
23
+ NumericComponentType,
24
+ getOffsetsForProperty,
25
+ parseVariableLengthArrayNumeric,
26
+ parseFixedLengthArrayNumeric,
27
+ getPropertyDataString
28
+ } from '../utils/3d-tiles-utils';
17
29
 
18
30
  /** Extension name */
19
- const EXT_FEATURE_METADATA = 'EXT_feature_metadata';
20
-
21
- export const name = EXT_FEATURE_METADATA;
31
+ const EXT_FEATURE_METADATA_NAME = 'EXT_feature_metadata';
32
+ export const name = EXT_FEATURE_METADATA_NAME;
22
33
 
23
34
  export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions): Promise<void> {
24
35
  const scenegraph = new GLTFScenegraph(gltfData);
@@ -26,32 +37,108 @@ export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions)
26
37
  }
27
38
 
28
39
  /**
29
- * Decodes feature metadata from extension
30
- * @param scenegraph
40
+ * Handles EXT_feature_metadata to get property table.
41
+ * @param extension - Global level of EXT_FEATURE_METADATA extension.
42
+ * @param metadataClass - User selected feature metadata class name.
43
+ * @returns {FeatureTableJson | null} Property table or null if the extension can't be handled properly.
31
44
  */
32
- function decodeExtFeatureMetadata(scenegraph: GLTFScenegraph, options: GLTFLoaderOptions): void {
33
- const extension: GLTF_EXT_feature_metadata | null = scenegraph.getExtension(EXT_FEATURE_METADATA);
34
- if (!extension) return;
45
+ export function getPropertyTableFromExtFeatureMetadata(
46
+ extension: GLTF_EXT_feature_metadata_GLTF,
47
+ metadataClass?: string
48
+ ): FeatureTableJson | null {
49
+ if (extension.featureTables) {
50
+ /**
51
+ * Take only first feature table to generate attributes storage info object.
52
+ * TODO: Think about getting data from all feature tables?
53
+ * It can be tricky just because 3dTiles is able to have multiple featureId attributes and multiple feature tables.
54
+ * In I3S we should decide which featureIds attribute will be passed to geometry data.
55
+ */
56
+ const firstFeatureTableName = Object.keys(extension.featureTables)?.[0];
57
+
58
+ if (firstFeatureTableName) {
59
+ const featureTable = extension.featureTables[firstFeatureTableName];
60
+ const propertyTable = {};
61
+
62
+ for (const propertyName in featureTable.properties) {
63
+ propertyTable[propertyName] = featureTable.properties[propertyName].data;
64
+ }
35
65
 
36
- const schemaClasses = extension.schema?.classes;
66
+ return propertyTable;
67
+ }
68
+ }
37
69
 
38
- const {featureTables} = extension;
39
- if (schemaClasses && featureTables) {
40
- for (const schemaName in schemaClasses) {
41
- const schemaClass = schemaClasses[schemaName];
42
- const featureTable = findFeatureTableByName(featureTables, schemaName);
70
+ if (extension.featureTextures) {
71
+ let featureTexture: string | undefined;
72
+ for (const textureKey in extension.featureTextures) {
73
+ const texture = extension.featureTextures[textureKey];
74
+ if (texture.class === metadataClass) {
75
+ featureTexture = textureKey;
76
+ }
77
+ }
43
78
 
44
- if (featureTable) {
45
- handleFeatureTableProperties(scenegraph, featureTable, schemaClass);
79
+ if (typeof featureTexture === 'string') {
80
+ const featureTable = extension.featureTextures[featureTexture];
81
+ const propertyTable = {};
82
+
83
+ for (const propertyName in featureTable.properties) {
84
+ propertyTable[propertyName] = featureTable.properties[propertyName].data;
46
85
  }
86
+
87
+ return propertyTable;
47
88
  }
48
89
  }
49
90
 
91
+ // eslint-disable-next-line no-console
92
+ console.warn(
93
+ 'Cannot get property table from EXT_feature_metadata extension. There is neither featureTables, nor featureTextures in the extension.'
94
+ );
95
+ return null;
96
+ }
97
+
98
+ /**
99
+ * Decodes feature metadata from extension.
100
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
101
+ * @param options - GLTFLoader options.
102
+ */
103
+ function decodeExtFeatureMetadata(scenegraph: GLTFScenegraph, options: GLTFLoaderOptions): void {
104
+ // Decoding metadata involves buffers processing.
105
+ // So, if buffers have not been loaded, there is no reason to process metadata.
106
+ if (!options.gltf?.loadBuffers) {
107
+ return;
108
+ }
109
+ const extension: GLTF_EXT_feature_metadata_GLTF | null =
110
+ scenegraph.getExtension(EXT_FEATURE_METADATA_NAME);
111
+ if (!extension) {
112
+ return;
113
+ }
114
+
115
+ if (options.gltf?.loadImages) {
116
+ decodePropertyTextures(scenegraph, extension);
117
+ }
118
+
119
+ decodePropertyTables(scenegraph, extension);
120
+ }
121
+
122
+ /**
123
+ * Processes the data stored in the textures
124
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
125
+ * @param extension - Top-level extension.
126
+ */
127
+ function decodePropertyTextures(
128
+ scenegraph: GLTFScenegraph,
129
+ extension: GLTF_EXT_feature_metadata_GLTF
130
+ ): void {
131
+ const schema = extension.schema;
132
+ if (!schema) {
133
+ return;
134
+ }
135
+ const schemaClasses = schema.classes;
136
+
50
137
  const {featureTextures} = extension;
51
- if (schemaClasses && featureTextures && options.gltf?.loadImages) {
138
+ if (schemaClasses && featureTextures) {
52
139
  for (const schemaName in schemaClasses) {
53
140
  const schemaClass = schemaClasses[schemaName];
54
- const featureTexture = findFeatureTextureByName(featureTextures, schemaName);
141
+ const featureTexture = findFeatureTextureByClass(featureTextures, schemaName);
55
142
 
56
143
  if (featureTexture) {
57
144
  handleFeatureTextureProperties(scenegraph, featureTexture, schemaClass);
@@ -61,44 +148,121 @@ function decodeExtFeatureMetadata(scenegraph: GLTFScenegraph, options: GLTFLoade
61
148
  }
62
149
 
63
150
  /**
64
- * Navigate throw all properies in feature table and gets properties data.
65
- * @param scenegraph
66
- * @param featureTable
67
- * @param schemaClass
151
+ * Processes the data stored in the property tables.
152
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
153
+ * @param extension - Top-level extension.
68
154
  */
69
- function handleFeatureTableProperties(
155
+ function decodePropertyTables(
70
156
  scenegraph: GLTFScenegraph,
71
- featureTable: EXT_feature_metadata_feature_table,
72
- schemaClass: EXT_feature_metadata_class_object
157
+ extension: GLTF_EXT_feature_metadata_GLTF
73
158
  ): void {
159
+ const schema = extension.schema;
160
+ if (!schema) {
161
+ return;
162
+ }
163
+ const schemaClasses = schema.classes;
164
+ const propertyTables = extension.featureTables;
165
+ if (schemaClasses && propertyTables) {
166
+ for (const schemaName in schemaClasses) {
167
+ const propertyTable = findPropertyTableByClass(propertyTables, schemaName);
168
+ if (propertyTable) {
169
+ processPropertyTable(scenegraph, schema, propertyTable);
170
+ }
171
+ }
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Finds the property table by class name.
177
+ * @param propertyTables - propertyTable definition taken from the top-level extension.
178
+ * @param schemaClassName - class name in the extension schema.
179
+ */
180
+ function findPropertyTableByClass(
181
+ propertyTables: {[key: string]: GLTF_EXT_feature_metadata_FeatureTable},
182
+ schemaClassName: string
183
+ ): GLTF_EXT_feature_metadata_FeatureTable | null {
184
+ for (const propertyTableName in propertyTables) {
185
+ const propertyTable = propertyTables[propertyTableName];
186
+ if (propertyTable.class === schemaClassName) {
187
+ return propertyTable;
188
+ }
189
+ }
190
+
191
+ return null;
192
+ }
193
+
194
+ function findFeatureTextureByClass(
195
+ featureTextures: {[key: string]: GLTF_EXT_feature_metadata_FeatureTexture},
196
+ schemaClassName: string
197
+ ): GLTF_EXT_feature_metadata_FeatureTexture | null {
198
+ for (const featureTexturesName in featureTextures) {
199
+ const featureTable = featureTextures[featureTexturesName];
200
+
201
+ if (featureTable.class === schemaClassName) {
202
+ return featureTable;
203
+ }
204
+ }
205
+
206
+ return null;
207
+ }
208
+
209
+ /**
210
+ * Navigates through all properies in the property table, gets properties data,
211
+ * and put the data to `propertyTable.data` as an array.
212
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
213
+ * @param schema - schema object.
214
+ * @param propertyTable - propertyTable definition taken from the top-level extension.
215
+ */
216
+ function processPropertyTable(
217
+ scenegraph: GLTFScenegraph,
218
+ schema: GLTF_EXT_feature_metadata_Schema,
219
+ propertyTable: GLTF_EXT_feature_metadata_FeatureTable
220
+ ): void {
221
+ // Though 'class' is not required by spec, it doesn't make any scence when it's not provided.
222
+ // So, bale out here.
223
+ if (!propertyTable.class) {
224
+ return;
225
+ }
226
+
227
+ const schemaClass = schema.classes?.[propertyTable.class];
228
+ if (!schemaClass) {
229
+ throw new Error(
230
+ `Incorrect data in the EXT_structural_metadata extension: no schema class with name ${propertyTable.class}`
231
+ );
232
+ }
233
+
234
+ const numberOfElements = propertyTable.count; // `propertyTable.count` is a number of elements in each property array.
235
+
74
236
  for (const propertyName in schemaClass.properties) {
75
- const schemaProperty = schemaClass.properties[propertyName];
76
- const featureTableProperty = featureTable?.properties?.[propertyName];
77
- const numberOfFeatures = featureTable.count;
237
+ const classProperty = schemaClass.properties[propertyName];
238
+ const propertyTableProperty: GLTF_EXT_feature_metadata_FeatureTableProperty | undefined =
239
+ propertyTable.properties?.[propertyName];
78
240
 
79
- if (featureTableProperty) {
241
+ if (propertyTableProperty) {
242
+ // Getting all elements (`numberOfElements`) of the array in the `propertyTableProperty`
80
243
  const data = getPropertyDataFromBinarySource(
81
244
  scenegraph,
82
- schemaProperty,
83
- numberOfFeatures,
84
- featureTableProperty
245
+ schema,
246
+ classProperty,
247
+ numberOfElements,
248
+ propertyTableProperty
85
249
  );
86
- featureTableProperty.data = data;
250
+ propertyTableProperty.data = data;
87
251
  }
88
252
  }
89
253
  }
90
254
 
91
255
  /**
92
- * Navigate throw all properies in feature texture and gets properties data.
93
- * Data will be stored in featureTexture.properties[propertyName].data
94
- * @param scenegraph
256
+ * Navigates through all properies in feature texture and gets properties data.
257
+ * Data will be stored in featureTexture.properties[propertyName].data.
258
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
95
259
  * @param featureTexture
96
260
  * @param schemaClass
97
261
  */
98
262
  function handleFeatureTextureProperties(
99
263
  scenegraph: GLTFScenegraph,
100
- featureTexture: EXT_feature_metadata_feature_texture,
101
- schemaClass: EXT_feature_metadata_class_object
264
+ featureTexture: GLTF_EXT_feature_metadata_FeatureTexture,
265
+ schemaClass: GLTF_EXT_feature_metadata_Class
102
266
  ): void {
103
267
  const attributeName = featureTexture.class;
104
268
 
@@ -113,45 +277,201 @@ function handleFeatureTextureProperties(
113
277
  }
114
278
 
115
279
  /**
116
- * Decode properties from binary sourse based on property type.
117
- * @param scenegraph
280
+ * Decodes properties from binary sourse based on property type.
281
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
118
282
  * @param schemaProperty
119
283
  * @param numberOfFeatures
120
284
  * @param featureTableProperty
121
285
  */
122
286
  function getPropertyDataFromBinarySource(
123
287
  scenegraph: GLTFScenegraph,
124
- schemaProperty: ClassProperty,
288
+ schema: GLTF_EXT_feature_metadata_Schema,
289
+ classProperty: GLTF_EXT_feature_metadata_ClassProperty,
125
290
  numberOfFeatures: number,
126
- featureTableProperty: FeatureTableProperty
127
- ): Uint8Array | string[] {
291
+ featureTableProperty: GLTF_EXT_feature_metadata_FeatureTableProperty
292
+ ): string[] | BigTypedArray | string[][] | BigTypedArray[] {
293
+ let data: string[] | BigTypedArray | string[][] | BigTypedArray[] = [];
128
294
  const bufferView = featureTableProperty.bufferView;
129
- // TODO think maybe we shouldn't get data only in Uint8Array format.
130
295
  const dataArray: Uint8Array = scenegraph.getTypedArrayForBufferView(bufferView);
131
296
 
132
- switch (schemaProperty.type) {
133
- case 'STRING': {
134
- // stringOffsetBufferView should be available for string type.
135
- const stringOffsetBufferView = featureTableProperty.stringOffsetBufferView!;
136
- const offsetsData = scenegraph.getTypedArrayForBufferView(stringOffsetBufferView);
137
- return getStringAttributes(dataArray, offsetsData, numberOfFeatures);
297
+ const arrayOffsets = getArrayOffsetsForProperty(
298
+ scenegraph,
299
+ classProperty,
300
+ featureTableProperty,
301
+ numberOfFeatures
302
+ );
303
+ const stringOffsets = getStringOffsetsForProperty(
304
+ scenegraph,
305
+ classProperty,
306
+ featureTableProperty,
307
+ numberOfFeatures
308
+ );
309
+
310
+ if (classProperty.type === 'STRING' || classProperty.componentType === 'STRING') {
311
+ data = getPropertyDataString(numberOfFeatures, dataArray, arrayOffsets, stringOffsets);
312
+ } else if (isNumericProperty(classProperty)) {
313
+ data = getPropertyDataNumeric(classProperty, numberOfFeatures, dataArray, arrayOffsets);
314
+ }
315
+
316
+ return data;
317
+ }
318
+
319
+ /**
320
+ * Parses propertyTable.property.arrayOffsets that are offsets of sub-arrays in a flatten array of values.
321
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
322
+ * @param classProperty - class property object.
323
+ * @param propertyTableProperty - propertyTable's property metadata.
324
+ * @param numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
325
+ * @returns Typed array with offset values.
326
+ * @see https://github.com/CesiumGS/glTF/blob/2976f1183343a47a29e4059a70961371cd2fcee8/extensions/2.0/Vendor/EXT_structural_metadata/schema/propertyTable.property.schema.json#L21
327
+ */
328
+ function getArrayOffsetsForProperty(
329
+ scenegraph: GLTFScenegraph,
330
+ classProperty: GLTF_EXT_feature_metadata_ClassProperty,
331
+ propertyTableProperty: GLTF_EXT_feature_metadata_FeatureTableProperty,
332
+ numberOfElements: number
333
+ ): TypedArray | null {
334
+ /*
335
+ If ARRAY is used, then componentType must also be specified.
336
+ ARRAY is a fixed-length array when componentCount is defined, and variable-length otherwise.
337
+ */
338
+ if (
339
+ classProperty.type === 'ARRAY' &&
340
+ // `componentCount` is a number of fixed-length array elements.
341
+ // If `componentCount` is NOT defined, it's a VARIABLE-length array
342
+ typeof classProperty.componentCount === 'undefined' &&
343
+ // `arrayOffsetBufferView` is an index of the buffer view containing offsets for variable-length arrays.
344
+ typeof propertyTableProperty.arrayOffsetBufferView !== 'undefined'
345
+ ) {
346
+ // Data are in a VARIABLE-length array
347
+ return getOffsetsForProperty(
348
+ scenegraph,
349
+ propertyTableProperty.arrayOffsetBufferView,
350
+ propertyTableProperty.offsetType || 'UINT32', // offsetType is used both for stringOffsetBufferView and arrayOffsetBufferView
351
+ numberOfElements
352
+ );
353
+ }
354
+ return null;
355
+ }
356
+
357
+ /**
358
+ * Parses featureTable.property.stringOffsetBufferView.
359
+ * String offsets is an array of offsets of strings in the united array of characters.
360
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
361
+ * @param propertyTableProperty - propertyTable's property metadata.
362
+ * @param numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
363
+ * @returns Typed array of offset values. The number of offsets in the array is equal to `numberOfElements` plus one.
364
+ * @see https://github.com/CesiumGS/glTF/blob/c38f7f37e894004353c15cd0481bc5b7381ce841/extensions/2.0/Vendor/EXT_feature_metadata/schema/featureTable.property.schema.json#L50C10-L50C32
365
+ */
366
+ function getStringOffsetsForProperty(
367
+ scenegraph: GLTFScenegraph,
368
+ classProperty: GLTF_EXT_feature_metadata_ClassProperty,
369
+ propertyTableProperty: GLTF_EXT_feature_metadata_FeatureTableProperty,
370
+ numberOfElements: number
371
+ ): TypedArray | null {
372
+ if (
373
+ typeof propertyTableProperty.stringOffsetBufferView !== 'undefined' // `stringOffsetBufferView` is an index of the buffer view containing offsets for strings.
374
+ ) {
375
+ // Data are in a FIXED-length array
376
+ return getOffsetsForProperty(
377
+ scenegraph,
378
+ propertyTableProperty.stringOffsetBufferView,
379
+ propertyTableProperty.offsetType || 'UINT32', // offsetType is used both for stringOffsetBufferView and arrayOffsetBufferView
380
+ numberOfElements
381
+ );
382
+ }
383
+ return null;
384
+ }
385
+
386
+ /**
387
+ * Checks if the feature table property is of numeric type.
388
+ * @param schemaPropertyType - feature table property
389
+ * @returns true if property is numeric, else - false
390
+ */
391
+ function isNumericProperty(schemaProperty: GLTF_EXT_feature_metadata_ClassProperty): boolean {
392
+ const types = [
393
+ 'UINT8',
394
+ 'INT16',
395
+ 'UINT16',
396
+ 'INT32',
397
+ 'UINT32',
398
+ 'INT64',
399
+ 'UINT64',
400
+ 'FLOAT32',
401
+ 'FLOAT64'
402
+ ];
403
+ return (
404
+ types.includes(schemaProperty.type) ||
405
+ (typeof schemaProperty.componentType !== 'undefined' &&
406
+ types.includes(schemaProperty.componentType))
407
+ );
408
+ }
409
+
410
+ /**
411
+ * Decodes properties of numeric types from binary sourse.
412
+ * @param classProperty - class property object.
413
+ * @param numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table.
414
+ * @param valuesDataBytes - Data taken from values property of the property table property.
415
+ * @param arrayOffsets - Offsets for variable-length arrays. It's null for fixed-length arrays or scalar types.
416
+ * @returns Property values in a typed array or in an array of typed arrays.
417
+ */
418
+ function getPropertyDataNumeric(
419
+ classProperty: GLTF_EXT_feature_metadata_ClassProperty,
420
+ numberOfElements: number,
421
+ valuesDataBytes: Uint8Array,
422
+ arrayOffsets: TypedArray | null
423
+ ): BigTypedArray | BigTypedArray[] {
424
+ const isArray = classProperty.type === 'ARRAY';
425
+ const arrayCount = classProperty.componentCount;
426
+
427
+ /*
428
+ We are getting Numeric data. So,
429
+ the component type can be one of NumericComponentType,
430
+ the attribute type should be 'SCALAR'
431
+ */
432
+ const attributeType = 'SCALAR';
433
+ const componentType = classProperty.componentType || classProperty.type;
434
+ const elementSize = getArrayElementByteSize(attributeType, componentType);
435
+ const elementCount = valuesDataBytes.byteLength / elementSize;
436
+
437
+ const valuesData: BigTypedArray = convertRawBufferToMetadataArray(
438
+ valuesDataBytes,
439
+ attributeType,
440
+ componentType as NumericComponentType,
441
+ elementCount
442
+ );
443
+
444
+ if (isArray) {
445
+ if (arrayOffsets) {
446
+ // VARIABLE-length array
447
+ return parseVariableLengthArrayNumeric(
448
+ valuesData,
449
+ numberOfElements,
450
+ arrayOffsets,
451
+ valuesDataBytes.length,
452
+ elementSize
453
+ );
454
+ }
455
+ if (arrayCount) {
456
+ // FIXED-length array
457
+ return parseFixedLengthArrayNumeric(valuesData, numberOfElements, arrayCount);
138
458
  }
139
- default:
459
+ return [];
140
460
  }
141
461
 
142
- return dataArray;
462
+ return valuesData;
143
463
  }
144
464
 
145
465
  /**
146
- * Get properties from texture associated with all mesh primitives.
147
- * @param scenegraph
466
+ * Gets properties from texture associated with all mesh primitives.
467
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
148
468
  * @param featureTextureProperty
149
469
  * @param attributeName
150
470
  * @returns Feature texture data
151
471
  */
152
472
  function getPropertyDataFromTexture(
153
473
  scenegraph: GLTFScenegraph,
154
- featureTextureProperty: FeatureTextureProperty,
474
+ featureTextureProperty: GLTF_EXT_feature_metadata_TextureAccessor,
155
475
  attributeName: string
156
476
  ): number[] {
157
477
  const json = scenegraph.gltf.json;
@@ -173,10 +493,9 @@ function getPropertyDataFromTexture(
173
493
  return featureTextureTable;
174
494
  }
175
495
 
176
- // eslint-disable-next-line max-statements
177
496
  /**
178
497
  * Processes data encoded in the texture associated with the primitive. This data will be accessible through the attributes.
179
- * @param scenegraph
498
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
180
499
  * @param attributeName
181
500
  * @param featureTextureProperty
182
501
  * @param featureTextureTable
@@ -185,220 +504,27 @@ function getPropertyDataFromTexture(
185
504
  function processPrimitiveTextures(
186
505
  scenegraph: GLTFScenegraph,
187
506
  attributeName: string,
188
- featureTextureProperty: FeatureTextureProperty,
507
+ featureTextureProperty: GLTF_EXT_feature_metadata_TextureAccessor,
189
508
  featureTextureTable: number[],
190
509
  primitive: GLTFMeshPrimitive
191
510
  ): void {
192
- /*
193
- texture.index is an index for the "textures" array.
194
- The texture object referenced by this index looks like this:
195
- {
196
- "sampler": 0,
197
- "source": 0
198
- }
199
- "sampler" is an index for the "samplers" array
200
- "source" is an index for the "images" array that contains data. These data are stored in rgba channels of the image.
201
-
202
- texture.texCoord is a number-suffix (like 1) for an attribute like "TEXCOORD_1" in meshes.primitives
203
- The value of "TEXCOORD_1" is an accessor that is used to get coordinates. These coordinates ared used to get data from the image.
204
- */
205
- const json = scenegraph.gltf.json;
206
- const textureData: number[] = [];
207
- const texCoordAccessorKey = `TEXCOORD_${featureTextureProperty.texture.texCoord}`;
208
- const texCoordAccessorIndex = primitive.attributes[texCoordAccessorKey];
209
- const texCoordBufferView = scenegraph.getBufferView(texCoordAccessorIndex);
210
- const texCoordArray: Uint8Array = scenegraph.getTypedArrayForBufferView(texCoordBufferView);
211
-
212
- const textureCoordinates: Float32Array = new Float32Array(
213
- texCoordArray.buffer,
214
- texCoordArray.byteOffset,
215
- texCoordArray.length / 4
216
- );
217
- // textureCoordinates contains UV coordinates of the actual data stored in the texture
218
- // accessor.count is a number of UV pairs (they are stored as VEC2)
219
-
220
- const textureIndex = featureTextureProperty.texture.index;
221
- const texture = json.textures?.[textureIndex];
222
- const imageIndex = texture?.source;
223
- if (typeof imageIndex !== 'undefined') {
224
- const image = json.images?.[imageIndex];
225
- const mimeType = image?.mimeType;
226
- const parsedImage = scenegraph.gltf.images?.[imageIndex];
227
- if (parsedImage) {
228
- for (let index = 0; index < textureCoordinates.length; index += 2) {
229
- const value = getImageValueByCoordinates(
230
- parsedImage,
231
- mimeType,
232
- textureCoordinates,
233
- index,
234
- featureTextureProperty.channels
235
- );
236
- textureData.push(value);
237
- }
238
- }
239
- }
240
- /*
241
- featureTextureTable will contain unique values, e.g.
242
- textureData = [24, 35, 28, 24]
243
- featureTextureTable = [24, 35, 28]
244
- featureIndices will contain indices hat refer featureTextureTable, e.g.
245
- featureIndices = [0, 1, 2, 0]
246
- */
247
- const featureIndices: number[] = [];
248
- for (const texelData of textureData) {
249
- let index = featureTextureTable.findIndex((item) => item === texelData);
250
- if (index === -1) {
251
- index = featureTextureTable.push(texelData) - 1;
252
- }
253
- featureIndices.push(index);
254
- }
255
- const typedArray = new Uint32Array(featureIndices);
256
- const bufferIndex =
257
- scenegraph.gltf.buffers.push({
258
- arrayBuffer: typedArray.buffer,
259
- byteOffset: 0,
260
- byteLength: typedArray.byteLength
261
- }) - 1;
262
- const bufferViewIndex = scenegraph.addBufferView(typedArray, bufferIndex, 0);
263
- const accessorIndex = scenegraph.addAccessor(bufferViewIndex, {
264
- size: 1,
265
- componentType: getComponentTypeFromArray(typedArray),
266
- count: typedArray.length
267
- });
268
- primitive.attributes[attributeName] = accessorIndex;
269
- }
270
-
271
- function getImageValueByCoordinates(
272
- parsedImage: any,
273
- mimeType: string | undefined,
274
- textureCoordinates: Float32Array,
275
- index: number,
276
- channels: string
277
- ) {
278
- const CHANNELS_MAP = {
279
- r: {offset: 0, shift: 0},
280
- g: {offset: 1, shift: 8},
281
- b: {offset: 2, shift: 16},
282
- a: {offset: 3, shift: 24}
511
+ const textureInfoTopLevel: GLTFTextureInfoMetadata = {
512
+ channels: featureTextureProperty.channels,
513
+ ...featureTextureProperty.texture
283
514
  };
284
-
285
- const u = textureCoordinates[index];
286
- const v = textureCoordinates[index + 1];
287
-
288
- let components = 1;
289
- if (mimeType && (mimeType.indexOf('image/jpeg') !== -1 || mimeType.indexOf('image/png') !== -1))
290
- components = 4;
291
- const offset = coordinatesToOffset(u, v, parsedImage, components);
292
- let value = 0;
293
- for (const c of channels) {
294
- const map = CHANNELS_MAP[c];
295
- const val = getVal(parsedImage, offset + map.offset);
296
- value |= val << map.shift;
297
- }
298
- return value;
299
- }
300
-
301
- function getVal(parsedImage: any, offset: number): number {
302
- const imageData = getImageData(parsedImage);
303
- if (imageData.data.length <= offset) {
304
- throw new Error(`${imageData.data.length} <= ${offset}`);
305
- }
306
- return imageData.data[offset];
307
- }
308
-
309
- function coordinatesToOffset(
310
- u: number,
311
- v: number,
312
- parsedImage: any,
313
- componentsCount: number = 1
314
- ): number {
315
- const w = parsedImage.width;
316
- const iX = emod(u) * (w - 1);
317
- const indX = Math.round(iX);
318
-
319
- const h = parsedImage.height;
320
- const iY = emod(v) * (h - 1);
321
- const indY = Math.round(iY);
322
- const components = parsedImage.components ? parsedImage.components : componentsCount;
323
- // components is a number of channels in the image
324
- const offset = (indY * w + indX) * components;
325
- return offset;
326
- }
327
-
328
- // The following is taken from tile-converter\src\i3s-converter\helpers\batch-ids-extensions.ts
329
- /**
330
- * Handle UVs if they are out of range [0,1].
331
- * @param n
332
- * @param m
333
- */
334
- function emod(n: number): number {
335
- const a = ((n % 1) + 1) % 1;
336
- return a;
337
- }
338
-
339
- /**
340
- * Find the feature table by class name.
341
- * @param featureTables
342
- * @param schemaClassName
343
- */
344
- function findFeatureTableByName(
345
- featureTables: {[key: string]: EXT_feature_metadata_feature_table},
346
- schemaClassName: string
347
- ): EXT_feature_metadata_feature_table | null {
348
- for (const featureTableName in featureTables) {
349
- const featureTable = featureTables[featureTableName];
350
-
351
- if (featureTable.class === schemaClassName) {
352
- return featureTable;
353
- }
354
- }
355
-
356
- return null;
357
- }
358
-
359
- function findFeatureTextureByName(
360
- featureTextures: {[key: string]: EXT_feature_metadata_feature_texture},
361
- schemaClassName: string
362
- ): EXT_feature_metadata_feature_texture | null {
363
- for (const featureTexturesName in featureTextures) {
364
- const featureTable = featureTextures[featureTexturesName];
365
-
366
- if (featureTable.class === schemaClassName) {
367
- return featureTable;
368
- }
369
- }
370
-
371
- return null;
372
- }
373
-
374
- /**
375
- * Getting string attributes from binary data.
376
- * Spec - https://github.com/CesiumGS/3d-tiles/tree/main/specification/Metadata#strings
377
- * @param data
378
- * @param offsetsData
379
- * @param stringsCount
380
- */
381
- function getStringAttributes(
382
- data: Uint8Array,
383
- offsetsData: Uint8Array,
384
- stringsCount: number
385
- ): string[] {
386
- const stringsArray: string[] = [];
387
- const textDecoder = new TextDecoder('utf8');
388
-
389
- let stringOffset = 0;
390
- const bytesPerStringSize = 4;
391
-
392
- for (let index = 0; index < stringsCount; index++) {
393
- // TODO check if it is multiplication on bytesPerStringSize is valid operation?
394
- const stringByteSize =
395
- offsetsData[(index + 1) * bytesPerStringSize] - offsetsData[index * bytesPerStringSize];
396
- const stringData = data.subarray(stringOffset, stringByteSize + stringOffset);
397
- const stringAttribute = textDecoder.decode(stringData);
398
-
399
- stringsArray.push(stringAttribute);
400
- stringOffset += stringByteSize;
515
+ const propertyData: number[] | null = getPrimitiveTextureData(
516
+ scenegraph,
517
+ textureInfoTopLevel,
518
+ primitive
519
+ );
520
+ if (!propertyData) {
521
+ return;
401
522
  }
402
-
403
- return stringsArray;
523
+ primitivePropertyDataToAttributes(
524
+ scenegraph,
525
+ attributeName,
526
+ propertyData,
527
+ featureTextureTable,
528
+ primitive
529
+ );
404
530
  }