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

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,138 +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 draco_1 = require("@loaders.gl/draco");
8
- const loader_utils_1 = require("@loaders.gl/loader-utils");
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 { parse } = context;
66
- const dracoOptions = { ...options };
67
- // TODO - remove hack: The entire tileset might be included, too expensive to serialize
68
- delete dracoOptions['3d-tiles'];
69
- const decodedData = (await parse(bufferCopy, draco_1.DracoLoader, dracoOptions, context));
70
- const decodedAttributes = (0, gltf_attribute_utils_1.getGLTFAccessors)(decodedData.attributes);
71
- // Restore min/max values
72
- for (const [attributeName, decodedAttribute] of Object.entries(decodedAttributes)) {
73
- if (attributeName in primitive.attributes) {
74
- const accessorIndex = primitive.attributes[attributeName];
75
- const accessor = scenegraph.getAccessor(accessorIndex);
76
- if (accessor?.min && accessor?.max) {
77
- decodedAttribute.min = accessor.min;
78
- decodedAttribute.max = accessor.max;
79
- }
80
- }
81
- }
82
- // @ts-ignore
83
- primitive.attributes = decodedAttributes;
84
- if (decodedData.indices) {
85
- // @ts-ignore
86
- primitive.indices = (0, gltf_attribute_utils_1.getGLTFAccessor)(decodedData.indices);
87
- }
88
- // Extension has been processed, delete it
89
- scenegraph.removeObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION);
90
- checkPrimitive(primitive);
91
- }
92
- // ENCODE
93
- // eslint-disable-next-line max-len
94
- // Only TRIANGLES: 0x0004 and TRIANGLE_STRIP: 0x0005 are supported
95
- function compressMesh(attributes, indices, mode = 4, options, context) {
96
- if (!options.DracoWriter) {
97
- throw new Error('options.gltf.DracoWriter not provided');
98
- }
99
- // TODO - use DracoWriter using encode w/ registered DracoWriter...
100
- const compressedData = options.DracoWriter.encodeSync({ attributes });
101
- // Draco compression may change the order and number of vertices in a mesh.
102
- // To satisfy the requirement that accessors properties be correct for both
103
- // compressed and uncompressed data, generators should create uncompressed
104
- // attributes and indices using data that has been decompressed from the Draco buffer,
105
- // rather than the original source data.
106
- // @ts-ignore TODO this needs to be fixed
107
- const decodedData = context?.parseSync?.({ attributes });
108
- const fauxAccessors = options._addFauxAttributes(decodedData.attributes);
109
- const bufferViewIndex = options.addBufferView(compressedData);
110
- const glTFMesh = {
111
- primitives: [
112
- {
113
- attributes: fauxAccessors,
114
- mode,
115
- extensions: {
116
- [KHR_DRACO_MESH_COMPRESSION]: {
117
- bufferView: bufferViewIndex,
118
- attributes: fauxAccessors // TODO - verify with spec
119
- }
120
- }
121
- }
122
- ]
123
- };
124
- return glTFMesh;
125
- }
126
- // UTILS
127
- function checkPrimitive(primitive) {
128
- if (!primitive.attributes && Object.keys(primitive.attributes).length > 0) {
129
- throw new Error('glTF: Empty primitive detected: Draco decompression failure?');
130
- }
131
- }
132
- function* makeMeshPrimitiveIterator(scenegraph) {
133
- for (const mesh of scenegraph.json.meshes || []) {
134
- for (const primitive of mesh.primitives) {
135
- yield primitive;
136
- }
137
- }
138
- }
@@ -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) {
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,290 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decode = exports.name = void 0;
4
- const gltf_scenegraph_1 = require("../../api/gltf-scenegraph");
5
- const images_1 = require("@loaders.gl/images");
6
- const gltf_utils_1 = require("../../gltf-utils/gltf-utils");
7
- /** Extension name */
8
- const EXT_FEATURE_METADATA = 'EXT_feature_metadata';
9
- exports.name = EXT_FEATURE_METADATA;
10
- async function decode(gltfData, options) {
11
- const scenegraph = new gltf_scenegraph_1.GLTFScenegraph(gltfData);
12
- decodeExtFeatureMetadata(scenegraph, options);
13
- }
14
- exports.decode = decode;
15
- /**
16
- * Decodes feature metadata from extension
17
- * @param scenegraph
18
- */
19
- function decodeExtFeatureMetadata(scenegraph, options) {
20
- const extension = scenegraph.getExtension(EXT_FEATURE_METADATA);
21
- if (!extension)
22
- return;
23
- const schemaClasses = extension.schema?.classes;
24
- const { featureTables } = extension;
25
- if (schemaClasses && featureTables) {
26
- for (const schemaName in schemaClasses) {
27
- const schemaClass = schemaClasses[schemaName];
28
- const featureTable = findFeatureTableByName(featureTables, schemaName);
29
- if (featureTable) {
30
- handleFeatureTableProperties(scenegraph, featureTable, schemaClass);
31
- }
32
- }
33
- }
34
- const { featureTextures } = extension;
35
- if (schemaClasses && featureTextures && options.gltf?.loadImages) {
36
- for (const schemaName in schemaClasses) {
37
- const schemaClass = schemaClasses[schemaName];
38
- const featureTexture = findFeatureTextureByName(featureTextures, schemaName);
39
- if (featureTexture) {
40
- handleFeatureTextureProperties(scenegraph, featureTexture, schemaClass);
41
- }
42
- }
43
- }
44
- }
45
- /**
46
- * Navigate throw all properies in feature table and gets properties data.
47
- * @param scenegraph
48
- * @param featureTable
49
- * @param schemaClass
50
- */
51
- function handleFeatureTableProperties(scenegraph, featureTable, schemaClass) {
52
- for (const propertyName in schemaClass.properties) {
53
- const schemaProperty = schemaClass.properties[propertyName];
54
- const featureTableProperty = featureTable?.properties?.[propertyName];
55
- const numberOfFeatures = featureTable.count;
56
- if (featureTableProperty) {
57
- const data = getPropertyDataFromBinarySource(scenegraph, schemaProperty, numberOfFeatures, featureTableProperty);
58
- featureTableProperty.data = data;
59
- }
60
- }
61
- }
62
- /**
63
- * Navigate throw all properies in feature texture and gets properties data.
64
- * Data will be stored in featureTexture.properties[propertyName].data
65
- * @param scenegraph
66
- * @param featureTexture
67
- * @param schemaClass
68
- */
69
- function handleFeatureTextureProperties(scenegraph, featureTexture, schemaClass) {
70
- const attributeName = featureTexture.class;
71
- for (const propertyName in schemaClass.properties) {
72
- const featureTextureProperty = featureTexture?.properties?.[propertyName];
73
- if (featureTextureProperty) {
74
- const data = getPropertyDataFromTexture(scenegraph, featureTextureProperty, attributeName);
75
- featureTextureProperty.data = data;
76
- }
77
- }
78
- }
79
- /**
80
- * Decode properties from binary sourse based on property type.
81
- * @param scenegraph
82
- * @param schemaProperty
83
- * @param numberOfFeatures
84
- * @param featureTableProperty
85
- */
86
- function getPropertyDataFromBinarySource(scenegraph, schemaProperty, numberOfFeatures, featureTableProperty) {
87
- const bufferView = featureTableProperty.bufferView;
88
- // TODO think maybe we shouldn't get data only in Uint8Array format.
89
- const dataArray = scenegraph.getTypedArrayForBufferView(bufferView);
90
- switch (schemaProperty.type) {
91
- case 'STRING': {
92
- // stringOffsetBufferView should be available for string type.
93
- const stringOffsetBufferView = featureTableProperty.stringOffsetBufferView;
94
- const offsetsData = scenegraph.getTypedArrayForBufferView(stringOffsetBufferView);
95
- return getStringAttributes(dataArray, offsetsData, numberOfFeatures);
96
- }
97
- default:
98
- }
99
- return dataArray;
100
- }
101
- /**
102
- * Get properties from texture associated with all mesh primitives.
103
- * @param scenegraph
104
- * @param featureTextureProperty
105
- * @param attributeName
106
- * @returns Feature texture data
107
- */
108
- function getPropertyDataFromTexture(scenegraph, featureTextureProperty, attributeName) {
109
- const json = scenegraph.gltf.json;
110
- if (!json.meshes) {
111
- return [];
112
- }
113
- const featureTextureTable = [];
114
- for (const mesh of json.meshes) {
115
- for (const primitive of mesh.primitives) {
116
- processPrimitiveTextures(scenegraph, attributeName, featureTextureProperty, featureTextureTable, primitive);
117
- }
118
- }
119
- return featureTextureTable;
120
- }
121
- // eslint-disable-next-line max-statements
122
- /**
123
- * Processes data encoded in the texture associated with the primitive. This data will be accessible through the attributes.
124
- * @param scenegraph
125
- * @param attributeName
126
- * @param featureTextureProperty
127
- * @param featureTextureTable
128
- * @param primitive
129
- */
130
- function processPrimitiveTextures(scenegraph, attributeName, featureTextureProperty, featureTextureTable, primitive) {
131
- /*
132
- texture.index is an index for the "textures" array.
133
- The texture object referenced by this index looks like this:
134
- {
135
- "sampler": 0,
136
- "source": 0
137
- }
138
- "sampler" is an index for the "samplers" array
139
- "source" is an index for the "images" array that contains data. These data are stored in rgba channels of the image.
140
-
141
- texture.texCoord is a number-suffix (like 1) for an attribute like "TEXCOORD_1" in meshes.primitives
142
- The value of "TEXCOORD_1" is an accessor that is used to get coordinates. These coordinates ared used to get data from the image.
143
- */
144
- const json = scenegraph.gltf.json;
145
- const textureData = [];
146
- const texCoordAccessorKey = `TEXCOORD_${featureTextureProperty.texture.texCoord}`;
147
- const texCoordAccessorIndex = primitive.attributes[texCoordAccessorKey];
148
- const texCoordBufferView = scenegraph.getBufferView(texCoordAccessorIndex);
149
- const texCoordArray = scenegraph.getTypedArrayForBufferView(texCoordBufferView);
150
- const textureCoordinates = new Float32Array(texCoordArray.buffer, texCoordArray.byteOffset, texCoordArray.length / 4);
151
- // textureCoordinates contains UV coordinates of the actual data stored in the texture
152
- // accessor.count is a number of UV pairs (they are stored as VEC2)
153
- const textureIndex = featureTextureProperty.texture.index;
154
- const texture = json.textures?.[textureIndex];
155
- const imageIndex = texture?.source;
156
- if (typeof imageIndex !== 'undefined') {
157
- const image = json.images?.[imageIndex];
158
- const mimeType = image?.mimeType;
159
- const parsedImage = scenegraph.gltf.images?.[imageIndex];
160
- if (parsedImage) {
161
- for (let index = 0; index < textureCoordinates.length; index += 2) {
162
- const value = getImageValueByCoordinates(parsedImage, mimeType, textureCoordinates, index, featureTextureProperty.channels);
163
- textureData.push(value);
164
- }
165
- }
166
- }
167
- /*
168
- featureTextureTable will contain unique values, e.g.
169
- textureData = [24, 35, 28, 24]
170
- featureTextureTable = [24, 35, 28]
171
- featureIndices will contain indices hat refer featureTextureTable, e.g.
172
- featureIndices = [0, 1, 2, 0]
173
- */
174
- const featureIndices = [];
175
- for (const texelData of textureData) {
176
- let index = featureTextureTable.findIndex((item) => item === texelData);
177
- if (index === -1) {
178
- index = featureTextureTable.push(texelData) - 1;
179
- }
180
- featureIndices.push(index);
181
- }
182
- const typedArray = new Uint32Array(featureIndices);
183
- const bufferIndex = scenegraph.gltf.buffers.push({
184
- arrayBuffer: typedArray.buffer,
185
- byteOffset: 0,
186
- byteLength: typedArray.byteLength
187
- }) - 1;
188
- const bufferViewIndex = scenegraph.addBufferView(typedArray, bufferIndex, 0);
189
- const accessorIndex = scenegraph.addAccessor(bufferViewIndex, {
190
- size: 1,
191
- componentType: (0, gltf_utils_1.getComponentTypeFromArray)(typedArray),
192
- count: typedArray.length
193
- });
194
- primitive.attributes[attributeName] = accessorIndex;
195
- }
196
- function getImageValueByCoordinates(parsedImage, mimeType, textureCoordinates, index, channels) {
197
- const CHANNELS_MAP = {
198
- r: { offset: 0, shift: 0 },
199
- g: { offset: 1, shift: 8 },
200
- b: { offset: 2, shift: 16 },
201
- a: { offset: 3, shift: 24 }
202
- };
203
- const u = textureCoordinates[index];
204
- const v = textureCoordinates[index + 1];
205
- let components = 1;
206
- if (mimeType && (mimeType.indexOf('image/jpeg') !== -1 || mimeType.indexOf('image/png') !== -1))
207
- components = 4;
208
- const offset = coordinatesToOffset(u, v, parsedImage, components);
209
- let value = 0;
210
- for (const c of channels) {
211
- const map = CHANNELS_MAP[c];
212
- const val = getVal(parsedImage, offset + map.offset);
213
- value |= val << map.shift;
214
- }
215
- return value;
216
- }
217
- function getVal(parsedImage, offset) {
218
- const imageData = (0, images_1.getImageData)(parsedImage);
219
- if (imageData.data.length <= offset) {
220
- throw new Error(`${imageData.data.length} <= ${offset}`);
221
- }
222
- return imageData.data[offset];
223
- }
224
- function coordinatesToOffset(u, v, parsedImage, componentsCount = 1) {
225
- const w = parsedImage.width;
226
- const iX = emod(u) * (w - 1);
227
- const indX = Math.round(iX);
228
- const h = parsedImage.height;
229
- const iY = emod(v) * (h - 1);
230
- const indY = Math.round(iY);
231
- const components = parsedImage.components ? parsedImage.components : componentsCount;
232
- // components is a number of channels in the image
233
- const offset = (indY * w + indX) * components;
234
- return offset;
235
- }
236
- // The following is taken from tile-converter\src\i3s-converter\helpers\batch-ids-extensions.ts
237
- /**
238
- * Handle UVs if they are out of range [0,1].
239
- * @param n
240
- * @param m
241
- */
242
- function emod(n) {
243
- const a = ((n % 1) + 1) % 1;
244
- return a;
245
- }
246
- /**
247
- * Find the feature table by class name.
248
- * @param featureTables
249
- * @param schemaClassName
250
- */
251
- function findFeatureTableByName(featureTables, schemaClassName) {
252
- for (const featureTableName in featureTables) {
253
- const featureTable = featureTables[featureTableName];
254
- if (featureTable.class === schemaClassName) {
255
- return featureTable;
256
- }
257
- }
258
- return null;
259
- }
260
- function findFeatureTextureByName(featureTextures, schemaClassName) {
261
- for (const featureTexturesName in featureTextures) {
262
- const featureTable = featureTextures[featureTexturesName];
263
- if (featureTable.class === schemaClassName) {
264
- return featureTable;
265
- }
266
- }
267
- return null;
268
- }
269
- /**
270
- * Getting string attributes from binary data.
271
- * Spec - https://github.com/CesiumGS/3d-tiles/tree/main/specification/Metadata#strings
272
- * @param data
273
- * @param offsetsData
274
- * @param stringsCount
275
- */
276
- function getStringAttributes(data, offsetsData, stringsCount) {
277
- const stringsArray = [];
278
- const textDecoder = new TextDecoder('utf8');
279
- let stringOffset = 0;
280
- const bytesPerStringSize = 4;
281
- for (let index = 0; index < stringsCount; index++) {
282
- // TODO check if it is multiplication on bytesPerStringSize is valid operation?
283
- const stringByteSize = offsetsData[(index + 1) * bytesPerStringSize] - offsetsData[index * bytesPerStringSize];
284
- const stringData = data.subarray(stringOffset, stringByteSize + stringOffset);
285
- const stringAttribute = textDecoder.decode(stringData);
286
- stringsArray.push(stringAttribute);
287
- stringOffset += stringByteSize;
288
- }
289
- return stringsArray;
290
- }