@loaders.gl/gltf 4.2.0-alpha.4 → 4.2.0-alpha.5
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/dist.dev.js +1049 -517
- package/dist/dist.min.js +9 -0
- package/dist/glb-loader.d.ts +2 -2
- package/dist/glb-loader.d.ts.map +1 -1
- package/dist/glb-loader.js +22 -21
- package/dist/glb-writer.d.ts +2 -2
- package/dist/glb-writer.d.ts.map +1 -1
- package/dist/glb-writer.js +27 -24
- package/dist/gltf-loader.d.ts +3 -3
- package/dist/gltf-loader.d.ts.map +1 -1
- package/dist/gltf-loader.js +31 -36
- package/dist/gltf-writer.js +24 -26
- package/dist/index.cjs +95 -284
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +17 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/lib/api/gltf-extensions.d.ts +2 -2
- package/dist/lib/api/gltf-extensions.d.ts.map +1 -1
- package/dist/lib/api/gltf-extensions.js +45 -22
- package/dist/lib/api/gltf-scenegraph.d.ts +2 -2
- package/dist/lib/api/gltf-scenegraph.d.ts.map +1 -1
- package/dist/lib/api/gltf-scenegraph.js +561 -438
- package/dist/lib/api/normalize-gltf-v1.js +250 -181
- package/dist/lib/api/post-process-gltf.d.ts +3 -3
- package/dist/lib/api/post-process-gltf.d.ts.map +1 -1
- package/dist/lib/api/post-process-gltf.js +375 -339
- package/dist/lib/encoders/encode-glb.js +62 -48
- package/dist/lib/encoders/encode-gltf.js +24 -10
- package/dist/lib/extensions/EXT_mesh_features.d.ts +2 -2
- package/dist/lib/extensions/EXT_mesh_features.d.ts.map +1 -1
- package/dist/lib/extensions/EXT_mesh_features.js +55 -33
- package/dist/lib/extensions/EXT_meshopt_compression.d.ts +2 -2
- package/dist/lib/extensions/EXT_meshopt_compression.d.ts.map +1 -1
- package/dist/lib/extensions/EXT_meshopt_compression.js +27 -31
- package/dist/lib/extensions/EXT_structural_metadata.d.ts +2 -2
- package/dist/lib/extensions/EXT_structural_metadata.d.ts.map +1 -1
- package/dist/lib/extensions/EXT_structural_metadata.js +434 -230
- package/dist/lib/extensions/EXT_texture_webp.d.ts +2 -2
- package/dist/lib/extensions/EXT_texture_webp.d.ts.map +1 -1
- package/dist/lib/extensions/EXT_texture_webp.js +24 -17
- package/dist/lib/extensions/KHR_binary_gltf.d.ts +1 -1
- package/dist/lib/extensions/KHR_binary_gltf.d.ts.map +1 -1
- package/dist/lib/extensions/KHR_binary_gltf.js +29 -15
- package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts +2 -2
- package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts.map +1 -1
- package/dist/lib/extensions/KHR_draco_mesh_compression.js +110 -87
- package/dist/lib/extensions/KHR_texture_basisu.d.ts +2 -2
- package/dist/lib/extensions/KHR_texture_basisu.d.ts.map +1 -1
- package/dist/lib/extensions/KHR_texture_basisu.js +19 -12
- package/dist/lib/extensions/KHR_texture_transform.d.ts +2 -2
- package/dist/lib/extensions/KHR_texture_transform.d.ts.map +1 -1
- package/dist/lib/extensions/KHR_texture_transform.js +194 -154
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts +2 -2
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts.map +1 -1
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +263 -143
- package/dist/lib/extensions/deprecated/KHR_lights_punctual.d.ts +1 -1
- package/dist/lib/extensions/deprecated/KHR_lights_punctual.d.ts.map +1 -1
- package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +44 -32
- package/dist/lib/extensions/deprecated/KHR_materials_unlit.d.ts +1 -1
- package/dist/lib/extensions/deprecated/KHR_materials_unlit.d.ts.map +1 -1
- package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +30 -24
- package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts +1 -1
- package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts.map +1 -1
- package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +65 -52
- package/dist/lib/extensions/utils/3d-tiles-utils.d.ts +2 -2
- package/dist/lib/extensions/utils/3d-tiles-utils.d.ts.map +1 -1
- package/dist/lib/extensions/utils/3d-tiles-utils.js +298 -181
- package/dist/lib/gltf-utils/get-typed-array.d.ts +1 -1
- package/dist/lib/gltf-utils/get-typed-array.d.ts.map +1 -1
- package/dist/lib/gltf-utils/get-typed-array.js +54 -42
- package/dist/lib/gltf-utils/gltf-attribute-utils.d.ts +1 -1
- package/dist/lib/gltf-utils/gltf-attribute-utils.d.ts.map +1 -1
- package/dist/lib/gltf-utils/gltf-attribute-utils.js +58 -52
- package/dist/lib/gltf-utils/gltf-constants.js +27 -27
- package/dist/lib/gltf-utils/gltf-utils.d.ts +1 -1
- package/dist/lib/gltf-utils/gltf-utils.d.ts.map +1 -1
- package/dist/lib/gltf-utils/gltf-utils.js +67 -60
- package/dist/lib/gltf-utils/resolve-url.js +12 -10
- 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 +132 -89
- package/dist/lib/parsers/parse-gltf.d.ts +3 -3
- package/dist/lib/parsers/parse-gltf.d.ts.map +1 -1
- package/dist/lib/parsers/parse-gltf.js +155 -126
- package/dist/lib/types/glb-types.js +0 -1
- package/dist/lib/types/gltf-ext-feature-metadata-schema.d.ts +1 -1
- package/dist/lib/types/gltf-ext-feature-metadata-schema.d.ts.map +1 -1
- package/dist/lib/types/gltf-ext-feature-metadata-schema.js +0 -1
- package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts +1 -1
- package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts.map +1 -1
- package/dist/lib/types/gltf-ext-mesh-features-schema.js +0 -1
- package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts +1 -1
- package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts.map +1 -1
- package/dist/lib/types/gltf-ext-structural-metadata-schema.js +0 -1
- package/dist/lib/types/gltf-json-schema.js +2 -1
- package/dist/lib/types/gltf-postprocessed-schema.js +2 -1
- package/dist/lib/types/gltf-types.d.ts +3 -3
- package/dist/lib/types/gltf-types.d.ts.map +1 -1
- package/dist/lib/types/gltf-types.js +1 -1
- package/dist/lib/utils/assert.js +6 -4
- package/dist/lib/utils/version.js +3 -1
- package/dist/meshopt/meshopt-decoder.js +86 -67
- package/dist/webp/webp.js +28 -19
- package/package.json +12 -8
- 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_mesh_features.js.map +0 -1
- package/dist/lib/extensions/EXT_meshopt_compression.js.map +0 -1
- package/dist/lib/extensions/EXT_structural_metadata.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/KHR_texture_transform.js.map +0 -1
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.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/extensions/utils/3d-tiles-utils.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-ext-feature-metadata-schema.js.map +0 -1
- package/dist/lib/types/gltf-ext-mesh-features-schema.js.map +0 -1
- package/dist/lib/types/gltf-ext-structural-metadata-schema.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/meshopt/meshopt-encoder.ts.disabled +0 -409
- package/dist/webp/webp.js.map +0 -1
|
@@ -1,395 +1,431 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
1
4
|
import { assert } from "../utils/assert.js";
|
|
2
5
|
import { getAccessorArrayTypeAndLength } from "../gltf-utils/gltf-utils.js";
|
|
6
|
+
// This is a post processor for loaded glTF files
|
|
7
|
+
// The goal is to make the loaded data easier to use in WebGL applications
|
|
8
|
+
//
|
|
9
|
+
// Functions:
|
|
10
|
+
// * Resolve indexed arrays structure of glTF into a linked tree.
|
|
11
|
+
// * Translate stringified enum keys and values into WebGL constants.
|
|
12
|
+
// * Load images (optional)
|
|
13
|
+
// ENUM LOOKUP
|
|
3
14
|
const COMPONENTS = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
SCALAR: 1,
|
|
16
|
+
VEC2: 2,
|
|
17
|
+
VEC3: 3,
|
|
18
|
+
VEC4: 4,
|
|
19
|
+
MAT2: 4,
|
|
20
|
+
MAT3: 9,
|
|
21
|
+
MAT4: 16
|
|
11
22
|
};
|
|
12
23
|
const BYTES = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
5120: 1, // BYTE
|
|
25
|
+
5121: 1, // UNSIGNED_BYTE
|
|
26
|
+
5122: 2, // SHORT
|
|
27
|
+
5123: 2, // UNSIGNED_SHORT
|
|
28
|
+
5125: 4, // UNSIGNED_INT
|
|
29
|
+
5126: 4 // FLOAT
|
|
19
30
|
};
|
|
20
31
|
const GL_SAMPLER = {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
32
|
+
// Sampler parameters
|
|
33
|
+
TEXTURE_MAG_FILTER: 0x2800,
|
|
34
|
+
TEXTURE_MIN_FILTER: 0x2801,
|
|
35
|
+
TEXTURE_WRAP_S: 0x2802,
|
|
36
|
+
TEXTURE_WRAP_T: 0x2803,
|
|
37
|
+
// Sampler default values
|
|
38
|
+
REPEAT: 0x2901,
|
|
39
|
+
LINEAR: 0x2601,
|
|
40
|
+
NEAREST_MIPMAP_LINEAR: 0x2702
|
|
28
41
|
};
|
|
29
42
|
const SAMPLER_PARAMETER_GLTF_TO_GL = {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
43
|
+
magFilter: GL_SAMPLER.TEXTURE_MAG_FILTER,
|
|
44
|
+
minFilter: GL_SAMPLER.TEXTURE_MIN_FILTER,
|
|
45
|
+
wrapS: GL_SAMPLER.TEXTURE_WRAP_S,
|
|
46
|
+
wrapT: GL_SAMPLER.TEXTURE_WRAP_T
|
|
34
47
|
};
|
|
48
|
+
// When undefined, a sampler with repeat wrapping and auto filtering should be used.
|
|
49
|
+
// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texture
|
|
35
50
|
const DEFAULT_SAMPLER_PARAMETERS = {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
51
|
+
[GL_SAMPLER.TEXTURE_MAG_FILTER]: GL_SAMPLER.LINEAR,
|
|
52
|
+
[GL_SAMPLER.TEXTURE_MIN_FILTER]: GL_SAMPLER.NEAREST_MIPMAP_LINEAR,
|
|
53
|
+
[GL_SAMPLER.TEXTURE_WRAP_S]: GL_SAMPLER.REPEAT,
|
|
54
|
+
[GL_SAMPLER.TEXTURE_WRAP_T]: GL_SAMPLER.REPEAT
|
|
40
55
|
};
|
|
41
56
|
function makeDefaultSampler() {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
57
|
+
return {
|
|
58
|
+
id: 'default-sampler',
|
|
59
|
+
parameters: DEFAULT_SAMPLER_PARAMETERS
|
|
60
|
+
};
|
|
46
61
|
}
|
|
47
62
|
function getBytesFromComponentType(componentType) {
|
|
48
|
-
|
|
63
|
+
return BYTES[componentType];
|
|
49
64
|
}
|
|
50
65
|
function getSizeFromAccessorType(type) {
|
|
51
|
-
|
|
66
|
+
return COMPONENTS[type];
|
|
52
67
|
}
|
|
53
68
|
class GLTFPostProcessor {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.buffers = [];
|
|
59
|
-
this.images = [];
|
|
60
|
-
}
|
|
61
|
-
postProcess(gltf) {
|
|
62
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
63
|
-
const {
|
|
64
|
-
json,
|
|
65
|
-
buffers = [],
|
|
66
|
-
images = []
|
|
67
|
-
} = gltf;
|
|
68
|
-
const {
|
|
69
|
-
baseUri = ''
|
|
70
|
-
} = gltf;
|
|
71
|
-
assert(json);
|
|
72
|
-
this.baseUri = baseUri;
|
|
73
|
-
this.buffers = buffers;
|
|
74
|
-
this.images = images;
|
|
75
|
-
this.jsonUnprocessed = json;
|
|
76
|
-
this.json = this._resolveTree(gltf.json, options);
|
|
77
|
-
return this.json;
|
|
78
|
-
}
|
|
79
|
-
_resolveTree(gltf) {
|
|
80
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
81
|
-
const json = {
|
|
82
|
-
...gltf
|
|
83
|
-
};
|
|
84
|
-
this.json = json;
|
|
85
|
-
if (gltf.bufferViews) {
|
|
86
|
-
json.bufferViews = gltf.bufferViews.map((bufView, i) => this._resolveBufferView(bufView, i));
|
|
69
|
+
constructor() {
|
|
70
|
+
this.baseUri = '';
|
|
71
|
+
this.buffers = [];
|
|
72
|
+
this.images = [];
|
|
87
73
|
}
|
|
88
|
-
|
|
89
|
-
|
|
74
|
+
postProcess(gltf, options = {}) {
|
|
75
|
+
const { json, buffers = [], images = [] } = gltf;
|
|
76
|
+
// @ts-expect-error
|
|
77
|
+
const { baseUri = '' } = gltf;
|
|
78
|
+
assert(json);
|
|
79
|
+
this.baseUri = baseUri;
|
|
80
|
+
this.buffers = buffers;
|
|
81
|
+
this.images = images;
|
|
82
|
+
this.jsonUnprocessed = json;
|
|
83
|
+
this.json = this._resolveTree(gltf.json, options);
|
|
84
|
+
return this.json;
|
|
90
85
|
}
|
|
91
|
-
|
|
92
|
-
|
|
86
|
+
// Convert indexed glTF structure into tree structure
|
|
87
|
+
// cross-link index resolution, enum lookup, convenience calculations
|
|
88
|
+
// eslint-disable-next-line complexity, max-statements
|
|
89
|
+
_resolveTree(gltf, options = {}) {
|
|
90
|
+
// @ts-expect-error
|
|
91
|
+
const json = { ...gltf };
|
|
92
|
+
this.json = json;
|
|
93
|
+
if (gltf.bufferViews) {
|
|
94
|
+
json.bufferViews = gltf.bufferViews.map((bufView, i) => this._resolveBufferView(bufView, i));
|
|
95
|
+
}
|
|
96
|
+
if (gltf.images) {
|
|
97
|
+
json.images = gltf.images.map((image, i) => this._resolveImage(image, i));
|
|
98
|
+
}
|
|
99
|
+
if (gltf.samplers) {
|
|
100
|
+
json.samplers = gltf.samplers.map((sampler, i) => this._resolveSampler(sampler, i));
|
|
101
|
+
}
|
|
102
|
+
if (gltf.textures) {
|
|
103
|
+
json.textures = gltf.textures.map((texture, i) => this._resolveTexture(texture, i));
|
|
104
|
+
}
|
|
105
|
+
if (gltf.accessors) {
|
|
106
|
+
json.accessors = gltf.accessors.map((accessor, i) => this._resolveAccessor(accessor, i));
|
|
107
|
+
}
|
|
108
|
+
if (gltf.materials) {
|
|
109
|
+
json.materials = gltf.materials.map((material, i) => this._resolveMaterial(material, i));
|
|
110
|
+
}
|
|
111
|
+
if (gltf.meshes) {
|
|
112
|
+
json.meshes = gltf.meshes.map((mesh, i) => this._resolveMesh(mesh, i));
|
|
113
|
+
}
|
|
114
|
+
if (gltf.nodes) {
|
|
115
|
+
json.nodes = gltf.nodes.map((node, i) => this._resolveNode(node, i));
|
|
116
|
+
json.nodes = json.nodes.map((node, i) => this._resolveNodeChildren(node));
|
|
117
|
+
}
|
|
118
|
+
if (gltf.skins) {
|
|
119
|
+
json.skins = gltf.skins.map((skin, i) => this._resolveSkin(skin, i));
|
|
120
|
+
}
|
|
121
|
+
if (gltf.scenes) {
|
|
122
|
+
json.scenes = gltf.scenes.map((scene, i) => this._resolveScene(scene, i));
|
|
123
|
+
}
|
|
124
|
+
if (typeof this.json.scene === 'number' && json.scenes) {
|
|
125
|
+
json.scene = json.scenes[this.json.scene];
|
|
126
|
+
}
|
|
127
|
+
return json;
|
|
93
128
|
}
|
|
94
|
-
|
|
95
|
-
|
|
129
|
+
getScene(index) {
|
|
130
|
+
return this._get(this.json.scenes, index);
|
|
96
131
|
}
|
|
97
|
-
|
|
98
|
-
|
|
132
|
+
getNode(index) {
|
|
133
|
+
return this._get(this.json.nodes, index);
|
|
99
134
|
}
|
|
100
|
-
|
|
101
|
-
|
|
135
|
+
getSkin(index) {
|
|
136
|
+
return this._get(this.json.skins, index);
|
|
102
137
|
}
|
|
103
|
-
|
|
104
|
-
|
|
138
|
+
getMesh(index) {
|
|
139
|
+
return this._get(this.json.meshes, index);
|
|
105
140
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
json.nodes = json.nodes.map((node, i) => this._resolveNodeChildren(node));
|
|
141
|
+
getMaterial(index) {
|
|
142
|
+
return this._get(this.json.materials, index);
|
|
109
143
|
}
|
|
110
|
-
|
|
111
|
-
|
|
144
|
+
getAccessor(index) {
|
|
145
|
+
return this._get(this.json.accessors, index);
|
|
112
146
|
}
|
|
113
|
-
|
|
114
|
-
|
|
147
|
+
getCamera(index) {
|
|
148
|
+
return this._get(this.json.cameras, index);
|
|
115
149
|
}
|
|
116
|
-
|
|
117
|
-
|
|
150
|
+
getTexture(index) {
|
|
151
|
+
return this._get(this.json.textures, index);
|
|
118
152
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
getScene(index) {
|
|
122
|
-
return this._get(this.json.scenes, index);
|
|
123
|
-
}
|
|
124
|
-
getNode(index) {
|
|
125
|
-
return this._get(this.json.nodes, index);
|
|
126
|
-
}
|
|
127
|
-
getSkin(index) {
|
|
128
|
-
return this._get(this.json.skins, index);
|
|
129
|
-
}
|
|
130
|
-
getMesh(index) {
|
|
131
|
-
return this._get(this.json.meshes, index);
|
|
132
|
-
}
|
|
133
|
-
getMaterial(index) {
|
|
134
|
-
return this._get(this.json.materials, index);
|
|
135
|
-
}
|
|
136
|
-
getAccessor(index) {
|
|
137
|
-
return this._get(this.json.accessors, index);
|
|
138
|
-
}
|
|
139
|
-
getCamera(index) {
|
|
140
|
-
return this._get(this.json.cameras, index);
|
|
141
|
-
}
|
|
142
|
-
getTexture(index) {
|
|
143
|
-
return this._get(this.json.textures, index);
|
|
144
|
-
}
|
|
145
|
-
getSampler(index) {
|
|
146
|
-
return this._get(this.json.samplers, index);
|
|
147
|
-
}
|
|
148
|
-
getImage(index) {
|
|
149
|
-
return this._get(this.json.images, index);
|
|
150
|
-
}
|
|
151
|
-
getBufferView(index) {
|
|
152
|
-
return this._get(this.json.bufferViews, index);
|
|
153
|
-
}
|
|
154
|
-
getBuffer(index) {
|
|
155
|
-
return this._get(this.json.buffers, index);
|
|
156
|
-
}
|
|
157
|
-
_get(array, index) {
|
|
158
|
-
if (typeof index === 'object') {
|
|
159
|
-
return index;
|
|
153
|
+
getSampler(index) {
|
|
154
|
+
return this._get(this.json.samplers, index);
|
|
160
155
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
console.warn(`glTF file error: Could not find ${array}[${index}]`);
|
|
156
|
+
getImage(index) {
|
|
157
|
+
return this._get(this.json.images, index);
|
|
164
158
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
_resolveScene(scene, index) {
|
|
168
|
-
return {
|
|
169
|
-
...scene,
|
|
170
|
-
id: scene.id || `scene-${index}`,
|
|
171
|
-
nodes: (scene.nodes || []).map(node => this.getNode(node))
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
_resolveNode(gltfNode, index) {
|
|
175
|
-
const node = {
|
|
176
|
-
...gltfNode,
|
|
177
|
-
id: (gltfNode === null || gltfNode === void 0 ? void 0 : gltfNode.id) || `node-${index}`
|
|
178
|
-
};
|
|
179
|
-
if (gltfNode.mesh !== undefined) {
|
|
180
|
-
node.mesh = this.getMesh(gltfNode.mesh);
|
|
159
|
+
getBufferView(index) {
|
|
160
|
+
return this._get(this.json.bufferViews, index);
|
|
181
161
|
}
|
|
182
|
-
|
|
183
|
-
|
|
162
|
+
getBuffer(index) {
|
|
163
|
+
return this._get(this.json.buffers, index);
|
|
184
164
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
primitives: []
|
|
196
|
-
});
|
|
165
|
+
_get(array, index) {
|
|
166
|
+
// check if already resolved
|
|
167
|
+
if (typeof index === 'object') {
|
|
168
|
+
return index;
|
|
169
|
+
}
|
|
170
|
+
const object = array && array[index];
|
|
171
|
+
if (!object) {
|
|
172
|
+
console.warn(`glTF file error: Could not find ${array}[${index}]`); // eslint-disable-line
|
|
173
|
+
}
|
|
174
|
+
return object;
|
|
197
175
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
176
|
+
// PARSING HELPERS
|
|
177
|
+
_resolveScene(scene, index) {
|
|
178
|
+
return {
|
|
179
|
+
...scene,
|
|
180
|
+
// @ts-ignore
|
|
181
|
+
id: scene.id || `scene-${index}`,
|
|
182
|
+
nodes: (scene.nodes || []).map((node) => this.getNode(node))
|
|
183
|
+
};
|
|
203
184
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
id: gltfSkin.id || `skin-${index}`,
|
|
211
|
-
inverseBindMatrices
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
_resolveMesh(gltfMesh, index) {
|
|
215
|
-
const mesh = {
|
|
216
|
-
...gltfMesh,
|
|
217
|
-
id: gltfMesh.id || `mesh-${index}`,
|
|
218
|
-
primitives: []
|
|
219
|
-
};
|
|
220
|
-
if (gltfMesh.primitives) {
|
|
221
|
-
mesh.primitives = gltfMesh.primitives.map(gltfPrimitive => {
|
|
222
|
-
const primitive = {
|
|
223
|
-
...gltfPrimitive,
|
|
224
|
-
attributes: {},
|
|
225
|
-
indices: undefined,
|
|
226
|
-
material: undefined
|
|
185
|
+
_resolveNode(gltfNode, index) {
|
|
186
|
+
// @ts-expect-error
|
|
187
|
+
const node = {
|
|
188
|
+
...gltfNode,
|
|
189
|
+
// @ts-expect-error id could already be present, glTF standard does not prevent it
|
|
190
|
+
id: gltfNode?.id || `node-${index}`
|
|
227
191
|
};
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
primitive.attributes[attribute] = this.getAccessor(attributes[attribute]);
|
|
192
|
+
if (gltfNode.mesh !== undefined) {
|
|
193
|
+
node.mesh = this.getMesh(gltfNode.mesh);
|
|
231
194
|
}
|
|
232
|
-
if (
|
|
233
|
-
|
|
195
|
+
if (gltfNode.camera !== undefined) {
|
|
196
|
+
node.camera = this.getCamera(gltfNode.camera);
|
|
234
197
|
}
|
|
235
|
-
if (
|
|
236
|
-
|
|
198
|
+
if (gltfNode.skin !== undefined) {
|
|
199
|
+
node.skin = this.getSkin(gltfNode.skin);
|
|
237
200
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
...material.normalTexture
|
|
251
|
-
};
|
|
252
|
-
material.normalTexture.texture = this.getTexture(material.normalTexture.index);
|
|
201
|
+
// TODO deprecated - Delete in v4.0?
|
|
202
|
+
// @ts-expect-error node.meshes does not seem to be part of the GLTF standard
|
|
203
|
+
if (gltfNode.meshes !== undefined && gltfNode.meshes.length) {
|
|
204
|
+
// @ts-expect-error
|
|
205
|
+
node.mesh = gltfNode.meshes.reduce((accum, meshIndex) => {
|
|
206
|
+
const mesh = this.getMesh(meshIndex);
|
|
207
|
+
accum.id = mesh.id;
|
|
208
|
+
accum.primitives = accum.primitives.concat(mesh.primitives);
|
|
209
|
+
return accum;
|
|
210
|
+
}, { primitives: [] });
|
|
211
|
+
}
|
|
212
|
+
return node;
|
|
253
213
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
214
|
+
_resolveNodeChildren(node) {
|
|
215
|
+
if (node.children) {
|
|
216
|
+
// @ts-expect-error node.children are numbers at this stage
|
|
217
|
+
node.children = node.children.map((child) => this.getNode(child));
|
|
218
|
+
}
|
|
219
|
+
return node;
|
|
259
220
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
221
|
+
_resolveSkin(gltfSkin, index) {
|
|
222
|
+
const inverseBindMatrices = typeof gltfSkin.inverseBindMatrices === 'number'
|
|
223
|
+
? this.getAccessor(gltfSkin.inverseBindMatrices)
|
|
224
|
+
: undefined;
|
|
225
|
+
return {
|
|
226
|
+
...gltfSkin,
|
|
227
|
+
id: gltfSkin.id || `skin-${index}`,
|
|
228
|
+
inverseBindMatrices
|
|
229
|
+
};
|
|
265
230
|
}
|
|
266
|
-
|
|
267
|
-
|
|
231
|
+
_resolveMesh(gltfMesh, index) {
|
|
232
|
+
const mesh = {
|
|
233
|
+
...gltfMesh,
|
|
234
|
+
id: gltfMesh.id || `mesh-${index}`,
|
|
235
|
+
primitives: []
|
|
236
|
+
};
|
|
237
|
+
if (gltfMesh.primitives) {
|
|
238
|
+
mesh.primitives = gltfMesh.primitives.map((gltfPrimitive) => {
|
|
239
|
+
const primitive = {
|
|
240
|
+
...gltfPrimitive,
|
|
241
|
+
attributes: {},
|
|
242
|
+
indices: undefined,
|
|
243
|
+
material: undefined
|
|
244
|
+
};
|
|
245
|
+
const attributes = gltfPrimitive.attributes;
|
|
246
|
+
for (const attribute in attributes) {
|
|
247
|
+
primitive.attributes[attribute] = this.getAccessor(attributes[attribute]);
|
|
248
|
+
}
|
|
249
|
+
if (gltfPrimitive.indices !== undefined) {
|
|
250
|
+
primitive.indices = this.getAccessor(gltfPrimitive.indices);
|
|
251
|
+
}
|
|
252
|
+
if (gltfPrimitive.material !== undefined) {
|
|
253
|
+
primitive.material = this.getMaterial(gltfPrimitive.material);
|
|
254
|
+
}
|
|
255
|
+
return primitive;
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return mesh;
|
|
268
259
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
mr.baseColorTexture = {
|
|
276
|
-
...mr.baseColorTexture
|
|
260
|
+
_resolveMaterial(gltfMaterial, index) {
|
|
261
|
+
// @ts-expect-error
|
|
262
|
+
const material = {
|
|
263
|
+
...gltfMaterial,
|
|
264
|
+
// @ts-expect-error
|
|
265
|
+
id: gltfMaterial.id || `material-${index}`
|
|
277
266
|
};
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
267
|
+
if (material.normalTexture) {
|
|
268
|
+
material.normalTexture = { ...material.normalTexture };
|
|
269
|
+
material.normalTexture.texture = this.getTexture(material.normalTexture.index);
|
|
270
|
+
}
|
|
271
|
+
if (material.occlusionTexture) {
|
|
272
|
+
material.occlusionTexture = { ...material.occlusionTexture };
|
|
273
|
+
material.occlusionTexture.texture = this.getTexture(material.occlusionTexture.index);
|
|
274
|
+
}
|
|
275
|
+
if (material.emissiveTexture) {
|
|
276
|
+
material.emissiveTexture = { ...material.emissiveTexture };
|
|
277
|
+
material.emissiveTexture.texture = this.getTexture(material.emissiveTexture.index);
|
|
278
|
+
}
|
|
279
|
+
if (!material.emissiveFactor) {
|
|
280
|
+
material.emissiveFactor = material.emissiveTexture ? [1, 1, 1] : [0, 0, 0];
|
|
281
|
+
}
|
|
282
|
+
if (material.pbrMetallicRoughness) {
|
|
283
|
+
material.pbrMetallicRoughness = { ...material.pbrMetallicRoughness };
|
|
284
|
+
const mr = material.pbrMetallicRoughness;
|
|
285
|
+
if (mr.baseColorTexture) {
|
|
286
|
+
mr.baseColorTexture = { ...mr.baseColorTexture };
|
|
287
|
+
mr.baseColorTexture.texture = this.getTexture(mr.baseColorTexture.index);
|
|
288
|
+
}
|
|
289
|
+
if (mr.metallicRoughnessTexture) {
|
|
290
|
+
mr.metallicRoughnessTexture = { ...mr.metallicRoughnessTexture };
|
|
291
|
+
mr.metallicRoughnessTexture.texture = this.getTexture(mr.metallicRoughnessTexture.index);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return material;
|
|
295
|
+
}
|
|
296
|
+
_resolveAccessor(gltfAccessor, index) {
|
|
297
|
+
// Look up enums
|
|
298
|
+
const bytesPerComponent = getBytesFromComponentType(gltfAccessor.componentType);
|
|
299
|
+
const components = getSizeFromAccessorType(gltfAccessor.type);
|
|
300
|
+
const bytesPerElement = bytesPerComponent * components;
|
|
301
|
+
const accessor = {
|
|
302
|
+
...gltfAccessor,
|
|
303
|
+
// @ts-expect-error
|
|
304
|
+
id: gltfAccessor.id || `accessor-${index}`,
|
|
305
|
+
bytesPerComponent,
|
|
306
|
+
components,
|
|
307
|
+
bytesPerElement,
|
|
308
|
+
value: undefined,
|
|
309
|
+
bufferView: undefined,
|
|
310
|
+
sparse: undefined
|
|
283
311
|
};
|
|
284
|
-
|
|
285
|
-
|
|
312
|
+
if (gltfAccessor.bufferView !== undefined) {
|
|
313
|
+
// Draco encoded meshes don't have bufferView
|
|
314
|
+
accessor.bufferView = this.getBufferView(gltfAccessor.bufferView);
|
|
315
|
+
}
|
|
316
|
+
// Create TypedArray for the accessor
|
|
317
|
+
// Note: The canonical way to instantiate is to ignore this array and create
|
|
318
|
+
// WebGLBuffer's using the bufferViews.
|
|
319
|
+
if (accessor.bufferView) {
|
|
320
|
+
const buffer = accessor.bufferView.buffer;
|
|
321
|
+
const { ArrayType, byteLength } = getAccessorArrayTypeAndLength(accessor, accessor.bufferView);
|
|
322
|
+
const byteOffset = (accessor.bufferView.byteOffset || 0) + (accessor.byteOffset || 0) + buffer.byteOffset;
|
|
323
|
+
let cutBuffer = buffer.arrayBuffer.slice(byteOffset, byteOffset + byteLength);
|
|
324
|
+
if (accessor.bufferView.byteStride) {
|
|
325
|
+
cutBuffer = this._getValueFromInterleavedBuffer(buffer, byteOffset, accessor.bufferView.byteStride, accessor.bytesPerElement, accessor.count);
|
|
326
|
+
}
|
|
327
|
+
accessor.value = new ArrayType(cutBuffer);
|
|
328
|
+
}
|
|
329
|
+
return accessor;
|
|
286
330
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
accessor.bufferView = this.getBufferView(gltfAccessor.bufferView);
|
|
331
|
+
/**
|
|
332
|
+
* Take values of particular accessor from interleaved buffer
|
|
333
|
+
* various parts of the buffer
|
|
334
|
+
* @param buffer
|
|
335
|
+
* @param byteOffset
|
|
336
|
+
* @param byteStride
|
|
337
|
+
* @param bytesPerElement
|
|
338
|
+
* @param count
|
|
339
|
+
* @returns
|
|
340
|
+
*/
|
|
341
|
+
_getValueFromInterleavedBuffer(buffer, byteOffset, byteStride, bytesPerElement, count) {
|
|
342
|
+
const result = new Uint8Array(count * bytesPerElement);
|
|
343
|
+
for (let i = 0; i < count; i++) {
|
|
344
|
+
const elementOffset = byteOffset + i * byteStride;
|
|
345
|
+
result.set(new Uint8Array(buffer.arrayBuffer.slice(elementOffset, elementOffset + bytesPerElement)), i * bytesPerElement);
|
|
346
|
+
}
|
|
347
|
+
return result.buffer;
|
|
305
348
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
}
|
|
317
|
-
accessor.value = new ArrayType(cutBuffer);
|
|
349
|
+
_resolveTexture(gltfTexture, index) {
|
|
350
|
+
return {
|
|
351
|
+
...gltfTexture,
|
|
352
|
+
// @ts-expect-error id could already be present, glTF standard does not prevent it
|
|
353
|
+
id: gltfTexture.id || `texture-${index}`,
|
|
354
|
+
sampler: typeof gltfTexture.sampler === 'number'
|
|
355
|
+
? this.getSampler(gltfTexture.sampler)
|
|
356
|
+
: makeDefaultSampler(),
|
|
357
|
+
source: typeof gltfTexture.source === 'number' ? this.getImage(gltfTexture.source) : undefined
|
|
358
|
+
};
|
|
318
359
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
360
|
+
_resolveSampler(gltfSampler, index) {
|
|
361
|
+
const sampler = {
|
|
362
|
+
// @ts-expect-error id could already be present, glTF standard does not prevent it
|
|
363
|
+
id: gltfSampler.id || `sampler-${index}`,
|
|
364
|
+
...gltfSampler,
|
|
365
|
+
parameters: {}
|
|
366
|
+
};
|
|
367
|
+
// Map textual parameters to GL parameter values
|
|
368
|
+
for (const key in sampler) {
|
|
369
|
+
const glEnum = this._enumSamplerParameter(key);
|
|
370
|
+
if (glEnum !== undefined) {
|
|
371
|
+
sampler.parameters[glEnum] = sampler[key];
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return sampler;
|
|
326
375
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
_resolveTexture(gltfTexture, index) {
|
|
330
|
-
return {
|
|
331
|
-
...gltfTexture,
|
|
332
|
-
id: gltfTexture.id || `texture-${index}`,
|
|
333
|
-
sampler: typeof gltfTexture.sampler === 'number' ? this.getSampler(gltfTexture.sampler) : makeDefaultSampler(),
|
|
334
|
-
source: typeof gltfTexture.source === 'number' ? this.getImage(gltfTexture.source) : undefined
|
|
335
|
-
};
|
|
336
|
-
}
|
|
337
|
-
_resolveSampler(gltfSampler, index) {
|
|
338
|
-
const sampler = {
|
|
339
|
-
id: gltfSampler.id || `sampler-${index}`,
|
|
340
|
-
...gltfSampler,
|
|
341
|
-
parameters: {}
|
|
342
|
-
};
|
|
343
|
-
for (const key in sampler) {
|
|
344
|
-
const glEnum = this._enumSamplerParameter(key);
|
|
345
|
-
if (glEnum !== undefined) {
|
|
346
|
-
sampler.parameters[glEnum] = sampler[key];
|
|
347
|
-
}
|
|
376
|
+
_enumSamplerParameter(key) {
|
|
377
|
+
return SAMPLER_PARAMETER_GLTF_TO_GL[key];
|
|
348
378
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
379
|
+
_resolveImage(gltfImage, index) {
|
|
380
|
+
const image = {
|
|
381
|
+
...gltfImage,
|
|
382
|
+
// @ts-expect-error id could already be present, glTF standard does not prevent it
|
|
383
|
+
id: gltfImage.id || `image-${index}`,
|
|
384
|
+
image: null,
|
|
385
|
+
bufferView: gltfImage.bufferView !== undefined ? this.getBufferView(gltfImage.bufferView) : undefined
|
|
386
|
+
};
|
|
387
|
+
// Check if image has been preloaded by the GLTFLoader
|
|
388
|
+
// If so, link it into the JSON and drop the URI
|
|
389
|
+
const preloadedImage = this.images[index];
|
|
390
|
+
if (preloadedImage) {
|
|
391
|
+
image.image = preloadedImage;
|
|
392
|
+
}
|
|
393
|
+
return image;
|
|
394
|
+
}
|
|
395
|
+
_resolveBufferView(gltfBufferView, index) {
|
|
396
|
+
const bufferIndex = gltfBufferView.buffer;
|
|
397
|
+
const arrayBuffer = this.buffers[bufferIndex].arrayBuffer;
|
|
398
|
+
// Add offset of buffer, then offset of buffer view
|
|
399
|
+
let byteOffset = this.buffers[bufferIndex].byteOffset || 0;
|
|
400
|
+
if (gltfBufferView.byteOffset) {
|
|
401
|
+
byteOffset += gltfBufferView.byteOffset;
|
|
402
|
+
}
|
|
403
|
+
const bufferView = {
|
|
404
|
+
// // @ts-expect-error id could already be present, glTF standard does not prevent it
|
|
405
|
+
id: `bufferView-${index}`,
|
|
406
|
+
...gltfBufferView,
|
|
407
|
+
// ...this.buffers[bufferIndex],
|
|
408
|
+
buffer: this.buffers[bufferIndex],
|
|
409
|
+
data: new Uint8Array(arrayBuffer, byteOffset, gltfBufferView.byteLength)
|
|
410
|
+
};
|
|
411
|
+
return bufferView;
|
|
364
412
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
413
|
+
_resolveCamera(gltfCamera, index) {
|
|
414
|
+
const camera = {
|
|
415
|
+
...gltfCamera,
|
|
416
|
+
// @ts-expect-error id could already be present, glTF standard does not prevent it
|
|
417
|
+
id: gltfCamera.id || `camera-${index}`
|
|
418
|
+
};
|
|
419
|
+
// TODO - create 4x4 matrices
|
|
420
|
+
if (camera.perspective) {
|
|
421
|
+
// camera.matrix = createPerspectiveMatrix(camera.perspective);
|
|
422
|
+
}
|
|
423
|
+
if (camera.orthographic) {
|
|
424
|
+
// camera.matrix = createOrthographicMatrix(camera.orthographic);
|
|
425
|
+
}
|
|
426
|
+
return camera;
|
|
373
427
|
}
|
|
374
|
-
const bufferView = {
|
|
375
|
-
id: `bufferView-${index}`,
|
|
376
|
-
...gltfBufferView,
|
|
377
|
-
buffer: this.buffers[bufferIndex],
|
|
378
|
-
data: new Uint8Array(arrayBuffer, byteOffset, gltfBufferView.byteLength)
|
|
379
|
-
};
|
|
380
|
-
return bufferView;
|
|
381
|
-
}
|
|
382
|
-
_resolveCamera(gltfCamera, index) {
|
|
383
|
-
const camera = {
|
|
384
|
-
...gltfCamera,
|
|
385
|
-
id: gltfCamera.id || `camera-${index}`
|
|
386
|
-
};
|
|
387
|
-
if (camera.perspective) {}
|
|
388
|
-
if (camera.orthographic) {}
|
|
389
|
-
return camera;
|
|
390
|
-
}
|
|
391
428
|
}
|
|
392
429
|
export function postProcessGLTF(gltf, options) {
|
|
393
|
-
|
|
430
|
+
return new GLTFPostProcessor().postProcess(gltf, options);
|
|
394
431
|
}
|
|
395
|
-
//# sourceMappingURL=post-process-gltf.js.map
|