-
Notifications
You must be signed in to change notification settings - Fork 0
/
FBXLoader.js
4174 lines (2486 loc) · 99.6 KB
/
FBXLoader.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
1000
/**
* Loader loads FBX file and generates Group representing FBX scene.
* Requires FBX file to be >= 7.0 and in ASCII or >= 6400 in Binary format
* Versions lower than this may load but will probably have errors
*
* Needs Support:
* Morph normals / blend shape normals
*
* FBX format references:
* https://wiki.blender.org/index.php/User:Mont29/Foundation/FBX_File_Structure
* http://help.autodesk.com/view/FBX/2017/ENU/?guid=__cpp_ref_index_html (C++ SDK reference)
*
* Binary format specification:
* https://code.blender.org/2013/08/fbx-binary-file-format-specification/
*/
THREE.FBXLoader = ( function () {
var fbxTree;
var connections;
var sceneGraph;
function FBXLoader( manager ) {
THREE.Loader.call( this, manager );
}
FBXLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
constructor: FBXLoader,
load: function ( url, onLoad, onProgress, onError ) {
var scope = this;
var path = ( scope.path === '' ) ? THREE.LoaderUtils.extractUrlBase( url ) : scope.path;
var loader = new THREE.FileLoader( this.manager );
loader.setPath( scope.path );
loader.setResponseType( 'arraybuffer' );
loader.setRequestHeader( scope.requestHeader );
loader.setWithCredentials( scope.withCredentials );
loader.load( url, function ( buffer ) {
try {
onLoad( scope.parse( buffer, path ) );
} catch ( e ) {
if ( onError ) {
onError( e );
} else {
console.error( e );
}
scope.manager.itemError( url );
}
}, onProgress, onError );
},
parse: function ( FBXBuffer, path ) {
if ( isFbxFormatBinary( FBXBuffer ) ) {
fbxTree = new BinaryParser().parse( FBXBuffer );
} else {
var FBXText = convertArrayBufferToString( FBXBuffer );
if ( ! isFbxFormatASCII( FBXText ) ) {
throw new Error( 'THREE.FBXLoader: Unknown format.' );
}
if ( getFbxVersion( FBXText ) < 7000 ) {
throw new Error( 'THREE.FBXLoader: FBX version not supported, FileVersion: ' + getFbxVersion( FBXText ) );
}
fbxTree = new TextParser().parse( FBXText );
}
// console.log( fbxTree );
var textureLoader = new THREE.TextureLoader( this.manager ).setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
return new FBXTreeParser( textureLoader, this.manager ).parse( fbxTree );
}
} );
// Parse the FBXTree object returned by the BinaryParser or TextParser and return a THREE.Group
function FBXTreeParser( textureLoader, manager ) {
this.textureLoader = textureLoader;
this.manager = manager;
}
FBXTreeParser.prototype = {
constructor: FBXTreeParser,
parse: function () {
connections = this.parseConnections();
var images = this.parseImages();
var textures = this.parseTextures( images );
var materials = this.parseMaterials( textures );
var deformers = this.parseDeformers();
var geometryMap = new GeometryParser().parse( deformers );
this.parseScene( deformers, geometryMap, materials );
return sceneGraph;
},
// Parses FBXTree.Connections which holds parent-child connections between objects (e.g. material -> texture, model->geometry )
// and details the connection type
parseConnections: function () {
var connectionMap = new Map();
if ( 'Connections' in fbxTree ) {
var rawConnections = fbxTree.Connections.connections;
rawConnections.forEach( function ( rawConnection ) {
var fromID = rawConnection[ 0 ];
var toID = rawConnection[ 1 ];
var relationship = rawConnection[ 2 ];
if ( ! connectionMap.has( fromID ) ) {
connectionMap.set( fromID, {
parents: [],
children: []
} );
}
var parentRelationship = { ID: toID, relationship: relationship };
connectionMap.get( fromID ).parents.push( parentRelationship );
if ( ! connectionMap.has( toID ) ) {
connectionMap.set( toID, {
parents: [],
children: []
} );
}
var childRelationship = { ID: fromID, relationship: relationship };
connectionMap.get( toID ).children.push( childRelationship );
} );
}
return connectionMap;
},
// Parse FBXTree.Objects.Video for embedded image data
// These images are connected to textures in FBXTree.Objects.Textures
// via FBXTree.Connections.
parseImages: function () {
var images = {};
var blobs = {};
if ( 'Video' in fbxTree.Objects ) {
var videoNodes = fbxTree.Objects.Video;
for ( var nodeID in videoNodes ) {
var videoNode = videoNodes[ nodeID ];
var id = parseInt( nodeID );
images[ id ] = videoNode.RelativeFilename || videoNode.Filename;
// raw image data is in videoNode.Content
if ( 'Content' in videoNode ) {
var arrayBufferContent = ( videoNode.Content instanceof ArrayBuffer ) && ( videoNode.Content.byteLength > 0 );
var base64Content = ( typeof videoNode.Content === 'string' ) && ( videoNode.Content !== '' );
if ( arrayBufferContent || base64Content ) {
var image = this.parseImage( videoNodes[ nodeID ] );
blobs[ videoNode.RelativeFilename || videoNode.Filename ] = image;
}
}
}
}
for ( var id in images ) {
var filename = images[ id ];
if ( blobs[ filename ] !== undefined ) images[ id ] = blobs[ filename ];
else images[ id ] = images[ id ].split( '\\' ).pop();
}
return images;
},
// Parse embedded image data in FBXTree.Video.Content
parseImage: function ( videoNode ) {
var content = videoNode.Content;
var fileName = videoNode.RelativeFilename || videoNode.Filename;
var extension = fileName.slice( fileName.lastIndexOf( '.' ) + 1 ).toLowerCase();
var type;
switch ( extension ) {
case 'bmp':
type = 'image/bmp';
break;
case 'jpg':
case 'jpeg':
type = 'image/jpeg';
break;
case 'png':
type = 'image/png';
break;
case 'tif':
type = 'image/tiff';
break;
case 'tga':
if ( this.manager.getHandler( '.tga' ) === null ) {
console.warn( 'FBXLoader: TGA loader not found, skipping ', fileName );
}
type = 'image/tga';
break;
default:
console.warn( 'FBXLoader: Image type "' + extension + '" is not supported.' );
return;
}
if ( typeof content === 'string' ) { // ASCII format
return 'data:' + type + ';base64,' + content;
} else { // Binary Format
var array = new Uint8Array( content );
return window.URL.createObjectURL( new Blob( [ array ], { type: type } ) );
}
},
// Parse nodes in FBXTree.Objects.Texture
// These contain details such as UV scaling, cropping, rotation etc and are connected
// to images in FBXTree.Objects.Video
parseTextures: function ( images ) {
var textureMap = new Map();
if ( 'Texture' in fbxTree.Objects ) {
var textureNodes = fbxTree.Objects.Texture;
for ( var nodeID in textureNodes ) {
var texture = this.parseTexture( textureNodes[ nodeID ], images );
textureMap.set( parseInt( nodeID ), texture );
}
}
return textureMap;
},
// Parse individual node in FBXTree.Objects.Texture
parseTexture: function ( textureNode, images ) {
var texture = this.loadTexture( textureNode, images );
texture.ID = textureNode.id;
texture.name = textureNode.attrName;
var wrapModeU = textureNode.WrapModeU;
var wrapModeV = textureNode.WrapModeV;
var valueU = wrapModeU !== undefined ? wrapModeU.value : 0;
var valueV = wrapModeV !== undefined ? wrapModeV.value : 0;
// http://download.autodesk.com/us/fbx/SDKdocs/FBX_SDK_Help/files/fbxsdkref/class_k_fbx_texture.html#889640e63e2e681259ea81061b85143a
// 0: repeat(default), 1: clamp
texture.wrapS = valueU === 0 ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
texture.wrapT = valueV === 0 ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
if ( 'Scaling' in textureNode ) {
var values = textureNode.Scaling.value;
texture.repeat.x = values[ 0 ];
texture.repeat.y = values[ 1 ];
}
return texture;
},
// load a texture specified as a blob or data URI, or via an external URL using THREE.TextureLoader
loadTexture: function ( textureNode, images ) {
var fileName;
var currentPath = this.textureLoader.path;
var children = connections.get( textureNode.id ).children;
if ( children !== undefined && children.length > 0 && images[ children[ 0 ].ID ] !== undefined ) {
fileName = images[ children[ 0 ].ID ];
if ( fileName.indexOf( 'blob:' ) === 0 || fileName.indexOf( 'data:' ) === 0 ) {
this.textureLoader.setPath( undefined );
}
}
var texture;
var extension = textureNode.FileName.slice( - 3 ).toLowerCase();
if ( extension === 'tga' ) {
var loader = this.manager.getHandler( '.tga' );
if ( loader === null ) {
console.warn( 'FBXLoader: TGA loader not found, creating placeholder texture for', textureNode.RelativeFilename );
texture = new THREE.Texture();
} else {
texture = loader.load( fileName );
}
} else if ( extension === 'psd' ) {
console.warn( 'FBXLoader: PSD textures are not supported, creating placeholder texture for', textureNode.RelativeFilename );
texture = new THREE.Texture();
} else {
texture = this.textureLoader.load( fileName );
}
this.textureLoader.setPath( currentPath );
return texture;
},
// Parse nodes in FBXTree.Objects.Material
parseMaterials: function ( textureMap ) {
var materialMap = new Map();
if ( 'Material' in fbxTree.Objects ) {
var materialNodes = fbxTree.Objects.Material;
for ( var nodeID in materialNodes ) {
var material = this.parseMaterial( materialNodes[ nodeID ], textureMap );
if ( material !== null ) materialMap.set( parseInt( nodeID ), material );
}
}
return materialMap;
},
// Parse single node in FBXTree.Objects.Material
// Materials are connected to texture maps in FBXTree.Objects.Textures
// FBX format currently only supports Lambert and Phong shading models
parseMaterial: function ( materialNode, textureMap ) {
var ID = materialNode.id;
var name = materialNode.attrName;
var type = materialNode.ShadingModel;
// Case where FBX wraps shading model in property object.
if ( typeof type === 'object' ) {
type = type.value;
}
// Ignore unused materials which don't have any connections.
if ( ! connections.has( ID ) ) return null;
var parameters = this.parseParameters( materialNode, textureMap, ID );
var material;
switch ( type.toLowerCase() ) {
case 'phong':
material = new THREE.MeshPhongMaterial();
break;
case 'lambert':
material = new THREE.MeshLambertMaterial();
break;
default:
console.warn( 'THREE.FBXLoader: unknown material type "%s". Defaulting to MeshPhongMaterial.', type );
material = new THREE.MeshPhongMaterial();
break;
}
material.setValues( parameters );
material.name = name;
return material;
},
// Parse FBX material and return parameters suitable for a three.js material
// Also parse the texture map and return any textures associated with the material
parseParameters: function ( materialNode, textureMap, ID ) {
var parameters = {};
if ( materialNode.BumpFactor ) {
parameters.bumpScale = materialNode.BumpFactor.value;
}
if ( materialNode.Diffuse ) {
parameters.color = new THREE.Color().fromArray( materialNode.Diffuse.value );
} else if ( materialNode.DiffuseColor && ( materialNode.DiffuseColor.type === 'Color' || materialNode.DiffuseColor.type === 'ColorRGB' ) ) {
// The blender exporter exports diffuse here instead of in materialNode.Diffuse
parameters.color = new THREE.Color().fromArray( materialNode.DiffuseColor.value );
}
if ( materialNode.DisplacementFactor ) {
parameters.displacementScale = materialNode.DisplacementFactor.value;
}
if ( materialNode.Emissive ) {
parameters.emissive = new THREE.Color().fromArray( materialNode.Emissive.value );
} else if ( materialNode.EmissiveColor && ( materialNode.EmissiveColor.type === 'Color' || materialNode.EmissiveColor.type === 'ColorRGB' ) ) {
// The blender exporter exports emissive color here instead of in materialNode.Emissive
parameters.emissive = new THREE.Color().fromArray( materialNode.EmissiveColor.value );
}
if ( materialNode.EmissiveFactor ) {
parameters.emissiveIntensity = parseFloat( materialNode.EmissiveFactor.value );
}
if ( materialNode.Opacity ) {
parameters.opacity = parseFloat( materialNode.Opacity.value );
}
if ( parameters.opacity < 1.0 ) {
parameters.transparent = true;
}
if ( materialNode.ReflectionFactor ) {
parameters.reflectivity = materialNode.ReflectionFactor.value;
}
if ( materialNode.Shininess ) {
parameters.shininess = materialNode.Shininess.value;
}
if ( materialNode.Specular ) {
parameters.specular = new THREE.Color().fromArray( materialNode.Specular.value );
} else if ( materialNode.SpecularColor && materialNode.SpecularColor.type === 'Color' ) {
// The blender exporter exports specular color here instead of in materialNode.Specular
parameters.specular = new THREE.Color().fromArray( materialNode.SpecularColor.value );
}
var scope = this;
connections.get( ID ).children.forEach( function ( child ) {
var type = child.relationship;
switch ( type ) {
case 'Bump':
parameters.bumpMap = scope.getTexture( textureMap, child.ID );
break;
case 'Maya|TEX_ao_map':
parameters.aoMap = scope.getTexture( textureMap, child.ID );
break;
case 'DiffuseColor':
case 'Maya|TEX_color_map':
parameters.map = scope.getTexture( textureMap, child.ID );
parameters.map.encoding = THREE.sRGBEncoding;
break;
case 'DisplacementColor':
parameters.displacementMap = scope.getTexture( textureMap, child.ID );
break;
case 'EmissiveColor':
parameters.emissiveMap = scope.getTexture( textureMap, child.ID );
parameters.emissiveMap.encoding = THREE.sRGBEncoding;
break;
case 'NormalMap':
case 'Maya|TEX_normal_map':
parameters.normalMap = scope.getTexture( textureMap, child.ID );
break;
case 'ReflectionColor':
parameters.envMap = scope.getTexture( textureMap, child.ID );
parameters.envMap.mapping = THREE.EquirectangularReflectionMapping;
parameters.envMap.encoding = THREE.sRGBEncoding;
break;
case 'SpecularColor':
parameters.specularMap = scope.getTexture( textureMap, child.ID );
parameters.specularMap.encoding = THREE.sRGBEncoding;
break;
case 'TransparentColor':
case 'TransparencyFactor':
parameters.alphaMap = scope.getTexture( textureMap, child.ID );
parameters.transparent = true;
break;
case 'AmbientColor':
case 'ShininessExponent': // AKA glossiness map
case 'SpecularFactor': // AKA specularLevel
case 'VectorDisplacementColor': // NOTE: Seems to be a copy of DisplacementColor
default:
console.warn( 'THREE.FBXLoader: %s map is not supported in three.js, skipping texture.', type );
break;
}
} );
return parameters;
},
// get a texture from the textureMap for use by a material.
getTexture: function ( textureMap, id ) {
// if the texture is a layered texture, just use the first layer and issue a warning
if ( 'LayeredTexture' in fbxTree.Objects && id in fbxTree.Objects.LayeredTexture ) {
console.warn( 'THREE.FBXLoader: layered textures are not supported in three.js. Discarding all but first layer.' );
id = connections.get( id ).children[ 0 ].ID;
}
return textureMap.get( id );
},
// Parse nodes in FBXTree.Objects.Deformer
// Deformer node can contain skinning or Vertex Cache animation data, however only skinning is supported here
// Generates map of Skeleton-like objects for use later when generating and binding skeletons.
parseDeformers: function () {
var skeletons = {};
var morphTargets = {};
if ( 'Deformer' in fbxTree.Objects ) {
var DeformerNodes = fbxTree.Objects.Deformer;
for ( var nodeID in DeformerNodes ) {
var deformerNode = DeformerNodes[ nodeID ];
var relationships = connections.get( parseInt( nodeID ) );
if ( deformerNode.attrType === 'Skin' ) {
var skeleton = this.parseSkeleton( relationships, DeformerNodes );
skeleton.ID = nodeID;
if ( relationships.parents.length > 1 ) console.warn( 'THREE.FBXLoader: skeleton attached to more than one geometry is not supported.' );
skeleton.geometryID = relationships.parents[ 0 ].ID;
skeletons[ nodeID ] = skeleton;
} else if ( deformerNode.attrType === 'BlendShape' ) {
var morphTarget = {
id: nodeID,
};
morphTarget.rawTargets = this.parseMorphTargets( relationships, DeformerNodes );
morphTarget.id = nodeID;
if ( relationships.parents.length > 1 ) console.warn( 'THREE.FBXLoader: morph target attached to more than one geometry is not supported.' );
morphTargets[ nodeID ] = morphTarget;
}
}
}
return {
skeletons: skeletons,
morphTargets: morphTargets,
};
},
// Parse single nodes in FBXTree.Objects.Deformer
// The top level skeleton node has type 'Skin' and sub nodes have type 'Cluster'
// Each skin node represents a skeleton and each cluster node represents a bone
parseSkeleton: function ( relationships, deformerNodes ) {
var rawBones = [];
relationships.children.forEach( function ( child ) {
var boneNode = deformerNodes[ child.ID ];
if ( boneNode.attrType !== 'Cluster' ) return;
var rawBone = {
ID: child.ID,
indices: [],
weights: [],
transformLink: new THREE.Matrix4().fromArray( boneNode.TransformLink.a ),
// transform: new THREE.Matrix4().fromArray( boneNode.Transform.a ),
// linkMode: boneNode.Mode,
};
if ( 'Indexes' in boneNode ) {
rawBone.indices = boneNode.Indexes.a;
rawBone.weights = boneNode.Weights.a;
}
rawBones.push( rawBone );
} );
return {
rawBones: rawBones,
bones: []
};
},
// The top level morph deformer node has type "BlendShape" and sub nodes have type "BlendShapeChannel"
parseMorphTargets: function ( relationships, deformerNodes ) {
var rawMorphTargets = [];
for ( var i = 0; i < relationships.children.length; i ++ ) {
var child = relationships.children[ i ];
var morphTargetNode = deformerNodes[ child.ID ];
var rawMorphTarget = {
name: morphTargetNode.attrName,
initialWeight: morphTargetNode.DeformPercent,
id: morphTargetNode.id,
fullWeights: morphTargetNode.FullWeights.a
};
if ( morphTargetNode.attrType !== 'BlendShapeChannel' ) return;
rawMorphTarget.geoID = connections.get( parseInt( child.ID ) ).children.filter( function ( child ) {
return child.relationship === undefined;
} )[ 0 ].ID;
rawMorphTargets.push( rawMorphTarget );
}
return rawMorphTargets;
},
// create the main THREE.Group() to be returned by the loader
parseScene: function ( deformers, geometryMap, materialMap ) {
sceneGraph = new THREE.Group();
var modelMap = this.parseModels( deformers.skeletons, geometryMap, materialMap );
var modelNodes = fbxTree.Objects.Model;
var scope = this;
modelMap.forEach( function ( model ) {
var modelNode = modelNodes[ model.ID ];
scope.setLookAtProperties( model, modelNode );
var parentConnections = connections.get( model.ID ).parents;
parentConnections.forEach( function ( connection ) {
var parent = modelMap.get( connection.ID );
if ( parent !== undefined ) parent.add( model );
} );
if ( model.parent === null ) {
sceneGraph.add( model );
}
} );
this.bindSkeleton( deformers.skeletons, geometryMap, modelMap );
this.createAmbientLight();
this.setupMorphMaterials();
sceneGraph.traverse( function ( node ) {
if ( node.userData.transformData ) {
if ( node.parent ) {
node.userData.transformData.parentMatrix = node.parent.matrix;
node.userData.transformData.parentMatrixWorld = node.parent.matrixWorld;
}
var transform = generateTransform( node.userData.transformData );
node.applyMatrix4( transform );
node.updateWorldMatrix();
}
} );
var animations = new AnimationParser().parse();
// if all the models where already combined in a single group, just return that
if ( sceneGraph.children.length === 1 && sceneGraph.children[ 0 ].isGroup ) {
sceneGraph.children[ 0 ].animations = animations;
sceneGraph = sceneGraph.children[ 0 ];
}
sceneGraph.animations = animations;
},
// parse nodes in FBXTree.Objects.Model
parseModels: function ( skeletons, geometryMap, materialMap ) {
var modelMap = new Map();
var modelNodes = fbxTree.Objects.Model;
for ( var nodeID in modelNodes ) {
var id = parseInt( nodeID );
var node = modelNodes[ nodeID ];
var relationships = connections.get( id );
var model = this.buildSkeleton( relationships, skeletons, id, node.attrName );
if ( ! model ) {
switch ( node.attrType ) {
case 'Camera':
model = this.createCamera( relationships );
break;
case 'Light':
model = this.createLight( relationships );
break;
case 'Mesh':
model = this.createMesh( relationships, geometryMap, materialMap );
break;
case 'NurbsCurve':
model = this.createCurve( relationships, geometryMap );
break;
case 'LimbNode':
case 'Root':
model = new THREE.Bone();
break;
case 'Null':
default:
model = new THREE.Group();
break;
}
model.name = node.attrName ? THREE.PropertyBinding.sanitizeNodeName( node.attrName ) : '';
model.ID = id;
}
this.getTransformData( model, node );
modelMap.set( id, model );
}
return modelMap;
},
buildSkeleton: function ( relationships, skeletons, id, name ) {
var bone = null;
relationships.parents.forEach( function ( parent ) {
for ( var ID in skeletons ) {
var skeleton = skeletons[ ID ];
skeleton.rawBones.forEach( function ( rawBone, i ) {
if ( rawBone.ID === parent.ID ) {
var subBone = bone;
bone = new THREE.Bone();
bone.matrixWorld.copy( rawBone.transformLink );
// set name and id here - otherwise in cases where "subBone" is created it will not have a name / id
bone.name = name ? THREE.PropertyBinding.sanitizeNodeName( name ) : '';
bone.ID = id;
skeleton.bones[ i ] = bone;
// In cases where a bone is shared between multiple meshes
// duplicate the bone here and and it as a child of the first bone
if ( subBone !== null ) {
bone.add( subBone );
}
}
} );
}
} );
return bone;
},
// create a THREE.PerspectiveCamera or THREE.OrthographicCamera
createCamera: function ( relationships ) {
var model;
var cameraAttribute;
relationships.children.forEach( function ( child ) {
var attr = fbxTree.Objects.NodeAttribute[ child.ID ];
if ( attr !== undefined ) {
cameraAttribute = attr;
}
} );
if ( cameraAttribute === undefined ) {
model = new THREE.Object3D();
} else {
var type = 0;
if ( cameraAttribute.CameraProjectionType !== undefined && cameraAttribute.CameraProjectionType.value === 1 ) {
type = 1;
}
var nearClippingPlane = 1;
if ( cameraAttribute.NearPlane !== undefined ) {
nearClippingPlane = cameraAttribute.NearPlane.value / 1000;
}
var farClippingPlane = 1000;
if ( cameraAttribute.FarPlane !== undefined ) {
farClippingPlane = cameraAttribute.FarPlane.value / 1000;
}