@loaders.gl/gltf 3.4.11 → 3.4.13

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 (40) hide show
  1. package/dist/es5/lib/utils/version.js +1 -1
  2. package/dist/esm/lib/utils/version.js +1 -1
  3. package/package.json +6 -6
  4. package/dist/bundle.js +0 -5
  5. package/dist/glb-loader.js +0 -39
  6. package/dist/glb-writer.js +0 -37
  7. package/dist/gltf-loader.js +0 -50
  8. package/dist/gltf-writer.js +0 -32
  9. package/dist/index.js +0 -23
  10. package/dist/lib/api/gltf-extensions.js +0 -83
  11. package/dist/lib/api/gltf-scenegraph.js +0 -566
  12. package/dist/lib/api/normalize-gltf-v1.js +0 -299
  13. package/dist/lib/api/post-process-gltf.js +0 -370
  14. package/dist/lib/encoders/encode-glb.js +0 -61
  15. package/dist/lib/encoders/encode-gltf.js +0 -27
  16. package/dist/lib/extensions/EXT_meshopt_compression.js +0 -45
  17. package/dist/lib/extensions/EXT_texture_webp.js +0 -39
  18. package/dist/lib/extensions/KHR_binary_gltf.js +0 -42
  19. package/dist/lib/extensions/KHR_draco_mesh_compression.js +0 -141
  20. package/dist/lib/extensions/KHR_texture_basisu.js +0 -32
  21. package/dist/lib/extensions/KHR_texture_transform.js +0 -230
  22. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +0 -118
  23. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +0 -62
  24. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +0 -47
  25. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +0 -82
  26. package/dist/lib/gltf-utils/get-typed-array.js +0 -41
  27. package/dist/lib/gltf-utils/gltf-attribute-utils.js +0 -73
  28. package/dist/lib/gltf-utils/gltf-constants.js +0 -43
  29. package/dist/lib/gltf-utils/gltf-utils.js +0 -85
  30. package/dist/lib/gltf-utils/resolve-url.js +0 -18
  31. package/dist/lib/parsers/parse-glb.js +0 -141
  32. package/dist/lib/parsers/parse-gltf.js +0 -203
  33. package/dist/lib/types/glb-types.js +0 -2
  34. package/dist/lib/types/gltf-json-schema.js +0 -4
  35. package/dist/lib/types/gltf-postprocessed-schema.js +0 -4
  36. package/dist/lib/types/gltf-types.js +0 -3
  37. package/dist/lib/utils/assert.js +0 -12
  38. package/dist/lib/utils/version.js +0 -7
  39. package/dist/meshopt/meshopt-decoder.js +0 -118
  40. package/dist/webp/webp.js +0 -38
@@ -1,61 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- /* eslint-disable camelcase, max-statements */
4
- const loader_utils_1 = require("@loaders.gl/loader-utils");
5
- const MAGIC_glTF = 0x46546c67; // glTF in ASCII
6
- const MAGIC_JSON = 0x4e4f534a; // JSON in ASCII
7
- const MAGIC_BIN = 0x004e4942; // BIN\0 in ASCII
8
- const LE = true; // Binary GLTF is little endian.
9
- // Encode the full GLB buffer with header etc
10
- // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#
11
- // glb-file-format-specification
12
- function encodeGLBSync(glb, dataView, byteOffset = 0, options = {}) {
13
- const { magic = MAGIC_glTF, version = 2, json = {}, binary } = glb;
14
- const byteOffsetStart = byteOffset;
15
- // Write GLB Header
16
- if (dataView) {
17
- dataView.setUint32(byteOffset + 0, magic, LE); // Magic number (the ASCII string 'glTF').
18
- dataView.setUint32(byteOffset + 4, version, LE); // Version 2 of binary glTF container format uint32
19
- dataView.setUint32(byteOffset + 8, 0, LE); // Total byte length of generated file (uint32), will be set last
20
- }
21
- const byteOffsetFileLength = byteOffset + 8;
22
- byteOffset += 12; // GLB_FILE_HEADER_SIZE
23
- // Write the JSON chunk header
24
- const byteOffsetJsonHeader = byteOffset;
25
- if (dataView) {
26
- dataView.setUint32(byteOffset + 0, 0, LE); // Byte length of json chunk (will be written later)
27
- dataView.setUint32(byteOffset + 4, MAGIC_JSON, LE); // Chunk type
28
- }
29
- byteOffset += 8; // GLB_CHUNK_HEADER_SIZE
30
- // Write the JSON chunk
31
- const jsonString = JSON.stringify(json);
32
- byteOffset = (0, loader_utils_1.copyPaddedStringToDataView)(dataView, byteOffset, jsonString, 4);
33
- // Now we know the JSON chunk length so we can write it.
34
- if (dataView) {
35
- const jsonByteLength = byteOffset - byteOffsetJsonHeader - 8; // GLB_CHUNK_HEADER_SIZE
36
- dataView.setUint32(byteOffsetJsonHeader + 0, jsonByteLength, LE); // Byte length of json chunk (uint32)
37
- }
38
- // Write the BIN chunk if present. The BIN chunk is optional.
39
- if (binary) {
40
- const byteOffsetBinHeader = byteOffset;
41
- // Write the BIN chunk header
42
- if (dataView) {
43
- dataView.setUint32(byteOffset + 0, 0, LE); // Byte length BIN (uint32)
44
- dataView.setUint32(byteOffset + 4, MAGIC_BIN, LE); // Chunk type
45
- }
46
- byteOffset += 8; // GLB_CHUNK_HEADER_SIZE
47
- byteOffset = (0, loader_utils_1.copyPaddedArrayBufferToDataView)(dataView, byteOffset, binary, 4);
48
- // Now we know the BIN chunk length so we can write it.
49
- if (dataView) {
50
- const binByteLength = byteOffset - byteOffsetBinHeader - 8; // GLB_CHUNK_HEADER_SIZE
51
- dataView.setUint32(byteOffsetBinHeader + 0, binByteLength, LE); // Byte length BIN (uint32)
52
- }
53
- }
54
- // Now we know the glb file length so we can write it.
55
- if (dataView) {
56
- const fileByteLength = byteOffset - byteOffsetStart;
57
- dataView.setUint32(byteOffsetFileLength, fileByteLength, LE); // Total byte length of generated file (uint32)
58
- }
59
- return byteOffset;
60
- }
61
- exports.default = encodeGLBSync;
@@ -1,27 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.encodeGLTFSync = void 0;
7
- const encode_glb_1 = __importDefault(require("./encode-glb"));
8
- // Encode the full glTF file as a binary GLB file
9
- // Returns an ArrayBuffer that represents the complete GLB image that can be saved to file
10
- //
11
- // TODO - Does not support encoding to non-GLB versions of glTF format
12
- // - Encode as a textual JSON file with binary data in base64 data URLs.
13
- // - Encode as a JSON with all images (and buffers?) in separate binary files
14
- //
15
- // glb-file-format-specification
16
- // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#
17
- function encodeGLTFSync(gltf, arrayBuffer, byteOffset, options) {
18
- convertBuffersToBase64(gltf);
19
- // TODO: Copy buffers to binary
20
- return (0, encode_glb_1.default)(gltf, arrayBuffer, byteOffset, options);
21
- }
22
- exports.encodeGLTFSync = encodeGLTFSync;
23
- function convertBuffersToBase64(gltf, { firstBuffer = 0 } = {}) {
24
- if (gltf.buffers && gltf.buffers.length > firstBuffer) {
25
- throw new Error('encodeGLTF: multiple buffers not yet implemented');
26
- }
27
- }
@@ -1,45 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.decode = exports.name = void 0;
7
- const gltf_scenegraph_1 = __importDefault(require("../api/gltf-scenegraph"));
8
- const meshopt_decoder_1 = require("../../meshopt/meshopt-decoder");
9
- // @ts-ignore
10
- // eslint-disable-next-line
11
- const DEFAULT_MESHOPT_OPTIONS = {
12
- byteOffset: 0,
13
- filter: 'NONE'
14
- };
15
- /** Extension name */
16
- const EXT_MESHOPT_COMPRESSION = 'EXT_meshopt_compression';
17
- exports.name = EXT_MESHOPT_COMPRESSION;
18
- async function decode(gltfData, options) {
19
- const scenegraph = new gltf_scenegraph_1.default(gltfData);
20
- if (!options?.gltf?.decompressMeshes) {
21
- return;
22
- }
23
- const promises = [];
24
- for (const bufferViewIndex of gltfData.json.bufferViews || []) {
25
- promises.push(decodeMeshoptBufferView(scenegraph, bufferViewIndex));
26
- }
27
- // Decompress meshes in parallel
28
- await Promise.all(promises);
29
- // We have now decompressed all primitives, so remove the top-level extension
30
- scenegraph.removeExtension(EXT_MESHOPT_COMPRESSION);
31
- }
32
- exports.decode = decode;
33
- /** Decode one meshopt buffer view */
34
- async function decodeMeshoptBufferView(scenegraph, bufferView) {
35
- const meshoptExtension = scenegraph.getObjectExtension(bufferView, EXT_MESHOPT_COMPRESSION);
36
- if (meshoptExtension) {
37
- const { byteOffset = 0, byteLength = 0, byteStride, count, mode, filter = 'NONE', buffer: bufferIndex } = meshoptExtension;
38
- const buffer = scenegraph.gltf.buffers[bufferIndex];
39
- const source = new Uint8Array(buffer.arrayBuffer, buffer.byteOffset + byteOffset, byteLength);
40
- const result = new Uint8Array(scenegraph.gltf.buffers[bufferView.buffer].arrayBuffer, bufferView.byteOffset, bufferView.byteLength);
41
- await (0, meshopt_decoder_1.meshoptDecodeGltfBuffer)(result, count, byteStride, source, mode, filter);
42
- return result;
43
- }
44
- return null;
45
- }
@@ -1,39 +0,0 @@
1
- "use strict";
2
- // GLTF EXTENSION: EXT_TEXTURE_WEBP
3
- // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/EXT_TEXTURE_WEBP
4
- /* eslint-disable camelcase */
5
- var __importDefault = (this && this.__importDefault) || function (mod) {
6
- return (mod && mod.__esModule) ? mod : { "default": mod };
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.preprocess = exports.name = void 0;
10
- const images_1 = require("@loaders.gl/images");
11
- const gltf_scenegraph_1 = __importDefault(require("../api/gltf-scenegraph"));
12
- const EXT_TEXTURE_WEBP = 'EXT_texture_webp';
13
- /** Extension name */
14
- exports.name = EXT_TEXTURE_WEBP;
15
- /**
16
- * Replaces a texture source reference with the extension texture
17
- * Done in preprocess() to prevent load of default image
18
- */
19
- function preprocess(gltfData, options) {
20
- const scenegraph = new gltf_scenegraph_1.default(gltfData);
21
- if (!(0, images_1.isImageFormatSupported)('image/webp')) {
22
- if (scenegraph.getRequiredExtensions().includes(EXT_TEXTURE_WEBP)) {
23
- throw new Error(`gltf: Required extension ${EXT_TEXTURE_WEBP} not supported by browser`);
24
- }
25
- return;
26
- }
27
- const { json } = scenegraph;
28
- for (const texture of json.textures || []) {
29
- const extension = scenegraph.getObjectExtension(texture, EXT_TEXTURE_WEBP);
30
- if (extension) {
31
- // TODO - if multiple texture extensions are present which one wins?
32
- texture.source = extension.source;
33
- }
34
- scenegraph.removeObjectExtension(texture, EXT_TEXTURE_WEBP);
35
- }
36
- // Remove the top-level extension
37
- scenegraph.removeExtension(EXT_TEXTURE_WEBP);
38
- }
39
- exports.preprocess = preprocess;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- // GLTF 1.0 EXTENSION: KHR_binary_glTF
3
- // https://github.com/KhronosGroup/glTF/tree/master/extensions/1.0/Khronos/KHR_binary_glTF
4
- /* eslint-disable camelcase */
5
- var __importDefault = (this && this.__importDefault) || function (mod) {
6
- return (mod && mod.__esModule) ? mod : { "default": mod };
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.preprocess = exports.name = void 0;
10
- const gltf_scenegraph_1 = __importDefault(require("../api/gltf-scenegraph"));
11
- const KHR_BINARY_GLTF = 'KHR_binary_glTF';
12
- /** Extension name */
13
- exports.name = KHR_BINARY_GLTF;
14
- function preprocess(gltfData) {
15
- const gltfScenegraph = new gltf_scenegraph_1.default(gltfData);
16
- const { json } = gltfScenegraph;
17
- // Note: json.buffers.binary_glTF also needs to be replaced
18
- // This is currently done during gltf normalization
19
- // Image and shader nodes can have the extension
20
- // https://github.com/KhronosGroup/glTF/blob/master/extensions/1.0/Khronos/KHR_binary_glTF/schema/image.KHR_binary_glTF.schema.json
21
- for (const image of json.images || []) {
22
- const extension = gltfScenegraph.getObjectExtension(image, KHR_BINARY_GLTF);
23
- // The data in the extension is valid as glTF 2.0 data inside the object, so just copy it in
24
- if (extension) {
25
- Object.assign(image, extension);
26
- }
27
- gltfScenegraph.removeObjectExtension(image, KHR_BINARY_GLTF);
28
- }
29
- // TODO shaders - At least traverse and throw error if used?
30
- // https://github.com/KhronosGroup/glTF/blob/master/extensions/1.0/Khronos/KHR_binary_glTF/schema/shader.KHR_binary_glTF.schema.json
31
- // glTF v1 one files have a partially formed URI field that is not expected in (and causes problems in) 2.0
32
- if (json.buffers && json.buffers[0]) {
33
- delete json.buffers[0].uri;
34
- }
35
- // Remove the top-level extension as it has now been processed
36
- gltfScenegraph.removeExtension(KHR_BINARY_GLTF);
37
- }
38
- exports.preprocess = preprocess;
39
- // KHR_binary_gltf is a 1.0 extension that is supported natively by 2.0
40
- // export function encode() {
41
- // throw new Error(KHR_BINARY_GLTF);
42
- // }
@@ -1,141 +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
- var __importDefault = (this && this.__importDefault) || function (mod) {
6
- return (mod && mod.__esModule) ? mod : { "default": mod };
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.encode = exports.decode = exports.preprocess = exports.name = void 0;
10
- const draco_1 = require("@loaders.gl/draco");
11
- const loader_utils_1 = require("@loaders.gl/loader-utils");
12
- const gltf_scenegraph_1 = __importDefault(require("../api/gltf-scenegraph"));
13
- const gltf_attribute_utils_1 = require("../gltf-utils/gltf-attribute-utils");
14
- const KHR_DRACO_MESH_COMPRESSION = 'KHR_draco_mesh_compression';
15
- /** Extension name */
16
- exports.name = KHR_DRACO_MESH_COMPRESSION;
17
- function preprocess(gltfData, options, context) {
18
- const scenegraph = new gltf_scenegraph_1.default(gltfData);
19
- for (const primitive of makeMeshPrimitiveIterator(scenegraph)) {
20
- if (scenegraph.getObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION)) {
21
- // TODO - Remove fallback accessors to make sure we don't load unnecessary buffers
22
- }
23
- }
24
- }
25
- exports.preprocess = preprocess;
26
- async function decode(gltfData, options, context) {
27
- if (!options?.gltf?.decompressMeshes) {
28
- return;
29
- }
30
- const scenegraph = new gltf_scenegraph_1.default(gltfData);
31
- const promises = [];
32
- for (const primitive of makeMeshPrimitiveIterator(scenegraph)) {
33
- if (scenegraph.getObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION)) {
34
- promises.push(decompressPrimitive(scenegraph, primitive, options, context));
35
- }
36
- }
37
- // Decompress meshes in parallel
38
- await Promise.all(promises);
39
- // We have now decompressed all primitives, so remove the top-level extension
40
- scenegraph.removeExtension(KHR_DRACO_MESH_COMPRESSION);
41
- }
42
- exports.decode = decode;
43
- function encode(gltfData, options = {}) {
44
- const scenegraph = new gltf_scenegraph_1.default(gltfData);
45
- for (const mesh of scenegraph.json.meshes || []) {
46
- // eslint-disable-next-line camelcase
47
- // @ts-ignore
48
- compressMesh(mesh, options);
49
- // NOTE: Only add the extension if something was actually compressed
50
- scenegraph.addRequiredExtension(KHR_DRACO_MESH_COMPRESSION);
51
- }
52
- }
53
- exports.encode = encode;
54
- // DECODE
55
- // Unpacks one mesh primitive and removes the extension from the primitive
56
- // DracoDecoder needs to be imported and registered by app
57
- // Returns: Promise that resolves when all pending draco decoder jobs for this mesh complete
58
- // TODO - Implement fallback behavior per KHR_DRACO_MESH_COMPRESSION spec
59
- async function decompressPrimitive(scenegraph, primitive, options, context) {
60
- const dracoExtension = scenegraph.getObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION);
61
- if (!dracoExtension) {
62
- return;
63
- }
64
- const buffer = scenegraph.getTypedArrayForBufferView(dracoExtension.bufferView);
65
- // TODO - parse does not yet deal well with byte offsets embedded in typed arrays. Copy buffer
66
- // TODO - remove when `parse` is fixed to handle `byteOffset`s
67
- const bufferCopy = (0, loader_utils_1.sliceArrayBuffer)(buffer.buffer, buffer.byteOffset); // , buffer.byteLength);
68
- const { parse } = context;
69
- const dracoOptions = { ...options };
70
- // TODO - remove hack: The entire tileset might be included, too expensive to serialize
71
- delete dracoOptions['3d-tiles'];
72
- const decodedData = (await parse(bufferCopy, draco_1.DracoLoader, dracoOptions, context));
73
- const decodedAttributes = (0, gltf_attribute_utils_1.getGLTFAccessors)(decodedData.attributes);
74
- // Restore min/max values
75
- for (const [attributeName, decodedAttribute] of Object.entries(decodedAttributes)) {
76
- if (attributeName in primitive.attributes) {
77
- const accessorIndex = primitive.attributes[attributeName];
78
- const accessor = scenegraph.getAccessor(accessorIndex);
79
- if (accessor?.min && accessor?.max) {
80
- decodedAttribute.min = accessor.min;
81
- decodedAttribute.max = accessor.max;
82
- }
83
- }
84
- }
85
- // @ts-ignore
86
- primitive.attributes = decodedAttributes;
87
- if (decodedData.indices) {
88
- // @ts-ignore
89
- primitive.indices = (0, gltf_attribute_utils_1.getGLTFAccessor)(decodedData.indices);
90
- }
91
- // Extension has been processed, delete it
92
- // delete primitive.extensions[KHR_DRACO_MESH_COMPRESSION];
93
- checkPrimitive(primitive);
94
- }
95
- // ENCODE
96
- // eslint-disable-next-line max-len
97
- // Only TRIANGLES: 0x0004 and TRIANGLE_STRIP: 0x0005 are supported
98
- function compressMesh(attributes, indices, mode = 4, options, context) {
99
- if (!options.DracoWriter) {
100
- throw new Error('options.gltf.DracoWriter not provided');
101
- }
102
- // TODO - use DracoWriter using encode w/ registered DracoWriter...
103
- const compressedData = options.DracoWriter.encodeSync({ attributes });
104
- // Draco compression may change the order and number of vertices in a mesh.
105
- // To satisfy the requirement that accessors properties be correct for both
106
- // compressed and uncompressed data, generators should create uncompressed
107
- // attributes and indices using data that has been decompressed from the Draco buffer,
108
- // rather than the original source data.
109
- // @ts-ignore TODO this needs to be fixed
110
- const decodedData = context?.parseSync?.({ attributes });
111
- const fauxAccessors = options._addFauxAttributes(decodedData.attributes);
112
- const bufferViewIndex = options.addBufferView(compressedData);
113
- const glTFMesh = {
114
- primitives: [
115
- {
116
- attributes: fauxAccessors,
117
- mode,
118
- extensions: {
119
- [KHR_DRACO_MESH_COMPRESSION]: {
120
- bufferView: bufferViewIndex,
121
- attributes: fauxAccessors // TODO - verify with spec
122
- }
123
- }
124
- }
125
- ]
126
- };
127
- return glTFMesh;
128
- }
129
- // UTILS
130
- function checkPrimitive(primitive) {
131
- if (!primitive.attributes && Object.keys(primitive.attributes).length > 0) {
132
- throw new Error('glTF: Empty primitive detected: Draco decompression failure?');
133
- }
134
- }
135
- function* makeMeshPrimitiveIterator(scenegraph) {
136
- for (const mesh of scenegraph.json.meshes || []) {
137
- for (const primitive of mesh.primitives) {
138
- yield primitive;
139
- }
140
- }
141
- }
@@ -1,32 +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
- var __importDefault = (this && this.__importDefault) || function (mod) {
6
- return (mod && mod.__esModule) ? mod : { "default": mod };
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.preprocess = exports.name = void 0;
10
- const gltf_scenegraph_1 = __importDefault(require("../api/gltf-scenegraph"));
11
- const KHR_TEXTURE_BASISU = 'KHR_texture_basisu';
12
- /** Extension name */
13
- exports.name = KHR_TEXTURE_BASISU;
14
- /**
15
- * Replaces a texture source reference with the extension texture
16
- * Done in preprocess() to prevent load of default image
17
- */
18
- function preprocess(gltfData, options) {
19
- const scene = new gltf_scenegraph_1.default(gltfData);
20
- const { json } = scene;
21
- for (const texture of json.textures || []) {
22
- const extension = scene.getObjectExtension(texture, KHR_TEXTURE_BASISU);
23
- if (extension) {
24
- // TODO - if multiple texture extensions are present which one wins?
25
- texture.source = extension.source;
26
- }
27
- scene.removeObjectExtension(texture, KHR_TEXTURE_BASISU);
28
- }
29
- // Remove the top-level extension
30
- scene.removeExtension(KHR_TEXTURE_BASISU);
31
- }
32
- exports.preprocess = preprocess;
@@ -1,230 +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
- var __importDefault = (this && this.__importDefault) || function (mod) {
6
- return (mod && mod.__esModule) ? mod : { "default": mod };
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.decode = exports.name = void 0;
10
- const core_1 = require("@math.gl/core");
11
- const gltf_utils_1 = require("../gltf-utils/gltf-utils");
12
- const gltf_constants_1 = require("../gltf-utils/gltf-constants");
13
- const gltf_scenegraph_1 = __importDefault(require("../api/gltf-scenegraph"));
14
- /** Extension name */
15
- const EXT_MESHOPT_TRANSFORM = 'KHR_texture_transform';
16
- exports.name = EXT_MESHOPT_TRANSFORM;
17
- const scratchVector = new core_1.Vector3();
18
- const scratchRotationMatrix = new core_1.Matrix3();
19
- const scratchScaleMatrix = new core_1.Matrix3();
20
- /**
21
- * The extension entry to process the transformation
22
- * @param gltfData gltf buffers and json
23
- * @param options GLTFLoader options
24
- */
25
- async function decode(gltfData, options) {
26
- const gltfScenegraph = new gltf_scenegraph_1.default(gltfData);
27
- const extension = gltfScenegraph.getExtension(EXT_MESHOPT_TRANSFORM);
28
- if (!extension) {
29
- return;
30
- }
31
- const materials = gltfData.json.materials || [];
32
- for (let i = 0; i < materials.length; i++) {
33
- transformTexCoords(i, gltfData);
34
- }
35
- }
36
- exports.decode = decode;
37
- /**
38
- * Transform TEXCOORD by material
39
- * @param materialIndex processing material index
40
- * @param gltfData gltf buffers and json
41
- */
42
- function transformTexCoords(materialIndex, gltfData) {
43
- // Save processed texCoords in order no to process the same twice
44
- const processedTexCoords = [];
45
- const material = gltfData.json.materials?.[materialIndex];
46
- const baseColorTexture = material?.pbrMetallicRoughness?.baseColorTexture;
47
- if (baseColorTexture) {
48
- transformPrimitives(gltfData, materialIndex, baseColorTexture, processedTexCoords);
49
- }
50
- const emisiveTexture = material?.emissiveTexture;
51
- if (emisiveTexture) {
52
- transformPrimitives(gltfData, materialIndex, emisiveTexture, processedTexCoords);
53
- }
54
- const normalTexture = material?.normalTexture;
55
- if (normalTexture) {
56
- transformPrimitives(gltfData, materialIndex, normalTexture, processedTexCoords);
57
- }
58
- const occlusionTexture = material?.occlusionTexture;
59
- if (occlusionTexture) {
60
- transformPrimitives(gltfData, materialIndex, occlusionTexture, processedTexCoords);
61
- }
62
- const metallicRoughnessTexture = material?.pbrMetallicRoughness?.metallicRoughnessTexture;
63
- if (metallicRoughnessTexture) {
64
- transformPrimitives(gltfData, materialIndex, metallicRoughnessTexture, processedTexCoords);
65
- }
66
- }
67
- /**
68
- * Transform primitives of the particular material
69
- * @param gltfData gltf data
70
- * @param materialIndex primitives with this material will be transformed
71
- * @param texture texture object
72
- * @param processedTexCoords storage to save already processed texCoords
73
- */
74
- function transformPrimitives(gltfData, materialIndex, texture, processedTexCoords) {
75
- const transformParameters = getTransformParameters(texture, processedTexCoords);
76
- if (!transformParameters) {
77
- return;
78
- }
79
- const meshes = gltfData.json.meshes || [];
80
- for (const mesh of meshes) {
81
- for (const primitive of mesh.primitives) {
82
- const material = primitive.material;
83
- if (Number.isFinite(material) && materialIndex === material) {
84
- transformPrimitive(gltfData, primitive, transformParameters);
85
- }
86
- }
87
- }
88
- }
89
- /**
90
- * Get parameters for TEXCOORD transformation
91
- * @param texture texture object
92
- * @param processedTexCoords storage to save already processed texCoords
93
- * @returns texCoord couple and transformation matrix
94
- */
95
- function getTransformParameters(texture, processedTexCoords) {
96
- const textureInfo = texture.extensions?.[EXT_MESHOPT_TRANSFORM];
97
- const { texCoord: originalTexCoord = 0 } = texture;
98
- // If texCoord is not set in the extension, original attribute data will be replaced
99
- const { texCoord = originalTexCoord } = textureInfo;
100
- // Make sure that couple [originalTexCoord, extensionTexCoord] is not processed twice
101
- const isProcessed = processedTexCoords.findIndex(([original, newTexCoord]) => original === originalTexCoord && newTexCoord === texCoord) !== -1;
102
- if (!isProcessed) {
103
- const matrix = makeTransformationMatrix(textureInfo);
104
- if (originalTexCoord !== texCoord) {
105
- texture.texCoord = texCoord;
106
- }
107
- processedTexCoords.push([originalTexCoord, texCoord]);
108
- return { originalTexCoord, texCoord, matrix };
109
- }
110
- return null;
111
- }
112
- /**
113
- * Transform `TEXCOORD_0` attribute in the primitive
114
- * @param gltfData gltf data
115
- * @param primitive primitive object
116
- * @param transformParameters texCoord couple and transformation matrix
117
- */
118
- function transformPrimitive(gltfData, primitive, transformParameters) {
119
- const { originalTexCoord, texCoord, matrix } = transformParameters;
120
- const texCoordAccessor = primitive.attributes[`TEXCOORD_${originalTexCoord}`];
121
- if (Number.isFinite(texCoordAccessor)) {
122
- // Get accessor of the `TEXCOORD_0` attribute
123
- const accessor = gltfData.json.accessors?.[texCoordAccessor];
124
- if (accessor && accessor.bufferView) {
125
- // Get `bufferView` of the `accessor`
126
- const bufferView = gltfData.json.bufferViews?.[accessor.bufferView];
127
- if (bufferView) {
128
- // Get `arrayBuffer` the `bufferView` look at
129
- const { arrayBuffer, byteOffset: bufferByteOffset } = gltfData.buffers[bufferView.buffer];
130
- // Resulting byteOffset is sum of the buffer, accessor and bufferView byte offsets
131
- const byteOffset = (bufferByteOffset || 0) + (accessor.byteOffset || 0) + (bufferView.byteOffset || 0);
132
- // Deduce TypedArray type and its length from `accessor` and `bufferView` data
133
- const { ArrayType, length } = (0, gltf_utils_1.getAccessorArrayTypeAndLength)(accessor, bufferView);
134
- // Number of bytes each component occupies
135
- const bytes = gltf_constants_1.BYTES[accessor.componentType];
136
- // Number of components. For the `TEXCOORD_0` with `VEC2` type, it must return 2
137
- const components = gltf_constants_1.COMPONENTS[accessor.type];
138
- // Multiplier to calculate the address of the `TEXCOORD_0` element in the arrayBuffer
139
- const elementAddressScale = bufferView.byteStride || bytes * components;
140
- // Data transform to Float32Array
141
- const result = new Float32Array(length);
142
- for (let i = 0; i < accessor.count; i++) {
143
- // Take [u, v] couple from the arrayBuffer
144
- const uv = new ArrayType(arrayBuffer, byteOffset + i * elementAddressScale, 2);
145
- // Set and transform Vector3 per https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_texture_transform#overview
146
- scratchVector.set(uv[0], uv[1], 1);
147
- scratchVector.transformByMatrix3(matrix);
148
- // Save result in Float32Array
149
- result.set([scratchVector[0], scratchVector[1]], i * components);
150
- }
151
- // If texCoord the same, replace gltf structural data
152
- if (originalTexCoord === texCoord) {
153
- updateGltf(accessor, bufferView, gltfData.buffers, result);
154
- }
155
- else {
156
- // If texCoord change, create new attribute
157
- createAttribute(texCoord, accessor, primitive, gltfData, result);
158
- }
159
- }
160
- }
161
- }
162
- }
163
- /**
164
- * Update GLTF structural objects with new data as we create new `Float32Array` for `TEXCOORD_0`.
165
- * @param accessor accessor to change
166
- * @param bufferView bufferView to change
167
- * @param buffers binary buffers
168
- * @param newTexcoordArray typed array with data after transformation
169
- */
170
- function updateGltf(accessor, bufferView, buffers, newTexCoordArray) {
171
- accessor.componentType = 5126;
172
- buffers.push({
173
- arrayBuffer: newTexCoordArray.buffer,
174
- byteOffset: 0,
175
- byteLength: newTexCoordArray.buffer.byteLength
176
- });
177
- bufferView.buffer = buffers.length - 1;
178
- bufferView.byteLength = newTexCoordArray.buffer.byteLength;
179
- bufferView.byteOffset = 0;
180
- delete bufferView.byteStride;
181
- }
182
- /**
183
- *
184
- * @param newTexCoord new `texCoord` value
185
- * @param originalAccessor original accessor object, that store data before transformation
186
- * @param primitive primitive object
187
- * @param gltfData gltf data
188
- * @param newTexCoordArray typed array with data after transformation
189
- * @returns
190
- */
191
- function createAttribute(newTexCoord, originalAccessor, primitive, gltfData, newTexCoordArray) {
192
- gltfData.buffers.push({
193
- arrayBuffer: newTexCoordArray.buffer,
194
- byteOffset: 0,
195
- byteLength: newTexCoordArray.buffer.byteLength
196
- });
197
- const bufferViews = gltfData.json.bufferViews;
198
- if (!bufferViews) {
199
- return;
200
- }
201
- bufferViews.push({
202
- buffer: gltfData.buffers.length - 1,
203
- byteLength: newTexCoordArray.buffer.byteLength,
204
- byteOffset: 0
205
- });
206
- const accessors = gltfData.json.accessors;
207
- if (!accessors) {
208
- return;
209
- }
210
- accessors.push({
211
- bufferView: bufferViews?.length - 1,
212
- byteOffset: 0,
213
- componentType: 5126,
214
- count: originalAccessor.count,
215
- type: 'VEC2'
216
- });
217
- primitive.attributes[`TEXCOORD_${newTexCoord}`] = accessors.length - 1;
218
- }
219
- /**
220
- * Construct transformation matrix from the extension data (transition, rotation, scale)
221
- * @param extensionData extension data
222
- * @returns transformation matrix
223
- */
224
- function makeTransformationMatrix(extensionData) {
225
- const { offset = [0, 0], rotation = 0, scale = [1, 1] } = extensionData;
226
- const translationMatirx = new core_1.Matrix3().set(1, 0, 0, 0, 1, 0, offset[0], offset[1], 1);
227
- const rotationMatirx = scratchRotationMatrix.set(Math.cos(rotation), Math.sin(rotation), 0, -Math.sin(rotation), Math.cos(rotation), 0, 0, 0, 1);
228
- const scaleMatrix = scratchScaleMatrix.set(scale[0], 0, 0, 0, scale[1], 0, 0, 0, 1);
229
- return translationMatirx.multiplyRight(rotationMatirx).multiplyRight(scaleMatrix);
230
- }