@loaders.gl/gltf 4.0.0-alpha.5 → 4.0.0-alpha.6
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.
- package/dist/bundle.js +2 -2
- package/dist/dist.min.js +2037 -1013
- package/dist/es5/bundle.js +6 -0
- package/dist/es5/bundle.js.map +1 -0
- package/dist/es5/glb-loader.js +56 -0
- package/dist/es5/glb-loader.js.map +1 -0
- package/dist/es5/glb-writer.js +35 -0
- package/dist/es5/glb-writer.js.map +1 -0
- package/dist/es5/gltf-loader.js +81 -0
- package/dist/es5/gltf-loader.js.map +1 -0
- package/dist/es5/gltf-writer.js +35 -0
- package/dist/es5/gltf-writer.js.map +1 -0
- package/dist/es5/index.js +49 -0
- package/dist/es5/index.js.map +1 -0
- package/dist/es5/lib/api/gltf-extensions.js +109 -0
- package/dist/es5/lib/api/gltf-extensions.js.map +1 -0
- package/dist/es5/lib/api/gltf-scenegraph.js +578 -0
- package/dist/es5/lib/api/gltf-scenegraph.js.map +1 -0
- package/dist/es5/lib/api/normalize-gltf-v1.js +331 -0
- package/dist/es5/lib/api/normalize-gltf-v1.js.map +1 -0
- package/dist/es5/lib/api/post-process-gltf.js +422 -0
- package/dist/es5/lib/api/post-process-gltf.js.map +1 -0
- package/dist/es5/lib/encoders/encode-glb.js +61 -0
- package/dist/es5/lib/encoders/encode-glb.js.map +1 -0
- package/dist/es5/lib/encoders/encode-gltf.js +21 -0
- package/dist/es5/lib/encoders/encode-gltf.js.map +1 -0
- package/dist/es5/lib/extensions/EXT_meshopt_compression.js +96 -0
- package/dist/es5/lib/extensions/EXT_meshopt_compression.js.map +1 -0
- package/dist/es5/lib/extensions/EXT_texture_webp.js +44 -0
- package/dist/es5/lib/extensions/EXT_texture_webp.js.map +1 -0
- package/dist/es5/lib/extensions/KHR_binary_gltf.js +40 -0
- package/dist/es5/lib/extensions/KHR_binary_gltf.js.map +1 -0
- package/dist/es5/lib/extensions/KHR_draco_mesh_compression.js +246 -0
- package/dist/es5/lib/extensions/KHR_draco_mesh_compression.js.map +1 -0
- package/dist/es5/lib/extensions/KHR_texture_basisu.js +37 -0
- package/dist/es5/lib/extensions/KHR_texture_basisu.js.map +1 -0
- package/dist/es5/lib/extensions/KHR_texture_transform.js +229 -0
- package/dist/es5/lib/extensions/KHR_texture_transform.js.map +1 -0
- package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js +103 -0
- package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -0
- package/dist/es5/lib/extensions/deprecated/KHR_lights_punctual.js +99 -0
- package/dist/es5/lib/extensions/deprecated/KHR_lights_punctual.js.map +1 -0
- package/dist/es5/lib/extensions/deprecated/KHR_materials_unlit.js +76 -0
- package/dist/es5/lib/extensions/deprecated/KHR_materials_unlit.js.map +1 -0
- package/dist/es5/lib/extensions/deprecated/KHR_techniques_webgl.js +113 -0
- package/dist/es5/lib/extensions/deprecated/KHR_techniques_webgl.js.map +1 -0
- package/dist/es5/lib/gltf-utils/get-typed-array.js +23 -0
- package/dist/es5/lib/gltf-utils/get-typed-array.js.map +1 -0
- package/dist/es5/lib/gltf-utils/gltf-attribute-utils.js +68 -0
- package/dist/es5/lib/gltf-utils/gltf-attribute-utils.js.map +1 -0
- package/dist/es5/lib/gltf-utils/gltf-constants.js +48 -0
- package/dist/es5/lib/gltf-utils/gltf-constants.js.map +1 -0
- package/dist/es5/lib/gltf-utils/gltf-utils.js +62 -0
- package/dist/es5/lib/gltf-utils/gltf-utils.js.map +1 -0
- package/dist/es5/lib/gltf-utils/resolve-url.js +18 -0
- package/dist/es5/lib/gltf-utils/resolve-url.js.map +1 -0
- package/dist/es5/lib/parsers/parse-glb.js +120 -0
- package/dist/es5/lib/parsers/parse-glb.js.map +1 -0
- package/dist/es5/lib/parsers/parse-gltf.js +287 -0
- package/dist/es5/lib/parsers/parse-gltf.js.map +1 -0
- package/dist/es5/lib/types/glb-types.js +2 -0
- package/dist/es5/lib/types/glb-types.js.map +1 -0
- package/dist/es5/lib/types/gltf-json-schema.js +2 -0
- package/dist/es5/lib/types/gltf-json-schema.js.map +1 -0
- package/dist/es5/lib/types/gltf-postprocessed-schema.js +2 -0
- package/dist/es5/lib/types/gltf-postprocessed-schema.js.map +1 -0
- package/dist/es5/lib/types/gltf-types.js +2 -0
- package/dist/es5/lib/types/gltf-types.js.map +1 -0
- package/dist/es5/lib/utils/assert.js +12 -0
- package/dist/es5/lib/utils/assert.js.map +1 -0
- package/dist/es5/lib/utils/version.js +9 -0
- package/dist/es5/lib/utils/version.js.map +1 -0
- package/dist/es5/meshopt/meshopt-decoder.js +215 -0
- package/dist/es5/meshopt/meshopt-decoder.js.map +1 -0
- package/dist/es5/webp/webp.js +83 -0
- package/dist/es5/webp/webp.js.map +1 -0
- package/dist/esm/bundle.js +4 -0
- package/dist/esm/bundle.js.map +1 -0
- package/dist/esm/glb-loader.js +31 -0
- package/dist/esm/glb-loader.js.map +1 -0
- package/dist/esm/glb-writer.js +27 -0
- package/dist/esm/glb-writer.js.map +1 -0
- package/dist/esm/gltf-loader.js +51 -0
- package/dist/esm/gltf-loader.js.map +1 -0
- package/dist/esm/gltf-writer.js +28 -0
- package/dist/esm/gltf-writer.js.map +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lib/api/gltf-extensions.js +35 -0
- package/dist/esm/lib/api/gltf-extensions.js.map +1 -0
- package/dist/esm/lib/api/gltf-scenegraph.js +441 -0
- package/dist/esm/lib/api/gltf-scenegraph.js.map +1 -0
- package/dist/esm/lib/api/normalize-gltf-v1.js +204 -0
- package/dist/esm/lib/api/normalize-gltf-v1.js.map +1 -0
- package/dist/esm/lib/api/post-process-gltf.js +341 -0
- package/dist/esm/lib/api/post-process-gltf.js.map +1 -0
- package/dist/esm/lib/encoders/encode-glb.js +54 -0
- package/dist/esm/lib/encoders/encode-glb.js.map +1 -0
- package/dist/esm/lib/encoders/encode-gltf.js +14 -0
- package/dist/esm/lib/encoders/encode-gltf.js.map +1 -0
- package/dist/esm/lib/extensions/EXT_meshopt_compression.js +42 -0
- package/dist/esm/lib/extensions/EXT_meshopt_compression.js.map +1 -0
- package/dist/esm/lib/extensions/EXT_texture_webp.js +25 -0
- package/dist/esm/lib/extensions/EXT_texture_webp.js.map +1 -0
- package/dist/esm/lib/extensions/KHR_binary_gltf.js +21 -0
- package/dist/esm/lib/extensions/KHR_binary_gltf.js.map +1 -0
- package/dist/esm/lib/extensions/KHR_draco_mesh_compression.js +110 -0
- package/dist/esm/lib/extensions/KHR_draco_mesh_compression.js.map +1 -0
- package/dist/esm/lib/extensions/KHR_texture_basisu.js +18 -0
- package/dist/esm/lib/extensions/KHR_texture_basisu.js.map +1 -0
- package/dist/esm/lib/extensions/KHR_texture_transform.js +181 -0
- package/dist/esm/lib/extensions/KHR_texture_transform.js.map +1 -0
- package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js +77 -0
- package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -0
- package/dist/esm/lib/extensions/deprecated/KHR_lights_punctual.js +42 -0
- package/dist/esm/lib/extensions/deprecated/KHR_lights_punctual.js.map +1 -0
- package/dist/esm/lib/extensions/deprecated/KHR_materials_unlit.js +33 -0
- package/dist/esm/lib/extensions/deprecated/KHR_materials_unlit.js.map +1 -0
- package/dist/esm/lib/extensions/deprecated/KHR_techniques_webgl.js +61 -0
- package/dist/esm/lib/extensions/deprecated/KHR_techniques_webgl.js.map +1 -0
- package/dist/esm/lib/gltf-utils/get-typed-array.js +16 -0
- package/dist/esm/lib/gltf-utils/get-typed-array.js.map +1 -0
- package/dist/esm/lib/gltf-utils/gltf-attribute-utils.js +62 -0
- package/dist/esm/lib/gltf-utils/gltf-attribute-utils.js.map +1 -0
- package/dist/esm/lib/gltf-utils/gltf-constants.js +37 -0
- package/dist/esm/lib/gltf-utils/gltf-constants.js.map +1 -0
- package/dist/esm/lib/gltf-utils/gltf-utils.js +54 -0
- package/dist/esm/lib/gltf-utils/gltf-utils.js.map +1 -0
- package/dist/esm/lib/gltf-utils/resolve-url.js +12 -0
- package/dist/esm/lib/gltf-utils/resolve-url.js.map +1 -0
- package/dist/esm/lib/parsers/parse-glb.js +114 -0
- package/dist/esm/lib/parsers/parse-glb.js.map +1 -0
- package/dist/esm/lib/parsers/parse-gltf.js +156 -0
- package/dist/esm/lib/parsers/parse-gltf.js.map +1 -0
- package/dist/esm/lib/types/glb-types.js +2 -0
- package/dist/esm/lib/types/glb-types.js.map +1 -0
- package/dist/esm/lib/types/gltf-json-schema.js +2 -0
- package/dist/esm/lib/types/gltf-json-schema.js.map +1 -0
- package/dist/esm/lib/types/gltf-postprocessed-schema.js +2 -0
- package/dist/esm/lib/types/gltf-postprocessed-schema.js.map +1 -0
- package/dist/esm/lib/types/gltf-types.js +2 -0
- package/dist/esm/lib/types/gltf-types.js.map +1 -0
- package/dist/esm/lib/utils/assert.js +6 -0
- package/dist/esm/lib/utils/assert.js.map +1 -0
- package/dist/esm/lib/utils/version.js +2 -0
- package/dist/esm/lib/utils/version.js.map +1 -0
- package/dist/esm/meshopt/meshopt-decoder.js +91 -0
- package/dist/esm/meshopt/meshopt-decoder.js.map +1 -0
- package/dist/esm/meshopt/meshopt-encoder.ts.disabled +409 -0
- package/dist/esm/webp/webp.js +25 -0
- package/dist/esm/webp/webp.js.map +1 -0
- package/dist/glb-loader.d.ts +3 -2
- package/dist/glb-loader.d.ts.map +1 -1
- package/dist/glb-loader.js +33 -28
- package/dist/glb-writer.d.ts +7 -16
- package/dist/glb-writer.d.ts.map +1 -1
- package/dist/glb-writer.js +34 -26
- package/dist/gltf-loader.d.ts +3 -2
- package/dist/gltf-loader.d.ts.map +1 -1
- package/dist/gltf-loader.js +46 -43
- package/dist/gltf-writer.d.ts +2 -2
- package/dist/gltf-writer.d.ts.map +1 -1
- package/dist/gltf-writer.js +29 -26
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -7
- package/dist/lib/api/gltf-extensions.d.ts +6 -1
- package/dist/lib/api/gltf-extensions.d.ts.map +1 -1
- package/dist/lib/api/gltf-extensions.js +79 -31
- package/dist/lib/api/gltf-scenegraph.d.ts +5 -11
- package/dist/lib/api/gltf-scenegraph.d.ts.map +1 -1
- package/dist/lib/api/gltf-scenegraph.js +563 -512
- package/dist/lib/api/normalize-gltf-v1.d.ts.map +1 -1
- package/dist/lib/api/normalize-gltf-v1.js +278 -229
- package/dist/lib/api/post-process-gltf.js +324 -376
- package/dist/lib/encoders/encode-glb.d.ts +2 -1
- package/dist/lib/encoders/encode-glb.d.ts.map +1 -1
- package/dist/lib/encoders/encode-glb.js +56 -58
- package/dist/lib/encoders/encode-gltf.d.ts +18 -0
- package/dist/lib/encoders/encode-gltf.d.ts.map +1 -1
- package/dist/lib/encoders/encode-gltf.js +33 -12
- package/dist/lib/extensions/EXT_meshopt_compression.d.ts +0 -3
- package/dist/lib/extensions/EXT_meshopt_compression.d.ts.map +1 -1
- package/dist/lib/extensions/EXT_meshopt_compression.js +39 -50
- package/dist/lib/extensions/EXT_texture_webp.d.ts.map +1 -1
- package/dist/lib/extensions/EXT_texture_webp.js +35 -28
- package/dist/lib/extensions/KHR_binary_gltf.js +39 -23
- package/dist/lib/extensions/KHR_draco_mesh_compression.js +126 -111
- package/dist/lib/extensions/KHR_texture_basisu.d.ts.map +1 -1
- package/dist/lib/extensions/KHR_texture_basisu.js +29 -19
- package/dist/lib/extensions/KHR_texture_transform.d.ts +13 -0
- package/dist/lib/extensions/KHR_texture_transform.d.ts.map +1 -0
- package/dist/lib/extensions/KHR_texture_transform.js +230 -0
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts +6 -0
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts.map +1 -0
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +118 -0
- package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +57 -44
- package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +42 -32
- package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts.map +1 -1
- package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +76 -62
- package/dist/lib/gltf-utils/get-typed-array.js +39 -14
- package/dist/lib/gltf-utils/gltf-attribute-utils.js +65 -65
- package/dist/lib/gltf-utils/gltf-constants.d.ts +17 -0
- package/dist/lib/gltf-utils/gltf-constants.d.ts.map +1 -1
- package/dist/lib/gltf-utils/gltf-constants.js +38 -32
- package/dist/lib/gltf-utils/gltf-utils.js +52 -45
- package/dist/lib/gltf-utils/resolve-url.js +17 -15
- package/dist/lib/parsers/parse-glb.d.ts +1 -1
- package/dist/lib/parsers/parse-glb.d.ts.map +1 -1
- package/dist/lib/parsers/parse-glb.js +120 -107
- package/dist/lib/parsers/parse-gltf.d.ts +2 -2
- package/dist/lib/parsers/parse-gltf.d.ts.map +1 -1
- package/dist/lib/parsers/parse-gltf.js +187 -154
- package/dist/lib/types/glb-types.d.ts +3 -3
- package/dist/lib/types/glb-types.d.ts.map +1 -1
- package/dist/lib/types/glb-types.js +2 -2
- package/dist/lib/types/gltf-json-schema.d.ts +396 -11
- package/dist/lib/types/gltf-json-schema.d.ts.map +1 -1
- package/dist/lib/types/gltf-json-schema.js +4 -2
- package/dist/lib/types/gltf-postprocessed-schema.d.ts +10 -10
- package/dist/lib/types/gltf-postprocessed-schema.d.ts.map +1 -1
- package/dist/lib/types/gltf-postprocessed-schema.js +4 -2
- package/dist/lib/types/gltf-types.d.ts +8 -5
- package/dist/lib/types/gltf-types.d.ts.map +1 -1
- package/dist/lib/types/gltf-types.js +3 -2
- package/dist/lib/utils/assert.js +11 -5
- package/dist/lib/utils/version.js +7 -2
- package/dist/meshopt/meshopt-decoder.js +97 -82
- package/dist/webp/webp.d.ts +1 -1
- package/dist/webp/webp.d.ts.map +1 -1
- package/dist/webp/webp.js +32 -23
- package/package.json +9 -8
- package/src/glb-loader.ts +1 -1
- package/src/glb-writer.ts +9 -3
- package/src/gltf-writer.ts +2 -2
- package/src/index.ts +10 -1
- package/src/lib/api/gltf-extensions.ts +13 -2
- package/src/lib/api/gltf-scenegraph.ts +21 -12
- package/src/lib/api/normalize-gltf-v1.ts +2 -1
- package/src/lib/encoders/encode-glb.ts +8 -1
- package/src/lib/encoders/encode-gltf.ts +18 -9
- package/src/lib/extensions/EXT_meshopt_compression.ts +12 -19
- package/src/lib/extensions/EXT_texture_webp.ts +3 -2
- package/src/lib/extensions/KHR_binary_gltf.ts +1 -1
- package/src/lib/extensions/KHR_draco_mesh_compression.ts +1 -1
- package/src/lib/extensions/KHR_texture_basisu.ts +1 -0
- package/src/lib/extensions/KHR_texture_transform.ts +305 -0
- package/src/lib/extensions/deprecated/EXT_feature_metadata.ts +163 -0
- package/src/lib/extensions/deprecated/KHR_materials_unlit.ts +3 -3
- package/src/lib/extensions/deprecated/KHR_techniques_webgl.ts +1 -0
- package/src/lib/gltf-utils/gltf-constants.ts +2 -2
- package/src/lib/parsers/parse-gltf.ts +10 -1
- package/src/lib/types/glb-types.ts +1 -1
- package/src/lib/types/gltf-json-schema.ts +434 -5
- package/src/lib/types/gltf-postprocessed-schema.ts +9 -9
- package/src/lib/types/gltf-types.ts +24 -3
- package/dist/bundle.js.map +0 -1
- package/dist/glb-loader.js.map +0 -1
- package/dist/glb-writer.js.map +0 -1
- package/dist/gltf-loader.js.map +0 -1
- package/dist/gltf-writer.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/api/gltf-extensions.js.map +0 -1
- package/dist/lib/api/gltf-scenegraph.js.map +0 -1
- package/dist/lib/api/normalize-gltf-v1.js.map +0 -1
- package/dist/lib/api/post-process-gltf.js.map +0 -1
- package/dist/lib/encoders/encode-glb.js.map +0 -1
- package/dist/lib/encoders/encode-gltf.js.map +0 -1
- package/dist/lib/extensions/EXT_meshopt_compression.js.map +0 -1
- package/dist/lib/extensions/EXT_texture_webp.js.map +0 -1
- package/dist/lib/extensions/KHR_binary_gltf.js.map +0 -1
- package/dist/lib/extensions/KHR_draco_mesh_compression.js.map +0 -1
- package/dist/lib/extensions/KHR_texture_basisu.js.map +0 -1
- package/dist/lib/extensions/deprecated/KHR_lights_punctual.js.map +0 -1
- package/dist/lib/extensions/deprecated/KHR_materials_unlit.js.map +0 -1
- package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js.map +0 -1
- package/dist/lib/gltf-utils/get-typed-array.js.map +0 -1
- package/dist/lib/gltf-utils/gltf-attribute-utils.js.map +0 -1
- package/dist/lib/gltf-utils/gltf-constants.js.map +0 -1
- package/dist/lib/gltf-utils/gltf-utils.js.map +0 -1
- package/dist/lib/gltf-utils/resolve-url.js.map +0 -1
- package/dist/lib/parsers/parse-glb.js.map +0 -1
- package/dist/lib/parsers/parse-gltf.js.map +0 -1
- package/dist/lib/types/glb-types.js.map +0 -1
- package/dist/lib/types/gltf-json-schema.js.map +0 -1
- package/dist/lib/types/gltf-postprocessed-schema.js.map +0 -1
- package/dist/lib/types/gltf-types.js.map +0 -1
- package/dist/lib/utils/assert.js.map +0 -1
- package/dist/lib/utils/version.js.map +0 -1
- package/dist/meshopt/meshopt-decoder.js.map +0 -1
- package/dist/webp/webp.js.map +0 -1
- /package/dist/{meshopt → es5/meshopt}/meshopt-encoder.ts.disabled +0 -0
|
@@ -10,7 +10,8 @@ import type {
|
|
|
10
10
|
GLTFTexture,
|
|
11
11
|
GLTFImage,
|
|
12
12
|
GLTFBuffer,
|
|
13
|
-
GLTFBufferView
|
|
13
|
+
GLTFBufferView,
|
|
14
|
+
GLTFWithBuffers
|
|
14
15
|
} from '../types/gltf-types';
|
|
15
16
|
|
|
16
17
|
import {getBinaryImageMetadata} from '@loaders.gl/images';
|
|
@@ -22,12 +23,6 @@ import {
|
|
|
22
23
|
getComponentTypeFromArray
|
|
23
24
|
} from '../gltf-utils/gltf-utils';
|
|
24
25
|
|
|
25
|
-
type GLTFWithBuffers = {
|
|
26
|
-
json: GLTF;
|
|
27
|
-
buffers: any[];
|
|
28
|
-
binary?: ArrayBuffer;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
26
|
const DEFAULT_GLTF_JSON: GLTF = {
|
|
32
27
|
asset: {
|
|
33
28
|
version: '2.0',
|
|
@@ -68,13 +63,13 @@ export default class GLTFScenegraph {
|
|
|
68
63
|
return this.gltf.json;
|
|
69
64
|
}
|
|
70
65
|
|
|
71
|
-
getApplicationData(key: string):
|
|
66
|
+
getApplicationData(key: string): unknown {
|
|
72
67
|
// TODO - Data is already unpacked by GLBParser
|
|
73
68
|
const data = this.json[key];
|
|
74
69
|
return data;
|
|
75
70
|
}
|
|
76
71
|
|
|
77
|
-
getExtraData(key: string): {[key: string]:
|
|
72
|
+
getExtraData(key: string): {[key: string]: unknown} {
|
|
78
73
|
// TODO - Data is already unpacked by GLBParser
|
|
79
74
|
const extras = this.json.extras || {};
|
|
80
75
|
return extras[key];
|
|
@@ -99,6 +94,10 @@ export default class GLTFScenegraph {
|
|
|
99
94
|
return this.json.extensionsUsed || [];
|
|
100
95
|
}
|
|
101
96
|
|
|
97
|
+
getRemovedExtensions(): string[] {
|
|
98
|
+
return (this.json.extensionsRemoved || []) as string[];
|
|
99
|
+
}
|
|
100
|
+
|
|
102
101
|
getObjectExtension<T = Extension>(object: {[key: string]: any}, extensionName: string): T | null {
|
|
103
102
|
const extensions = object.extensions || {};
|
|
104
103
|
return extensions[extensionName];
|
|
@@ -157,7 +156,7 @@ export default class GLTFScenegraph {
|
|
|
157
156
|
if (typeof index === 'object') {
|
|
158
157
|
return index;
|
|
159
158
|
}
|
|
160
|
-
const object = this.json[array] && this.json[array][index];
|
|
159
|
+
const object = this.json[array] && (this.json[array] as {}[])[index];
|
|
161
160
|
if (!object) {
|
|
162
161
|
throw new Error(`glTF file error: Could not find ${array}[${index}]`); // eslint-disable-line
|
|
163
162
|
}
|
|
@@ -234,7 +233,7 @@ export default class GLTFScenegraph {
|
|
|
234
233
|
*/
|
|
235
234
|
addExtraData(key: string, data: object): GLTFScenegraph {
|
|
236
235
|
this.json.extras = this.json.extras || {};
|
|
237
|
-
this.json.extras[key] = data;
|
|
236
|
+
(this.json.extras as Record<string, unknown>)[key] = data;
|
|
238
237
|
return this;
|
|
239
238
|
}
|
|
240
239
|
|
|
@@ -269,7 +268,7 @@ export default class GLTFScenegraph {
|
|
|
269
268
|
addExtension(extensionName: string, extensionData: object = {}): object {
|
|
270
269
|
assert(extensionData);
|
|
271
270
|
this.json.extensions = this.json.extensions || {};
|
|
272
|
-
this.json.extensions[extensionName] = extensionData;
|
|
271
|
+
(this.json.extensions as Record<string, unknown>)[extensionName] = extensionData;
|
|
273
272
|
this.registerUsedExtension(extensionName);
|
|
274
273
|
return extensionData;
|
|
275
274
|
}
|
|
@@ -309,6 +308,9 @@ export default class GLTFScenegraph {
|
|
|
309
308
|
* Removes an extension from the top-level list
|
|
310
309
|
*/
|
|
311
310
|
removeExtension(extensionName: string): void {
|
|
311
|
+
if (!this.getExtension(extensionName)) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
312
314
|
if (this.json.extensionsRequired) {
|
|
313
315
|
this._removeStringFromArray(this.json.extensionsRequired, extensionName);
|
|
314
316
|
}
|
|
@@ -318,6 +320,13 @@ export default class GLTFScenegraph {
|
|
|
318
320
|
if (this.json.extensions) {
|
|
319
321
|
delete this.json.extensions[extensionName];
|
|
320
322
|
}
|
|
323
|
+
if (!Array.isArray(this.json.extensionsRemoved)) {
|
|
324
|
+
this.json.extensionsRemoved = [];
|
|
325
|
+
}
|
|
326
|
+
const extensionsRemoved = this.json.extensionsRemoved as string[];
|
|
327
|
+
if (!extensionsRemoved.includes(extensionName)) {
|
|
328
|
+
extensionsRemoved.push(extensionName);
|
|
329
|
+
}
|
|
321
330
|
}
|
|
322
331
|
|
|
323
332
|
/**
|
|
@@ -298,7 +298,8 @@ class GLTFV1Normalizer {
|
|
|
298
298
|
roughnessFactor: 1
|
|
299
299
|
};
|
|
300
300
|
|
|
301
|
-
const textureId =
|
|
301
|
+
const textureId =
|
|
302
|
+
material.values?.tex || material.values?.texture2d_0 || material.values?.diffuseTex;
|
|
302
303
|
const textureIndex = json.textures.findIndex((texture) => texture.id === textureId);
|
|
303
304
|
if (textureIndex !== -1) {
|
|
304
305
|
material.pbrMetallicRoughness.baseColorTexture = {index: textureIndex};
|
|
@@ -10,10 +10,17 @@ const MAGIC_BIN = 0x004e4942; // BIN\0 in ASCII
|
|
|
10
10
|
|
|
11
11
|
const LE = true; // Binary GLTF is little endian.
|
|
12
12
|
|
|
13
|
+
export type GLBEncodeOptions = {};
|
|
14
|
+
|
|
13
15
|
// Encode the full GLB buffer with header etc
|
|
14
16
|
// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#
|
|
15
17
|
// glb-file-format-specification
|
|
16
|
-
export default function encodeGLBSync(
|
|
18
|
+
export default function encodeGLBSync(
|
|
19
|
+
glb,
|
|
20
|
+
dataView,
|
|
21
|
+
byteOffset = 0,
|
|
22
|
+
options: GLBEncodeOptions = {}
|
|
23
|
+
) {
|
|
17
24
|
const {magic = MAGIC_glTF, version = 2, json = {}, binary} = glb;
|
|
18
25
|
|
|
19
26
|
const byteOffsetStart = byteOffset;
|
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
import encodeGLBSync from './encode-glb';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
// Returns an ArrayBuffer that represents the complete GLB image that can be saved to file
|
|
5
|
-
//
|
|
6
|
-
// TODO - Does not support encoding to non-GLB versions of glTF format
|
|
7
|
-
// - Encode as a textual JSON file with binary data in base64 data URLs.
|
|
8
|
-
// - Encode as a JSON with all images (and buffers?) in separate binary files
|
|
9
|
-
//
|
|
10
|
-
// glb-file-format-specification
|
|
11
|
-
// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#
|
|
3
|
+
export type GLTFEncodeOptions = Record<string, any>;
|
|
12
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Encode the full glTF file as a binary GLB file
|
|
7
|
+
* Returns an ArrayBuffer that represents the complete GLB image that can be saved to file
|
|
8
|
+
*
|
|
9
|
+
* @todo - Does not support encoding to non-GLB versions of glTF format. Other formats
|
|
10
|
+
* - Encode as a textual JSON file with binary data in base64 data URLs.
|
|
11
|
+
* - Encode as a JSON with all images (and buffers?) in separate binary files
|
|
12
|
+
*
|
|
13
|
+
* glb-file-format-specification
|
|
14
|
+
* @see https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#
|
|
15
|
+
*
|
|
16
|
+
* @param gltf
|
|
17
|
+
* @param arrayBuffer
|
|
18
|
+
* @param byteOffset
|
|
19
|
+
* @param options
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
13
22
|
export function encodeGLTFSync(gltf, arrayBuffer, byteOffset, options) {
|
|
14
23
|
convertBuffersToBase64(gltf);
|
|
15
24
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import type {GLTF, GLTFBufferView, GLTF_EXT_meshopt_compression} from '../types/gltf-types';
|
|
3
3
|
import type {GLTFLoaderOptions} from '../../gltf-loader';
|
|
4
4
|
import GLTFScenegraph from '../api/gltf-scenegraph';
|
|
5
|
-
import {
|
|
5
|
+
import {meshoptDecodeGltfBuffer} from '../../meshopt/meshopt-decoder';
|
|
6
6
|
|
|
7
7
|
// @ts-ignore
|
|
8
8
|
// eslint-disable-next-line
|
|
@@ -16,16 +16,6 @@ const EXT_MESHOPT_COMPRESSION = 'EXT_meshopt_compression';
|
|
|
16
16
|
|
|
17
17
|
export const name = EXT_MESHOPT_COMPRESSION;
|
|
18
18
|
|
|
19
|
-
export function preprocess(gltfData: {json: GLTF}) {
|
|
20
|
-
const scenegraph = new GLTFScenegraph(gltfData);
|
|
21
|
-
if (
|
|
22
|
-
scenegraph.getRequiredExtensions().includes(EXT_MESHOPT_COMPRESSION) &&
|
|
23
|
-
!isMeshoptSupported()
|
|
24
|
-
) {
|
|
25
|
-
throw new Error(`gltf: Required extension ${EXT_MESHOPT_COMPRESSION} not supported by browser`);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
19
|
export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions) {
|
|
30
20
|
const scenegraph = new GLTFScenegraph(gltfData);
|
|
31
21
|
|
|
@@ -41,7 +31,7 @@ export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions)
|
|
|
41
31
|
// Decompress meshes in parallel
|
|
42
32
|
await Promise.all(promises);
|
|
43
33
|
|
|
44
|
-
// We have now decompressed all primitives, so remove the top-level
|
|
34
|
+
// We have now decompressed all primitives, so remove the top-level extension
|
|
45
35
|
scenegraph.removeExtension(EXT_MESHOPT_COMPRESSION);
|
|
46
36
|
}
|
|
47
37
|
|
|
@@ -55,21 +45,24 @@ async function decodeMeshoptBufferView(
|
|
|
55
45
|
EXT_MESHOPT_COMPRESSION
|
|
56
46
|
);
|
|
57
47
|
if (meshoptExtension) {
|
|
58
|
-
const buffer = bufferView.buffer;
|
|
59
|
-
|
|
60
48
|
const {
|
|
61
49
|
byteOffset = 0,
|
|
62
50
|
byteLength = 0,
|
|
63
51
|
byteStride,
|
|
64
52
|
count,
|
|
65
53
|
mode,
|
|
66
|
-
filter = 'NONE'
|
|
54
|
+
filter = 'NONE',
|
|
55
|
+
buffer: bufferIndex
|
|
67
56
|
} = meshoptExtension;
|
|
57
|
+
const buffer = scenegraph.gltf.buffers[bufferIndex];
|
|
68
58
|
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
59
|
+
const source = new Uint8Array(buffer.arrayBuffer, buffer.byteOffset + byteOffset, byteLength);
|
|
60
|
+
const result = new Uint8Array(
|
|
61
|
+
scenegraph.gltf.buffers[bufferView.buffer].arrayBuffer,
|
|
62
|
+
bufferView.byteOffset,
|
|
63
|
+
bufferView.byteLength
|
|
64
|
+
);
|
|
65
|
+
await meshoptDecodeGltfBuffer(result, count, byteStride, source, mode, filter);
|
|
73
66
|
return result;
|
|
74
67
|
}
|
|
75
68
|
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import type {GLTF, GLTF_EXT_texture_webp} from '../types/gltf-types';
|
|
6
6
|
import type {GLTFLoaderOptions} from '../../gltf-loader';
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import {isImageFormatSupported} from '@loaders.gl/images';
|
|
9
9
|
import GLTFScenegraph from '../api/gltf-scenegraph';
|
|
10
10
|
|
|
11
11
|
const EXT_TEXTURE_WEBP = 'EXT_texture_webp';
|
|
@@ -20,7 +20,7 @@ export const name = EXT_TEXTURE_WEBP;
|
|
|
20
20
|
export function preprocess(gltfData: {json: GLTF}, options: GLTFLoaderOptions): void {
|
|
21
21
|
const scenegraph = new GLTFScenegraph(gltfData);
|
|
22
22
|
|
|
23
|
-
if (!
|
|
23
|
+
if (!isImageFormatSupported('image/webp')) {
|
|
24
24
|
if (scenegraph.getRequiredExtensions().includes(EXT_TEXTURE_WEBP)) {
|
|
25
25
|
throw new Error(`gltf: Required extension ${EXT_TEXTURE_WEBP} not supported by browser`);
|
|
26
26
|
}
|
|
@@ -35,6 +35,7 @@ export function preprocess(gltfData: {json: GLTF}, options: GLTFLoaderOptions):
|
|
|
35
35
|
EXT_TEXTURE_WEBP
|
|
36
36
|
);
|
|
37
37
|
if (extension) {
|
|
38
|
+
// TODO - if multiple texture extensions are present which one wins?
|
|
38
39
|
texture.source = extension.source;
|
|
39
40
|
}
|
|
40
41
|
scenegraph.removeObjectExtension(texture, EXT_TEXTURE_WEBP);
|
|
@@ -40,7 +40,7 @@ export function preprocess(gltfData: {json: GLTF}): void {
|
|
|
40
40
|
delete json.buffers[0].uri;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
// Remove the top-level extension as it has now been
|
|
43
|
+
// Remove the top-level extension as it has now been processed
|
|
44
44
|
gltfScenegraph.removeExtension(KHR_BINARY_GLTF);
|
|
45
45
|
}
|
|
46
46
|
|
|
@@ -56,7 +56,7 @@ export async function decode(
|
|
|
56
56
|
// Decompress meshes in parallel
|
|
57
57
|
await Promise.all(promises);
|
|
58
58
|
|
|
59
|
-
// We have now decompressed all primitives, so remove the top-level
|
|
59
|
+
// We have now decompressed all primitives, so remove the top-level extension
|
|
60
60
|
scenegraph.removeExtension(KHR_DRACO_MESH_COMPRESSION);
|
|
61
61
|
}
|
|
62
62
|
|
|
@@ -26,6 +26,7 @@ export function preprocess(gltfData: {json: GLTF}, options: GLTFLoaderOptions):
|
|
|
26
26
|
KHR_TEXTURE_BASISU
|
|
27
27
|
);
|
|
28
28
|
if (extension) {
|
|
29
|
+
// TODO - if multiple texture extensions are present which one wins?
|
|
29
30
|
texture.source = extension.source;
|
|
30
31
|
}
|
|
31
32
|
scene.removeObjectExtension(texture, KHR_TEXTURE_BASISU);
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_texture_transform/README.md
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {Vector3, Matrix3} from '@math.gl/core';
|
|
6
|
+
import type {GLTFMeshPrimitive, GLTFWithBuffers} from '../types/gltf-types';
|
|
7
|
+
import type {GLTFLoaderOptions} from '../../gltf-loader';
|
|
8
|
+
import {getAccessorArrayTypeAndLength} from '../gltf-utils/gltf-utils';
|
|
9
|
+
import {BYTES, COMPONENTS} from '../gltf-utils/gltf-constants';
|
|
10
|
+
import {
|
|
11
|
+
Accessor,
|
|
12
|
+
BufferView,
|
|
13
|
+
MaterialNormalTextureInfo,
|
|
14
|
+
MaterialOcclusionTextureInfo,
|
|
15
|
+
TextureInfo as GLTFTextureInfo
|
|
16
|
+
} from '../types/gltf-json-schema';
|
|
17
|
+
import GLTFScenegraph from '../api/gltf-scenegraph';
|
|
18
|
+
|
|
19
|
+
/** Extension name */
|
|
20
|
+
const EXT_MESHOPT_TRANSFORM = 'KHR_texture_transform';
|
|
21
|
+
|
|
22
|
+
export const name = EXT_MESHOPT_TRANSFORM;
|
|
23
|
+
|
|
24
|
+
const scratchVector = new Vector3();
|
|
25
|
+
const scratchRotationMatrix = new Matrix3();
|
|
26
|
+
const scratchScaleMatrix = new Matrix3();
|
|
27
|
+
|
|
28
|
+
/** Extension textureInfo https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_texture_transform#gltf-schema-updates */
|
|
29
|
+
type TextureInfo = {
|
|
30
|
+
/** The offset of the UV coordinate origin as a factor of the texture dimensions. */
|
|
31
|
+
offset?: [number, number];
|
|
32
|
+
/** Rotate the UVs by this many radians counter-clockwise around the origin. This is equivalent to a similar rotation of the image clockwise. */
|
|
33
|
+
rotation?: number;
|
|
34
|
+
/** The scale factor applied to the components of the UV coordinates. */
|
|
35
|
+
scale?: [number, number];
|
|
36
|
+
/** Overrides the textureInfo texCoord value if supplied, and if this extension is supported. */
|
|
37
|
+
texCoord?: number;
|
|
38
|
+
};
|
|
39
|
+
/** Intersection of all GLTF textures */
|
|
40
|
+
type CompoundGLTFTextureInfo = GLTFTextureInfo &
|
|
41
|
+
MaterialNormalTextureInfo &
|
|
42
|
+
MaterialOcclusionTextureInfo;
|
|
43
|
+
/** Parameters for TEXCOORD transformation */
|
|
44
|
+
type TransformParameters = {
|
|
45
|
+
/** Original texCoord value https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#_textureinfo_texcoord */
|
|
46
|
+
originalTexCoord: number;
|
|
47
|
+
/** New texCoord value from extension https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_texture_transform#gltf-schema-updates */
|
|
48
|
+
texCoord: number;
|
|
49
|
+
/** Transformation matrix */
|
|
50
|
+
matrix: Matrix3;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The extension entry to process the transformation
|
|
55
|
+
* @param gltfData gltf buffers and json
|
|
56
|
+
* @param options GLTFLoader options
|
|
57
|
+
*/
|
|
58
|
+
export async function decode(gltfData: GLTFWithBuffers, options: GLTFLoaderOptions) {
|
|
59
|
+
const gltfScenegraph = new GLTFScenegraph(gltfData);
|
|
60
|
+
const extension = gltfScenegraph.getExtension(EXT_MESHOPT_TRANSFORM);
|
|
61
|
+
if (!extension) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const materials = gltfData.json.materials || [];
|
|
65
|
+
for (let i = 0; i < materials.length; i++) {
|
|
66
|
+
transformTexCoords(i, gltfData);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Transform TEXCOORD by material
|
|
72
|
+
* @param materialIndex processing material index
|
|
73
|
+
* @param gltfData gltf buffers and json
|
|
74
|
+
*/
|
|
75
|
+
function transformTexCoords(materialIndex: number, gltfData: GLTFWithBuffers): void {
|
|
76
|
+
// Save processed texCoords in order no to process the same twice
|
|
77
|
+
const processedTexCoords: [number, number][] = [];
|
|
78
|
+
const material = gltfData.json.materials?.[materialIndex];
|
|
79
|
+
const baseColorTexture = material?.pbrMetallicRoughness?.baseColorTexture;
|
|
80
|
+
if (baseColorTexture) {
|
|
81
|
+
transformPrimitives(gltfData, materialIndex, baseColorTexture, processedTexCoords);
|
|
82
|
+
}
|
|
83
|
+
const emisiveTexture = material?.emissiveTexture;
|
|
84
|
+
if (emisiveTexture) {
|
|
85
|
+
transformPrimitives(gltfData, materialIndex, emisiveTexture, processedTexCoords);
|
|
86
|
+
}
|
|
87
|
+
const normalTexture = material?.normalTexture;
|
|
88
|
+
if (normalTexture) {
|
|
89
|
+
transformPrimitives(gltfData, materialIndex, normalTexture, processedTexCoords);
|
|
90
|
+
}
|
|
91
|
+
const occlusionTexture = material?.occlusionTexture;
|
|
92
|
+
if (occlusionTexture) {
|
|
93
|
+
transformPrimitives(gltfData, materialIndex, occlusionTexture, processedTexCoords);
|
|
94
|
+
}
|
|
95
|
+
const metallicRoughnessTexture = material?.pbrMetallicRoughness?.metallicRoughnessTexture;
|
|
96
|
+
if (metallicRoughnessTexture) {
|
|
97
|
+
transformPrimitives(gltfData, materialIndex, metallicRoughnessTexture, processedTexCoords);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Transform primitives of the particular material
|
|
103
|
+
* @param gltfData gltf data
|
|
104
|
+
* @param materialIndex primitives with this material will be transformed
|
|
105
|
+
* @param texture texture object
|
|
106
|
+
* @param processedTexCoords storage to save already processed texCoords
|
|
107
|
+
*/
|
|
108
|
+
function transformPrimitives(
|
|
109
|
+
gltfData: GLTFWithBuffers,
|
|
110
|
+
materialIndex: number,
|
|
111
|
+
texture: CompoundGLTFTextureInfo,
|
|
112
|
+
processedTexCoords: [number, number][]
|
|
113
|
+
) {
|
|
114
|
+
const transformParameters = getTransformParameters(texture, processedTexCoords);
|
|
115
|
+
if (!transformParameters) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const meshes = gltfData.json.meshes || [];
|
|
119
|
+
for (const mesh of meshes) {
|
|
120
|
+
for (const primitive of mesh.primitives) {
|
|
121
|
+
const material = primitive.material;
|
|
122
|
+
if (Number.isFinite(material) && materialIndex === material) {
|
|
123
|
+
transformPrimitive(gltfData, primitive, transformParameters);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Get parameters for TEXCOORD transformation
|
|
131
|
+
* @param texture texture object
|
|
132
|
+
* @param processedTexCoords storage to save already processed texCoords
|
|
133
|
+
* @returns texCoord couple and transformation matrix
|
|
134
|
+
*/
|
|
135
|
+
function getTransformParameters(
|
|
136
|
+
texture: CompoundGLTFTextureInfo,
|
|
137
|
+
processedTexCoords: [number, number][]
|
|
138
|
+
): TransformParameters | null {
|
|
139
|
+
const textureInfo = texture.extensions?.[EXT_MESHOPT_TRANSFORM];
|
|
140
|
+
const {texCoord: originalTexCoord = 0} = texture;
|
|
141
|
+
// If texCoord is not set in the extension, original attribute data will be replaced
|
|
142
|
+
const {texCoord = originalTexCoord} = textureInfo;
|
|
143
|
+
// Make sure that couple [originalTexCoord, extensionTexCoord] is not processed twice
|
|
144
|
+
const isProcessed =
|
|
145
|
+
processedTexCoords.findIndex(
|
|
146
|
+
([original, newTexCoord]) => original === originalTexCoord && newTexCoord === texCoord
|
|
147
|
+
) !== -1;
|
|
148
|
+
if (!isProcessed) {
|
|
149
|
+
const matrix = makeTransformationMatrix(textureInfo);
|
|
150
|
+
if (originalTexCoord !== texCoord) {
|
|
151
|
+
texture.texCoord = texCoord;
|
|
152
|
+
}
|
|
153
|
+
processedTexCoords.push([originalTexCoord, texCoord]);
|
|
154
|
+
return {originalTexCoord, texCoord, matrix};
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Transform `TEXCOORD_0` attribute in the primitive
|
|
161
|
+
* @param gltfData gltf data
|
|
162
|
+
* @param primitive primitive object
|
|
163
|
+
* @param transformParameters texCoord couple and transformation matrix
|
|
164
|
+
*/
|
|
165
|
+
function transformPrimitive(
|
|
166
|
+
gltfData: GLTFWithBuffers,
|
|
167
|
+
primitive: GLTFMeshPrimitive,
|
|
168
|
+
transformParameters: TransformParameters
|
|
169
|
+
) {
|
|
170
|
+
const {originalTexCoord, texCoord, matrix} = transformParameters;
|
|
171
|
+
const texCoordAccessor = primitive.attributes[`TEXCOORD_${originalTexCoord}`];
|
|
172
|
+
if (Number.isFinite(texCoordAccessor)) {
|
|
173
|
+
// Get accessor of the `TEXCOORD_0` attribute
|
|
174
|
+
const accessor = gltfData.json.accessors?.[texCoordAccessor];
|
|
175
|
+
if (accessor && accessor.bufferView) {
|
|
176
|
+
// Get `bufferView` of the `accessor`
|
|
177
|
+
const bufferView = gltfData.json.bufferViews?.[accessor.bufferView];
|
|
178
|
+
if (bufferView) {
|
|
179
|
+
// Get `arrayBuffer` the `bufferView` look at
|
|
180
|
+
const {arrayBuffer, byteOffset: bufferByteOffset} = gltfData.buffers[bufferView.buffer];
|
|
181
|
+
// Resulting byteOffset is sum of the buffer, accessor and bufferView byte offsets
|
|
182
|
+
const byteOffset =
|
|
183
|
+
(bufferByteOffset || 0) + (accessor.byteOffset || 0) + (bufferView.byteOffset || 0);
|
|
184
|
+
// Deduce TypedArray type and its length from `accessor` and `bufferView` data
|
|
185
|
+
const {ArrayType, length} = getAccessorArrayTypeAndLength(accessor, bufferView);
|
|
186
|
+
// Number of bytes each component occupies
|
|
187
|
+
const bytes = BYTES[accessor.componentType];
|
|
188
|
+
// Number of components. For the `TEXCOORD_0` with `VEC2` type, it must return 2
|
|
189
|
+
const components = COMPONENTS[accessor.type];
|
|
190
|
+
// Multiplier to calculate the address of the `TEXCOORD_0` element in the arrayBuffer
|
|
191
|
+
const elementAddressScale = bufferView.byteStride || bytes * components;
|
|
192
|
+
// Data transform to Float32Array
|
|
193
|
+
const result = new Float32Array(length);
|
|
194
|
+
for (let i = 0; i < accessor.count; i++) {
|
|
195
|
+
// Take [u, v] couple from the arrayBuffer
|
|
196
|
+
const uv = new ArrayType(arrayBuffer, byteOffset + i * elementAddressScale, 2);
|
|
197
|
+
// Set and transform Vector3 per https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_texture_transform#overview
|
|
198
|
+
scratchVector.set(uv[0], uv[1], 1);
|
|
199
|
+
scratchVector.transformByMatrix3(matrix);
|
|
200
|
+
// Save result in Float32Array
|
|
201
|
+
result.set([scratchVector[0], scratchVector[1]], i * components);
|
|
202
|
+
}
|
|
203
|
+
// If texCoord the same, replace gltf structural data
|
|
204
|
+
if (originalTexCoord === texCoord) {
|
|
205
|
+
updateGltf(accessor, bufferView, gltfData.buffers, result);
|
|
206
|
+
} else {
|
|
207
|
+
// If texCoord change, create new attribute
|
|
208
|
+
createAttribute(texCoord, accessor, primitive, gltfData, result);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Update GLTF structural objects with new data as we create new `Float32Array` for `TEXCOORD_0`.
|
|
217
|
+
* @param accessor accessor to change
|
|
218
|
+
* @param bufferView bufferView to change
|
|
219
|
+
* @param buffers binary buffers
|
|
220
|
+
* @param newTexcoordArray typed array with data after transformation
|
|
221
|
+
*/
|
|
222
|
+
function updateGltf(
|
|
223
|
+
accessor: Accessor,
|
|
224
|
+
bufferView: BufferView,
|
|
225
|
+
buffers: {arrayBuffer: ArrayBuffer; byteOffset: number; byteLength: number}[],
|
|
226
|
+
newTexCoordArray: Float32Array
|
|
227
|
+
): void {
|
|
228
|
+
accessor.componentType = 5126;
|
|
229
|
+
buffers.push({
|
|
230
|
+
arrayBuffer: newTexCoordArray.buffer,
|
|
231
|
+
byteOffset: 0,
|
|
232
|
+
byteLength: newTexCoordArray.buffer.byteLength
|
|
233
|
+
});
|
|
234
|
+
bufferView.buffer = buffers.length - 1;
|
|
235
|
+
bufferView.byteLength = newTexCoordArray.buffer.byteLength;
|
|
236
|
+
bufferView.byteOffset = 0;
|
|
237
|
+
delete bufferView.byteStride;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
*
|
|
242
|
+
* @param newTexCoord new `texCoord` value
|
|
243
|
+
* @param originalAccessor original accessor object, that store data before transformation
|
|
244
|
+
* @param primitive primitive object
|
|
245
|
+
* @param gltfData gltf data
|
|
246
|
+
* @param newTexCoordArray typed array with data after transformation
|
|
247
|
+
* @returns
|
|
248
|
+
*/
|
|
249
|
+
function createAttribute(
|
|
250
|
+
newTexCoord: number,
|
|
251
|
+
originalAccessor: Accessor,
|
|
252
|
+
primitive: GLTFMeshPrimitive,
|
|
253
|
+
gltfData: GLTFWithBuffers,
|
|
254
|
+
newTexCoordArray: Float32Array
|
|
255
|
+
) {
|
|
256
|
+
gltfData.buffers.push({
|
|
257
|
+
arrayBuffer: newTexCoordArray.buffer,
|
|
258
|
+
byteOffset: 0,
|
|
259
|
+
byteLength: newTexCoordArray.buffer.byteLength
|
|
260
|
+
});
|
|
261
|
+
const bufferViews = gltfData.json.bufferViews;
|
|
262
|
+
if (!bufferViews) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
bufferViews.push({
|
|
266
|
+
buffer: gltfData.buffers.length - 1,
|
|
267
|
+
byteLength: newTexCoordArray.buffer.byteLength,
|
|
268
|
+
byteOffset: 0
|
|
269
|
+
});
|
|
270
|
+
const accessors = gltfData.json.accessors;
|
|
271
|
+
if (!accessors) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
accessors.push({
|
|
275
|
+
bufferView: bufferViews?.length - 1,
|
|
276
|
+
byteOffset: 0,
|
|
277
|
+
componentType: 5126,
|
|
278
|
+
count: originalAccessor.count,
|
|
279
|
+
type: 'VEC2'
|
|
280
|
+
});
|
|
281
|
+
primitive.attributes[`TEXCOORD_${newTexCoord}`] = accessors.length - 1;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Construct transformation matrix from the extension data (transition, rotation, scale)
|
|
286
|
+
* @param extensionData extension data
|
|
287
|
+
* @returns transformation matrix
|
|
288
|
+
*/
|
|
289
|
+
function makeTransformationMatrix(extensionData: TextureInfo): Matrix3 {
|
|
290
|
+
const {offset = [0, 0], rotation = 0, scale = [1, 1]} = extensionData;
|
|
291
|
+
const translationMatirx = new Matrix3().set(1, 0, 0, 0, 1, 0, offset[0], offset[1], 1);
|
|
292
|
+
const rotationMatirx = scratchRotationMatrix.set(
|
|
293
|
+
Math.cos(rotation),
|
|
294
|
+
Math.sin(rotation),
|
|
295
|
+
0,
|
|
296
|
+
-Math.sin(rotation),
|
|
297
|
+
Math.cos(rotation),
|
|
298
|
+
0,
|
|
299
|
+
0,
|
|
300
|
+
0,
|
|
301
|
+
1
|
|
302
|
+
);
|
|
303
|
+
const scaleMatrix = scratchScaleMatrix.set(scale[0], 0, 0, 0, scale[1], 0, 0, 0, 1);
|
|
304
|
+
return translationMatirx.multiplyRight(rotationMatirx).multiplyRight(scaleMatrix);
|
|
305
|
+
}
|