@loaders.gl/gltf 4.0.0-alpha.23 → 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 (85) hide show
  1. package/dist/dist.min.js +148 -102
  2. package/dist/es5/lib/extensions/EXT_mesh_features.js +3 -3
  3. package/dist/es5/lib/extensions/EXT_mesh_features.js.map +1 -1
  4. package/dist/es5/lib/extensions/EXT_structural_metadata.js +88 -86
  5. package/dist/es5/lib/extensions/EXT_structural_metadata.js.map +1 -1
  6. package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js +26 -13
  7. package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -1
  8. package/dist/es5/lib/extensions/{data-processing.js → utils/3d-tiles-utils.js} +25 -6
  9. package/dist/es5/lib/extensions/utils/3d-tiles-utils.js.map +1 -0
  10. package/dist/es5/lib/types/gltf-ext-structural-metadata-schema.js.map +1 -1
  11. package/dist/es5/lib/types/gltf-json-schema.js.map +1 -1
  12. package/dist/es5/lib/utils/version.js +1 -1
  13. package/dist/esm/lib/extensions/EXT_mesh_features.js +1 -1
  14. package/dist/esm/lib/extensions/EXT_mesh_features.js.map +1 -1
  15. package/dist/esm/lib/extensions/EXT_structural_metadata.js +85 -81
  16. package/dist/esm/lib/extensions/EXT_structural_metadata.js.map +1 -1
  17. package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js +26 -13
  18. package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -1
  19. package/dist/esm/lib/extensions/{data-processing.js → utils/3d-tiles-utils.js} +24 -6
  20. package/dist/esm/lib/extensions/utils/3d-tiles-utils.js.map +1 -0
  21. package/dist/esm/lib/types/gltf-ext-structural-metadata-schema.js.map +1 -1
  22. package/dist/esm/lib/types/gltf-json-schema.js.map +1 -1
  23. package/dist/esm/lib/utils/version.js +1 -1
  24. package/dist/lib/extensions/EXT_structural_metadata.d.ts +3 -3
  25. package/dist/lib/extensions/EXT_structural_metadata.d.ts.map +1 -1
  26. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts.map +1 -1
  27. package/dist/lib/extensions/utils/3d-tiles-utils.d.ts +52 -0
  28. package/dist/lib/extensions/utils/3d-tiles-utils.d.ts.map +1 -0
  29. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts +4 -4
  30. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts.map +1 -1
  31. package/dist/lib/types/gltf-json-schema.d.ts +1 -1
  32. package/dist/lib/types/gltf-json-schema.d.ts.map +1 -1
  33. package/package.json +6 -6
  34. package/src/lib/extensions/EXT_mesh_features.ts +1 -1
  35. package/src/lib/extensions/EXT_structural_metadata.ts +267 -200
  36. package/src/lib/extensions/deprecated/EXT_feature_metadata.ts +123 -19
  37. package/src/lib/extensions/{data-processing.ts → utils/3d-tiles-utils.ts} +95 -34
  38. package/src/lib/types/gltf-ext-structural-metadata-schema.ts +6 -4
  39. package/src/lib/types/gltf-json-schema.ts +1 -1
  40. package/dist/bundle.js +0 -5
  41. package/dist/es5/lib/extensions/data-processing.js.map +0 -1
  42. package/dist/esm/lib/extensions/data-processing.js.map +0 -1
  43. package/dist/glb-loader.js +0 -34
  44. package/dist/glb-writer.js +0 -35
  45. package/dist/gltf-loader.js +0 -50
  46. package/dist/gltf-writer.js +0 -32
  47. package/dist/index.js +0 -34
  48. package/dist/lib/api/gltf-extensions.js +0 -88
  49. package/dist/lib/api/gltf-scenegraph.js +0 -580
  50. package/dist/lib/api/normalize-gltf-v1.js +0 -299
  51. package/dist/lib/api/post-process-gltf.js +0 -433
  52. package/dist/lib/encoders/encode-glb.js +0 -72
  53. package/dist/lib/encoders/encode-gltf.js +0 -32
  54. package/dist/lib/extensions/EXT_mesh_features.js +0 -89
  55. package/dist/lib/extensions/EXT_meshopt_compression.js +0 -41
  56. package/dist/lib/extensions/EXT_structural_metadata.js +0 -504
  57. package/dist/lib/extensions/EXT_texture_webp.js +0 -36
  58. package/dist/lib/extensions/KHR_binary_gltf.js +0 -39
  59. package/dist/lib/extensions/KHR_draco_mesh_compression.js +0 -137
  60. package/dist/lib/extensions/KHR_texture_basisu.js +0 -29
  61. package/dist/lib/extensions/KHR_texture_transform.js +0 -227
  62. package/dist/lib/extensions/data-processing.d.ts +0 -34
  63. package/dist/lib/extensions/data-processing.d.ts.map +0 -1
  64. package/dist/lib/extensions/data-processing.js +0 -212
  65. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +0 -282
  66. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +0 -59
  67. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +0 -44
  68. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +0 -79
  69. package/dist/lib/gltf-utils/get-typed-array.js +0 -41
  70. package/dist/lib/gltf-utils/gltf-attribute-utils.js +0 -73
  71. package/dist/lib/gltf-utils/gltf-constants.js +0 -43
  72. package/dist/lib/gltf-utils/gltf-utils.js +0 -90
  73. package/dist/lib/gltf-utils/resolve-url.js +0 -18
  74. package/dist/lib/parsers/parse-glb.js +0 -166
  75. package/dist/lib/parsers/parse-gltf.js +0 -185
  76. package/dist/lib/types/glb-types.js +0 -2
  77. package/dist/lib/types/gltf-ext-mesh-features-schema.js +0 -2
  78. package/dist/lib/types/gltf-ext-structural-metadata-schema.js +0 -2
  79. package/dist/lib/types/gltf-json-schema.js +0 -4
  80. package/dist/lib/types/gltf-postprocessed-schema.js +0 -4
  81. package/dist/lib/types/gltf-types.js +0 -3
  82. package/dist/lib/utils/assert.js +0 -12
  83. package/dist/lib/utils/version.js +0 -7
  84. package/dist/meshopt/meshopt-decoder.js +0 -118
  85. package/dist/webp/webp.js +0 -38
@@ -1,137 +0,0 @@
1
- "use strict";
2
- // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression
3
- // Only TRIANGLES: 0x0004 and TRIANGLE_STRIP: 0x0005 are supported
4
- /* eslint-disable camelcase */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.encode = exports.decode = exports.preprocess = exports.name = void 0;
7
- const loader_utils_1 = require("@loaders.gl/loader-utils");
8
- const draco_1 = require("@loaders.gl/draco");
9
- const gltf_scenegraph_1 = require("../api/gltf-scenegraph");
10
- const gltf_attribute_utils_1 = require("../gltf-utils/gltf-attribute-utils");
11
- const KHR_DRACO_MESH_COMPRESSION = 'KHR_draco_mesh_compression';
12
- /** Extension name */
13
- exports.name = KHR_DRACO_MESH_COMPRESSION;
14
- function preprocess(gltfData, options, context) {
15
- const scenegraph = new gltf_scenegraph_1.GLTFScenegraph(gltfData);
16
- for (const primitive of makeMeshPrimitiveIterator(scenegraph)) {
17
- if (scenegraph.getObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION)) {
18
- // TODO - Remove fallback accessors to make sure we don't load unnecessary buffers
19
- }
20
- }
21
- }
22
- exports.preprocess = preprocess;
23
- async function decode(gltfData, options, context) {
24
- if (!options?.gltf?.decompressMeshes) {
25
- return;
26
- }
27
- const scenegraph = new gltf_scenegraph_1.GLTFScenegraph(gltfData);
28
- const promises = [];
29
- for (const primitive of makeMeshPrimitiveIterator(scenegraph)) {
30
- if (scenegraph.getObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION)) {
31
- promises.push(decompressPrimitive(scenegraph, primitive, options, context));
32
- }
33
- }
34
- // Decompress meshes in parallel
35
- await Promise.all(promises);
36
- // We have now decompressed all primitives, so remove the top-level extension
37
- scenegraph.removeExtension(KHR_DRACO_MESH_COMPRESSION);
38
- }
39
- exports.decode = decode;
40
- function encode(gltfData, options = {}) {
41
- const scenegraph = new gltf_scenegraph_1.GLTFScenegraph(gltfData);
42
- for (const mesh of scenegraph.json.meshes || []) {
43
- // eslint-disable-next-line camelcase
44
- // @ts-ignore
45
- compressMesh(mesh, options);
46
- // NOTE: Only add the extension if something was actually compressed
47
- scenegraph.addRequiredExtension(KHR_DRACO_MESH_COMPRESSION);
48
- }
49
- }
50
- exports.encode = encode;
51
- // DECODE
52
- // Unpacks one mesh primitive and removes the extension from the primitive
53
- // DracoDecoder needs to be imported and registered by app
54
- // Returns: Promise that resolves when all pending draco decoder jobs for this mesh complete
55
- // TODO - Implement fallback behavior per KHR_DRACO_MESH_COMPRESSION spec
56
- async function decompressPrimitive(scenegraph, primitive, options, context) {
57
- const dracoExtension = scenegraph.getObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION);
58
- if (!dracoExtension) {
59
- return;
60
- }
61
- const buffer = scenegraph.getTypedArrayForBufferView(dracoExtension.bufferView);
62
- // TODO - parse does not yet deal well with byte offsets embedded in typed arrays. Copy buffer
63
- // TODO - remove when `parse` is fixed to handle `byteOffset`s
64
- const bufferCopy = (0, loader_utils_1.sliceArrayBuffer)(buffer.buffer, buffer.byteOffset); // , buffer.byteLength);
65
- const dracoOptions = { ...options };
66
- // TODO - remove hack: The entire tileset might be included, too expensive to serialize
67
- delete dracoOptions['3d-tiles'];
68
- const decodedData = await (0, loader_utils_1.parseFromContext)(bufferCopy, draco_1.DracoLoader, dracoOptions, context);
69
- const decodedAttributes = (0, gltf_attribute_utils_1.getGLTFAccessors)(decodedData.attributes);
70
- // Restore min/max values
71
- for (const [attributeName, decodedAttribute] of Object.entries(decodedAttributes)) {
72
- if (attributeName in primitive.attributes) {
73
- const accessorIndex = primitive.attributes[attributeName];
74
- const accessor = scenegraph.getAccessor(accessorIndex);
75
- if (accessor?.min && accessor?.max) {
76
- decodedAttribute.min = accessor.min;
77
- decodedAttribute.max = accessor.max;
78
- }
79
- }
80
- }
81
- // @ts-ignore
82
- primitive.attributes = decodedAttributes;
83
- if (decodedData.indices) {
84
- // @ts-ignore
85
- primitive.indices = (0, gltf_attribute_utils_1.getGLTFAccessor)(decodedData.indices);
86
- }
87
- // Extension has been processed, delete it
88
- scenegraph.removeObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION);
89
- checkPrimitive(primitive);
90
- }
91
- // ENCODE
92
- // eslint-disable-next-line max-len
93
- // Only TRIANGLES: 0x0004 and TRIANGLE_STRIP: 0x0005 are supported
94
- function compressMesh(attributes, indices, mode = 4, options, context) {
95
- if (!options.DracoWriter) {
96
- throw new Error('options.gltf.DracoWriter not provided');
97
- }
98
- // TODO - use DracoWriter using encode w/ registered DracoWriter...
99
- const compressedData = options.DracoWriter.encodeSync({ attributes });
100
- // Draco compression may change the order and number of vertices in a mesh.
101
- // To satisfy the requirement that accessors properties be correct for both
102
- // compressed and uncompressed data, generators should create uncompressed
103
- // attributes and indices using data that has been decompressed from the Draco buffer,
104
- // rather than the original source data.
105
- // @ts-ignore TODO this needs to be fixed
106
- const decodedData = context?.parseSync?.({ attributes });
107
- const fauxAccessors = options._addFauxAttributes(decodedData.attributes);
108
- const bufferViewIndex = options.addBufferView(compressedData);
109
- const glTFMesh = {
110
- primitives: [
111
- {
112
- attributes: fauxAccessors,
113
- mode,
114
- extensions: {
115
- [KHR_DRACO_MESH_COMPRESSION]: {
116
- bufferView: bufferViewIndex,
117
- attributes: fauxAccessors // TODO - verify with spec
118
- }
119
- }
120
- }
121
- ]
122
- };
123
- return glTFMesh;
124
- }
125
- // UTILS
126
- function checkPrimitive(primitive) {
127
- if (!primitive.attributes && Object.keys(primitive.attributes).length > 0) {
128
- throw new Error('glTF: Empty primitive detected: Draco decompression failure?');
129
- }
130
- }
131
- function* makeMeshPrimitiveIterator(scenegraph) {
132
- for (const mesh of scenegraph.json.meshes || []) {
133
- for (const primitive of mesh.primitives) {
134
- yield primitive;
135
- }
136
- }
137
- }
@@ -1,29 +0,0 @@
1
- "use strict";
2
- // GLTF EXTENSION: KHR_texture_basisu
3
- // https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_texture_basisu
4
- /* eslint-disable camelcase */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.preprocess = exports.name = void 0;
7
- const gltf_scenegraph_1 = require("../api/gltf-scenegraph");
8
- const KHR_TEXTURE_BASISU = 'KHR_texture_basisu';
9
- /** Extension name */
10
- exports.name = KHR_TEXTURE_BASISU;
11
- /**
12
- * Replaces a texture source reference with the extension texture
13
- * Done in preprocess() to prevent load of default image
14
- */
15
- function preprocess(gltfData, options) {
16
- const scene = new gltf_scenegraph_1.GLTFScenegraph(gltfData);
17
- const { json } = scene;
18
- for (const texture of json.textures || []) {
19
- const extension = scene.getObjectExtension(texture, KHR_TEXTURE_BASISU);
20
- if (extension) {
21
- // TODO - if multiple texture extensions are present which one wins?
22
- texture.source = extension.source;
23
- scene.removeObjectExtension(texture, KHR_TEXTURE_BASISU);
24
- }
25
- }
26
- // Remove the top-level extension
27
- scene.removeExtension(KHR_TEXTURE_BASISU);
28
- }
29
- exports.preprocess = preprocess;
@@ -1,227 +0,0 @@
1
- "use strict";
2
- /**
3
- * https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_texture_transform/README.md
4
- */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.decode = exports.name = void 0;
7
- const core_1 = require("@math.gl/core");
8
- const gltf_utils_1 = require("../gltf-utils/gltf-utils");
9
- const gltf_constants_1 = require("../gltf-utils/gltf-constants");
10
- const gltf_scenegraph_1 = require("../api/gltf-scenegraph");
11
- /** Extension name */
12
- const EXT_MESHOPT_TRANSFORM = 'KHR_texture_transform';
13
- exports.name = EXT_MESHOPT_TRANSFORM;
14
- const scratchVector = new core_1.Vector3();
15
- const scratchRotationMatrix = new core_1.Matrix3();
16
- const scratchScaleMatrix = new core_1.Matrix3();
17
- /**
18
- * The extension entry to process the transformation
19
- * @param gltfData gltf buffers and json
20
- * @param options GLTFLoader options
21
- */
22
- async function decode(gltfData, options) {
23
- const gltfScenegraph = new gltf_scenegraph_1.GLTFScenegraph(gltfData);
24
- const hasExtension = gltfScenegraph.hasExtension(EXT_MESHOPT_TRANSFORM);
25
- if (!hasExtension || !options.gltf?.loadBuffers) {
26
- return;
27
- }
28
- const materials = gltfData.json.materials || [];
29
- for (let i = 0; i < materials.length; i++) {
30
- transformTexCoords(i, gltfData);
31
- }
32
- }
33
- exports.decode = decode;
34
- /**
35
- * Transform TEXCOORD by material
36
- * @param materialIndex processing material index
37
- * @param gltfData gltf buffers and json
38
- */
39
- function transformTexCoords(materialIndex, gltfData) {
40
- // Save processed texCoords in order no to process the same twice
41
- const processedTexCoords = [];
42
- const material = gltfData.json.materials?.[materialIndex];
43
- const baseColorTexture = material?.pbrMetallicRoughness?.baseColorTexture;
44
- if (baseColorTexture) {
45
- transformPrimitives(gltfData, materialIndex, baseColorTexture, processedTexCoords);
46
- }
47
- const emisiveTexture = material?.emissiveTexture;
48
- if (emisiveTexture) {
49
- transformPrimitives(gltfData, materialIndex, emisiveTexture, processedTexCoords);
50
- }
51
- const normalTexture = material?.normalTexture;
52
- if (normalTexture) {
53
- transformPrimitives(gltfData, materialIndex, normalTexture, processedTexCoords);
54
- }
55
- const occlusionTexture = material?.occlusionTexture;
56
- if (occlusionTexture) {
57
- transformPrimitives(gltfData, materialIndex, occlusionTexture, processedTexCoords);
58
- }
59
- const metallicRoughnessTexture = material?.pbrMetallicRoughness?.metallicRoughnessTexture;
60
- if (metallicRoughnessTexture) {
61
- transformPrimitives(gltfData, materialIndex, metallicRoughnessTexture, processedTexCoords);
62
- }
63
- }
64
- /**
65
- * Transform primitives of the particular material
66
- * @param gltfData gltf data
67
- * @param materialIndex primitives with this material will be transformed
68
- * @param texture texture object
69
- * @param processedTexCoords storage to save already processed texCoords
70
- */
71
- function transformPrimitives(gltfData, materialIndex, texture, processedTexCoords) {
72
- const transformParameters = getTransformParameters(texture, processedTexCoords);
73
- if (!transformParameters) {
74
- return;
75
- }
76
- const meshes = gltfData.json.meshes || [];
77
- for (const mesh of meshes) {
78
- for (const primitive of mesh.primitives) {
79
- const material = primitive.material;
80
- if (Number.isFinite(material) && materialIndex === material) {
81
- transformPrimitive(gltfData, primitive, transformParameters);
82
- }
83
- }
84
- }
85
- }
86
- /**
87
- * Get parameters for TEXCOORD transformation
88
- * @param texture texture object
89
- * @param processedTexCoords storage to save already processed texCoords
90
- * @returns texCoord couple and transformation matrix
91
- */
92
- function getTransformParameters(texture, processedTexCoords) {
93
- const textureInfo = texture.extensions?.[EXT_MESHOPT_TRANSFORM];
94
- const { texCoord: originalTexCoord = 0 } = texture;
95
- // If texCoord is not set in the extension, original attribute data will be replaced
96
- const { texCoord = originalTexCoord } = textureInfo;
97
- // Make sure that couple [originalTexCoord, extensionTexCoord] is not processed twice
98
- const isProcessed = processedTexCoords.findIndex(([original, newTexCoord]) => original === originalTexCoord && newTexCoord === texCoord) !== -1;
99
- if (!isProcessed) {
100
- const matrix = makeTransformationMatrix(textureInfo);
101
- if (originalTexCoord !== texCoord) {
102
- texture.texCoord = texCoord;
103
- }
104
- processedTexCoords.push([originalTexCoord, texCoord]);
105
- return { originalTexCoord, texCoord, matrix };
106
- }
107
- return null;
108
- }
109
- /**
110
- * Transform `TEXCOORD_0` attribute in the primitive
111
- * @param gltfData gltf data
112
- * @param primitive primitive object
113
- * @param transformParameters texCoord couple and transformation matrix
114
- */
115
- function transformPrimitive(gltfData, primitive, transformParameters) {
116
- const { originalTexCoord, texCoord, matrix } = transformParameters;
117
- const texCoordAccessor = primitive.attributes[`TEXCOORD_${originalTexCoord}`];
118
- if (Number.isFinite(texCoordAccessor)) {
119
- // Get accessor of the `TEXCOORD_0` attribute
120
- const accessor = gltfData.json.accessors?.[texCoordAccessor];
121
- if (accessor && accessor.bufferView) {
122
- // Get `bufferView` of the `accessor`
123
- const bufferView = gltfData.json.bufferViews?.[accessor.bufferView];
124
- if (bufferView) {
125
- // Get `arrayBuffer` the `bufferView` look at
126
- const { arrayBuffer, byteOffset: bufferByteOffset } = gltfData.buffers[bufferView.buffer];
127
- // Resulting byteOffset is sum of the buffer, accessor and bufferView byte offsets
128
- const byteOffset = (bufferByteOffset || 0) + (accessor.byteOffset || 0) + (bufferView.byteOffset || 0);
129
- // Deduce TypedArray type and its length from `accessor` and `bufferView` data
130
- const { ArrayType, length } = (0, gltf_utils_1.getAccessorArrayTypeAndLength)(accessor, bufferView);
131
- // Number of bytes each component occupies
132
- const bytes = gltf_constants_1.BYTES[accessor.componentType];
133
- // Number of components. For the `TEXCOORD_0` with `VEC2` type, it must return 2
134
- const components = gltf_constants_1.COMPONENTS[accessor.type];
135
- // Multiplier to calculate the address of the `TEXCOORD_0` element in the arrayBuffer
136
- const elementAddressScale = bufferView.byteStride || bytes * components;
137
- // Data transform to Float32Array
138
- const result = new Float32Array(length);
139
- for (let i = 0; i < accessor.count; i++) {
140
- // Take [u, v] couple from the arrayBuffer
141
- const uv = new ArrayType(arrayBuffer, byteOffset + i * elementAddressScale, 2);
142
- // Set and transform Vector3 per https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_texture_transform#overview
143
- scratchVector.set(uv[0], uv[1], 1);
144
- scratchVector.transformByMatrix3(matrix);
145
- // Save result in Float32Array
146
- result.set([scratchVector[0], scratchVector[1]], i * components);
147
- }
148
- // If texCoord the same, replace gltf structural data
149
- if (originalTexCoord === texCoord) {
150
- updateGltf(accessor, bufferView, gltfData.buffers, result);
151
- }
152
- else {
153
- // If texCoord change, create new attribute
154
- createAttribute(texCoord, accessor, primitive, gltfData, result);
155
- }
156
- }
157
- }
158
- }
159
- }
160
- /**
161
- * Update GLTF structural objects with new data as we create new `Float32Array` for `TEXCOORD_0`.
162
- * @param accessor accessor to change
163
- * @param bufferView bufferView to change
164
- * @param buffers binary buffers
165
- * @param newTexcoordArray typed array with data after transformation
166
- */
167
- function updateGltf(accessor, bufferView, buffers, newTexCoordArray) {
168
- accessor.componentType = 5126;
169
- buffers.push({
170
- arrayBuffer: newTexCoordArray.buffer,
171
- byteOffset: 0,
172
- byteLength: newTexCoordArray.buffer.byteLength
173
- });
174
- bufferView.buffer = buffers.length - 1;
175
- bufferView.byteLength = newTexCoordArray.buffer.byteLength;
176
- bufferView.byteOffset = 0;
177
- delete bufferView.byteStride;
178
- }
179
- /**
180
- *
181
- * @param newTexCoord new `texCoord` value
182
- * @param originalAccessor original accessor object, that store data before transformation
183
- * @param primitive primitive object
184
- * @param gltfData gltf data
185
- * @param newTexCoordArray typed array with data after transformation
186
- * @returns
187
- */
188
- function createAttribute(newTexCoord, originalAccessor, primitive, gltfData, newTexCoordArray) {
189
- gltfData.buffers.push({
190
- arrayBuffer: newTexCoordArray.buffer,
191
- byteOffset: 0,
192
- byteLength: newTexCoordArray.buffer.byteLength
193
- });
194
- const bufferViews = gltfData.json.bufferViews;
195
- if (!bufferViews) {
196
- return;
197
- }
198
- bufferViews.push({
199
- buffer: gltfData.buffers.length - 1,
200
- byteLength: newTexCoordArray.buffer.byteLength,
201
- byteOffset: 0
202
- });
203
- const accessors = gltfData.json.accessors;
204
- if (!accessors) {
205
- return;
206
- }
207
- accessors.push({
208
- bufferView: bufferViews?.length - 1,
209
- byteOffset: 0,
210
- componentType: 5126,
211
- count: originalAccessor.count,
212
- type: 'VEC2'
213
- });
214
- primitive.attributes[`TEXCOORD_${newTexCoord}`] = accessors.length - 1;
215
- }
216
- /**
217
- * Construct transformation matrix from the extension data (transition, rotation, scale)
218
- * @param extensionData extension data
219
- * @returns transformation matrix
220
- */
221
- function makeTransformationMatrix(extensionData) {
222
- const { offset = [0, 0], rotation = 0, scale = [1, 1] } = extensionData;
223
- const translationMatrix = new core_1.Matrix3().set(1, 0, 0, 0, 1, 0, offset[0], offset[1], 1);
224
- const rotationMatrix = scratchRotationMatrix.set(Math.cos(rotation), Math.sin(rotation), 0, -Math.sin(rotation), Math.cos(rotation), 0, 0, 0, 1);
225
- const scaleMatrix = scratchScaleMatrix.set(scale[0], 0, 0, 0, scale[1], 0, 0, 0, 1);
226
- return translationMatrix.multiplyRight(rotationMatrix).multiplyRight(scaleMatrix);
227
- }
@@ -1,34 +0,0 @@
1
- import type { GLTFTextureInfoMetadata, GLTFMeshPrimitive } from '../types/gltf-json-schema';
2
- import type { TypedArray } from '@loaders.gl/schema';
3
- import { GLTFScenegraph } from '../api/gltf-scenegraph';
4
- export declare function getArrayElementByteSize(attributeType: any, componentType: any): number;
5
- /**
6
- * Converts raw bytes that are in the buffer to an array of the type defined by the schema.
7
- * @param {Uint8Array} typedArray - raw bytes in the buffer
8
- * @param {string} attributeType - SCALAR, VECN, MATN
9
- * @param {string} componentType - type of the component in elements, e.g. 'UINT8' or 'FLOAT32'
10
- * @param {number} elementCount - number of elements in the array. Default value is 1.
11
- * @returns {TypedArray} Data array
12
- */
13
- export declare function convertRawBufferToMetadataArray(typedArray: Uint8Array, attributeType: string, componentType: string, elementCount?: number): TypedArray;
14
- /**
15
- * Processes data encoded in the texture associated with the primitive.
16
- * If Ext_mesh_featues is combined with the Ext_structural_metadata, propertyTable will also be processed.
17
- * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
18
- * @param {GLTFTextureInfoMetadata} textureInfo - reference to the texture where extension data are stored.
19
- * @param {GLTFMeshPrimitive} primitive - primitive object in the mesh
20
- * @returns {number[] | null} Array of data taken. Null if data can't be taken from the texture.
21
- */
22
- export declare function getPrimitiveTextureData(scenegraph: GLTFScenegraph, textureInfo: GLTFTextureInfoMetadata, primitive: GLTFMeshPrimitive): number[] | null;
23
- /**
24
- * Puts property data to attributes.
25
- * It creates corresponding buffer, bufferView and accessor
26
- * so the data can be accessed like regular data stored in buffers.
27
- * @param {GLTFScenegraph} scenegraph - scenegraph object
28
- * @param {string} attributeName - name of the attribute
29
- * @param {number[]} propertyData - property data to store
30
- * @param {number[]} featureTable - an array where unique data from the property data are being stored
31
- * @param {GLTFMeshPrimitive} primitive - primitive object
32
- */
33
- export declare function primitivePropertyDataToAttributes(scenegraph: GLTFScenegraph, attributeName: string, propertyData: number[], featureTable: number[], primitive: GLTFMeshPrimitive): void;
34
- //# sourceMappingURL=data-processing.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"data-processing.d.ts","sourceRoot":"","sources":["../../../src/lib/extensions/data-processing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,uBAAuB,EAAE,iBAAiB,EAAC,MAAM,2BAA2B,CAAC;AAC1F,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,oBAAoB,CAAC;AAGnD,OAAO,EAAC,cAAc,EAAC,MAAM,wBAAwB,CAAC;AA4CtD,wBAAgB,uBAAuB,CAAC,aAAa,KAAA,EAAE,aAAa,KAAA,GAAG,MAAM,CAK5E;AAED;;;;;;;GAOG;AACH,wBAAgB,+BAA+B,CAC7C,UAAU,EAAE,UAAU,EACtB,aAAa,EAAE,MAAM,EACrB,aAAa,EAAE,MAAM,EACrB,YAAY,GAAE,MAAU,GACvB,UAAU,CAWZ;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,UAAU,EAAE,cAAc,EAC1B,WAAW,EAAE,uBAAuB,EACpC,SAAS,EAAE,iBAAiB,GAC3B,MAAM,EAAE,GAAG,IAAI,CAkDjB;AAED;;;;;;;;;GASG;AACH,wBAAgB,iCAAiC,CAC/C,UAAU,EAAE,cAAc,EAC1B,aAAa,EAAE,MAAM,EACrB,YAAY,EAAE,MAAM,EAAE,EACtB,YAAY,EAAE,MAAM,EAAE,EACtB,SAAS,EAAE,iBAAiB,GAC3B,IAAI,CA+BN"}
@@ -1,212 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.primitivePropertyDataToAttributes = exports.getPrimitiveTextureData = exports.convertRawBufferToMetadataArray = exports.getArrayElementByteSize = void 0;
4
- const gltf_utils_1 = require("../gltf-utils/gltf-utils");
5
- const images_1 = require("@loaders.gl/images");
6
- const math_1 = require("@loaders.gl/math");
7
- const ATTRIBUTE_TYPE_TO_COMPONENTS = {
8
- SCALAR: 1,
9
- VEC2: 2,
10
- VEC3: 3,
11
- VEC4: 4,
12
- MAT2: 4,
13
- MAT3: 9,
14
- MAT4: 16,
15
- BOOLEAN: 1,
16
- STRING: 1,
17
- ENUM: 1
18
- };
19
- const ATTRIBUTE_COMPONENT_TYPE_TO_ARRAY = {
20
- INT8: Int8Array,
21
- UINT8: Uint8Array,
22
- INT16: Int16Array,
23
- UINT16: Uint16Array,
24
- INT32: Int32Array,
25
- UINT32: Uint32Array,
26
- INT64: BigInt64Array,
27
- UINT64: BigUint64Array,
28
- FLOAT32: Float32Array,
29
- FLOAT64: Float64Array
30
- };
31
- const ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE = {
32
- INT8: 1,
33
- UINT8: 1,
34
- INT16: 2,
35
- UINT16: 2,
36
- INT32: 4,
37
- UINT32: 4,
38
- INT64: 8,
39
- UINT64: 8,
40
- FLOAT32: 4,
41
- FLOAT64: 8
42
- };
43
- function getArrayElementByteSize(attributeType, componentType) {
44
- return (ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE[componentType] *
45
- ATTRIBUTE_TYPE_TO_COMPONENTS[attributeType]);
46
- }
47
- exports.getArrayElementByteSize = getArrayElementByteSize;
48
- /**
49
- * Converts raw bytes that are in the buffer to an array of the type defined by the schema.
50
- * @param {Uint8Array} typedArray - raw bytes in the buffer
51
- * @param {string} attributeType - SCALAR, VECN, MATN
52
- * @param {string} componentType - type of the component in elements, e.g. 'UINT8' or 'FLOAT32'
53
- * @param {number} elementCount - number of elements in the array. Default value is 1.
54
- * @returns {TypedArray} Data array
55
- */
56
- function convertRawBufferToMetadataArray(typedArray, attributeType, componentType, elementCount = 1) {
57
- const numberOfComponents = ATTRIBUTE_TYPE_TO_COMPONENTS[attributeType];
58
- const ArrayType = ATTRIBUTE_COMPONENT_TYPE_TO_ARRAY[componentType];
59
- const length = elementCount * numberOfComponents;
60
- const size = ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE[componentType];
61
- // the buffer view `byteOffset` must be aligned to a multiple of the `componentType` size.
62
- const offset = typedArray.byteOffset % size
63
- ? Math.ceil(typedArray.byteOffset / size) * size
64
- : typedArray.byteOffset;
65
- return new ArrayType(typedArray.buffer, offset, length);
66
- }
67
- exports.convertRawBufferToMetadataArray = convertRawBufferToMetadataArray;
68
- /**
69
- * Processes data encoded in the texture associated with the primitive.
70
- * If Ext_mesh_featues is combined with the Ext_structural_metadata, propertyTable will also be processed.
71
- * @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
72
- * @param {GLTFTextureInfoMetadata} textureInfo - reference to the texture where extension data are stored.
73
- * @param {GLTFMeshPrimitive} primitive - primitive object in the mesh
74
- * @returns {number[] | null} Array of data taken. Null if data can't be taken from the texture.
75
- */
76
- function getPrimitiveTextureData(scenegraph, textureInfo, primitive) {
77
- /*
78
- texture.index is an index for the "textures" array.
79
- The texture object referenced by this index looks like this:
80
- {
81
- "sampler": 0,
82
- "source": 0
83
- }
84
- "sampler" is an index for the "samplers" array
85
- "source" is an index for the "images" array that contains data. These data are stored in rgba channels of the image.
86
-
87
- texture.texCoord is a number-suffix (like 1) for an attribute like "TEXCOORD_1" in meshes.primitives
88
- 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.
89
- */
90
- const json = scenegraph.gltf.json;
91
- const texCoordAccessorKey = `TEXCOORD_${textureInfo.texCoord || 0}`;
92
- const texCoordAccessorIndex = primitive.attributes[texCoordAccessorKey];
93
- const texCoordBufferView = scenegraph.getBufferView(texCoordAccessorIndex);
94
- const texCoordArray = scenegraph.getTypedArrayForBufferView(texCoordBufferView);
95
- // textureCoordinates array contains UV coordinates of the actual data stored in the texture
96
- const textureCoordinates = new Float32Array(texCoordArray.buffer, texCoordArray.byteOffset, texCoordArray.length / 4);
97
- const textureIndex = textureInfo.index;
98
- const imageIndex = json.textures?.[textureIndex]?.source;
99
- if (typeof imageIndex !== 'undefined') {
100
- const mimeType = json.images?.[imageIndex]?.mimeType;
101
- const parsedImage = scenegraph.gltf.images?.[imageIndex];
102
- // Checking for width is to prevent handling Un-processed images (e.g. [analyze] stage, where loadImages option is set to false)
103
- if (parsedImage && typeof parsedImage.width !== 'undefined') {
104
- const textureData = [];
105
- for (let index = 0; index < textureCoordinates.length; index += 2) {
106
- const value = getImageValueByCoordinates(parsedImage, mimeType, textureCoordinates, index, textureInfo.channels);
107
- textureData.push(value);
108
- }
109
- return textureData;
110
- }
111
- }
112
- return null;
113
- }
114
- exports.getPrimitiveTextureData = getPrimitiveTextureData;
115
- /**
116
- * Puts property data to attributes.
117
- * It creates corresponding buffer, bufferView and accessor
118
- * so the data can be accessed like regular data stored in buffers.
119
- * @param {GLTFScenegraph} scenegraph - scenegraph object
120
- * @param {string} attributeName - name of the attribute
121
- * @param {number[]} propertyData - property data to store
122
- * @param {number[]} featureTable - an array where unique data from the property data are being stored
123
- * @param {GLTFMeshPrimitive} primitive - primitive object
124
- */
125
- function primitivePropertyDataToAttributes(scenegraph, attributeName, propertyData, featureTable, primitive) {
126
- if (propertyData === null)
127
- return;
128
- /*
129
- featureTable will contain unique values, e.g.
130
- propertyData = [24, 35, 28, 24]
131
- featureTable = [24, 35, 28]
132
- featureIndices will contain indices that refer featureTextureTable, e.g.
133
- featureIndices = [0, 1, 2, 0]
134
- */
135
- const featureIndices = [];
136
- for (const texelData of propertyData) {
137
- let index = featureTable.findIndex((item) => item === texelData);
138
- if (index === -1) {
139
- index = featureTable.push(texelData) - 1;
140
- }
141
- featureIndices.push(index);
142
- }
143
- const typedArray = new Uint32Array(featureIndices);
144
- const bufferIndex = scenegraph.gltf.buffers.push({
145
- arrayBuffer: typedArray.buffer,
146
- byteOffset: typedArray.byteOffset,
147
- byteLength: typedArray.byteLength
148
- }) - 1;
149
- const bufferViewIndex = scenegraph.addBufferView(typedArray, bufferIndex, 0);
150
- const accessorIndex = scenegraph.addAccessor(bufferViewIndex, {
151
- size: 1,
152
- componentType: (0, gltf_utils_1.getComponentTypeFromArray)(typedArray),
153
- count: typedArray.length
154
- });
155
- primitive.attributes[attributeName] = accessorIndex;
156
- }
157
- exports.primitivePropertyDataToAttributes = primitivePropertyDataToAttributes;
158
- /**
159
- * Gets the value from the texture by coordinates provided.
160
- * @param {ImageType} parsedImage - image where the data are stored.
161
- * @param {string | undefined} mimeType - MIME type
162
- * @param {Float32Array} textureCoordinates - uv coordinates to access data in the image.
163
- * @param {number} index - index of uv coordinates in the array textureCoordinates
164
- * @param {channels} channels - image channels where data are stored. Channels of an RGBA texture are numbered 0..3 respectively.
165
- * @returns {number} Value taken from the image.
166
- */
167
- function getImageValueByCoordinates(parsedImage, mimeType, textureCoordinates, index, channels = [0]) {
168
- const CHANNELS_MAP = [
169
- { offset: 0, shift: 0 },
170
- { offset: 1, shift: 8 },
171
- { offset: 2, shift: 16 },
172
- { offset: 3, shift: 24 }
173
- ];
174
- const u = textureCoordinates[index];
175
- const v = textureCoordinates[index + 1];
176
- let components = 1;
177
- if (mimeType && (mimeType.indexOf('image/jpeg') !== -1 || mimeType.indexOf('image/png') !== -1))
178
- components = 4;
179
- const offset = coordinatesToOffset(u, v, parsedImage, components);
180
- let value = 0;
181
- for (const c of channels) {
182
- const map = CHANNELS_MAP[c];
183
- const imageOffset = offset + map.offset;
184
- const imageData = (0, images_1.getImageData)(parsedImage);
185
- if (imageData.data.length <= imageOffset) {
186
- throw new Error(`${imageData.data.length} <= ${imageOffset}`);
187
- }
188
- const imageValue = imageData.data[imageOffset];
189
- value |= imageValue << map.shift;
190
- }
191
- return value;
192
- }
193
- /**
194
- * Retrieves the offset in the image where the data are stored
195
- * @param u - u-coordinate
196
- * @param v - v-coordinate
197
- * @param parsedImage - image where the data are stored
198
- * @param componentsCount - number of components the data consists of.
199
- * @returns offset in the image where the data are stored
200
- */
201
- function coordinatesToOffset(u, v, parsedImage, componentsCount = 1) {
202
- const w = parsedImage.width;
203
- const iX = (0, math_1.emod)(u) * (w - 1);
204
- const indX = Math.round(iX);
205
- const h = parsedImage.height;
206
- const iY = (0, math_1.emod)(v) * (h - 1);
207
- const indY = Math.round(iY);
208
- const components = parsedImage.components ? parsedImage.components : componentsCount;
209
- // components is a number of channels in the image
210
- const offset = (indY * w + indX) * components;
211
- return offset;
212
- }