-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathGLTFLoader.js
More file actions
863 lines (803 loc) · 33.9 KB
/
GLTFLoader.js
File metadata and controls
863 lines (803 loc) · 33.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
import GLTFV1 from './adapters/GLTFV1.js';
import GLTFV2 from './adapters/GLTFV2.js';
import Accessor from './core/Accessor.js';
import GLBReader from './GLBReader.js';
import { defined, extend, isNumber, isString, getTypedArrayCtor, readInterleavedArray } from './common/Util.js';
import AnimationClip from './core/AnimationClip.js';
import { vec3 } from 'gl-matrix';
import { EXT_MESH_FEATURES } from './extensions/ExtMeshFeatures.js';
import {
EXT_MESH_GPU_INSTANCING,
collectInstancingAccessors,
getGpuInstancingExtension,
} from './extensions/ExtMeshGpuInstancing.js';
import {
EXT_STRUCTURAL_METADATA,
collectPropertyTableBufferViews,
getStructuralMetadataData,
} from './extensions/ExtStructuralMetadata.js';
let supportOffscreenLoad = false;
if (typeof OffscreenCanvas !== 'undefined') {
let ctx;
try {
ctx = new OffscreenCanvas(2, 2).getContext('2d');
} catch (err) {
//nothing need to do
}
if (ctx && typeof createImageBitmap !== 'undefined') {
supportOffscreenLoad = true;
}
}
const canvas = typeof document === 'undefined' ? null : document.createElement('canvas');
export default class GLTFLoader {
constructor(rootPath, gltf, options) {
this.options = options || {};
if (!this.options.decoders) {
this.options.decoders = {};
}
this._fetchOptions = this.options.fetchOptions || {};
if (gltf.buffer instanceof ArrayBuffer) {
const textDecoder = new TextDecoder();
const magic = textDecoder.decode(new Uint8Array(gltf.buffer, 0, 4));
if (magic === 'glTF') {
const { json, glbBuffer } = GLBReader.read(gltf.buffer, gltf.byteOffset, gltf.byteLength);
this._init(rootPath, json, glbBuffer);
} else {
const json = JSON.parse(textDecoder.decode(gltf.buffer));
this._init(rootPath, json);
}
} else {
this._init(rootPath, gltf);
}
this._accessor = new Accessor(this.rootPath, this.gltf, this.glbBuffer, this._fetchOptions, this.options.urlModifier);
this._checkExtensions();
}
_checkExtensions() {
const extensionsRequired = this.gltf['extensionsRequired'];
if (extensionsRequired) {
if (extensionsRequired.indexOf('KHR_draco_mesh_compression') >= 0 && !this.options.decoders['draco']) {
throw new Error('KHR_draco_mesh_compression is required but @maptalks/transcoders.draco is not loaded');
}
if (extensionsRequired.indexOf('KHR_texture_basisu') >= 0 && !this.options.decoders['image/ktx2']) {
throw new Error('KHR_texture_basisu is required but @maptalks/transcoders.ktx2 is not loaded');
}
if (extensionsRequired.indexOf('EXT_meshopt_compression') >= 0) {
throw new Error('EXT_meshopt_compression extension is not supported yet.');
}
}
}
_loadExtensions() {
const extensions = this.gltf['extensions'];
if (!extensions || !extensions['KHR_techniques_webgl']) {
return Promise.resolve(extensions);
}
return this._accessor.requestKHRTechniquesWebgl(extensions['KHR_techniques_webgl']).then(extension => {
extensions['KHR_techniques_webgl'] = extension;
return extensions;
});
}
_convertKhrTechiqueToTechiques() {
// convert extension KHR_technique_webgl (don't have "s") to KHR_techniques_webgl
if (!Array.isArray(this.gltf['programs'])) {
return;
}
const materials = this.gltf.materials;
for (let i = 0; i < materials.length; i++) {
if (materials[i] && materials[i].extensions && materials[i].extensions['KHR_technique_webgl']) {
materials[i].extensions['KHR_techniques_webgl'] = materials[i].extensions['KHR_technique_webgl'];
delete materials[i].extensions['KHR_technique_webgl'];
}
}
const extensions = this.gltf.extensions || {};
const techniques = this.gltf.techniques;
extensions['KHR_techniques_webgl'] = {
programs: this.gltf.programs,
shaders: this.gltf.shaders,
techniques: techniques
};
for (let i = 0; i < materials.length; i++) {
const techValues = getTechniqueValues(materials[i]);
if (techValues) {
const { values, technique: techIndex } = techValues;
const technique = techniques[techIndex];
if (!technique || !values) {
continue;
}
const { uniforms, parameters } = technique;
const realValues = {};
for (const p in values) {
const uniName = findUniformName(uniforms, p);
realValues[uniName] = values[p];
if (parameters[p] && parameters[p].type === 35678) {
// texture
realValues[uniName] = { index: values[p] };
}
}
techValues.values = realValues;
}
}
for (let i = 0; i < techniques.length; i++) {
const technique = techniques[i];
if (!technique) { continue; }
const { attributes, uniforms, parameters } = technique;
if (attributes) {
for (const p in attributes) {
const param = attributes[p];
attributes[p] = parameters[param];
}
}
if (uniforms) {
for (const p in uniforms) {
const param = uniforms[p];
uniforms[p] = parameters[param];
}
}
delete technique.parameters;
}
delete this.gltf.programs;
delete this.gltf.shaders;
delete this.gltf.techniques;
this.gltf.extensions = extensions;
return extensions;
}
load(options) {
options = options || {};
// this._initTexture();
this._convertKhrTechiqueToTechiques();
const gltf = this._loadScene(options);
const animations = this._loadAnimations();
const textures = this._loadTextures();
const extensions = this._loadExtensions();
const metadata = this._loadStructuralMetadata();
return Promise.all([gltf, animations, textures, extensions, metadata]).then(fullfilled => {
fullfilled[0].animations = fullfilled[1];
fullfilled[0].textures = fullfilled[2];
fullfilled[0].extensions = fullfilled[3];
fullfilled[0].transferables = this.transferables || [];
fullfilled[0].structuralMetadata = fullfilled[4];
//预先将channels以key-value的形式存储起来,提高查找效率
this.createChannelsMap(fullfilled[0]);
return fullfilled[0];
});
}
createChannelsMap(gltf) {
const animations = gltf.animations;
if (animations) {
for (let i = 0; i < animations.length; i++) {
const animation = animations[i];
animation.channelsMap = {};
for (let j = 0; j < animation.channels.length; j++) {
const channel = animation.channels[j];
if (!animation.channelsMap[channel.target.node]) {
animation.channelsMap[channel.target.node] = [];
}
animation.channelsMap[channel.target.node].push(channel);
}
}
}
}
getExternalResources() {
const resources = [];
if (this.gltf) {
const { buffers, images } = this.gltf;
for (let i = 0; i < buffers.length; i++) {
//https://github.com/KhronosGroup/glTF-Tutorials/issues/21
if (buffers[i].uri && buffers[i].uri.indexOf('data:application/octet-stream;base64') < 0) {
resources.push({
type: 'buffer',
uri: buffers[i].uri
});
}
}
for (let i = 0; i < images.length; i++) {
if (images[i].uri && images[i].uri.indexOf('data:image/') < 0) {
resources.push({
type: 'image',
uri: images[i].uri
});
}
}
}
return resources;
}
static getAnimationClip(gltf, node, time, name) {
return AnimationClip.getAnimationClip(gltf, node, time, name);
}
static getAnimationTimeSpan(gltf, name) {
return AnimationClip.getTimeSpanByName(gltf, name);
}
static getTypedArrayCtor(componentType) {
return getTypedArrayCtor(componentType);
}
static readInterleavedArray(out, arrayBuffer, count, size, stride, byteOffset, componentType) {
return readInterleavedArray(out, arrayBuffer, count, size, stride, byteOffset, componentType);
}
_init(rootPath, gltf, glbBuffer) {
this.gltf = gltf;
this.glbBuffer = glbBuffer;
this.version = gltf.asset ? +gltf.asset.version : 1;
this.rootPath = rootPath;
this.buffers = {};
this.requests = {};
// this.options.requestImage = this.options.requestImage ? this.options.requestImage : (supportOffscreenLoad ? requestImageOffscreen.bind(this) : requestImage);
this.options.requestImage = supportOffscreenLoad ? requestImageOffscreen.bind(this) : this.options.requestImage || requestImage;
if (this.options['transferable']) {
//用于在worker中解析gltf
this.transferables = [];
}
//在修改莆田osgb => box模型的bug时,发现不同gltf版本对glbBuffer的offset处理方式不同
//2.0中要忽略glbBuffer的byteOffset
//1.0中不能忽略
if (this.version === 2) {
this.adapter = new GLTFV2(rootPath, gltf, glbBuffer, this.options.requestImage, this.options.decoders || {}, this.options.supportedFormats || {}, this._fetchOptions, this.options.urlModifier);
// NOTE: buffer.id和image.id都会作为GLTFV2.requests的key,用前缀加以区分
this.adapter.iterate((_key, buffer, index) => {
buffer.id = 'buffer_' + index;
}, 'buffers');
// NOTE: image.bufferView(如果存在)也会作为GLTFV2.images的key,用前缀加以区分
this.adapter.iterate((_key, image, index) => {
image.id = 'image_' + index;
}, 'images');
this.adapter.iterate((_key, accessor, index) => {
accessor.id = 'accessor_' + index;
}, 'accessors');
} else {
this.adapter = new GLTFV1(rootPath, gltf, glbBuffer, this.options.requestImage, this.options.decoders || {}, this.options.supportedFormats || {}, this._fetchOptions, this.options.urlModifier);
this.adapter.iterate((_key, accessor, index) => {
accessor.id = 'accessor_' + index;
}, 'accessors');
this.adapter.iterate((_key, image, index) => {
image.id = 'image_' + index;
}, 'images');
}
}
// _loadImages()
//V1和V2需要实现的方法
// iterateMesh, iterateNode,
_parseNodes(node, nodeMap) {
if (node.children && node.children.length > 0) {
if (!isNumber(node.children[0]) && !isString(node.children[0])) {
return node;
}
const children = node.children.map((c) => {
const childNode = nodeMap[c];
childNode.nodeIndex = c;
return this._parseNodes(childNode, nodeMap);
});
node.children = children;
}
return node;
}
_loadScene(options) {
return this._loadNodes(options).then(nodeMap => {
const scenes = this.scenes = [];
let defaultScene;
for (const index in nodeMap) {
nodeMap[index] = this._parseNodes(nodeMap[index], nodeMap);
nodeMap[index].nodeIndex = Number(index) ? Number(index) : index;
}
this.adapter.iterate((key, sceneJSON, idx) => {
const scene = {};
if (sceneJSON.name) scene.name = sceneJSON.name;
if (sceneJSON.nodes) {
scene.nodes = sceneJSON.nodes.map(n => {
return nodeMap[n];
});
}
if (this.gltf.scene === key) {
defaultScene = idx;
}
scenes.push(scene);
}, 'scenes');
const gltf = {
textures: this.gltf.textures,
asset: this.gltf.asset,
scene : defaultScene,
scenes : scenes,
nodes : nodeMap,
meshes : this.meshes,
materials: this.gltf.materials,
skins : this.skins,
extensionsRequired: this.gltf.extensionsRequired,
extensionsUsed: this.gltf.extensionsUsed
};
if (this.gltf.extensions) {
gltf.extensions = this.gltf.extensions;
}
if (this.version === 1) {
const materials = this.adapter._loadMaterials(this.gltf.materials);
gltf.materials = materials;
}
delete this.gltf.buffers;
gltf.json = this.gltf;
return gltf;
});
}
_loadNodes(options) {
// node -> meshes -> primitives ->
const promise = this._loadMeshes(options);
return promise.then(() => {
const nodes = this.nodes = {};
const instancingPromises = [];
this.adapter.iterate((key, nodeJSON) => {
//TODO 缺少 skin, morgh targets 和 extensions
const node = this.adapter.createNode(nodeJSON, this.meshes, this.skins);
nodes[key] = node;
// Handle EXT_mesh_gpu_instancing extension
const instancingExt = getGpuInstancingExtension(nodeJSON);
if (instancingExt) {
const loadPromise = this._loadGpuInstancingData(
instancingExt,
).then((instancingData) => {
if (!node.extensions) {
node.extensions = {};
}
node.extensions[EXT_MESH_GPU_INSTANCING] =
instancingData;
});
instancingPromises.push(loadPromise);
}
}, 'nodes');
// Wait for all GPU instancing data to finish loading
return Promise.all(instancingPromises).then(() => nodes);
});
}
/**
* Load accessor data for EXT_mesh_gpu_instancing extension
* @param {Object} instancingExt - EXT_mesh_gpu_instancing extension object
* @returns {Promise<Object>} Object containing loaded attribute data
*/
_loadGpuInstancingData(instancingExt) {
const accessors = collectInstancingAccessors(instancingExt);
if (accessors.length === 0) {
return Promise.resolve({ attributes: {} });
}
const promises = accessors.map(({ name, accessorIndex }) => {
return this.adapter.accessor
._requestData(name, accessorIndex)
.then((data) => {
if (
data &&
data.array &&
data.array.buffer &&
this.transferables
) {
if (this.transferables.indexOf(data.array.buffer) < 0) {
this.transferables.push(data.array.buffer);
}
}
return { name, data };
});
});
return Promise.all(promises).then((results) => {
const attributes = {};
results.forEach(({ name, data }) => {
attributes[name] = data;
});
return { attributes };
});
}
_loadSkins() {
this.skins = [];
const promises = [];
this.adapter.iterate((key, skinJSON, index) => {
promises.push(this._loadSkin(skinJSON).then(skin => {
skin.index = index;
this.skins.push(skin);
}));
}, 'skins');
return promises;
}
_loadSkin(skin) {
const inverseBindMatrices = skin.inverseBindMatrices;
return this.adapter.accessor._requestData('inverseBindMatrices', inverseBindMatrices).then(res => {
skin.inverseBindMatrices = res;
if (res && res.buffer && this.transferables && this.transferables.indexOf(res.buffer) < 0) {
this.transferables.push(res.buffer);
}
return skin;
});
}
_loadAnimations() {
const animations = this.gltf.animations;
const promise = defined(animations) ? this.adapter.getAnimations(animations) : null;
return promise;
}
_loadMeshes(options) {
this.meshes = {};
let promises = [];
this.adapter.iterate((key, meshJSON, index) => {
promises.push(this._loadMesh(meshJSON, options).then(mesh => {
//record mesh's index to reuse it as possible
mesh.index = index;
this.meshes[key] = mesh;
//return mesh;
}));
}, 'meshes');
promises = promises.concat(this._loadSkins());
return Promise.all(promises);
}
_loadMesh(mesh, options) {
//TODO 解析材质
const primitives = mesh.primitives; // attributes
const promises = primitives.map(p => {
return this._loadPrimitive(p, options);
}).filter(p => !!p);
return Promise.all(promises).then(primitives => {
const out = {};
extend(out, mesh);
out.primitives = primitives;
return out;
});
}
_loadTextures() {
const textures = this.gltf.textures;
if (!textures) {
return null;
}
const promises = [];
for (const i in textures) {
promises.push(this.adapter._getTexture(i));
}
return Promise.all(promises).then(loaded => {
if (this.transferables) {
for (let i = 0; i < loaded.length; i++) {
const array = loaded[i] && loaded[i].image.array;
if (array) {
let buffer;
if (array instanceof ImageBitmap) {
buffer = array;
} else {
buffer = array.buffer;
}
if (buffer && this.transferables.indexOf(buffer) < 0) {
this.transferables.push(buffer);
}
}
}
}
if (!Array.isArray(textures)) {
//v1
const out = {};
const keys = Object.keys(textures);
for (let i = 0; i < loaded.length; i++) {
if (loaded[i]) {
out[keys[i]] = loaded[i];
}
}
return out;
}
return loaded;
});
}
_loadPrimitive(primJSON, options) {
// Type Description Required
// attributes object A dictionary object, where each key corresponds to mesh attribute semantic and each value is the index of the accessor containing attribute's data. ✅ Yes
// indices integer The index of the accessor that contains the indices. No
// material integer The index of the material to apply to this primitive when rendering. No
// mode integer The type of primitives to render. No, default: 4
// targets object [1-*] An array of Morph Targets, each Morph Target is a dictionary mapping attributes (only POSITION, NORMAL, and TANGENT supported) to their deviations in the Morph Target. No
// extensions object Dictionary object with extension-specific objects. No
// extras any Application-specific data. No
let promise;
const promises = [];
const extensions = primJSON.extensions;
//targets不是attributes,对于draco和非draco的数据都需要处理
if (defined(primJSON.targets)) {
for (let i = 0; i < primJSON.targets.length; i++) {
const target = primJSON.targets[i];
for (const attr in target) {
const promise = this.adapter.accessor._requestData(`morphTargets_${attr}_${i}`, target[attr]);
if (promise) {
promises.push(promise);
}
}
}
}
if (extensions && extensions['KHR_draco_mesh_compression']) {
// 没有载入draco时,如果 KHR_draco_mesh_compression 不在 extensionsRequired 中,则忽略该 primitive
if (!this.options.decoders['draco'] && (!this.gltf.extensionsRequired || !this.gltf.extensionsRequired.indexOf('KHR_draco_mesh_compression') < 0)) {
return null;
}
const draco = this.options.decoders['draco'];
const { bufferView: bufferViewIndex, attributes } = extensions['KHR_draco_mesh_compression'];
const bufferView = this.gltf.bufferViews[bufferViewIndex];
const p = this._accessor._requestBufferOfBufferView(bufferView).then(buf => {
// debugger
const { buffer, byteOffset } = buf;
let { byteOffset: viewOffset } = bufferView;
const byteLength = bufferView.byteLength;
if (!viewOffset) {
viewOffset = 0;
}
const dataView = new DataView(buffer, byteOffset + viewOffset, byteLength);
// const dataView = buffer.slice(byteOffset + viewOffset, byteOffset + viewOffset + byteLength);
const dracoOptions = {
attributes,
useUniqueIDs: false,
skipAttributeTransform: options.skipAttributeTransform
};
return draco(dataView, dracoOptions).then(data => {
const assets = Object.values(data.attributes);
if (data.indices) {
assets.push(data.indices);
}
return assets;
});
});
promises.push(p);
promise = Promise.all(promises);
} else {
const attributes = primJSON.attributes;
for (const attr in attributes) {
//e.g. NORMAL, 0
const promise = this.adapter.accessor._requestData(attr, attributes[attr]);
if (promise) {
promises.push(promise);
}
}
if (defined(primJSON.indices)) {
const promise = this.adapter.accessor._requestData('indices', primJSON.indices);
if (promise) {
promises.push(promise);
}
}
promise = Promise.all(promises);
}
return promise.then(assets => {
if (extensions && extensions['KHR_draco_mesh_compression']) {
//assets的结构可能包含targets, 例如[POSITION_1, POSITION_2,...,[attributes...]],这里是为了将assets重组成一维数组。
const targetLength = primJSON.targets ? primJSON.targets.length : 0;
assets[targetLength] = assets[targetLength].concat(assets.slice(0, targetLength));
assets = assets[targetLength];
}
let indices, morphTargets;
const attrData = assets.reduce((accumulator, currentValue) => {
if (currentValue.name === 'indices') {
indices = currentValue;
} else if (currentValue.name.indexOf('morphTargets_') > -1) {
morphTargets = morphTargets || {};
morphTargets[currentValue.name.slice(13)] = currentValue;
} else {
if (currentValue.name === 'POSITION' && (!currentValue.min || !currentValue.max)) {
const min = [Infinity, Infinity, Infinity];
const max = [-Infinity, -Infinity, -Infinity];
const { itemSize, array } = currentValue;
const count = array.length / itemSize;
for (let i = 0; i < count; i++) {
for (let j = 0; j < itemSize; j++) {
const idx = i * itemSize + j;
if (array[idx] < min[j]) {
min[j] = array[idx];
}
if (array[idx] > max[j]) {
max[j] = array[idx];
}
}
}
if (currentValue.quantization) {
const quantization = currentValue.quantization;
const constant = quantization.range / (1 << quantization.quantizationBits);
const minValues = quantization.minValues;
vec3.scale(min, min, constant);
vec3.add(min, min, minValues);
vec3.scale(max, max, constant);
vec3.add(max, max, minValues);
}
currentValue.min = min;
currentValue.max = max;
}
accumulator[currentValue.name] = currentValue;
}
if (this.transferables && currentValue.array.buffer && this.transferables.indexOf(currentValue.array.buffer) < 0) {
this.transferables.push(currentValue.array.buffer);
}
return accumulator;
}, {});
const primitive = {
attributes : attrData,
material: primJSON.material
};
if (indices) primitive.indices = indices;
if (morphTargets) primitive.morphTargets = morphTargets;
primitive.mode = defined(primJSON.mode) ? primJSON.mode : 4; //default mode is triangles
if (defined(primJSON.extras)) primitive.extras = primJSON.extras;
// Save original extension information for subsequent parsing of EXT_mesh_features and EXT_structural_metadata
if (primJSON.extensions) {
primitive.extensions = primJSON.extensions;
}
//TODO material 和 targets 没有处理
return primitive;
});
}
/**
* Load EXT_mesh_features and EXT_structural_metadata extension data
* Only prepare data, do not instantiate plugins (plugins are instantiated in the main thread)
*/
_loadStructuralMetadata() {
const extensionsUsed = this.gltf.extensionsUsed || [];
const hasMeshFeatures = extensionsUsed.includes(EXT_MESH_FEATURES);
const hasStructuralMetadata = extensionsUsed.includes(
EXT_STRUCTURAL_METADATA,
);
if (!hasMeshFeatures && !hasStructuralMetadata) {
return Promise.resolve();
}
const promises = [];
// Load buffer views required for structural metadata
if (hasStructuralMetadata) {
const ext = this.gltf.extensions?.[EXT_STRUCTURAL_METADATA];
if (ext?.propertyTables) {
promises.push(
this._loadPropertyTableBuffers(ext.propertyTables),
);
}
}
return Promise.all(promises).then((results) => {
const buffers = results[0] || [];
// Prepare structural metadata data (for constructing plugins in the main thread)
if (hasStructuralMetadata) {
return getStructuralMetadataData(
this.gltf,
buffers,
this.rootPath,
this._fetchOptions,
this.options.urlModifier,
).then((metadataData) => {
if (metadataData) {
buffers.forEach((buf) => {
if (buf && this.transferables.indexOf(buf) < 0) {
this.transferables.push(buf);
}
});
}
return metadataData;
});
}
// EXT_mesh_features data is already in primitive.extensions
// No extra processing needed, main thread can directly use primitive.extensions['EXT_mesh_features']
});
}
/**
* Load buffer views required for property tables
*/
_loadPropertyTableBuffers(propertyTables) {
const bufferViews = this.gltf.bufferViews || [];
const bufferViewsToLoad =
collectPropertyTableBufferViews(propertyTables);
// Load all required buffer views
const result = new Array(bufferViews.length).fill(null);
const promises = [];
bufferViewsToLoad.forEach((index) => {
const bufferView = bufferViews[index];
if (!bufferView) return;
const promise = this._accessor
._requestBufferOfBufferView(bufferView)
.then((buf) => {
const { buffer, byteOffset } = buf;
const start = (bufferView.byteOffset || 0) + byteOffset;
result[index] = buffer.slice(
start,
start + bufferView.byteLength,
);
});
promises.push(promise);
});
return Promise.all(promises).then(() => result);
}
}
//默认的image读取方法,可在options中替换,例如由worker传回给主线程解析
function requestImage(url, fetchOptions, cb) {
const image = new Image();
image.crossOrigin = '';
image.onload = () => {
if (!canvas) {
cb(new Error('There is no canvas to draw image!'));
return;
}
canvas.width = image.width;
canvas.height = image.height;
const ctx = canvas.getContext('2d', { willReadFrequently: true });
ctx.drawImage(image, 0, 0, image.width, image.height);
const imgData = ctx.getImageData(0, 0, image.width, image.height);
//TODO, retina may need special operations
const result = { width : image.width, height : image.height, data : new Uint8Array(imgData.data) };
cb(null, result);
};
image.onerror = function (err) {
cb(err);
};
image.src = url;
}
const requests = [];
const workingRequests = [];
const requestLimit = 30;
let offCanvas, offCtx;
function requestImageOffscreen(url, fetchOptions, cb) {
if (!offCanvas) {
offCanvas = new OffscreenCanvas(2, 2);
offCtx = offCanvas.getContext('2d', { willReadFrequently: true });
}
let promise = null;
if (isString(url)) {
if (this.options.urlModifier) {
url = this.options.urlModifier(url);
}
requests.push([url, fetchOptions, cb, this]);
loopRequests();
} else {
const data = url;
const blob = new Blob([data]);
promise = createImageBitmap(blob);
promise.then(thenMethod.bind(this)).then(res =>{
cb(null, res);
}).catch(err => {
console.warn(err);
cb(err);
});
}
}
function loopRequests() {
if (!requests.length || workingRequests.length > requestLimit) {
return;
}
const request = requests.shift();
const [url, fetchOptions, cb, context] = request;
workingRequests.push(request);
fetch(url, fetchOptions)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
// response.blob()方法似乎有内存泄漏
// 2022-03-09
const blob = new Blob([new Uint8Array(arrayBuffer)]);
return createImageBitmap(blob);
}).then(thenMethod.bind(context)).then(res =>{
cb.call(context, null, res);
const index = workingRequests.indexOf(request);
workingRequests.splice(index, 1);
loopRequests();
}).catch(err => {
console.warn(err);
cb.call(context, err);
const index = workingRequests.indexOf(request);
workingRequests.splice(index, 1);
loopRequests();
});
}
function thenMethod(bitmap) {
let { width, height } = bitmap;
if (!isPowerOfTwo(width)) {
width = floorPowerOfTwo(width);
}
if (!isPowerOfTwo(height)) {
height = floorPowerOfTwo(height);
}
const maxSize = this.options['maxTextureSize'];
if (maxSize) {
width = Math.min(maxSize, width);
height = Math.min(maxSize, height);
}
offCanvas.width = width;
offCanvas.height = height;
offCtx.drawImage(bitmap, 0, 0, width, height);
bitmap.close();
const imgData = offCtx.getImageData(0, 0, width, height);
return { width, height, data: new Uint8Array(imgData.data) };
}
function isPowerOfTwo(value) {
return (value & (value - 1)) === 0 && value !== 0;
}
function floorPowerOfTwo(value) {
return Math.pow(2, Math.floor(Math.log(value) / Math.LN2));
}
function getTechniqueValues(material) {
return material && material.extensions && material.extensions['KHR_techniques_webgl']
}
function findUniformName(uniforms, value) {
for (const p in uniforms) {
if (uniforms[p] === value) {
return p;
}
}
return value;
}