This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 202
/
ReaderWriterGLTF.js
999 lines (871 loc) · 36.1 KB
/
ReaderWriterGLTF.js
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
import P from 'bluebird';
import BoundingBox from 'osg/BoundingBox';
import StateSet from 'osg/StateSet';
import Input from 'osgDB/Input';
import Registry from 'osgDB/Registry';
import BasicAnimationManager from 'osgAnimation/BasicAnimationManager';
import Skeleton from 'osgAnimation/Skeleton';
import Bone from 'osgAnimation/Bone';
import StackedTranslate from 'osgAnimation/StackedTranslate';
import StackedQuaternion from 'osgAnimation/StackedQuaternion';
import StackedScale from 'osgAnimation/StackedScale';
import RigGeometry from 'osgAnimation/RigGeometry';
import MorphGeometry from 'osgAnimation/MorphGeometry';
import channelFactory from 'osgAnimation/channel';
import animationFactory from 'osgAnimation/animation';
import notify from 'osg/notify';
import Node from 'osg/Node';
import Geometry from 'osg/Geometry';
import Texture from 'osg/Texture';
import MatrixTransform from 'osg/MatrixTransform';
import BlendFunc from 'osg/BlendFunc';
import CullFace from 'osg/CullFace';
import Image from 'osg/Image';
import DrawElements from 'osg/DrawElements';
import primitiveSet from 'osg/primitiveSet';
import BufferArray from 'osg/BufferArray';
import UpdateBone from 'osgAnimation/UpdateBone';
import UpdateMorph from 'osgAnimation/UpdateMorph';
import UpdateMatrixTransform from 'osgAnimation/UpdateMatrixTransform';
import fileHelper from 'osgDB/fileHelper';
import Uniform from 'osg/Uniform';
import { mat4, vec3 } from 'osg/glMatrix';
var createQuatChannel = channelFactory.createQuatChannel;
var createVec3Channel = channelFactory.createVec3Channel;
var createFloatChannel = channelFactory.createFloatChannel;
var ReaderWriterGLTF = function() {
// Contains all the needed glTF files (.gltf, .bin, etc...)
this._filesMap = undefined;
this._loadedFiles = undefined;
this._bufferViewCache = undefined;
this._basicAnimationManager = undefined;
this._visitedNodes = undefined;
this._animatedNodes = undefined;
this._bones = undefined;
this._skeletonToInfluenceMap = undefined;
this._inputImgReader = undefined;
this._localPath = '';
this._extensions = [];
this._nodeAnimationTypes = undefined;
this.init();
};
function base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
ReaderWriterGLTF.WEBGL_COMPONENT_TYPES = {
5120: Int8Array,
5121: Uint8Array,
5122: Int16Array,
5123: Uint16Array,
5125: Uint32Array,
5126: Float32Array
};
ReaderWriterGLTF.TYPE_TABLE = {
SCALAR: 1,
VEC2: 2,
VEC3: 3,
VEC4: 4,
MAT2: 4,
MAT3: 9,
MAT4: 16
};
ReaderWriterGLTF.ATTRIBUTE_OSGJS_TABLE = {
POSITION: 'Vertex',
NORMAL: 'Normal',
TANGENT: 'Tangent',
TEXCOORD_0: 'TexCoord0',
TEXCOORD_1: 'TexCoord1',
TEXCOORD_2: 'TexCoord2',
TEXCOORD_3: 'TexCoord3',
TEXCOORD_4: 'TexCoord4',
TEXCOORD_5: 'TexCoord5',
TEXCOORD_6: 'TexCoord6',
TEXCOORD_7: 'TexCoord7',
TEXCOORD_8: 'TexCoord8',
TEXCOORD_9: 'TexCoord9',
TEXCOORD_10: 'TexCoord10',
TEXCOORD_11: 'TexCoord11',
TEXCOORD_12: 'TexCoord12',
TEXCOORD_13: 'TexCoord13',
TEXCOORD_14: 'TexCoord14',
TEXCOORD_15: 'TexCoord15',
COLOR_0: 'Color',
JOINTS_0: 'Bones',
WEIGHTS_0: 'Weights'
};
ReaderWriterGLTF.TEXTURE_FORMAT = {
6406: Texture.ALPHA,
6407: Texture.RGB,
6408: Texture.RGBA,
6409: Texture.LUMINANCE,
6410: Texture.LUMINANCE_ALPHA
};
ReaderWriterGLTF.TYPE_CHANNEL_PATH = {
translation: {
VEC3: createVec3Channel
},
scale: {
VEC3: createVec3Channel
},
rotation: {
VEC4: createQuatChannel
},
weights: {
SCALAR: createFloatChannel
}
};
ReaderWriterGLTF.TYPE_STACKED_TRANSFORMS = {
translation: StackedTranslate,
scale: StackedScale,
rotation: StackedQuaternion
};
ReaderWriterGLTF.PBR_SPEC_EXT = 'KHR_materials_pbrSpecularGlossiness';
ReaderWriterGLTF.PBR_SPEC_MODE = 'PBR_specular_glossiness';
ReaderWriterGLTF.PBR_METAL_MODE = 'PBR_metal_roughness';
ReaderWriterGLTF.ALBEDO_TEXTURE_UNIT = 2;
ReaderWriterGLTF.DIFFUSE_TEXTURE_UNIT = 2;
ReaderWriterGLTF.SPECULAR_GLOSSINESS_TEXTURE_UNIT = 3;
ReaderWriterGLTF.METALLIC_ROUGHNESS_TEXTURE_UNIT = 3;
ReaderWriterGLTF.SPECULAR_TEXTURE_UNIT = 4;
ReaderWriterGLTF.NORMAL_TEXTURE_UNIT = 5;
ReaderWriterGLTF.AO_TEXTURE_UNIT = 6;
ReaderWriterGLTF.EMISSIVE_TEXTURE_UNIT = 7;
ReaderWriterGLTF.ALBEDO_UNIFORM = 'albedoMap';
ReaderWriterGLTF.METALLIC_ROUGHNESS_UNIFORM = 'metallicRoughnessMap';
ReaderWriterGLTF.SPECULAR_UNIFORM = 'specularMap';
ReaderWriterGLTF.NORMAL_UNIFORM = 'normalMap';
ReaderWriterGLTF.AO_UNIFORM = 'aoMap';
ReaderWriterGLTF.EMISSIVE_UNIFORM = 'emissiveMap';
ReaderWriterGLTF.prototype = {
init: function() {
this._glTFJSON = undefined;
this._nodeAnimationTypes = [];
this._bufferViewCache = {};
this._basicAnimationManager = undefined;
this._localPath = '';
this._visitedNodes = {};
this._animatedNodes = {};
this._bones = {};
this._skeletonToInfluenceMap = {};
this._stateSetMap = {};
this._filesMap = {};
this._inputReader = new Input();
this._rootStateSet = new StateSet();
this._defaultBlendFunc = new BlendFunc();
this._transparentBlendFunc = new BlendFunc('SRC_ALPHA', 'ONE_MINUS_SRC_ALPHA');
this._defaultCullFace = new CullFace();
this._doubleSideCullFace = new CullFace(CullFace.DISABLE);
},
// Adds variable to help to convert nodes to osgjs
// - add parent variable to all nodes
// - add unique node name
_preProcessNodes: function() {
var nodes = this._gltfJSON.nodes;
var meshes = this._gltfJSON.meshes;
var name;
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
name = node.name || '';
name += '_' + i.toString();
node.osgjsNodeName = name;
var children = node.children;
if (!children) continue;
for (var j = 0; j < children.length; j++) {
var child = children[j];
nodes[child].parent = i;
}
}
for (var k = 0; k < meshes.length; k++) {
var mesh = meshes[k];
name = mesh.name || '';
name += '_' + k.toString();
mesh.osgjsNodeName = name;
}
},
loadBuffers: P.method(function() {
var promises = [];
var buffers = this._gltfJSON.buffers;
for (var i = 0; i < buffers.length; i++) {
var buffer = buffers[i];
promises.push(
this.loadURI(buffer.uri, { responseType: 'arraybuffer' }).then(function(
arrayBuffer
) {
buffer.data = arrayBuffer;
})
);
}
return P.all(promises);
}),
loadBufferViews: function() {
var buffers = this._gltfJSON.buffers;
var bufferViews = this._gltfJSON.bufferViews;
for (var i = 0; i < bufferViews.length; i++) {
var bufferView = bufferViews[i];
var bufferIndex = bufferView.buffer;
var buffer = buffers[bufferIndex];
var byteLength = bufferView.byteLength || 0;
var byteOffset = bufferView.byteOffset || 0;
bufferView.data = buffer.data.slice(byteOffset, byteOffset + byteLength);
}
},
loadAccessors: function() {
var bufferViews = this._gltfJSON.bufferViews;
var accessors = this._gltfJSON.accessors;
for (var i = 0; i < accessors.length; i++) {
var accessor = accessors[i];
var bufferViewIndex = accessor.bufferView;
var bufferView = bufferViews[bufferViewIndex];
var itemSize = ReaderWriterGLTF.TYPE_TABLE[accessor.type];
var TypedArray = ReaderWriterGLTF.WEBGL_COMPONENT_TYPES[accessor.componentType];
// For VEC3: itemSize is 3, elementBytes is 4, itemBytes is 12.
var elementBytes = TypedArray.BYTES_PER_ELEMENT;
var itemBytes = elementBytes * itemSize;
var byteStride = bufferView.byteStride;
var normalized = accessor.normalized === true;
var bufferArray;
// The buffer is not interleaved if the stride is the item size in bytes.
if (byteStride && byteStride !== itemBytes) {
// Use the full buffer if it's interleaved.
notify.warn('GLTF interleaved accessors not supported');
} else {
var data = new TypedArray(
bufferView.data,
accessor.byteOffset,
accessor.count * itemSize
);
bufferArray = new BufferArray(undefined, data, itemSize);
bufferArray.setNormalize(normalized);
}
accessor.data = bufferArray;
}
},
_computeNodeMatrix: function(node) {
var matrix;
if (node.matrix) {
matrix = mat4.clone(node.matrix);
} else if (node.translation || node.rotation || node.scale) {
var translation = mat4.IDENTITY;
var rotation = mat4.IDENTITY;
var scale = mat4.IDENTITY;
if (node.translation)
translation = mat4.fromTranslation(mat4.create(), node.translation);
if (node.rotation) rotation = mat4.fromQuat(mat4.create(), node.rotation);
if (node.scale) scale = mat4.fromScaling(mat4.create(), node.scale);
matrix = mat4.create();
mat4.multiply(matrix, rotation, scale);
mat4.multiply(matrix, translation, matrix);
}
return matrix;
},
loadNodes: function() {
var nodes = this._gltfJSON.nodes;
var skins = this._gltfJSON.skins;
var meshes = this._gltfJSON.meshes;
// create all nodes
// children relationship will be done in loadScenes
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if (node.usedAsBone) continue;
if (node.skin !== undefined) {
var skin = skins[node.skin];
var skeleton = skin.osgjsNode;
if (!skin.osgjsNodeAttached) {
node.osgjsNode = skeleton;
skin.osgjsNodeAttached = i;
}
var mesh = meshes[node.mesh];
mesh.osgjsGeometry.setBoneNameID(skin.osgjsBoneMapID);
skeleton.addChild(mesh.osgjsGeometry);
} else {
if (node.matrix || node.translation || node.rotation || node.scale) {
node.osgjsNode = this._createMatrixTransform(i);
}
if (node.mesh !== undefined) {
if (node.osgjsNode) {
node.osgjsNode.addChild(meshes[node.mesh].osgjsGeometry);
} else {
node.osgjsNode = meshes[node.mesh].osgjsGeometry;
}
}
if (!node.osgjsNode) {
node.osgjsNode = new Node();
}
}
if (node.name) node.osgjsNode.setName(node.name);
}
},
_linkNodes: function(parent) {
var nodes = this._gltfJSON.nodes;
var children = parent.children;
if (!children) return;
for (var i = 0; i < children.length; i++) {
var node = nodes[children[i]];
// skip node used as bone
if (node.usedAsBone) continue;
var osgjsChild = node.osgjsNode;
if (osgjsChild && !parent.osgjsNode.hasChild(osgjsChild)) {
parent.osgjsNode.addChild(osgjsChild);
this._linkNodes(node);
}
}
},
loadSkins: function() {
var skins = this._gltfJSON.skins;
if (!skins) return;
for (var i = 0; i < skins.length; i++) {
this._processSkin(i);
}
},
loadScenes: function() {
var nodes = this._gltfJSON.nodes;
var scenes = this._gltfJSON.scenes;
this._osgjsScene = [];
for (var i = 0; i < scenes.length; i++) {
var scene = scenes[i];
var sceneNodes = scene.nodes;
var rootNodes = [];
for (var j = 0; j < sceneNodes.length; j++) {
var node = nodes[sceneNodes[j]];
this._linkNodes(node);
rootNodes.push(node.osgjsNode);
}
var root = rootNodes[0];
if (rootNodes.length > 1) {
root = new Node();
for (var r = 0; r < rootNodes.length; r++) {
root.addChild(rootNodes[r]);
}
}
this._osgjsScene.push(root);
}
},
_assignGeometryAttributes: function(osgjsGeometry, gltfAttributes, index) {
var accessors = this._gltfJSON.accessors;
for (var attribute in gltfAttributes) {
var accessorIndex = gltfAttributes[attribute];
var accessor = accessors[accessorIndex];
var osgjsAttributeName = ReaderWriterGLTF.ATTRIBUTE_OSGJS_TABLE[attribute];
if (index !== undefined) osgjsAttributeName += '_' + index.toString();
osgjsGeometry.getVertexAttributeList()[osgjsAttributeName] = accessor.data;
accessor.data._target = BufferArray.ARRAY_BUFFER;
}
},
_assignGeometryPrimitives: function(osgjsGeometry, primitive) {
var accessors = this._gltfJSON.accessors;
var indexes = accessors[primitive.indices];
indexes.data._target = BufferArray.ELEMENT_ARRAY_BUFFER;
var primitiveMode = primitive.mode !== undefined ? primitive.mode : primitiveSet.TRIANGLES;
var osgPrimitive = new DrawElements(primitiveMode, indexes.data);
osgjsGeometry.getPrimitiveSetList().push(osgPrimitive);
},
_getMorphTargetName: function(mesh, index) {
return mesh.osgjsNodeName + '_target_' + index.toString();
},
_computeAbsoluteMorphData: function(baseAttributes, morphAttributes) {
var accessors = this._gltfJSON.accessors;
for (var attribute in morphAttributes) {
var accessorIndex = morphAttributes[attribute];
var accessor = accessors[accessorIndex];
if (accessor.morphAbsoluteComputed) continue; // dont compute twice absolute targets
accessor.morphAbsoluteComputed = true;
var baseAccessorIndex = baseAttributes[attribute];
var baseAccessor = accessors[baseAccessorIndex];
var base = baseAccessor.data.getElements();
var target = accessor.data.getElements();
var nbElements = base.length;
for (var i = 0; i < nbElements; i++) {
target[i] += base[i];
}
}
},
_processMorphGeometry: function(mesh, primitiveIndex) {
var primitive = mesh.primitives[primitiveIndex];
var geometry = new MorphGeometry();
var morphUpdateCallback = new UpdateMorph();
morphUpdateCallback.setName(mesh.osgjsNodeName);
geometry.setName(mesh.osgjsNodeName);
for (var t = 0; t < primitive.targets.length; t++) {
this._computeAbsoluteMorphData(primitive.attributes, primitive.targets[t]);
var targetName = this._getMorphTargetName(mesh, t);
var morphTarget = new Geometry();
morphTarget.setName(targetName);
morphUpdateCallback.addTarget(targetName, t);
this._assignGeometryAttributes(morphTarget, primitive.targets[t]);
geometry.getMorphTargets().push(morphTarget);
}
this._assignGeometryAttributes(geometry, primitive.attributes);
this._assignGeometryPrimitives(geometry, primitive);
geometry.mergeChildrenVertexAttributeList();
// set result in gltf mesh
mesh.osgjsMorphGeometry = geometry;
mesh.osgjsUpdateMorph = morphUpdateCallback;
},
loadMeshes: function() {
var meshes = this._gltfJSON.meshes;
var materials = this._gltfJSON.materials;
for (var i = 0; i < meshes.length; i++) {
var mesh = meshes[i];
var name = mesh.name;
var osgjsGeometries = [];
for (var j = 0; j < mesh.primitives.length; j++) {
var geometry = undefined;
var primitive = mesh.primitives[j];
var morphUpdateCallback = undefined;
// means we have morph target
if (primitive.targets && primitive.targets.length) {
this._processMorphGeometry(mesh, j);
geometry = mesh.osgjsMorphGeometry;
morphUpdateCallback = mesh.osgjsUpdateMorph;
}
if (primitive.attributes.JOINTS_0) {
var rigGeometry = new RigGeometry();
if (!geometry) {
geometry = new Geometry();
this._assignGeometryAttributes(geometry, primitive.attributes);
this._assignGeometryPrimitives(geometry, primitive);
}
var bbox = new BoundingBox();
geometry.computeBoundingBox(bbox);
rigGeometry.setBound(bbox);
rigGeometry.setSourceGeometry(geometry);
rigGeometry.mergeChildrenData();
geometry = rigGeometry;
}
if (!geometry) {
geometry = new Geometry();
this._assignGeometryAttributes(geometry, primitive.attributes);
this._assignGeometryPrimitives(geometry, primitive);
}
if (name) geometry.setName(name);
// if has a morph geometry we addd the update callback on the top geometry
if (morphUpdateCallback) geometry.addUpdateCallback(morphUpdateCallback);
var osgjsStateSet = materials[primitive.material].osgjsStateSet;
geometry.setStateSet(osgjsStateSet);
geometry.setUserData(osgjsStateSet.getUserData());
osgjsGeometries.push(geometry);
}
if (osgjsGeometries.length > 1) {
var node = new Node();
for (var c = 0; c < osgjsGeometries.length; c++) node.addChild(osgjsGeometries[c]);
geometry = node;
}
mesh.osgjsGeometry = geometry;
}
},
loadImages: function() {
var images = this._gltfJSON.images;
if (!images) return P.resolve();
var promises = [];
for (var i = 0; i < images.length; i++) {
var image = images[i];
var url = window.decodeURI(image.uri);
var promise = this.loadURI(url).then(
function(imageData) {
var osgjsImage = new Image();
osgjsImage.setImage(imageData);
this.osgjsImage = osgjsImage;
}.bind(image)
);
promises.push(promise);
}
return P.all(promises);
},
_texture: function(gltfTexture) {
var textures = this._gltfJSON.textures;
var images = this._gltfJSON.images;
var texture = textures[gltfTexture.index];
var image = images[texture.source];
if (texture.osgjsTexture) return texture.osgjsTexture;
var osgjsTexture = new Texture();
texture.osgjsTexture = osgjsTexture;
// GLTF texture origin is correct
osgjsTexture.setFlipY(false);
osgjsTexture.setWrapS('REPEAT');
osgjsTexture.setWrapT('REPEAT');
if (image.osgjsImage) {
var format = ReaderWriterGLTF.TEXTURE_FORMAT[texture.format];
osgjsTexture.setImage(image.osgjsImage, format);
}
return osgjsTexture;
},
_pbrMetallicRoughnessTextureUniforms: function() {
var stateSet = this._rootStateSet;
if (stateSet.getUniformList()[ReaderWriterGLTF.ALBEDO_UNIFORM]) return;
var albedo = Uniform.createInt(
ReaderWriterGLTF.ALBEDO_TEXTURE_UNIT,
ReaderWriterGLTF.ALBEDO_UNIFORM
);
var metalnessRoughness = Uniform.createInt(
ReaderWriterGLTF.METALLIC_ROUGHNESS_TEXTURE_UNIT,
ReaderWriterGLTF.METALLIC_ROUGHNESS_UNIFORM
);
stateSet.addUniform(albedo);
stateSet.addUniform(metalnessRoughness);
},
_pbrMetallicRoughness: function(material, stateSet) {
stateSet.setUserData({
pbrWorklow: ReaderWriterGLTF.PBR_METAL_MODE
});
this._pbrMetallicRoughnessTextureUniforms();
var osgjsTexture;
// baseColor
if (material.baseColorTexture) {
osgjsTexture = this._texture(material.baseColorTexture);
stateSet.setTextureAttributeAndModes(
ReaderWriterGLTF.ALBEDO_TEXTURE_UNIT,
osgjsTexture
);
} else if (material.baseColorFactor) {
//PBR default uniforms
var color = Uniform.createFloat4(material.baseColorFactor, 'uBaseColorFactor');
stateSet.addUniform(color);
}
// metallic
if (material.metallicFactor !== undefined) {
var metallic = Uniform.createFloat1(material.metallicFactor, 'uMetallicFactor');
stateSet.addUniform(metallic);
}
if (material.roughnessFactor !== undefined) {
var roughness = Uniform.createFloat1(material.roughnessFactor, 'uRoughnessFactor');
stateSet.addUniform(roughness);
}
if (material.metallicRoughnessTexture) {
osgjsTexture = this._texture(material.metallicRoughnessTexture);
stateSet.setTextureAttributeAndModes(
ReaderWriterGLTF.METALLIC_ROUGHNESS_TEXTURE_UNIT,
osgjsTexture
);
}
},
_KHR_materials_pbrSpecularGlossinessTextureUniforms: function() {
if (this._extensions['KHR_materials_pbrSpecularGlossinessTextureUniforms']) return;
this._extensions['KHR_materials_pbrSpecularGlossinessTextureUniforms'] = true;
var stateSet = this._rootStateSet;
var albedo = Uniform.createInt(
ReaderWriterGLTF.DIFFUSE_TEXTURE_UNIT,
ReaderWriterGLTF.ALBEDO_UNIFORM
);
var specluarGlossiness = Uniform.createInt(
ReaderWriterGLTF.SPECULAR_GLOSSINESS_TEXTURE_UNIT,
ReaderWriterGLTF.METALLIC_ROUGHNESS_UNIFORM
);
var specular = Uniform.createInt(
ReaderWriterGLTF.SPECULAR_TEXTURE_UNIT,
ReaderWriterGLTF.SPECULAR_UNIFORM
);
stateSet.addUniform(albedo);
stateSet.addUniform(specluarGlossiness);
stateSet.addUniform(specular);
},
_KHR_materials_pbrSpecularGlossiness: function(material, stateSet) {
stateSet.setUserData({
pbrWorklow: ReaderWriterGLTF.PBR_SPEC_MODE
});
this._KHR_materials_pbrSpecularGlossinessTextureUniforms();
var osgjsTexture;
var color;
if (material.diffuseTexture) {
osgjsTexture = this._texture(material.diffuseTexture);
stateSet.setTextureAttributeAndModes(
ReaderWriterGLTF.DIFFUSE_TEXTURE_UNIT,
osgjsTexture
);
} else if (material.diffuseFactor) {
color = Uniform.createFloat4(material.diffuseFactor, 'uBaseColorFactor');
stateSet.addUniform(color);
}
if (material.specularFactor) {
color = Uniform.createFloat3(material.specularFactor, 'uSpecularFactor');
stateSet.addUniform(color);
}
if (material.glossinessFactor !== undefined) {
var factor = Uniform.createFloat(material.glossinessFactor, 'uGlossinessFactor');
stateSet.addUniform(factor);
}
if (material.specularGlossinessTexture) {
osgjsTexture = this._texture(material.specularGlossinessTexture);
stateSet.setTextureAttributeAndModes(
ReaderWriterGLTF.SPECULAR_GLOSSINESS_TEXTURE_UNIT,
osgjsTexture
);
}
},
loadMaterials: function() {
var materials = this._gltfJSON.materials;
var hasTransparentMaterial = false;
var hasDoubleSidedMaterial = false;
var rootStateSet = this._rootStateSet;
rootStateSet.addUniform(
Uniform.createInt(
ReaderWriterGLTF.EMISSIVE_TEXTURE_UNIT,
ReaderWriterGLTF.EMISSIVE_UNIFORM
)
);
rootStateSet.addUniform(
Uniform.createInt(ReaderWriterGLTF.AO_TEXTURE_UNIT, ReaderWriterGLTF.AO_UNIFORM)
);
rootStateSet.addUniform(
Uniform.createInt(ReaderWriterGLTF.NORMAL_TEXTURE_UNIT, ReaderWriterGLTF.NORMAL_UNIFORM)
);
rootStateSet.addUniform(Uniform.createFloat3(vec3.ZERO, 'uEmissiveFactor'));
for (var i = 0; i < materials.length; i++) {
var material = materials[i];
var stateSet = new StateSet();
var osgjsTexture;
material.osgjsStateSet = stateSet;
if (material.pbrMetallicRoughness) {
this._pbrMetallicRoughness(material.pbrMetallicRoughness, stateSet);
} else if (material.extensions.KHR_materials_pbrSpecularGlossiness) {
// https://github.com/KhronosGroup/glTF/blob/master/extensions/Khronos/KHR_materials_pbrSpecularGlossiness/README.md
this._KHR_materials_pbrSpecularGlossiness(
material.extensions.KHR_materials_pbrSpecularGlossiness,
stateSet
);
}
if (material.normalTexture) {
osgjsTexture = this._texture(material.normalTexture);
stateSet.setTextureAttributeAndModes(
ReaderWriterGLTF.NORMAL_TEXTURE_UNIT,
osgjsTexture
);
}
if (material.occlusionTexture) {
osgjsTexture = this._texture(material.occlusionTexture);
stateSet.setTextureAttributeAndModes(
ReaderWriterGLTF.AO_TEXTURE_UNIT,
osgjsTexture
);
}
if (material.emissiveTexture) {
osgjsTexture = this._texture(material.emissiveTexture);
stateSet.setTextureAttributeAndModes(
ReaderWriterGLTF.EMISSIVE_TEXTURE_UNIT,
osgjsTexture
);
}
if (material.emissiveFactor) {
stateSet.addUniform(
Uniform.createFloat3(material.emissiveFactor, 'uEmissiveFactor')
);
}
if (material.alphaMode === 'BLEND') {
hasTransparentMaterial = true;
stateSet.setAttributeAndModes(this._transparentBlendFunc);
stateSet.setRenderingHint('TRANSPARENT_BIN');
}
if (material.doubleSided) {
hasDoubleSidedMaterial = true;
stateSet.setAttributeAndModes(this._doubleSideCullFace);
}
}
// need to add a default blend func if we have transparent materials in the model
if (hasTransparentMaterial) {
rootStateSet.setAttributeAndModes(this._defaultBlendFunc);
}
if (hasDoubleSidedMaterial) {
rootStateSet.setAttributeAndModes(this._defaultCullFace);
}
},
_createBone: function(nodeID) {
var nodes = this._gltfJSON.nodes;
var node = nodes[nodeID];
node.usedAsBone = true;
var nodeAnimationTypes = this._nodeAnimationTypes;
var osgjsNode = new Bone();
var name = node.osgjsNodeName;
osgjsNode.setName(name);
var matrix = this._computeNodeMatrix(node) || mat4.IDENTITY;
osgjsNode.setMatrix(matrix);
var updateCallback = new UpdateBone();
updateCallback.setName(osgjsNode.getName());
osgjsNode.addUpdateCallback(updateCallback);
if (nodeAnimationTypes[nodeID]) {
var stackTransforms = updateCallback.getStackedTransforms();
for (var channelType in nodeAnimationTypes[nodeID]) {
var StackedType = ReaderWriterGLTF.TYPE_STACKED_TRANSFORMS[channelType];
var stack = new StackedType(channelType);
stackTransforms.push(stack);
}
}
// we set the default updateBone matrix with the bone matrix to avoid to copy identity
// matrix when no animation is yet started
updateCallback.setMatrix(matrix);
//osgjsNode.addChild(shape.createAxisGeometry());
return osgjsNode;
},
_createMatrixTransform: function(nodeID) {
var nodes = this._gltfJSON.nodes;
var node = nodes[nodeID];
var nodeAnimationTypes = this._nodeAnimationTypes;
var osgjsNode = new MatrixTransform();
var name = node.osgjsNodeName;
osgjsNode.setName(name);
var matrix = this._computeNodeMatrix(node) || mat4.IDENTITY;
osgjsNode.setMatrix(matrix);
if (nodeAnimationTypes[nodeID]) {
var updateCallback = new UpdateMatrixTransform();
updateCallback.setName(osgjsNode.getName());
osgjsNode.addUpdateCallback(updateCallback);
updateCallback.setMatrix(matrix);
var stackTransforms = updateCallback.getStackedTransforms();
for (var channelType in nodeAnimationTypes[nodeID]) {
var StackedType = ReaderWriterGLTF.TYPE_STACKED_TRANSFORMS[channelType];
var stack = new StackedType(channelType);
stackTransforms.push(stack);
}
}
return osgjsNode;
},
_processSkin: function(skinID) {
var skins = this._gltfJSON.skins;
var nodes = this._gltfJSON.nodes;
var skin = skins[skinID];
var joints = skin.joints;
var inverseBindMatrixAccessor, arrayBuffer;
if (skin.inverseBindMatrices !== undefined) {
inverseBindMatrixAccessor = this._gltfJSON.accessors[skin.inverseBindMatrices].data;
arrayBuffer = inverseBindMatrixAccessor.getElements().buffer;
}
var skeleton = new Skeleton();
skin.osgjsNode = skeleton;
skeleton.setName('skin ID ' + skinID);
var nodeID;
var boneMap = {};
var boneMapID = {};
var node;
var bone;
// first create all bones
for (var i = 0; i < joints.length; i++) {
nodeID = joints[i];
bone = this._createBone(nodeID);
if (skin.inverseBindMatrices !== undefined) {
var matrix = new Float32Array(arrayBuffer, i * 16 * 4, 16);
bone.setInvBindMatrixInSkeletonSpace(matrix);
}
boneMap[nodeID] = bone;
boneMapID[bone.getName()] = i;
}
skin.osgjsBoneMapID = boneMapID;
for (var boneID in boneMap) {
node = nodes[boneID];
var parentID = node.parent;
bone = boneMap[boneID];
if (parentID === undefined || joints.indexOf(parentID) === -1) {
skeleton.addChild(bone);
} else {
var boneParent = boneMap[parentID];
boneParent.addChild(bone);
}
}
},
loadAnimations: function() {
var animations = this._gltfJSON.animations;
if (!animations) return;
var accessors = this._gltfJSON.accessors;
var nodeAnimationTypes = this._nodeAnimationTypes;
var nodes = this._gltfJSON.nodes;
var meshes = this._gltfJSON.meshes;
var osgjsAnimations = [];
for (var i = 0; i < animations.length; i++) {
var channels = animations[i].channels;
var samplers = animations[i].samplers;
var osgjsChannels = [];
for (var j = 0; j < channels.length; j++) {
var channel = channels[j];
var sampler = samplers[channel.sampler];
var times = accessors[sampler.input].data.getElements();
var accessorValues = accessors[sampler.output];
var values = accessorValues.data.getElements();
// target.id is deprecated
var target = channel.target;
var createChannel =
ReaderWriterGLTF.TYPE_CHANNEL_PATH[target.path][accessorValues.type];
var nodeIndex = target.node !== undefined ? target.node : target.id;
var node = nodes[nodeIndex];
var channelName = target.path;
var targetName = nodes[nodeIndex].osgjsNodeName;
// needs to get the updateCallback name of the morph geometry
if (channelName === 'weights') {
// if morph target we need to split each channel into scalar for each target
var mesh = meshes[node.mesh];
var nbTargets = mesh.primitives[0].targets.length;
var nkKeys = times.length;
for (var targetIndex = 0; targetIndex < nbTargets; targetIndex++) {
var weights = new Float32Array(nkKeys);
for (var v = 0; v < nkKeys; v++) {
weights[v] = values[v * nbTargets + targetIndex];
}
targetName = this._getMorphTargetName(mesh, targetIndex);
osgjsChannels.push(createChannel(weights, times, targetName, targetIndex));
}
} else {
osgjsChannels.push(createChannel(values, times, targetName, channelName));
}
if (!nodeAnimationTypes[nodeIndex]) nodeAnimationTypes[nodeIndex] = {};
nodeAnimationTypes[nodeIndex][channelName] = true;
}
var animationName = 'animation-' + i.toString();
osgjsAnimations.push(animationFactory.createAnimation(osgjsChannels, animationName));
}
var animationManager = new BasicAnimationManager();
animationManager.init(osgjsAnimations);
this._animationManager = animationManager;
},
loadURI: P.method(function(uri, options) {
// is base64 inline data
if (uri.substr(0, 5) === 'data:') {
return base64ToArrayBuffer(uri);
}
var result = this._filesMap[uri];
if (result !== undefined) return result;
return fileHelper.requestURI(uri, options);
}),
readNodeURL: function(url, options) {
var self = this;
this.init();
if (options && options.filesMap !== undefined && Object.keys(options.filesMap).length > 0) {
// it comes from the ZIP plugin or from drag'n drop
// So we already have all the files.
this._filesMap = options.filesMap;
var glTFFile = this._filesMap[url];
return this.readJSON(glTFFile, url);
}
var index = url.lastIndexOf('/');
this._localPath = index === -1 ? '' : url.substr(0, index + 1);
// Else it is a usual XHR request
return fileHelper.requestURI(url).then(function(file) {
return self.readJSON(file);
});
},
readJSON: P.method(function(json, url) {
this._gltfJSON = json;
return P.all([this.loadBuffers(), this.loadImages()]).then(
function() {
this.loadBufferViews();
this.loadAccessors();
this._preProcessNodes();
this.loadMaterials();
this.loadMeshes();
this.loadAnimations();
this.loadSkins();
this.loadNodes();
this.loadScenes();
var root = new MatrixTransform();
root.addChild(this._osgjsScene[0]);
root.setName(url);
if (this._animationManager) root.addUpdateCallback(this._animationManager);
root.setStateSet(this._rootStateSet);
return root;
}.bind(this)
);
})
};
Registry.instance().addReaderWriter('gltf', new ReaderWriterGLTF());
export default ReaderWriterGLTF;