@loaders.gl/gltf 3.4.11 → 3.4.12
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/es5/lib/utils/version.js +1 -1
- package/dist/esm/lib/utils/version.js +1 -1
- package/package.json +6 -6
- package/dist/bundle.js +0 -5
- package/dist/glb-loader.js +0 -39
- package/dist/glb-writer.js +0 -37
- package/dist/gltf-loader.js +0 -50
- package/dist/gltf-writer.js +0 -32
- package/dist/index.js +0 -23
- package/dist/lib/api/gltf-extensions.js +0 -83
- package/dist/lib/api/gltf-scenegraph.js +0 -566
- package/dist/lib/api/normalize-gltf-v1.js +0 -299
- package/dist/lib/api/post-process-gltf.js +0 -370
- package/dist/lib/encoders/encode-glb.js +0 -61
- package/dist/lib/encoders/encode-gltf.js +0 -27
- package/dist/lib/extensions/EXT_meshopt_compression.js +0 -45
- package/dist/lib/extensions/EXT_texture_webp.js +0 -39
- package/dist/lib/extensions/KHR_binary_gltf.js +0 -42
- package/dist/lib/extensions/KHR_draco_mesh_compression.js +0 -141
- package/dist/lib/extensions/KHR_texture_basisu.js +0 -32
- package/dist/lib/extensions/KHR_texture_transform.js +0 -230
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +0 -118
- package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +0 -62
- package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +0 -47
- package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +0 -82
- package/dist/lib/gltf-utils/get-typed-array.js +0 -41
- package/dist/lib/gltf-utils/gltf-attribute-utils.js +0 -73
- package/dist/lib/gltf-utils/gltf-constants.js +0 -43
- package/dist/lib/gltf-utils/gltf-utils.js +0 -85
- package/dist/lib/gltf-utils/resolve-url.js +0 -18
- package/dist/lib/parsers/parse-glb.js +0 -141
- package/dist/lib/parsers/parse-gltf.js +0 -203
- package/dist/lib/types/glb-types.js +0 -2
- package/dist/lib/types/gltf-json-schema.js +0 -4
- package/dist/lib/types/gltf-postprocessed-schema.js +0 -4
- package/dist/lib/types/gltf-types.js +0 -3
- package/dist/lib/utils/assert.js +0 -12
- package/dist/lib/utils/version.js +0 -7
- package/dist/meshopt/meshopt-decoder.js +0 -118
- package/dist/webp/webp.js +0 -38
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.normalizeGLTFV1 = void 0;
|
|
27
|
-
/* eslint-disable camelcase */
|
|
28
|
-
const KHR_binary_glTF = __importStar(require("../extensions/KHR_binary_gltf"));
|
|
29
|
-
// Binary format changes (mainly implemented by GLBLoader)
|
|
30
|
-
// https://github.com/KhronosGroup/glTF/tree/master/extensions/1.0/Khronos/KHR_binary_glTF
|
|
31
|
-
// JSON format changes:
|
|
32
|
-
// https://github.com/khronosgroup/gltf/issues/605
|
|
33
|
-
// - [x] Top-level JSON objects are arrays now
|
|
34
|
-
// - [ ] Removed indirection from animation: sampler now refers directly to accessors, #712
|
|
35
|
-
// - [ ] material.parameter.value and technique.parameter.value must be an array, #690
|
|
36
|
-
// - [ ] Node can have only one mesh #821
|
|
37
|
-
// - [ ] Added reqs on JSON encoding
|
|
38
|
-
// - [ ] Added reqs on binary data alignment #802 (comment)
|
|
39
|
-
// Additions:
|
|
40
|
-
// - [ ] Added accessor.normalized, #691, #706
|
|
41
|
-
// - [ ] Added glExtensionsUsed property and 5125 (UNSIGNED_INT) accessor.componentType value, #619
|
|
42
|
-
// - [ ] Added extensionsRequired property, #720, #721
|
|
43
|
-
// - [ ] Added "STEP" as valid animation.sampler.interpolation value, #712
|
|
44
|
-
// Removals:
|
|
45
|
-
// - [x] Removed buffer.type, #786, #629
|
|
46
|
-
// - [ ] Removed revision number from profile.version, #709
|
|
47
|
-
// - [ ] Removed technique.functions.scissor and removed 3089 (SCISSOR_TEST) as a valid value for technique.states.enable, #681
|
|
48
|
-
// - [ ] Techniques, programs, and shaders were moved out to KHR_technique_webgl extension.
|
|
49
|
-
// Other edits:
|
|
50
|
-
// - [x] asset is now required, #642
|
|
51
|
-
// - [ ] buffer.byteLength and bufferView.byteLength are now required, #560.
|
|
52
|
-
// - [ ] accessor.min and accessor.max are now required, #593, and clarified that the JSON value and binary data must be the same, #628.
|
|
53
|
-
// - [ ] Clarified animation.sampler and animation.channel restrictions, #712
|
|
54
|
-
// - [ ] skin.inverseBindMatrices is now optional, #461.
|
|
55
|
-
// - [ ] Attribute parameters can't have a value defined in the technique or parameter, #563 (comment).
|
|
56
|
-
// - [ ] Only TEXCOORD and COLOR attribute semantics can be written in the form [semantic]_[set_index], #563 (comment).
|
|
57
|
-
// - [ ] TEXCOORD and COLOR attribute semantics must be written in the form [semantic]_[set_index], e.g., just TEXCOORD should be TEXCOORD_0, and just COLOR should be COLOR_0, #649
|
|
58
|
-
// - [ ] camera.perspective.aspectRatio and camera.perspective.yfov must now be > 0, not >= 0, #563 (comment).
|
|
59
|
-
// - [ ] Application-specific parameter semantics must start with an underscore, e.g., _TEMPERATURE and _SIMULATION_TIME, #563 (comment).
|
|
60
|
-
// - [ ] Properties in technique.parameters must be defined in technique.uniforms or technique.attributes,
|
|
61
|
-
// #563 (comment).
|
|
62
|
-
// - [ ] technique.parameter.count can only be defined when the semantic is JOINTMATRIX or an application-specific semantic is used. It can never be defined for attribute parameters; only uniforms, d2f6945
|
|
63
|
-
// - [ ] technique.parameter.semantic is required when the parameter is an attribute, 28e113d
|
|
64
|
-
// - [ ] Mesh-only models are allowed, e.g., without materials, #642
|
|
65
|
-
// - [ ] Skeleton hierarchies (nodes containing jointName) must be separated from non-skeleton hierarchies., #647
|
|
66
|
-
// - [ ] technique.states.functions.blendColor and technique.states.functions.depthRange parameters now must match WebGL function min/max, #707
|
|
67
|
-
const GLTF_ARRAYS = {
|
|
68
|
-
accessors: 'accessor',
|
|
69
|
-
animations: 'animation',
|
|
70
|
-
buffers: 'buffer',
|
|
71
|
-
bufferViews: 'bufferView',
|
|
72
|
-
images: 'image',
|
|
73
|
-
materials: 'material',
|
|
74
|
-
meshes: 'mesh',
|
|
75
|
-
nodes: 'node',
|
|
76
|
-
samplers: 'sampler',
|
|
77
|
-
scenes: 'scene',
|
|
78
|
-
skins: 'skin',
|
|
79
|
-
textures: 'texture'
|
|
80
|
-
};
|
|
81
|
-
const GLTF_KEYS = {
|
|
82
|
-
accessor: 'accessors',
|
|
83
|
-
animations: 'animation',
|
|
84
|
-
buffer: 'buffers',
|
|
85
|
-
bufferView: 'bufferViews',
|
|
86
|
-
image: 'images',
|
|
87
|
-
material: 'materials',
|
|
88
|
-
mesh: 'meshes',
|
|
89
|
-
node: 'nodes',
|
|
90
|
-
sampler: 'samplers',
|
|
91
|
-
scene: 'scenes',
|
|
92
|
-
skin: 'skins',
|
|
93
|
-
texture: 'textures'
|
|
94
|
-
};
|
|
95
|
-
/**
|
|
96
|
-
* Converts (normalizes) glTF v1 to v2
|
|
97
|
-
*/
|
|
98
|
-
class GLTFV1Normalizer {
|
|
99
|
-
constructor() {
|
|
100
|
-
this.idToIndexMap = {
|
|
101
|
-
animations: {},
|
|
102
|
-
accessors: {},
|
|
103
|
-
buffers: {},
|
|
104
|
-
bufferViews: {},
|
|
105
|
-
images: {},
|
|
106
|
-
materials: {},
|
|
107
|
-
meshes: {},
|
|
108
|
-
nodes: {},
|
|
109
|
-
samplers: {},
|
|
110
|
-
scenes: {},
|
|
111
|
-
skins: {},
|
|
112
|
-
textures: {}
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
// constructor() {}
|
|
116
|
-
/**
|
|
117
|
-
* Convert (normalize) glTF < 2.0 to glTF 2.0
|
|
118
|
-
* @param gltf - object with json and binChunks
|
|
119
|
-
* @param options
|
|
120
|
-
* @param options normalize Whether to actually normalize
|
|
121
|
-
*/
|
|
122
|
-
normalize(gltf, options) {
|
|
123
|
-
this.json = gltf.json;
|
|
124
|
-
const json = gltf.json;
|
|
125
|
-
// Check version
|
|
126
|
-
switch (json.asset && json.asset.version) {
|
|
127
|
-
// We are converting to v2 format. Return if there is nothing to do
|
|
128
|
-
case '2.0':
|
|
129
|
-
return;
|
|
130
|
-
// This class is written to convert 1.0
|
|
131
|
-
case undefined:
|
|
132
|
-
case '1.0':
|
|
133
|
-
break;
|
|
134
|
-
default:
|
|
135
|
-
// eslint-disable-next-line no-undef, no-console
|
|
136
|
-
console.warn(`glTF: Unknown version ${json.asset.version}`);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
if (!options.normalize) {
|
|
140
|
-
// We are still missing a few conversion tricks, remove once addressed
|
|
141
|
-
throw new Error('glTF v1 is not supported.');
|
|
142
|
-
}
|
|
143
|
-
// eslint-disable-next-line no-undef, no-console
|
|
144
|
-
console.warn('Converting glTF v1 to glTF v2 format. This is experimental and may fail.');
|
|
145
|
-
this._addAsset(json);
|
|
146
|
-
// In glTF2 top-level fields are Arrays not Object maps
|
|
147
|
-
this._convertTopLevelObjectsToArrays(json);
|
|
148
|
-
// Extract bufferView indices for images
|
|
149
|
-
// (this extension needs to be invoked early in the normalization process)
|
|
150
|
-
// TODO can this be handled by standard extension processing instead of called explicitly?
|
|
151
|
-
KHR_binary_glTF.preprocess(gltf);
|
|
152
|
-
// Convert object references from ids to indices
|
|
153
|
-
this._convertObjectIdsToArrayIndices(json);
|
|
154
|
-
this._updateObjects(json);
|
|
155
|
-
this._updateMaterial(json);
|
|
156
|
-
}
|
|
157
|
-
// asset is now required, #642 https://github.com/KhronosGroup/glTF/issues/639
|
|
158
|
-
_addAsset(json) {
|
|
159
|
-
json.asset = json.asset || {};
|
|
160
|
-
// We are normalizing to glTF v2, so change version to "2.0"
|
|
161
|
-
json.asset.version = '2.0';
|
|
162
|
-
json.asset.generator = json.asset.generator || 'Normalized to glTF 2.0 by loaders.gl';
|
|
163
|
-
}
|
|
164
|
-
_convertTopLevelObjectsToArrays(json) {
|
|
165
|
-
// TODO check that all arrays are covered
|
|
166
|
-
for (const arrayName in GLTF_ARRAYS) {
|
|
167
|
-
this._convertTopLevelObjectToArray(json, arrayName);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
/** Convert one top level object to array */
|
|
171
|
-
_convertTopLevelObjectToArray(json, mapName) {
|
|
172
|
-
const objectMap = json[mapName];
|
|
173
|
-
if (!objectMap || Array.isArray(objectMap)) {
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
// Rewrite the top-level field as an array
|
|
177
|
-
json[mapName] = [];
|
|
178
|
-
// Copy the map key into object.id
|
|
179
|
-
for (const id in objectMap) {
|
|
180
|
-
const object = objectMap[id];
|
|
181
|
-
object.id = object.id || id; // Mutates the loaded object
|
|
182
|
-
const index = json[mapName].length;
|
|
183
|
-
json[mapName].push(object);
|
|
184
|
-
this.idToIndexMap[mapName][id] = index;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
/** Go through all objects in all top-level arrays and replace ids with indices */
|
|
188
|
-
_convertObjectIdsToArrayIndices(json) {
|
|
189
|
-
for (const arrayName in GLTF_ARRAYS) {
|
|
190
|
-
this._convertIdsToIndices(json, arrayName);
|
|
191
|
-
}
|
|
192
|
-
if ('scene' in json) {
|
|
193
|
-
json.scene = this._convertIdToIndex(json.scene, 'scene');
|
|
194
|
-
}
|
|
195
|
-
// Convert any index references that are not using array names
|
|
196
|
-
// texture.source (image)
|
|
197
|
-
for (const texture of json.textures) {
|
|
198
|
-
this._convertTextureIds(texture);
|
|
199
|
-
}
|
|
200
|
-
for (const mesh of json.meshes) {
|
|
201
|
-
this._convertMeshIds(mesh);
|
|
202
|
-
}
|
|
203
|
-
for (const node of json.nodes) {
|
|
204
|
-
this._convertNodeIds(node);
|
|
205
|
-
}
|
|
206
|
-
for (const node of json.scenes) {
|
|
207
|
-
this._convertSceneIds(node);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
_convertTextureIds(texture) {
|
|
211
|
-
if (texture.source) {
|
|
212
|
-
texture.source = this._convertIdToIndex(texture.source, 'image');
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
_convertMeshIds(mesh) {
|
|
216
|
-
for (const primitive of mesh.primitives) {
|
|
217
|
-
const { attributes, indices, material } = primitive;
|
|
218
|
-
for (const attributeName in attributes) {
|
|
219
|
-
attributes[attributeName] = this._convertIdToIndex(attributes[attributeName], 'accessor');
|
|
220
|
-
}
|
|
221
|
-
if (indices) {
|
|
222
|
-
primitive.indices = this._convertIdToIndex(indices, 'accessor');
|
|
223
|
-
}
|
|
224
|
-
if (material) {
|
|
225
|
-
primitive.material = this._convertIdToIndex(material, 'material');
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
_convertNodeIds(node) {
|
|
230
|
-
if (node.children) {
|
|
231
|
-
node.children = node.children.map((child) => this._convertIdToIndex(child, 'node'));
|
|
232
|
-
}
|
|
233
|
-
if (node.meshes) {
|
|
234
|
-
node.meshes = node.meshes.map((mesh) => this._convertIdToIndex(mesh, 'mesh'));
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
_convertSceneIds(scene) {
|
|
238
|
-
if (scene.nodes) {
|
|
239
|
-
scene.nodes = scene.nodes.map((node) => this._convertIdToIndex(node, 'node'));
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
/** Go through all objects in a top-level array and replace ids with indices */
|
|
243
|
-
_convertIdsToIndices(json, topLevelArrayName) {
|
|
244
|
-
if (!json[topLevelArrayName]) {
|
|
245
|
-
console.warn(`gltf v1: json doesn't contain attribute ${topLevelArrayName}`); // eslint-disable-line no-console, no-undef
|
|
246
|
-
json[topLevelArrayName] = [];
|
|
247
|
-
}
|
|
248
|
-
for (const object of json[topLevelArrayName]) {
|
|
249
|
-
for (const key in object) {
|
|
250
|
-
const id = object[key];
|
|
251
|
-
const index = this._convertIdToIndex(id, key);
|
|
252
|
-
object[key] = index;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
_convertIdToIndex(id, key) {
|
|
257
|
-
const arrayName = GLTF_KEYS[key];
|
|
258
|
-
if (arrayName in this.idToIndexMap) {
|
|
259
|
-
const index = this.idToIndexMap[arrayName][id];
|
|
260
|
-
if (!Number.isFinite(index)) {
|
|
261
|
-
throw new Error(`gltf v1: failed to resolve ${key} with id ${id}`);
|
|
262
|
-
}
|
|
263
|
-
return index;
|
|
264
|
-
}
|
|
265
|
-
return id;
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
*
|
|
269
|
-
* @param {*} json
|
|
270
|
-
*/
|
|
271
|
-
_updateObjects(json) {
|
|
272
|
-
for (const buffer of this.json.buffers) {
|
|
273
|
-
// - [x] Removed buffer.type, #786, #629
|
|
274
|
-
delete buffer.type;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Update material (set pbrMetallicRoughness)
|
|
279
|
-
* @param {*} json
|
|
280
|
-
*/
|
|
281
|
-
_updateMaterial(json) {
|
|
282
|
-
for (const material of json.materials) {
|
|
283
|
-
material.pbrMetallicRoughness = {
|
|
284
|
-
baseColorFactor: [1, 1, 1, 1],
|
|
285
|
-
metallicFactor: 1,
|
|
286
|
-
roughnessFactor: 1
|
|
287
|
-
};
|
|
288
|
-
const textureId = material.values?.tex || material.values?.texture2d_0 || material.values?.diffuseTex;
|
|
289
|
-
const textureIndex = json.textures.findIndex((texture) => texture.id === textureId);
|
|
290
|
-
if (textureIndex !== -1) {
|
|
291
|
-
material.pbrMetallicRoughness.baseColorTexture = { index: textureIndex };
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
function normalizeGLTFV1(gltf, options = {}) {
|
|
297
|
-
return new GLTFV1Normalizer().normalize(gltf, options);
|
|
298
|
-
}
|
|
299
|
-
exports.normalizeGLTFV1 = normalizeGLTFV1;
|
|
@@ -1,370 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.postProcessGLTF = void 0;
|
|
4
|
-
const assert_1 = require("../utils/assert");
|
|
5
|
-
const gltf_utils_1 = require("../gltf-utils/gltf-utils");
|
|
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
|
|
14
|
-
const COMPONENTS = {
|
|
15
|
-
SCALAR: 1,
|
|
16
|
-
VEC2: 2,
|
|
17
|
-
VEC3: 3,
|
|
18
|
-
VEC4: 4,
|
|
19
|
-
MAT2: 4,
|
|
20
|
-
MAT3: 9,
|
|
21
|
-
MAT4: 16
|
|
22
|
-
};
|
|
23
|
-
const BYTES = {
|
|
24
|
-
5120: 1,
|
|
25
|
-
5121: 1,
|
|
26
|
-
5122: 2,
|
|
27
|
-
5123: 2,
|
|
28
|
-
5125: 4,
|
|
29
|
-
5126: 4 // FLOAT
|
|
30
|
-
};
|
|
31
|
-
const GL_SAMPLER = {
|
|
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
|
|
41
|
-
};
|
|
42
|
-
const SAMPLER_PARAMETER_GLTF_TO_GL = {
|
|
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
|
|
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
|
|
50
|
-
const DEFAULT_SAMPLER = {
|
|
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
|
|
55
|
-
};
|
|
56
|
-
function getBytesFromComponentType(componentType) {
|
|
57
|
-
return BYTES[componentType];
|
|
58
|
-
}
|
|
59
|
-
function getSizeFromAccessorType(type) {
|
|
60
|
-
return COMPONENTS[type];
|
|
61
|
-
}
|
|
62
|
-
class GLTFPostProcessor {
|
|
63
|
-
constructor() {
|
|
64
|
-
this.baseUri = '';
|
|
65
|
-
this.json = {};
|
|
66
|
-
this.buffers = [];
|
|
67
|
-
this.images = [];
|
|
68
|
-
}
|
|
69
|
-
postProcess(gltf, options = {}) {
|
|
70
|
-
const { json, buffers = [], images = [], baseUri = '' } = gltf;
|
|
71
|
-
(0, assert_1.assert)(json);
|
|
72
|
-
this.baseUri = baseUri;
|
|
73
|
-
this.json = json;
|
|
74
|
-
this.buffers = buffers;
|
|
75
|
-
this.images = images;
|
|
76
|
-
this._resolveTree(this.json, options);
|
|
77
|
-
return this.json;
|
|
78
|
-
}
|
|
79
|
-
// Convert indexed glTF structure into tree structure
|
|
80
|
-
// cross-link index resolution, enum lookup, convenience calculations
|
|
81
|
-
// eslint-disable-next-line complexity
|
|
82
|
-
_resolveTree(json, options = {}) {
|
|
83
|
-
if (json.bufferViews) {
|
|
84
|
-
json.bufferViews = json.bufferViews.map((bufView, i) => this._resolveBufferView(bufView, i));
|
|
85
|
-
}
|
|
86
|
-
if (json.images) {
|
|
87
|
-
json.images = json.images.map((image, i) => this._resolveImage(image, i));
|
|
88
|
-
}
|
|
89
|
-
if (json.samplers) {
|
|
90
|
-
json.samplers = json.samplers.map((sampler, i) => this._resolveSampler(sampler, i));
|
|
91
|
-
}
|
|
92
|
-
if (json.textures) {
|
|
93
|
-
json.textures = json.textures.map((texture, i) => this._resolveTexture(texture, i));
|
|
94
|
-
}
|
|
95
|
-
if (json.accessors) {
|
|
96
|
-
json.accessors = json.accessors.map((accessor, i) => this._resolveAccessor(accessor, i));
|
|
97
|
-
}
|
|
98
|
-
if (json.materials) {
|
|
99
|
-
json.materials = json.materials.map((material, i) => this._resolveMaterial(material, i));
|
|
100
|
-
}
|
|
101
|
-
if (json.meshes) {
|
|
102
|
-
json.meshes = json.meshes.map((mesh, i) => this._resolveMesh(mesh, i));
|
|
103
|
-
}
|
|
104
|
-
if (json.nodes) {
|
|
105
|
-
json.nodes = json.nodes.map((node, i) => this._resolveNode(node, i));
|
|
106
|
-
}
|
|
107
|
-
if (json.skins) {
|
|
108
|
-
json.skins = json.skins.map((skin, i) => this._resolveSkin(skin, i));
|
|
109
|
-
}
|
|
110
|
-
if (json.scenes) {
|
|
111
|
-
json.scenes = json.scenes.map((scene, i) => this._resolveScene(scene, i));
|
|
112
|
-
}
|
|
113
|
-
if (json.scene !== undefined) {
|
|
114
|
-
json.scene = json.scenes[this.json.scene];
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
getScene(index) {
|
|
118
|
-
return this._get('scenes', index);
|
|
119
|
-
}
|
|
120
|
-
getNode(index) {
|
|
121
|
-
return this._get('nodes', index);
|
|
122
|
-
}
|
|
123
|
-
getSkin(index) {
|
|
124
|
-
return this._get('skins', index);
|
|
125
|
-
}
|
|
126
|
-
getMesh(index) {
|
|
127
|
-
return this._get('meshes', index);
|
|
128
|
-
}
|
|
129
|
-
getMaterial(index) {
|
|
130
|
-
return this._get('materials', index);
|
|
131
|
-
}
|
|
132
|
-
getAccessor(index) {
|
|
133
|
-
return this._get('accessors', index);
|
|
134
|
-
}
|
|
135
|
-
getCamera(index) {
|
|
136
|
-
return null; // TODO: fix this
|
|
137
|
-
}
|
|
138
|
-
getTexture(index) {
|
|
139
|
-
return this._get('textures', index);
|
|
140
|
-
}
|
|
141
|
-
getSampler(index) {
|
|
142
|
-
return this._get('samplers', index);
|
|
143
|
-
}
|
|
144
|
-
getImage(index) {
|
|
145
|
-
return this._get('images', index);
|
|
146
|
-
}
|
|
147
|
-
getBufferView(index) {
|
|
148
|
-
return this._get('bufferViews', index);
|
|
149
|
-
}
|
|
150
|
-
getBuffer(index) {
|
|
151
|
-
return this._get('buffers', index);
|
|
152
|
-
}
|
|
153
|
-
_get(array, index) {
|
|
154
|
-
// check if already resolved
|
|
155
|
-
if (typeof index === 'object') {
|
|
156
|
-
return index;
|
|
157
|
-
}
|
|
158
|
-
const object = this.json[array] && this.json[array][index];
|
|
159
|
-
if (!object) {
|
|
160
|
-
console.warn(`glTF file error: Could not find ${array}[${index}]`); // eslint-disable-line
|
|
161
|
-
}
|
|
162
|
-
return object;
|
|
163
|
-
}
|
|
164
|
-
// PARSING HELPERS
|
|
165
|
-
_resolveScene(scene, index) {
|
|
166
|
-
// scene = {...scene};
|
|
167
|
-
scene.id = scene.id || `scene-${index}`;
|
|
168
|
-
scene.nodes = (scene.nodes || []).map((node) => this.getNode(node));
|
|
169
|
-
return scene;
|
|
170
|
-
}
|
|
171
|
-
_resolveNode(node, index) {
|
|
172
|
-
// node = {...node};
|
|
173
|
-
node.id = node.id || `node-${index}`;
|
|
174
|
-
if (node.children) {
|
|
175
|
-
node.children = node.children.map((child) => this.getNode(child));
|
|
176
|
-
}
|
|
177
|
-
if (node.mesh !== undefined) {
|
|
178
|
-
node.mesh = this.getMesh(node.mesh);
|
|
179
|
-
}
|
|
180
|
-
else if (node.meshes !== undefined && node.meshes.length) {
|
|
181
|
-
node.mesh = node.meshes.reduce((accum, meshIndex) => {
|
|
182
|
-
const mesh = this.getMesh(meshIndex);
|
|
183
|
-
accum.id = mesh.id;
|
|
184
|
-
accum.primitives = accum.primitives.concat(mesh.primitives);
|
|
185
|
-
return accum;
|
|
186
|
-
}, { primitives: [] });
|
|
187
|
-
}
|
|
188
|
-
if (node.camera !== undefined) {
|
|
189
|
-
node.camera = this.getCamera(node.camera);
|
|
190
|
-
}
|
|
191
|
-
if (node.skin !== undefined) {
|
|
192
|
-
node.skin = this.getSkin(node.skin);
|
|
193
|
-
}
|
|
194
|
-
return node;
|
|
195
|
-
}
|
|
196
|
-
_resolveSkin(skin, index) {
|
|
197
|
-
// skin = {...skin};
|
|
198
|
-
skin.id = skin.id || `skin-${index}`;
|
|
199
|
-
skin.inverseBindMatrices = this.getAccessor(skin.inverseBindMatrices);
|
|
200
|
-
return skin;
|
|
201
|
-
}
|
|
202
|
-
_resolveMesh(mesh, index) {
|
|
203
|
-
// mesh = {...mesh};
|
|
204
|
-
mesh.id = mesh.id || `mesh-${index}`;
|
|
205
|
-
if (mesh.primitives) {
|
|
206
|
-
mesh.primitives = mesh.primitives.map((primitive) => {
|
|
207
|
-
primitive = { ...primitive };
|
|
208
|
-
const attributes = primitive.attributes;
|
|
209
|
-
primitive.attributes = {};
|
|
210
|
-
for (const attribute in attributes) {
|
|
211
|
-
primitive.attributes[attribute] = this.getAccessor(attributes[attribute]);
|
|
212
|
-
}
|
|
213
|
-
if (primitive.indices !== undefined) {
|
|
214
|
-
primitive.indices = this.getAccessor(primitive.indices);
|
|
215
|
-
}
|
|
216
|
-
if (primitive.material !== undefined) {
|
|
217
|
-
primitive.material = this.getMaterial(primitive.material);
|
|
218
|
-
}
|
|
219
|
-
return primitive;
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
return mesh;
|
|
223
|
-
}
|
|
224
|
-
_resolveMaterial(material, index) {
|
|
225
|
-
// material = {...material};
|
|
226
|
-
material.id = material.id || `material-${index}`;
|
|
227
|
-
if (material.normalTexture) {
|
|
228
|
-
material.normalTexture = { ...material.normalTexture };
|
|
229
|
-
material.normalTexture.texture = this.getTexture(material.normalTexture.index);
|
|
230
|
-
}
|
|
231
|
-
if (material.occlusionTexture) {
|
|
232
|
-
material.occlustionTexture = { ...material.occlustionTexture };
|
|
233
|
-
material.occlusionTexture.texture = this.getTexture(material.occlusionTexture.index);
|
|
234
|
-
}
|
|
235
|
-
if (material.emissiveTexture) {
|
|
236
|
-
material.emmisiveTexture = { ...material.emmisiveTexture };
|
|
237
|
-
material.emissiveTexture.texture = this.getTexture(material.emissiveTexture.index);
|
|
238
|
-
}
|
|
239
|
-
if (!material.emissiveFactor) {
|
|
240
|
-
material.emissiveFactor = material.emmisiveTexture ? [1, 1, 1] : [0, 0, 0];
|
|
241
|
-
}
|
|
242
|
-
if (material.pbrMetallicRoughness) {
|
|
243
|
-
material.pbrMetallicRoughness = { ...material.pbrMetallicRoughness };
|
|
244
|
-
const mr = material.pbrMetallicRoughness;
|
|
245
|
-
if (mr.baseColorTexture) {
|
|
246
|
-
mr.baseColorTexture = { ...mr.baseColorTexture };
|
|
247
|
-
mr.baseColorTexture.texture = this.getTexture(mr.baseColorTexture.index);
|
|
248
|
-
}
|
|
249
|
-
if (mr.metallicRoughnessTexture) {
|
|
250
|
-
mr.metallicRoughnessTexture = { ...mr.metallicRoughnessTexture };
|
|
251
|
-
mr.metallicRoughnessTexture.texture = this.getTexture(mr.metallicRoughnessTexture.index);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return material;
|
|
255
|
-
}
|
|
256
|
-
_resolveAccessor(accessor, index) {
|
|
257
|
-
// accessor = {...accessor};
|
|
258
|
-
accessor.id = accessor.id || `accessor-${index}`;
|
|
259
|
-
if (accessor.bufferView !== undefined) {
|
|
260
|
-
// Draco encoded meshes don't have bufferView
|
|
261
|
-
accessor.bufferView = this.getBufferView(accessor.bufferView);
|
|
262
|
-
}
|
|
263
|
-
// Look up enums
|
|
264
|
-
accessor.bytesPerComponent = getBytesFromComponentType(accessor.componentType);
|
|
265
|
-
accessor.components = getSizeFromAccessorType(accessor.type);
|
|
266
|
-
accessor.bytesPerElement = accessor.bytesPerComponent * accessor.components;
|
|
267
|
-
// Create TypedArray for the accessor
|
|
268
|
-
// Note: The canonical way to instantiate is to ignore this array and create
|
|
269
|
-
// WebGLBuffer's using the bufferViews.
|
|
270
|
-
if (accessor.bufferView) {
|
|
271
|
-
const buffer = accessor.bufferView.buffer;
|
|
272
|
-
const { ArrayType, byteLength } = (0, gltf_utils_1.getAccessorArrayTypeAndLength)(accessor, accessor.bufferView);
|
|
273
|
-
const byteOffset = (accessor.bufferView.byteOffset || 0) + (accessor.byteOffset || 0) + buffer.byteOffset;
|
|
274
|
-
let cutBuffer = buffer.arrayBuffer.slice(byteOffset, byteOffset + byteLength);
|
|
275
|
-
if (accessor.bufferView.byteStride) {
|
|
276
|
-
cutBuffer = this._getValueFromInterleavedBuffer(buffer, byteOffset, accessor.bufferView.byteStride, accessor.bytesPerElement, accessor.count);
|
|
277
|
-
}
|
|
278
|
-
accessor.value = new ArrayType(cutBuffer);
|
|
279
|
-
}
|
|
280
|
-
return accessor;
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Take values of particular accessor from interleaved buffer
|
|
284
|
-
* various parts of the buffer
|
|
285
|
-
* @param buffer
|
|
286
|
-
* @param byteOffset
|
|
287
|
-
* @param byteStride
|
|
288
|
-
* @param bytesPerElement
|
|
289
|
-
* @param count
|
|
290
|
-
* @returns
|
|
291
|
-
*/
|
|
292
|
-
_getValueFromInterleavedBuffer(buffer, byteOffset, byteStride, bytesPerElement, count) {
|
|
293
|
-
const result = new Uint8Array(count * bytesPerElement);
|
|
294
|
-
for (let i = 0; i < count; i++) {
|
|
295
|
-
const elementOffset = byteOffset + i * byteStride;
|
|
296
|
-
result.set(new Uint8Array(buffer.arrayBuffer.slice(elementOffset, elementOffset + bytesPerElement)), i * bytesPerElement);
|
|
297
|
-
}
|
|
298
|
-
return result.buffer;
|
|
299
|
-
}
|
|
300
|
-
_resolveTexture(texture, index) {
|
|
301
|
-
// texture = {...texture};
|
|
302
|
-
texture.id = texture.id || `texture-${index}`;
|
|
303
|
-
texture.sampler = 'sampler' in texture ? this.getSampler(texture.sampler) : DEFAULT_SAMPLER;
|
|
304
|
-
texture.source = this.getImage(texture.source);
|
|
305
|
-
return texture;
|
|
306
|
-
}
|
|
307
|
-
_resolveSampler(sampler, index) {
|
|
308
|
-
// sampler = {...sampler};
|
|
309
|
-
sampler.id = sampler.id || `sampler-${index}`;
|
|
310
|
-
// Map textual parameters to GL parameter values
|
|
311
|
-
sampler.parameters = {};
|
|
312
|
-
for (const key in sampler) {
|
|
313
|
-
const glEnum = this._enumSamplerParameter(key);
|
|
314
|
-
if (glEnum !== undefined) {
|
|
315
|
-
sampler.parameters[glEnum] = sampler[key];
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
return sampler;
|
|
319
|
-
}
|
|
320
|
-
_enumSamplerParameter(key) {
|
|
321
|
-
return SAMPLER_PARAMETER_GLTF_TO_GL[key];
|
|
322
|
-
}
|
|
323
|
-
_resolveImage(image, index) {
|
|
324
|
-
// image = {...image};
|
|
325
|
-
image.id = image.id || `image-${index}`;
|
|
326
|
-
if (image.bufferView !== undefined) {
|
|
327
|
-
image.bufferView = this.getBufferView(image.bufferView);
|
|
328
|
-
}
|
|
329
|
-
// Check if image has been preloaded by the GLTFLoader
|
|
330
|
-
// If so, link it into the JSON and drop the URI
|
|
331
|
-
const preloadedImage = this.images[index];
|
|
332
|
-
if (preloadedImage) {
|
|
333
|
-
image.image = preloadedImage;
|
|
334
|
-
}
|
|
335
|
-
return image;
|
|
336
|
-
}
|
|
337
|
-
_resolveBufferView(bufferView, index) {
|
|
338
|
-
// bufferView = {...bufferView};
|
|
339
|
-
const bufferIndex = bufferView.buffer;
|
|
340
|
-
const result = {
|
|
341
|
-
id: `bufferView-${index}`,
|
|
342
|
-
...bufferView,
|
|
343
|
-
buffer: this.buffers[bufferIndex]
|
|
344
|
-
};
|
|
345
|
-
// @ts-expect-error
|
|
346
|
-
const arrayBuffer = this.buffers[bufferIndex].arrayBuffer;
|
|
347
|
-
// @ts-expect-error
|
|
348
|
-
let byteOffset = this.buffers[bufferIndex].byteOffset || 0;
|
|
349
|
-
if ('byteOffset' in bufferView) {
|
|
350
|
-
byteOffset += bufferView.byteOffset;
|
|
351
|
-
}
|
|
352
|
-
result.data = new Uint8Array(arrayBuffer, byteOffset, bufferView.byteLength);
|
|
353
|
-
return result;
|
|
354
|
-
}
|
|
355
|
-
_resolveCamera(camera, index) {
|
|
356
|
-
camera.id = camera.id || `camera-${index}`;
|
|
357
|
-
// TODO - create 4x4 matrices
|
|
358
|
-
if (camera.perspective) {
|
|
359
|
-
// camera.matrix = createPerspectiveMatrix(camera.perspective);
|
|
360
|
-
}
|
|
361
|
-
if (camera.orthographic) {
|
|
362
|
-
// camera.matrix = createOrthographicMatrix(camera.orthographic);
|
|
363
|
-
}
|
|
364
|
-
return camera;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
function postProcessGLTF(gltf, options) {
|
|
368
|
-
return new GLTFPostProcessor().postProcess(gltf, options);
|
|
369
|
-
}
|
|
370
|
-
exports.postProcessGLTF = postProcessGLTF;
|