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

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 (100) hide show
  1. package/dist/dist.min.js +3626 -3028
  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 +388 -0
  9. package/dist/es5/lib/extensions/EXT_structural_metadata.js.map +1 -0
  10. package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js +32 -22
  11. package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -1
  12. package/dist/es5/lib/extensions/utils/3d-tiles-utils.js +200 -0
  13. package/dist/es5/lib/extensions/utils/3d-tiles-utils.js.map +1 -0
  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 +324 -0
  27. package/dist/esm/lib/extensions/EXT_structural_metadata.js.map +1 -0
  28. package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js +30 -20
  29. package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -1
  30. package/dist/esm/lib/extensions/utils/3d-tiles-utils.js +164 -0
  31. package/dist/esm/lib/extensions/utils/3d-tiles-utils.js.map +1 -0
  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/lib/api/gltf-extensions.d.ts.map +1 -1
  41. package/dist/lib/extensions/EXT_mesh_features.d.ts +7 -0
  42. package/dist/lib/extensions/EXT_mesh_features.d.ts.map +1 -0
  43. package/dist/lib/extensions/EXT_structural_metadata.d.ts +17 -0
  44. package/dist/lib/extensions/EXT_structural_metadata.d.ts.map +1 -0
  45. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts.map +1 -1
  46. package/dist/lib/extensions/utils/3d-tiles-utils.d.ts +52 -0
  47. package/dist/lib/extensions/utils/3d-tiles-utils.d.ts.map +1 -0
  48. package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts +45 -0
  49. package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts.map +1 -0
  50. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts +310 -0
  51. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts.map +1 -0
  52. package/dist/lib/types/gltf-json-schema.d.ts +12 -58
  53. package/dist/lib/types/gltf-json-schema.d.ts.map +1 -1
  54. package/package.json +6 -6
  55. package/src/index.ts +11 -3
  56. package/src/lib/api/gltf-extensions.ts +6 -2
  57. package/src/lib/extensions/EXT_mesh_features.ts +117 -0
  58. package/src/lib/extensions/EXT_structural_metadata.ts +797 -0
  59. package/src/lib/extensions/deprecated/EXT_feature_metadata.ts +129 -36
  60. package/src/lib/extensions/utils/3d-tiles-utils.ts +325 -0
  61. package/src/lib/types/gltf-ext-mesh-features-schema.ts +48 -0
  62. package/src/lib/types/gltf-ext-structural-metadata-schema.ts +359 -0
  63. package/src/lib/types/gltf-json-schema.ts +13 -61
  64. package/dist/bundle.js +0 -5
  65. package/dist/glb-loader.js +0 -34
  66. package/dist/glb-writer.js +0 -35
  67. package/dist/gltf-loader.js +0 -50
  68. package/dist/gltf-writer.js +0 -32
  69. package/dist/index.js +0 -28
  70. package/dist/lib/api/gltf-extensions.js +0 -83
  71. package/dist/lib/api/gltf-scenegraph.js +0 -580
  72. package/dist/lib/api/normalize-gltf-v1.js +0 -299
  73. package/dist/lib/api/post-process-gltf.js +0 -433
  74. package/dist/lib/encoders/encode-glb.js +0 -72
  75. package/dist/lib/encoders/encode-gltf.js +0 -32
  76. package/dist/lib/extensions/EXT_meshopt_compression.js +0 -41
  77. package/dist/lib/extensions/EXT_texture_webp.js +0 -36
  78. package/dist/lib/extensions/KHR_binary_gltf.js +0 -39
  79. package/dist/lib/extensions/KHR_draco_mesh_compression.js +0 -137
  80. package/dist/lib/extensions/KHR_texture_basisu.js +0 -29
  81. package/dist/lib/extensions/KHR_texture_transform.js +0 -227
  82. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +0 -291
  83. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +0 -59
  84. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +0 -44
  85. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +0 -79
  86. package/dist/lib/gltf-utils/get-typed-array.js +0 -41
  87. package/dist/lib/gltf-utils/gltf-attribute-utils.js +0 -73
  88. package/dist/lib/gltf-utils/gltf-constants.js +0 -43
  89. package/dist/lib/gltf-utils/gltf-utils.js +0 -90
  90. package/dist/lib/gltf-utils/resolve-url.js +0 -18
  91. package/dist/lib/parsers/parse-glb.js +0 -166
  92. package/dist/lib/parsers/parse-gltf.js +0 -185
  93. package/dist/lib/types/glb-types.js +0 -2
  94. package/dist/lib/types/gltf-json-schema.js +0 -4
  95. package/dist/lib/types/gltf-postprocessed-schema.js +0 -4
  96. package/dist/lib/types/gltf-types.js +0 -3
  97. package/dist/lib/utils/assert.js +0 -12
  98. package/dist/lib/utils/version.js +0 -7
  99. package/dist/meshopt/meshopt-decoder.js +0 -118
  100. package/dist/webp/webp.js +0 -38
@@ -9,16 +9,18 @@ import type {
9
9
  GLTF_EXT_feature_metadata_GLTF,
10
10
  GLTF_EXT_feature_metadata_TextureAccessor
11
11
  } from '../../types/gltf-json-schema';
12
+ import type {BigTypedArray, TypedArray} from '@loaders.gl/schema';
12
13
  import {GLTFScenegraph} from '../../api/gltf-scenegraph';
13
14
  import {getImageData} from '@loaders.gl/images';
14
15
  import {GLTFMeshPrimitive} from '../../types/gltf-json-schema';
15
16
  import {getComponentTypeFromArray} from '../../gltf-utils/gltf-utils';
16
17
  import {GLTFLoaderOptions} from '../../../gltf-loader';
18
+ import {emod} from '@loaders.gl/math';
19
+ import {convertRawBufferToMetadataArray, getOffsetsForProperty} from '../utils/3d-tiles-utils';
17
20
 
18
21
  /** Extension name */
19
- const EXT_FEATURE_METADATA = 'EXT_feature_metadata';
20
-
21
- export const name = EXT_FEATURE_METADATA;
22
+ const EXT_FEATURE_METADATA_NAME = 'EXT_feature_metadata';
23
+ export const name = EXT_FEATURE_METADATA_NAME;
22
24
 
23
25
  export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions): Promise<void> {
24
26
  const scenegraph = new GLTFScenegraph(gltfData);
@@ -31,7 +33,7 @@ export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions)
31
33
  */
32
34
  function decodeExtFeatureMetadata(scenegraph: GLTFScenegraph, options: GLTFLoaderOptions): void {
33
35
  const extension: GLTF_EXT_feature_metadata_GLTF | null =
34
- scenegraph.getExtension(EXT_FEATURE_METADATA);
36
+ scenegraph.getExtension(EXT_FEATURE_METADATA_NAME);
35
37
  if (!extension) return;
36
38
 
37
39
  const schemaClasses = extension.schema?.classes;
@@ -62,7 +64,7 @@ function decodeExtFeatureMetadata(scenegraph: GLTFScenegraph, options: GLTFLoade
62
64
  }
63
65
 
64
66
  /**
65
- * Navigate throw all properies in feature table and gets properties data.
67
+ * Navigates through all properies in feature table and gets properties data.
66
68
  * @param scenegraph
67
69
  * @param featureTable
68
70
  * @param schemaClass
@@ -90,7 +92,7 @@ function handleFeatureTableProperties(
90
92
  }
91
93
 
92
94
  /**
93
- * Navigate throw all properies in feature texture and gets properties data.
95
+ * Navigates through all properies in feature texture and gets properties data.
94
96
  * Data will be stored in featureTexture.properties[propertyName].data
95
97
  * @param scenegraph
96
98
  * @param featureTexture
@@ -125,24 +127,134 @@ function getPropertyDataFromBinarySource(
125
127
  schemaProperty: GLTF_EXT_feature_metadata_ClassProperty,
126
128
  numberOfFeatures: number,
127
129
  featureTableProperty: GLTF_EXT_feature_metadata_FeatureTableProperty
128
- ): Uint8Array | string[] {
130
+ ): BigTypedArray | string[] {
129
131
  const bufferView = featureTableProperty.bufferView;
130
- // TODO think maybe we shouldn't get data only in Uint8Array format.
131
132
  const dataArray: Uint8Array = scenegraph.getTypedArrayForBufferView(bufferView);
132
133
 
133
- switch (schemaProperty.type) {
134
- case 'STRING': {
135
- // stringOffsetBufferView should be available for string type.
136
- const stringOffsetBufferView = featureTableProperty.stringOffsetBufferView!;
137
- const offsetsData = scenegraph.getTypedArrayForBufferView(stringOffsetBufferView);
138
- return getStringAttributes(dataArray, offsetsData, numberOfFeatures);
134
+ if (schemaProperty.type === 'STRING') {
135
+ const offsetsData = getStringOffsets(scenegraph, featureTableProperty, numberOfFeatures);
136
+ if (!offsetsData) {
137
+ return [];
139
138
  }
140
- default:
139
+ return getStringAttributes(dataArray, offsetsData, numberOfFeatures);
140
+ } else if (isNumericProperty(schemaProperty.type)) {
141
+ return getNumericAttributes(
142
+ dataArray,
143
+ schemaProperty.type as
144
+ | 'INT8'
145
+ | 'UINT8'
146
+ | 'INT16'
147
+ | 'UINT16'
148
+ | 'INT32'
149
+ | 'UINT32'
150
+ | 'INT64'
151
+ | 'UINT64'
152
+ | 'FLOAT32'
153
+ | 'FLOAT64',
154
+ numberOfFeatures
155
+ );
141
156
  }
142
157
 
143
158
  return dataArray;
144
159
  }
145
160
 
161
+ /**
162
+ * Check if the feature table property is of numeric type
163
+ * @param schemaPropertyType - feature table property
164
+ * @returns true if property is numeric, else - false
165
+ */
166
+ function isNumericProperty(
167
+ schemaPropertyType:
168
+ | 'INT8'
169
+ | 'UINT8'
170
+ | 'INT16'
171
+ | 'UINT16'
172
+ | 'INT32'
173
+ | 'UINT32'
174
+ | 'INT64'
175
+ | 'UINT64'
176
+ | 'FLOAT32'
177
+ | 'FLOAT64'
178
+ | 'BOOLEAN'
179
+ | 'STRING'
180
+ | 'ENUM'
181
+ | 'ARRAY'
182
+ | string
183
+ ): boolean {
184
+ return [
185
+ 'UINT8',
186
+ 'INT16',
187
+ 'UINT16',
188
+ 'INT32',
189
+ 'UINT32',
190
+ 'INT64',
191
+ 'UINT64',
192
+ 'FLOAT32',
193
+ 'FLOAT64'
194
+ ].includes(schemaPropertyType);
195
+ }
196
+
197
+ /**
198
+ * Parse featureTable.property.stringOffsetBufferView.
199
+ * String offsets is an array of offsets of strings in the united array of charactersz
200
+ * @param scenegraph - Instance of the class for structured access to GLTF data
201
+ * @param propertyTableProperty - propertyTable's property metadata
202
+ * @param numberOfElements - The number of elements in each property array that propertyTableProperty contains. It's a number of rows in the table
203
+ * @returns typed array with offset values
204
+ * @see https://github.com/CesiumGS/glTF/blob/c38f7f37e894004353c15cd0481bc5b7381ce841/extensions/2.0/Vendor/EXT_feature_metadata/schema/featureTable.property.schema.json#L50C10-L50C32
205
+ */
206
+ function getStringOffsets(
207
+ scenegraph: GLTFScenegraph,
208
+ featureTableProperty: GLTF_EXT_feature_metadata_FeatureTableProperty,
209
+ numberOfElements: number
210
+ ): TypedArray | null {
211
+ if (typeof featureTableProperty.stringOffsetBufferView !== 'undefined') {
212
+ // Data are in a FIXED-length array
213
+ return getOffsetsForProperty(
214
+ scenegraph,
215
+ featureTableProperty.stringOffsetBufferView,
216
+ featureTableProperty.offsetType || 'UINT32', // UINT32 is the default by the spec
217
+ numberOfElements
218
+ );
219
+ }
220
+ return null;
221
+ }
222
+
223
+ /**
224
+ * Parse numeric property values
225
+ * @param valuesDataBytes - values data array
226
+ * @param propertyType - type of the property
227
+ * @param elementCount - number of rows in the featureTable
228
+ * @returns Number data in a typed array
229
+ */
230
+ function getNumericAttributes(
231
+ valuesDataBytes: Uint8Array,
232
+ propertyType:
233
+ | 'INT8'
234
+ | 'UINT8'
235
+ | 'INT16'
236
+ | 'UINT16'
237
+ | 'INT32'
238
+ | 'UINT32'
239
+ | 'INT64'
240
+ | 'UINT64'
241
+ | 'FLOAT32'
242
+ | 'FLOAT64',
243
+ elementCount: number
244
+ ): BigTypedArray {
245
+ let valuesData = convertRawBufferToMetadataArray(
246
+ valuesDataBytes,
247
+ 'SCALAR',
248
+ // The datatype of the element's components. Only applicable to `SCALAR`, `VECN`, and `MATN` types.
249
+ propertyType,
250
+ elementCount
251
+ );
252
+ if (!valuesData) {
253
+ valuesData = valuesDataBytes;
254
+ }
255
+ return valuesData;
256
+ }
257
+
146
258
  /**
147
259
  * Get properties from texture associated with all mesh primitives.
148
260
  * @param scenegraph
@@ -327,17 +439,6 @@ function coordinatesToOffset(
327
439
  return offset;
328
440
  }
329
441
 
330
- // The following is taken from tile-converter\src\i3s-converter\helpers\batch-ids-extensions.ts
331
- /**
332
- * Handle UVs if they are out of range [0,1].
333
- * @param n
334
- * @param m
335
- */
336
- function emod(n: number): number {
337
- const a = ((n % 1) + 1) % 1;
338
- return a;
339
- }
340
-
341
442
  /**
342
443
  * Find the feature table by class name.
343
444
  * @param featureTables
@@ -382,24 +483,16 @@ function findFeatureTextureByName(
382
483
  */
383
484
  function getStringAttributes(
384
485
  data: Uint8Array,
385
- offsetsData: Uint8Array,
486
+ offsetsData: TypedArray,
386
487
  stringsCount: number
387
488
  ): string[] {
388
489
  const stringsArray: string[] = [];
389
490
  const textDecoder = new TextDecoder('utf8');
390
491
 
391
- let stringOffset = 0;
392
- const bytesPerStringSize = 4;
393
-
394
492
  for (let index = 0; index < stringsCount; index++) {
395
- // TODO check if it is multiplication on bytesPerStringSize is valid operation?
396
- const stringByteSize =
397
- offsetsData[(index + 1) * bytesPerStringSize] - offsetsData[index * bytesPerStringSize];
398
- const stringData = data.subarray(stringOffset, stringByteSize + stringOffset);
493
+ const stringData = data.slice(offsetsData[index], offsetsData[index + 1]);
399
494
  const stringAttribute = textDecoder.decode(stringData);
400
-
401
495
  stringsArray.push(stringAttribute);
402
- stringOffset += stringByteSize;
403
496
  }
404
497
 
405
498
  return stringsArray;
@@ -0,0 +1,325 @@
1
+ /**
2
+ * loaders.gl, MIT license
3
+ *
4
+ * Shared code for 3DTiles extensions:
5
+ * * EXT_feature_metadata
6
+ * * EXT_mesh_features
7
+ * * EXT_structural_metadata
8
+ */
9
+
10
+ import type {GLTFTextureInfoMetadata, GLTFMeshPrimitive} from '../../types/gltf-json-schema';
11
+ import type {BigTypedArray, TypedArray} from '@loaders.gl/schema';
12
+ import type {ImageType} from '@loaders.gl/images';
13
+
14
+ import {GLTFScenegraph} from '../../api/gltf-scenegraph';
15
+ import {getComponentTypeFromArray} from '../../gltf-utils/gltf-utils';
16
+ import {getImageData} from '@loaders.gl/images';
17
+ import {emod} from '@loaders.gl/math';
18
+
19
+ export type NumericComponentType =
20
+ | 'INT8'
21
+ | 'UINT8'
22
+ | 'INT16'
23
+ | 'UINT16'
24
+ | 'INT32'
25
+ | 'UINT32'
26
+ | 'INT64'
27
+ | 'UINT64'
28
+ | 'FLOAT32'
29
+ | 'FLOAT64';
30
+
31
+ const ATTRIBUTE_TYPE_TO_COMPONENTS = {
32
+ SCALAR: 1,
33
+ VEC2: 2,
34
+ VEC3: 3,
35
+ VEC4: 4,
36
+ MAT2: 4,
37
+ MAT3: 9,
38
+ MAT4: 16,
39
+ BOOLEAN: 1,
40
+ STRING: 1,
41
+ ENUM: 1
42
+ };
43
+
44
+ const ATTRIBUTE_COMPONENT_TYPE_TO_ARRAY = {
45
+ INT8: Int8Array,
46
+ UINT8: Uint8Array,
47
+ INT16: Int16Array,
48
+ UINT16: Uint16Array,
49
+ INT32: Int32Array,
50
+ UINT32: Uint32Array,
51
+ INT64: BigInt64Array,
52
+ UINT64: BigUint64Array,
53
+ FLOAT32: Float32Array,
54
+ FLOAT64: Float64Array
55
+ };
56
+
57
+ const ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE = {
58
+ INT8: 1,
59
+ UINT8: 1,
60
+ INT16: 2,
61
+ UINT16: 2,
62
+ INT32: 4,
63
+ UINT32: 4,
64
+ INT64: 8,
65
+ UINT64: 8,
66
+ FLOAT32: 4,
67
+ FLOAT64: 8
68
+ };
69
+
70
+ export function getArrayElementByteSize(attributeType, componentType): number {
71
+ return (
72
+ ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE[componentType] *
73
+ ATTRIBUTE_TYPE_TO_COMPONENTS[attributeType]
74
+ );
75
+ }
76
+
77
+ /**
78
+ * Gets offset array from `arrayOffsets` or `stringOffsets`.
79
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
80
+ * @param bufferViewIndex - Buffer view index
81
+ * @param offsetType - The type of values in `arrayOffsets` or `stringOffsets`.
82
+ * @param numberOfElements - The number of elements in each property array.
83
+ * @returns array with values offsets
84
+ */
85
+ export function getOffsetsForProperty(
86
+ scenegraph: GLTFScenegraph,
87
+ bufferViewIndex: number,
88
+ offsetType: 'UINT8' | 'UINT16' | 'UINT32' | 'UINT64' | string,
89
+ numberOfElements: number
90
+ ): TypedArray | null {
91
+ if (
92
+ offsetType !== 'UINT8' &&
93
+ offsetType !== 'UINT16' &&
94
+ offsetType !== 'UINT32' &&
95
+ offsetType !== 'UINT64'
96
+ ) {
97
+ return null;
98
+ }
99
+ const arrayOffsetsBytes = scenegraph.getTypedArrayForBufferView(bufferViewIndex);
100
+ const arrayOffsets = convertRawBufferToMetadataArray(
101
+ arrayOffsetsBytes,
102
+ 'SCALAR', // offsets consist of ONE component
103
+ offsetType,
104
+ numberOfElements + 1 // The number of offsets is equal to the property table `count` plus one.
105
+ );
106
+
107
+ // We don't support BigInt offsets at the moment. It requires additional logic and potential issues in Safari
108
+ if (arrayOffsets instanceof BigInt64Array || arrayOffsets instanceof BigUint64Array) {
109
+ return null;
110
+ }
111
+ return arrayOffsets;
112
+ }
113
+
114
+ /**
115
+ * Converts raw bytes that are in the buffer to an array of the type defined by the schema.
116
+ * @param data - raw bytes in the buffer
117
+ * @param attributeType - SCALAR, VECN, MATN
118
+ * @param componentType - type of the component in elements, e.g. 'UINT8' or 'FLOAT32'
119
+ * @param elementCount - number of elements in the array. Default value is 1.
120
+ * @returns Data array
121
+ */
122
+ export function convertRawBufferToMetadataArray(
123
+ data: Uint8Array,
124
+ attributeType: string,
125
+ componentType: NumericComponentType,
126
+ elementCount: number = 1
127
+ ): BigTypedArray | null {
128
+ const numberOfComponents = ATTRIBUTE_TYPE_TO_COMPONENTS[attributeType];
129
+ const ArrayType = ATTRIBUTE_COMPONENT_TYPE_TO_ARRAY[componentType];
130
+ const size = ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE[componentType];
131
+ const length = elementCount * numberOfComponents;
132
+ const byteLength = length * size;
133
+ let buffer = data.buffer;
134
+ let offset = data.byteOffset;
135
+ if (offset % size !== 0) {
136
+ const bufferArray = new Uint8Array(buffer);
137
+ buffer = bufferArray.slice(offset, offset + byteLength).buffer;
138
+ offset = 0;
139
+ }
140
+ return new ArrayType(buffer, offset, length);
141
+ }
142
+
143
+ /**
144
+ * Processes data encoded in the texture associated with the primitive.
145
+ * If Ext_mesh_featues is combined with the Ext_structural_metadata, propertyTable will also be processed.
146
+ * @param scenegraph - Instance of the class for structured access to GLTF data.
147
+ * @param textureInfo - reference to the texture where extension data are stored.
148
+ * @param primitive - primitive object in the mesh
149
+ * @returns Array of data taken. Null if data can't be taken from the texture.
150
+ */
151
+ export function getPrimitiveTextureData(
152
+ scenegraph: GLTFScenegraph,
153
+ textureInfo: GLTFTextureInfoMetadata,
154
+ primitive: GLTFMeshPrimitive
155
+ ): number[] | null {
156
+ /*
157
+ texture.index is an index for the "textures" array.
158
+ The texture object referenced by this index looks like this:
159
+ {
160
+ "sampler": 0,
161
+ "source": 0
162
+ }
163
+ "sampler" is an index for the "samplers" array
164
+ "source" is an index for the "images" array that contains data. These data are stored in rgba channels of the image.
165
+
166
+ texture.texCoord is a number-suffix (like 1) for an attribute like "TEXCOORD_1" in meshes.primitives
167
+ The value of "TEXCOORD_1" is an accessor that is used to get coordinates. These coordinates are being used to get data from the image.
168
+ */
169
+ const json = scenegraph.gltf.json;
170
+
171
+ const texCoordAccessorKey = `TEXCOORD_${textureInfo.texCoord || 0}`;
172
+ const texCoordAccessorIndex = primitive.attributes[texCoordAccessorKey];
173
+ const texCoordBufferView = scenegraph.getBufferView(texCoordAccessorIndex);
174
+ const texCoordArray: Uint8Array = scenegraph.getTypedArrayForBufferView(texCoordBufferView);
175
+
176
+ // textureCoordinates array contains UV coordinates of the actual data stored in the texture
177
+ const textureCoordinates: Float32Array = new Float32Array(
178
+ texCoordArray.buffer,
179
+ texCoordArray.byteOffset,
180
+ texCoordArray.length / 4
181
+ );
182
+
183
+ const textureIndex: number = textureInfo.index;
184
+ const imageIndex = json.textures?.[textureIndex]?.source;
185
+ if (typeof imageIndex !== 'undefined') {
186
+ const mimeType = json.images?.[imageIndex]?.mimeType;
187
+ const parsedImage = scenegraph.gltf.images?.[imageIndex];
188
+ // Checking for width is to prevent handling Un-processed images (e.g. [analyze] stage, where loadImages option is set to false)
189
+ if (parsedImage && typeof parsedImage.width !== 'undefined') {
190
+ const textureData: number[] = [];
191
+ for (let index = 0; index < textureCoordinates.length; index += 2) {
192
+ const value = getImageValueByCoordinates(
193
+ parsedImage,
194
+ mimeType,
195
+ textureCoordinates,
196
+ index,
197
+ textureInfo.channels
198
+ );
199
+ textureData.push(value);
200
+ }
201
+ return textureData;
202
+ }
203
+ }
204
+ return null;
205
+ }
206
+
207
+ /**
208
+ * Puts property data to attributes.
209
+ * It creates corresponding buffer, bufferView and accessor
210
+ * so the data can be accessed like regular data stored in buffers.
211
+ * @param scenegraph - scenegraph object
212
+ * @param attributeName - name of the attribute
213
+ * @param propertyData - property data to store
214
+ * @param featureTable - an array where unique data from the property data are being stored
215
+ * @param primitive - primitive object
216
+ */
217
+ export function primitivePropertyDataToAttributes(
218
+ scenegraph: GLTFScenegraph,
219
+ attributeName: string,
220
+ propertyData: number[],
221
+ featureTable: number[],
222
+ primitive: GLTFMeshPrimitive
223
+ ): void {
224
+ if (propertyData === null) return;
225
+ /*
226
+ featureTable will contain unique values, e.g.
227
+ propertyData = [24, 35, 28, 24]
228
+ featureTable = [24, 35, 28]
229
+ featureIndices will contain indices that refer featureTextureTable, e.g.
230
+ featureIndices = [0, 1, 2, 0]
231
+ */
232
+ const featureIndices: number[] = [];
233
+ for (const texelData of propertyData) {
234
+ let index = featureTable.findIndex((item) => item === texelData);
235
+ if (index === -1) {
236
+ index = featureTable.push(texelData) - 1;
237
+ }
238
+ featureIndices.push(index);
239
+ }
240
+ const typedArray = new Uint32Array(featureIndices);
241
+ const bufferIndex =
242
+ scenegraph.gltf.buffers.push({
243
+ arrayBuffer: typedArray.buffer,
244
+ byteOffset: typedArray.byteOffset,
245
+ byteLength: typedArray.byteLength
246
+ }) - 1;
247
+ const bufferViewIndex = scenegraph.addBufferView(typedArray, bufferIndex, 0);
248
+ const accessorIndex = scenegraph.addAccessor(bufferViewIndex, {
249
+ size: 1,
250
+ componentType: getComponentTypeFromArray(typedArray),
251
+ count: typedArray.length
252
+ });
253
+ primitive.attributes[attributeName] = accessorIndex;
254
+ }
255
+
256
+ /**
257
+ * Gets the value from the texture by coordinates provided.
258
+ * @param parsedImage - image where the data are stored.
259
+ * @param mimeType - MIME type
260
+ * @param textureCoordinates - uv coordinates to access data in the image.
261
+ * @param index - index of uv coordinates in the array textureCoordinates
262
+ * @param channels - image channels where data are stored. Channels of an RGBA texture are numbered 0..3 respectively.
263
+ * @returns Value taken from the image.
264
+ */
265
+ function getImageValueByCoordinates(
266
+ parsedImage: ImageType,
267
+ mimeType: string | undefined,
268
+ textureCoordinates: Float32Array,
269
+ index: number,
270
+ channels: number[] = [0]
271
+ ) {
272
+ const CHANNELS_MAP = [
273
+ {offset: 0, shift: 0},
274
+ {offset: 1, shift: 8},
275
+ {offset: 2, shift: 16},
276
+ {offset: 3, shift: 24}
277
+ ];
278
+
279
+ const u = textureCoordinates[index];
280
+ const v = textureCoordinates[index + 1];
281
+
282
+ let components = 1;
283
+ if (mimeType && (mimeType.indexOf('image/jpeg') !== -1 || mimeType.indexOf('image/png') !== -1))
284
+ components = 4;
285
+ const offset = coordinatesToOffset(u, v, parsedImage, components);
286
+ let value: number = 0;
287
+ for (const c of channels) {
288
+ const map = CHANNELS_MAP[c];
289
+ const imageOffset = offset + map.offset;
290
+ const imageData = getImageData(parsedImage);
291
+ if (imageData.data.length <= imageOffset) {
292
+ throw new Error(`${imageData.data.length} <= ${imageOffset}`);
293
+ }
294
+ const imageValue = imageData.data[imageOffset];
295
+ value |= imageValue << map.shift;
296
+ }
297
+ return value;
298
+ }
299
+
300
+ /**
301
+ * Retrieves the offset in the image where the data are stored
302
+ * @param u - u-coordinate
303
+ * @param v - v-coordinate
304
+ * @param parsedImage - image where the data are stored
305
+ * @param componentsCount - number of components the data consists of.
306
+ * @returns offset in the image where the data are stored
307
+ */
308
+ function coordinatesToOffset(
309
+ u: number,
310
+ v: number,
311
+ parsedImage: any,
312
+ componentsCount: number = 1
313
+ ): number {
314
+ const w = parsedImage.width;
315
+ const iX = emod(u) * (w - 1);
316
+ const indX = Math.round(iX);
317
+
318
+ const h = parsedImage.height;
319
+ const iY = emod(v) * (h - 1);
320
+ const indY = Math.round(iY);
321
+ const components = parsedImage.components ? parsedImage.components : componentsCount;
322
+ // components is a number of channels in the image
323
+ const offset = (indY * w + indX) * components;
324
+ return offset;
325
+ }
@@ -0,0 +1,48 @@
1
+ import {GLTFTextureInfoMetadata} from './gltf-json-schema';
2
+ /* eslint-disable camelcase */
3
+
4
+ /**
5
+ * EXT_mesh_features extension types
6
+ * This is a primitive-level extension
7
+ * @see https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_mesh_features
8
+ * or https://github.com/CesiumGS/glTF/blob/c38f7f37e894004353c15cd0481bc5b7381ce841/extensions/2.0/Vendor/EXT_mesh_features/schema/mesh.primitive.EXT_mesh_features.schema.json
9
+ * An object describing feature IDs for a mesh primitive.
10
+ */
11
+ export type GLTF_EXT_mesh_features = {
12
+ /** An array of feature ID sets. */
13
+ featureIds: GLTF_EXT_mesh_features_featureId[];
14
+ extensions?: Record<string, unknown>;
15
+ extras?: unknown;
16
+ /** For internal usage */
17
+ dataAttributeNames?: string[];
18
+ };
19
+
20
+ /**
21
+ * @see https://github.com/CesiumGS/glTF/blob/c38f7f37e894004353c15cd0481bc5b7381ce841/extensions/2.0/Vendor/EXT_mesh_features/schema/featureId.schema.json
22
+ * Feature IDs stored in an attribute or texture.
23
+ */
24
+ export type GLTF_EXT_mesh_features_featureId = {
25
+ /** The number of unique features in the attribute or texture. */
26
+ featureCount: number;
27
+ /** A value that indicates that no feature is associated with this vertex or texel. */
28
+ nullFeatureId?: number;
29
+ /** A label assigned to this feature ID set. Labels must be alphanumeric identifiers matching the regular expression `^[a-zA-Z_][a-zA-Z0-9_]*$`. */
30
+ label?: string;
31
+ /**
32
+ * An attribute containing feature IDs.
33
+ * When `attribute` and `texture` are omitted the feature IDs are assigned to vertices by their index.
34
+ * @see https://github.com/CesiumGS/glTF/blob/3d-tiles-next/extensions/2.0/Vendor/EXT_mesh_features/schema/featureIdAttribute.schema.json
35
+ * An integer value used to construct a string in the format `_FEATURE_ID_<set index>` which is a reference to a key in `mesh.primitives.attributes`
36
+ * (e.g. a value of `0` corresponds to `_FEATURE_ID_0`).
37
+ */
38
+ attribute?: number;
39
+ /** A texture containing feature IDs. */
40
+ texture?: GLTFTextureInfoMetadata;
41
+ /** The index of the property table containing per-feature property values. Only applicable when using the `EXT_structural_metadata` extension. */
42
+ propertyTable?: number;
43
+ extensions?: Record<string, unknown>;
44
+ extras?: unknown;
45
+
46
+ /** For internal usage */
47
+ data?: unknown;
48
+ };