FBXLoader.js 63.3 KB
Newer Older
Y
yamahigashi 已提交
1 2
/**
 * @author yamahigashi https://github.com/yamahigashi
K
Kyle Larson 已提交
3
 * @author Kyle-Larson https://github.com/Kyle-Larson
Y
yamahigashi 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This loader loads FBX file in *ASCII and version 7 format*.
 *
 * Support
 *  - mesh
 *  - skinning
 *  - normal / uv
 *
 *  Not Support
 *  - material
 *  - texture
 *  - morph
 */

18
( function () {
Y
yamahigashi 已提交
19

M
Mr.doob 已提交
20
	THREE.FBXLoader = function ( manager ) {
Y
yamahigashi 已提交
21

M
Mr.doob 已提交
22
		THREE.Loader.call( this );
Y
yamahigashi 已提交
23 24 25
		this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
		this.textureLoader = null;
		this.textureBasePath = null;
Y
yamahigashi 已提交
26

Y
yamahigashi 已提交
27
	};
Y
yamahigashi 已提交
28

Y
yamahigashi 已提交
29
	THREE.FBXLoader.prototype = Object.create( THREE.Loader.prototype );
Y
yamahigashi 已提交
30

Y
yamahigashi 已提交
31
	THREE.FBXLoader.prototype.constructor = THREE.FBXLoader;
Y
yamahigashi 已提交
32

33
	Object.assign( THREE.FBXLoader.prototype, {
Y
yamahigashi 已提交
34

35
		load: function ( url, onLoad, onProgress, onError ) {
Y
yamahigashi 已提交
36

37
			var scope = this;
Y
yamahigashi 已提交
38

39 40 41
			var loader = new THREE.FileLoader( scope.manager );
			// loader.setCrossOrigin( this.crossOrigin );
			loader.load( url, function ( text ) {
Y
yamahigashi 已提交
42

43
				if ( ! scope.isFbxFormatASCII( text ) ) {
Y
yamahigashi 已提交
44

45
					console.warn( 'FBXLoader: !!! FBX Binary format not supported !!!' );
Y
yamahigashi 已提交
46

47
				} else if ( ! scope.isFbxVersionSupported( text ) ) {
Y
yamahigashi 已提交
48

49
					console.warn( 'FBXLoader: !!! FBX Version below 7 not supported !!!' );
Y
yamahigashi 已提交
50

51
				} else {
Y
yamahigashi 已提交
52

53 54
					scope.textureBasePath = scope.extractUrlBase( url );
					onLoad( scope.parse( text ) );
Y
yamahigashi 已提交
55

56
				}
Y
yamahigashi 已提交
57

58
			}, onProgress, onError );
Y
yamahigashi 已提交
59

60
		},
Y
yamahigashi 已提交
61

62
		setCrossOrigin: function ( value ) {
Y
yamahigashi 已提交
63

64
			this.crossOrigin = value;
Y
yamahigashi 已提交
65

66
		},
Y
yamahigashi 已提交
67

68
		isFbxFormatASCII: function ( body ) {
Y
yamahigashi 已提交
69

70
			var CORRECT = [ 'K', 'a', 'y', 'd', 'a', 'r', 'a', '\\', 'F', 'B', 'X', '\\', 'B', 'i', 'n', 'a', 'r', 'y', '\\', '\\' ];
Y
yamahigashi 已提交
71

72 73
			var cursor = 0;
			var read = function ( offset ) {
Y
yamahigashi 已提交
74

75 76 77 78
				var result = body[ offset - 1 ];
				body = body.slice( cursor + offset );
				cursor ++;
				return result;
Y
yamahigashi 已提交
79

80
			};
Y
yamahigashi 已提交
81

82
			for ( var i = 0; i < CORRECT.length; ++ i ) {
Y
yamahigashi 已提交
83

84 85
				var num = read( 1 );
				if ( num == CORRECT[ i ] ) {
Y
yamahigashi 已提交
86

87
					return false;
Y
yamahigashi 已提交
88

89
				}
Y
yamahigashi 已提交
90

91
			}
Y
yamahigashi 已提交
92

93
			return true;
Y
yamahigashi 已提交
94

95
		},
Y
yamahigashi 已提交
96

97
		isFbxVersionSupported: function ( body ) {
Y
yamahigashi 已提交
98

99 100 101
			var versionExp = /FBXVersion: (\d+)/;
			var match = body.match( versionExp );
			if ( match ) {
Y
yamahigashi 已提交
102

103 104 105
				var version = parseInt( match[ 1 ] );
				console.log( 'FBXLoader: FBX version ' + version );
				return version >= 7000;
Y
yamahigashi 已提交
106

107 108
			}
			return false;
M
Mr.doob 已提交
109

110
		},
M
Mr.doob 已提交
111

112
		parse: function ( text ) {
Y
yamahigashi 已提交
113

114
			var scope = this;
Y
yamahigashi 已提交
115

116
			console.time( 'FBXLoader' );
M
Mr.doob 已提交
117

118 119 120
			console.time( 'FBXLoader: TextParser' );
			var nodes = new FBXParser().parse( text );
			console.timeEnd( 'FBXLoader: TextParser' );
Y
yamahigashi 已提交
121

122 123 124 125 126
			console.time( 'FBXLoader: ObjectParser' );
			scope.hierarchy = ( new Bones() ).parseHierarchy( nodes );
			scope.weights	= ( new Weights() ).parse( nodes, scope.hierarchy );
			scope.animations = ( new Animation() ).parse( nodes, scope.hierarchy );
			scope.textures = ( new Textures() ).parse( nodes, scope.hierarchy );
K
Kyle Larson 已提交
127
			scope.materials = ( new Materials() ).parse( nodes, scope.hierarchy );
128
			console.timeEnd( 'FBXLoader: ObjectParser' );
Y
yamahigashi 已提交
129

130 131 132
			console.time( 'FBXLoader: GeometryParser' );
			var geometries = this.parseGeometries( nodes );
			console.timeEnd( 'FBXLoader: GeometryParser' );
Y
yamahigashi 已提交
133

134
			var container = new THREE.Group();
Y
yamahigashi 已提交
135

136
			for ( var i = 0; i < geometries.length; ++ i ) {
Y
yamahigashi 已提交
137

138
				if ( geometries[ i ] === undefined ) {
Y
yamahigashi 已提交
139

140
					continue;
Y
yamahigashi 已提交
141

142
				}
Y
yamahigashi 已提交
143

144
				container.add( geometries[ i ] );
Y
yamahigashi 已提交
145

146 147
				//wireframe = new THREE.WireframeHelper( geometries[i], 0x00ff00 );
				//container.add( wireframe );
Y
yamahigashi 已提交
148

149 150
				//vnh = new THREE.VertexNormalsHelper( geometries[i], 0.6 );
				//container.add( vnh );
Y
yamahigashi 已提交
151

152 153
				//skh = new THREE.SkeletonHelper( geometries[i] );
				//container.add( skh );
Y
yamahigashi 已提交
154

155
				// container.add( new THREE.BoxHelper( geometries[i] ) );
Y
yamahigashi 已提交
156

157
			}
Y
yamahigashi 已提交
158

159 160
			console.timeEnd( 'FBXLoader' );
			return container;
Y
yamahigashi 已提交
161

162
		},
Y
yamahigashi 已提交
163

164
		parseGeometries: function ( node ) {
Y
yamahigashi 已提交
165

166 167
			// has not geo, return []
			if ( ! ( 'Geometry' in node.Objects.subNodes ) ) {
Y
yamahigashi 已提交
168

169
				return [];
Y
yamahigashi 已提交
170

Y
yamahigashi 已提交
171
			}
Y
yamahigashi 已提交
172

173 174 175
			// has many
			var geoCount = 0;
			for ( var geo in node.Objects.subNodes.Geometry ) {
Y
yamahigashi 已提交
176

177
				if ( geo.match( /^\d+$/ ) ) {
Y
yamahigashi 已提交
178

179
					geoCount ++;
Y
yamahigashi 已提交
180

Y
yamahigashi 已提交
181
				}
Y
yamahigashi 已提交
182

Y
yamahigashi 已提交
183
			}
Y
yamahigashi 已提交
184

185 186
			var res = [];
			if ( geoCount > 0 ) {
Y
yamahigashi 已提交
187

188
				for ( geo in node.Objects.subNodes.Geometry ) {
Y
yamahigashi 已提交
189

190
					if ( node.Objects.subNodes.Geometry[ geo ].attrType === 'Mesh' ) {
Y
yamahigashi 已提交
191

192
						res.push( this.parseGeometry( node.Objects.subNodes.Geometry[ geo ], node ) );
Y
yamahigashi 已提交
193

194
					}
Y
yamahigashi 已提交
195

196
				}
Y
yamahigashi 已提交
197

198
			} else {
Y
yamahigashi 已提交
199

200
				res.push( this.parseGeometry( node.Objects.subNodes.Geometry, node ) );
Y
yamahigashi 已提交
201

202
			}
Y
yamahigashi 已提交
203

204
			return res;
Y
yamahigashi 已提交
205

206
		},
Y
yamahigashi 已提交
207

208
		parseGeometry: function ( node, nodes ) {
Y
yamahigashi 已提交
209

210 211
			var geo = ( new Geometry() ).parse( node );
			geo.addBones( this.hierarchy.hierarchy );
Y
yamahigashi 已提交
212

213 214 215 216
			//*
			var geometry = new THREE.BufferGeometry();
			geometry.name = geo.name;
			geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( geo.vertices ), 3 ) );
Y
yamahigashi 已提交
217

218
			if ( geo.normals !== undefined && geo.normals.length > 0 ) {
Y
yamahigashi 已提交
219

220
				geometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( geo.normals ), 3 ) );
Y
yamahigashi 已提交
221

222
			}
Y
yamahigashi 已提交
223

224
			if ( geo.uvs !== undefined && geo.uvs.length > 0 ) {
Y
yamahigashi 已提交
225

226
				geometry.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( geo.uvs ), 2 ) );
Y
yamahigashi 已提交
227

Y
yamahigashi 已提交
228
			}
Y
yamahigashi 已提交
229

230
			if ( geo.indices !== undefined && geo.indices.length > 65535 ) {
Y
yamahigashi 已提交
231

232
				geometry.setIndex( new THREE.BufferAttribute( new Uint32Array( geo.indices ), 1 ) );
Y
yamahigashi 已提交
233

234
			} else if ( geo.indices !== undefined ) {
Y
yamahigashi 已提交
235

236
				geometry.setIndex( new THREE.BufferAttribute( new Uint16Array( geo.indices ), 1 ) );
Y
yamahigashi 已提交
237

238
			}
Y
yamahigashi 已提交
239

240 241 242
			geometry.verticesNeedUpdate = true;
			geometry.computeBoundingSphere();
			geometry.computeBoundingBox();
Y
yamahigashi 已提交
243

244 245 246
			var texture;
			var texs = this.textures.getById( nodes.searchConnectionParent( geo.id ) );
			if ( texs !== undefined && texs.length > 0 ) {
Y
yamahigashi 已提交
247

248
				if ( this.textureLoader === null ) {
Y
yamahigashi 已提交
249

250
					this.textureLoader = new THREE.TextureLoader();
Y
yamahigashi 已提交
251

252 253
				}
				texture = this.textureLoader.load( this.textureBasePath + '/' + texs[ 0 ].fileName );
Y
yamahigashi 已提交
254

255
			}
Y
yamahigashi 已提交
256

K
Kyle Larson 已提交
257 258 259 260
			var materials = [];
			var material;
			var mats = this.materials.getById( nodes.searchConnectionParent( geo.id ) );
			if ( mats !== undefined && mats.length > 0 ) {
Y
yamahigashi 已提交
261

K
Kyle Larson 已提交
262
				for ( var i = 0; i < mats.length; ++ i ) {
263

K
Kyle Larson 已提交
264 265
					var mat_data = mats[ i ];
					var tmpMat;
266

K
Kyle Larson 已提交
267 268 269 270 271 272 273 274 275
					// TODO:
					// Cannot find a list of possible ShadingModel values.
					// If someone finds a list, please add additional cases
					// and map to appropriate materials.
					switch ( mat_data.type ) {

						case "phong":
							tmpMat = new THREE.MeshPhongMaterial();
							break;
276
						case "lambert":
K
Kyle Larson 已提交
277 278 279 280 281 282
							tmpMat = new THREE.MeshLambertMaterial();
							break;
						default:
							console.warn( "No implementation given for material type " + mat_data.type + " in FBXLoader.js.  Defaulting to basic material" );
							tmpMat = new THREE.MeshBasicMaterial( { color: 0x3300ff } );
							break;
283 284

					}
K
Kyle Larson 已提交
285
					if ( texture !== undefined ) {
286

K
Kyle Larson 已提交
287
						mat_data.parameters.map = texture;
288

K
Kyle Larson 已提交
289 290
					}
					tmpMat.setValues( mat_data.parameters );
Y
yamahigashi 已提交
291

K
Kyle Larson 已提交
292
					materials.push( tmpMat );
Y
yamahigashi 已提交
293

K
Kyle Larson 已提交
294
				}
Y
yamahigashi 已提交
295

K
Kyle Larson 已提交
296
				if ( materials.length === 1 ) {
Y
yamahigashi 已提交
297

K
Kyle Larson 已提交
298
					material = materials[ 0 ];
Y
yamahigashi 已提交
299

K
Kyle Larson 已提交
300
				} else {
Y
yamahigashi 已提交
301

K
Kyle Larson 已提交
302 303 304 305
					//Set up for multi-material
					material = new THREE.MultiMaterial( materials );
					var material_groupings = [];
					var last_material_group = - 1;
306
					var material_index_list = parseArrayToInt( node.subNodes.LayerElementMaterial[ 0 ].subNodes.Materials.properties.a );
K
Kyle Larson 已提交
307
					for ( var i = 0; i < geo.polyIndices.length; ++ i ) {
Y
yamahigashi 已提交
308

K
Kyle Larson 已提交
309
						if ( last_material_group !== material_index_list[ geo.polyIndices[ i ] ] ) {
Y
yamahigashi 已提交
310

K
Kyle Larson 已提交
311 312
							material_groupings.push( { start: i * 3, count: 0, materialIndex: material_index_list[ geo.polyIndices[ i ] ] } );
							last_material_group = material_index_list[ geo.polyIndices[ i ] ];
Y
yamahigashi 已提交
313

K
Kyle Larson 已提交
314 315
						}
						material_groupings[ material_groupings.length - 1 ].count += 3;
Y
yamahigashi 已提交
316

K
Kyle Larson 已提交
317 318
					}
					geometry.groups = material_groupings;
Y
yamahigashi 已提交
319

K
Kyle Larson 已提交
320
				}
Y
yamahigashi 已提交
321 322


K
Kyle Larson 已提交
323 324 325 326
			} else {

				//No material found for this geometry, create default
				if ( texture !== undefined ) {
Y
yamahigashi 已提交
327

K
Kyle Larson 已提交
328
					material = new THREE.MeshBasicMaterial( { map: texture } );
Y
yamahigashi 已提交
329

K
Kyle Larson 已提交
330
				} else {
Y
yamahigashi 已提交
331

K
Kyle Larson 已提交
332
					material = new THREE.MeshBasicMaterial( { color: 0x3300ff } );
Y
yamahigashi 已提交
333

K
Kyle Larson 已提交
334
				}
Y
yamahigashi 已提交
335

K
Kyle Larson 已提交
336
			}
Y
yamahigashi 已提交
337

338 339
			var material;
			if ( texture !== undefined ) {
Y
yamahigashi 已提交
340

341
				material = new THREE.MeshBasicMaterial( { map: texture } );
Y
yamahigashi 已提交
342

343
			} else {
Y
yamahigashi 已提交
344

345
				material = new THREE.MeshBasicMaterial( { color: 0x3300ff } );
Y
yamahigashi 已提交
346

347
			}
Y
yamahigashi 已提交
348

349 350 351 352
			geometry = new THREE.Geometry().fromBufferGeometry( geometry );
			geometry.bones = geo.bones;
			geometry.skinIndices = this.weights.skinIndices;
			geometry.skinWeights = this.weights.skinWeights;
Y
yamahigashi 已提交
353

354
			var mesh = null;
355
			if ( geo.bones === undefined || geo.skins === undefined || this.animations === undefined ) {
Y
yamahigashi 已提交
356

357
				mesh = new THREE.Mesh( geometry, material );
Y
yamahigashi 已提交
358

359
			} else {
Y
yamahigashi 已提交
360

361 362 363
				material.skinning = true;
				mesh = new THREE.SkinnedMesh( geometry, material );
				this.addAnimation( mesh, this.weights.matrices, this.animations );
Y
yamahigashi 已提交
364

Y
yamahigashi 已提交
365
			}
Y
yamahigashi 已提交
366

367
			return mesh;
Y
yamahigashi 已提交
368

369
		},
Y
yamahigashi 已提交
370

371
		addAnimation: function ( mesh, matrices, animations ) {
Y
yamahigashi 已提交
372

373
			for ( var key in animations.stacks ) {
Y
yamahigashi 已提交
374

375 376 377
				var animationData = {
					name: animations.stacks[ key ].name,
					fps: 30,
378
					length: animations.stacks[ key ].length,
379 380
					hierarchy: []
				};
Y
yamahigashi 已提交
381

382
				for ( var i = 0; i < mesh.geometry.bones.length; ++ i ) {
Y
yamahigashi 已提交
383

384 385 386
					var name = mesh.geometry.bones[ i ].name;
					name = name.replace( /.*:/, '' );
					animationData.hierarchy.push( { parent: mesh.geometry.bones[ i ].parent, name: name, keys: [] } );
Y
yamahigashi 已提交
387

388
				}
Y
yamahigashi 已提交
389

390
				function hasCurve( animNode, attr ) {
Y
yamahigashi 已提交
391

392
					if ( animNode === undefined ) {
Y
yamahigashi 已提交
393

394
						return false;
Y
yamahigashi 已提交
395

396
					}
Y
yamahigashi 已提交
397

398 399
					var attrNode;
					switch ( attr ) {
Y
yamahigashi 已提交
400

401 402
						case 'S':
							if ( ! ( animNode.S ) ) {
Y
yamahigashi 已提交
403

404
								return false;
Y
yamahigashi 已提交
405

406 407 408
							}
							attrNode = animNode.S;
							break;
Y
yamahigashi 已提交
409

410 411
						case 'R':
							if ( ! ( animNode.R ) ) {
Y
yamahigashi 已提交
412

413
								return false;
Y
yamahigashi 已提交
414

415 416 417
							}
							attrNode = animNode.R;
							break;
Y
yamahigashi 已提交
418

419 420
						case 'T':
							if ( ! ( animNode.T ) ) {
Y
yamahigashi 已提交
421

422
								return false;
Y
yamahigashi 已提交
423

424 425 426
							}
							attrNode = animNode.T;
							break;
Y
yamahigashi 已提交
427

428
					}
Y
yamahigashi 已提交
429

430
					if ( attrNode.curves.x === undefined ) {
Y
yamahigashi 已提交
431

432
						return false;
Y
yamahigashi 已提交
433

434
					}
Y
yamahigashi 已提交
435

436
					if ( attrNode.curves.y === undefined ) {
Y
yamahigashi 已提交
437

438
						return false;
Y
yamahigashi 已提交
439

440
					}
Y
yamahigashi 已提交
441

442
					if ( attrNode.curves.z === undefined ) {
Y
yamahigashi 已提交
443

444
						return false;
Y
yamahigashi 已提交
445

446
					}
Y
yamahigashi 已提交
447

448
					return true;
Y
yamahigashi 已提交
449

450
				}
Y
yamahigashi 已提交
451

452
				function hasKeyOnFrame( attrNode, frame ) {
Y
yamahigashi 已提交
453

454 455 456
					var x = isKeyExistOnFrame( attrNode.curves.x, frame );
					var y = isKeyExistOnFrame( attrNode.curves.y, frame );
					var z = isKeyExistOnFrame( attrNode.curves.z, frame );
Y
yamahigashi 已提交
457

458
					return x && y && z;
Y
yamahigashi 已提交
459

Y
yamahigashi 已提交
460
				}
Y
yamahigashi 已提交
461

462
				function isKeyExistOnFrame( curve, frame ) {
Y
yamahigashi 已提交
463

464 465
					var value = curve.values[ frame ];
					return value !== undefined;
Y
yamahigashi 已提交
466

Y
yamahigashi 已提交
467
				}
Y
yamahigashi 已提交
468

469
				function genKey( animNode, bone ) {
Y
yamahigashi 已提交
470

471 472 473 474 475 476
					// key initialize with its bone's bind pose at first
					var key = {};
					key.time = frame / animations.fps; // TODO:
					key.pos = bone.pos;
					key.rot = bone.rotq;
					key.scl = bone.scl;
Y
yamahigashi 已提交
477

478
					if ( animNode === undefined ) {
Y
yamahigashi 已提交
479

480
						return key;
Y
yamahigashi 已提交
481

482
					}
Y
yamahigashi 已提交
483

484
					try {
Y
yamahigashi 已提交
485

486
						if ( hasCurve( animNode, 'T' ) && hasKeyOnFrame( animNode.T, frame ) ) {
Y
yamahigashi 已提交
487

488 489 490 491 492
							var pos = new THREE.Vector3(
								animNode.T.curves.x.values[ frame ],
								animNode.T.curves.y.values[ frame ],
								animNode.T.curves.z.values[ frame ] );
							key.pos = [ pos.x, pos.y, pos.z ];
Y
yamahigashi 已提交
493

494
						}
Y
yamahigashi 已提交
495

496
						if ( hasCurve( animNode, 'R' ) && hasKeyOnFrame( animNode.R, frame ) ) {
Y
yamahigashi 已提交
497

498 499 500 501 502 503
							var rx = degToRad( animNode.R.curves.x.values[ frame ] );
							var ry = degToRad( animNode.R.curves.y.values[ frame ] );
							var rz = degToRad( animNode.R.curves.z.values[ frame ] );
							var eul = new THREE.Vector3( rx, ry, rz );
							var rot = quatFromVec( eul.x, eul.y, eul.z );
							key.rot = [ rot.x, rot.y, rot.z, rot.w ];
Y
yamahigashi 已提交
504

505
						}
Y
yamahigashi 已提交
506

507
						if ( hasCurve( animNode, 'S' ) && hasKeyOnFrame( animNode.S, frame ) ) {
Y
yamahigashi 已提交
508

509 510 511 512 513
							var scl = new THREE.Vector3(
								animNode.S.curves.x.values[ frame ],
								animNode.S.curves.y.values[ frame ],
								animNode.S.curves.z.values[ frame ] );
							key.scl = [ scl.x, scl.y, scl.z ];
Y
yamahigashi 已提交
514

515
						}
Y
yamahigashi 已提交
516

517
					} catch ( e ) {
Y
yamahigashi 已提交
518

519 520 521
						// curve is not full plotted
						console.log( bone );
						console.log( e );
Y
yamahigashi 已提交
522

Y
yamahigashi 已提交
523
					}
Y
yamahigashi 已提交
524

525
					return key;
Y
yamahigashi 已提交
526

527
				}
Y
yamahigashi 已提交
528

529
				var bones = mesh.geometry.bones;
530
				for ( var frame = 0; frame < animations.stacks[ key ].frames; frame ++ ) {
Y
yamahigashi 已提交
531 532


533
					for ( i = 0; i < bones.length; i ++ ) {
Y
yamahigashi 已提交
534

535 536
						var bone = bones[ i ];
						var animNode = animations.stacks[ key ].layers[ 0 ][ i ];
Y
yamahigashi 已提交
537

538
						for ( var j = 0; j < animationData.hierarchy.length; j ++ ) {
Y
yamahigashi 已提交
539

540
							if ( animationData.hierarchy[ j ].name === bone.name ) {
Y
yamahigashi 已提交
541

542
								animationData.hierarchy[ j ].keys.push( genKey( animNode, bone ) );
Y
yamahigashi 已提交
543

544
							}
Y
yamahigashi 已提交
545

546
						}
Y
yamahigashi 已提交
547

548
					}
Y
yamahigashi 已提交
549

550
				}
Y
yamahigashi 已提交
551

552
				if ( mesh.geometry.animations === undefined ) {
Y
yamahigashi 已提交
553

554
					mesh.geometry.animations = [];
Y
yamahigashi 已提交
555

556
				}
Y
yamahigashi 已提交
557

558
				mesh.geometry.animations.push( THREE.AnimationClip.parseAnimation( animationData, mesh.geometry.bones ) );
Y
yamahigashi 已提交
559

Y
yamahigashi 已提交
560
			}
Y
yamahigashi 已提交
561

562
		},
Y
yamahigashi 已提交
563

564
		loadFile: function ( url, onLoad, onProgress, onError, responseType ) {
Y
yamahigashi 已提交
565

566
			var loader = new THREE.FileLoader( this.manager );
Y
yamahigashi 已提交
567

568
			loader.setResponseType( responseType );
Y
yamahigashi 已提交
569

570
			var request = loader.load( url, onLoad, onProgress, onError );
Y
yamahigashi 已提交
571

572
			return request;
Y
yamahigashi 已提交
573

574
		},
Y
yamahigashi 已提交
575

576
		loadFileAsBuffer: function ( url, onLoad, onProgress, onError ) {
Y
yamahigashi 已提交
577

578
			this.loadFile( url, onLoad, onProgress, onError, 'arraybuffer' );
Y
yamahigashi 已提交
579

580
		},
Y
yamahigashi 已提交
581

582
		loadFileAsText: function ( url, onLoad, onProgress, onError ) {
Y
yamahigashi 已提交
583

584
			this.loadFile( url, onLoad, onProgress, onError, 'text' );
Y
yamahigashi 已提交
585

586
		}
Y
yamahigashi 已提交
587

588
	} );
Y
yamahigashi 已提交
589

Y
yamahigashi 已提交
590
	/* ----------------------------------------------------------------- */
Y
yamahigashi 已提交
591

Y
yamahigashi 已提交
592
	function FBXNodes() {}
Y
yamahigashi 已提交
593

594
	Object.assign( FBXNodes.prototype, {
Y
yamahigashi 已提交
595

596
		add: function ( key, val ) {
Y
yamahigashi 已提交
597

598
			this[ key ] = val;
Y
yamahigashi 已提交
599

600
		},
Y
yamahigashi 已提交
601

602
		searchConnectionParent: function ( id ) {
Y
yamahigashi 已提交
603

604
			if ( this.__cache_search_connection_parent === undefined ) {
Y
yamahigashi 已提交
605

606
				this.__cache_search_connection_parent = [];
Y
yamahigashi 已提交
607

608
			}
Y
yamahigashi 已提交
609

610
			if ( this.__cache_search_connection_parent[ id ] !== undefined ) {
Y
yamahigashi 已提交
611

612
				return this.__cache_search_connection_parent[ id ];
Y
yamahigashi 已提交
613

614
			} else {
Y
yamahigashi 已提交
615

616
				this.__cache_search_connection_parent[ id ] = [];
Y
yamahigashi 已提交
617

618
			}
Y
yamahigashi 已提交
619

620
			var conns = this.Connections.properties.connections;
Y
yamahigashi 已提交
621

622 623
			var results = [];
			for ( var i = 0; i < conns.length; ++ i ) {
Y
yamahigashi 已提交
624

625 626 627 628 629 630 631
				if ( conns[ i ][ 0 ] == id ) {

					// 0 means scene root
					var res = conns[ i ][ 1 ] === 0 ? - 1 : conns[ i ][ 1 ];
					results.push( res );

				}
Y
yamahigashi 已提交
632

Y
yamahigashi 已提交
633
			}
Y
yamahigashi 已提交
634

635
			if ( results.length > 0 ) {
Y
yamahigashi 已提交
636

637 638
				this.__cache_search_connection_parent[ id ] = this.__cache_search_connection_parent[ id ].concat( results );
				return results;
Y
yamahigashi 已提交
639

640
			} else {
Y
yamahigashi 已提交
641

642 643
				this.__cache_search_connection_parent[ id ] = [ - 1 ];
				return [ - 1 ];
Y
yamahigashi 已提交
644

645
			}
Y
yamahigashi 已提交
646

647
		},
Y
yamahigashi 已提交
648

649
		searchConnectionChildren: function ( id ) {
Y
yamahigashi 已提交
650

651
			if ( this.__cache_search_connection_children === undefined ) {
Y
yamahigashi 已提交
652

653
				this.__cache_search_connection_children = [];
Y
yamahigashi 已提交
654

655
			}
Y
yamahigashi 已提交
656

657
			if ( this.__cache_search_connection_children[ id ] !== undefined ) {
Y
yamahigashi 已提交
658

659
				return this.__cache_search_connection_children[ id ];
Y
yamahigashi 已提交
660

661
			} else {
Y
yamahigashi 已提交
662

663
				this.__cache_search_connection_children[ id ] = [];
Y
yamahigashi 已提交
664

665
			}
Y
yamahigashi 已提交
666

667
			var conns = this.Connections.properties.connections;
Y
yamahigashi 已提交
668

669 670
			var res = [];
			for ( var i = 0; i < conns.length; ++ i ) {
Y
yamahigashi 已提交
671

672
				if ( conns[ i ][ 1 ] == id ) {
Y
yamahigashi 已提交
673

674 675 676
					// 0 means scene root
					res.push( conns[ i ][ 0 ] === 0 ? - 1 : conns[ i ][ 0 ] );
					// there may more than one kid, then search to the end
Y
yamahigashi 已提交
677

678
				}
Y
yamahigashi 已提交
679

Y
yamahigashi 已提交
680
			}
Y
yamahigashi 已提交
681

682
			if ( res.length > 0 ) {
Y
yamahigashi 已提交
683

684 685
				this.__cache_search_connection_children[ id ] = this.__cache_search_connection_children[ id ].concat( res );
				return res;
Y
yamahigashi 已提交
686

687
			} else {
Y
yamahigashi 已提交
688

689 690
				this.__cache_search_connection_children[ id ] = [ ];
				return [ ];
Y
yamahigashi 已提交
691

692
			}
Y
yamahigashi 已提交
693

694
		},
Y
yamahigashi 已提交
695

696
		searchConnectionType: function ( id, to ) {
Y
yamahigashi 已提交
697

698 699
			var key = id + ',' + to; // TODO: to hash
			if ( this.__cache_search_connection_type === undefined ) {
Y
yamahigashi 已提交
700

701
				this.__cache_search_connection_type = {};
Y
yamahigashi 已提交
702

703
			}
Y
yamahigashi 已提交
704

705
			if ( this.__cache_search_connection_type[ key ] !== undefined ) {
Y
yamahigashi 已提交
706

707
				return this.__cache_search_connection_type[ key ];
Y
yamahigashi 已提交
708

709
			} else {
Y
yamahigashi 已提交
710

711
				this.__cache_search_connection_type[ key ] = '';
Y
yamahigashi 已提交
712

713
			}
Y
yamahigashi 已提交
714

715
			var conns = this.Connections.properties.connections;
Y
yamahigashi 已提交
716

717
			for ( var i = 0; i < conns.length; ++ i ) {
Y
yamahigashi 已提交
718

719
				if ( conns[ i ][ 0 ] == id && conns[ i ][ 1 ] == to ) {
Y
yamahigashi 已提交
720

721 722 723
					// 0 means scene root
					this.__cache_search_connection_type[ key ] = conns[ i ][ 2 ];
					return conns[ i ][ 2 ];
Y
yamahigashi 已提交
724

725
				}
Y
yamahigashi 已提交
726

Y
yamahigashi 已提交
727
			}
Y
yamahigashi 已提交
728

729 730 731
			this.__cache_search_connection_type[ id ] = null;
			return null;

Y
yamahigashi 已提交
732
		}
Y
yamahigashi 已提交
733

734
	} );
Y
yamahigashi 已提交
735

Y
yamahigashi 已提交
736
	function FBXParser() {}
Y
yamahigashi 已提交
737

738
	Object.assign( FBXParser.prototype, {
Y
yamahigashi 已提交
739

Y
yamahigashi 已提交
740
		getPrevNode: function () {
Y
yamahigashi 已提交
741

Y
yamahigashi 已提交
742
			return this.nodeStack[ this.currentIndent - 2 ];
Y
yamahigashi 已提交
743

Y
yamahigashi 已提交
744
		},
Y
yamahigashi 已提交
745

Y
yamahigashi 已提交
746
		getCurrentNode: function () {
Y
yamahigashi 已提交
747

Y
yamahigashi 已提交
748
			return this.nodeStack[ this.currentIndent - 1 ];
Y
yamahigashi 已提交
749

Y
yamahigashi 已提交
750
		},
Y
yamahigashi 已提交
751

Y
yamahigashi 已提交
752
		getCurrentProp: function () {
Y
yamahigashi 已提交
753

Y
yamahigashi 已提交
754
			return this.currentProp;
Y
yamahigashi 已提交
755

Y
yamahigashi 已提交
756
		},
Y
yamahigashi 已提交
757

Y
yamahigashi 已提交
758
		pushStack: function ( node ) {
Y
yamahigashi 已提交
759

Y
yamahigashi 已提交
760 761
			this.nodeStack.push( node );
			this.currentIndent += 1;
Y
yamahigashi 已提交
762

Y
yamahigashi 已提交
763
		},
Y
yamahigashi 已提交
764

Y
yamahigashi 已提交
765
		popStack: function () {
Y
yamahigashi 已提交
766

Y
yamahigashi 已提交
767 768
			this.nodeStack.pop();
			this.currentIndent -= 1;
Y
yamahigashi 已提交
769

Y
yamahigashi 已提交
770
		},
Y
yamahigashi 已提交
771

Y
yamahigashi 已提交
772
		setCurrentProp: function ( val, name ) {
Y
yamahigashi 已提交
773

Y
yamahigashi 已提交
774 775
			this.currentProp = val;
			this.currentPropName = name;
Y
yamahigashi 已提交
776

Y
yamahigashi 已提交
777
		},
Y
yamahigashi 已提交
778

Y
yamahigashi 已提交
779 780
		// ----------parse ---------------------------------------------------
		parse: function ( text ) {
Y
yamahigashi 已提交
781

Y
yamahigashi 已提交
782 783 784 785 786
			this.currentIndent = 0;
			this.allNodes = new FBXNodes();
			this.nodeStack = [];
			this.currentProp = [];
			this.currentPropName = '';
Y
yamahigashi 已提交
787

Y
yamahigashi 已提交
788 789
			var split = text.split( "\n" );
			for ( var line in split ) {
Y
yamahigashi 已提交
790

Y
yamahigashi 已提交
791
				var l = split[ line ];
Y
yamahigashi 已提交
792

Y
yamahigashi 已提交
793 794
				// short cut
				if ( l.match( /^[\s\t]*;/ ) ) {
Y
yamahigashi 已提交
795

Y
yamahigashi 已提交
796
					continue;
Y
yamahigashi 已提交
797

Y
yamahigashi 已提交
798 799
				} // skip comment line
				if ( l.match( /^[\s\t]*$/ ) ) {
Y
yamahigashi 已提交
800

Y
yamahigashi 已提交
801
					continue;
Y
yamahigashi 已提交
802

Y
yamahigashi 已提交
803
				} // skip empty line
Y
yamahigashi 已提交
804

Y
yamahigashi 已提交
805 806
				// beginning of node
				var beginningOfNodeExp = new RegExp( "^\\t{" + this.currentIndent + "}(\\w+):(.*){", '' );
K
Kyle Larson 已提交
807
				var match = l.match( beginningOfNodeExp );
Y
yamahigashi 已提交
808
				if ( match ) {
Y
yamahigashi 已提交
809

Y
yamahigashi 已提交
810 811
					var nodeName = match[ 1 ].trim().replace( /^"/, '' ).replace( /"$/, "" );
					var nodeAttrs = match[ 2 ].split( ',' ).map( function ( element ) {
Y
yamahigashi 已提交
812

Y
yamahigashi 已提交
813
						return element.trim().replace( /^"/, '' ).replace( /"$/, '' );
Y
yamahigashi 已提交
814

Y
yamahigashi 已提交
815
					} );
Y
yamahigashi 已提交
816

Y
yamahigashi 已提交
817 818
					this.parseNodeBegin( l, nodeName, nodeAttrs || null );
					continue;
Y
yamahigashi 已提交
819

Y
yamahigashi 已提交
820
				}
Y
yamahigashi 已提交
821

Y
yamahigashi 已提交
822 823
				// node's property
				var propExp = new RegExp( "^\\t{" + ( this.currentIndent ) + "}(\\w+):[\\s\\t\\r\\n](.*)" );
824
				var match = l.match( propExp );
Y
yamahigashi 已提交
825
				if ( match ) {
Y
yamahigashi 已提交
826

Y
yamahigashi 已提交
827 828
					var propName = match[ 1 ].replace( /^"/, '' ).replace( /"$/, "" ).trim();
					var propValue = match[ 2 ].replace( /^"/, '' ).replace( /"$/, "" ).trim();
Y
yamahigashi 已提交
829

Y
yamahigashi 已提交
830 831
					this.parseNodeProperty( l, propName, propValue );
					continue;
Y
yamahigashi 已提交
832

Y
yamahigashi 已提交
833
				}
Y
yamahigashi 已提交
834

Y
yamahigashi 已提交
835 836 837
				// end of node
				var endOfNodeExp = new RegExp( "^\\t{" + ( this.currentIndent - 1 ) + "}}" );
				if ( l.match( endOfNodeExp ) ) {
Y
yamahigashi 已提交
838

Y
yamahigashi 已提交
839 840
					this.nodeEnd();
					continue;
Y
yamahigashi 已提交
841

Y
yamahigashi 已提交
842
				}
Y
yamahigashi 已提交
843

Y
yamahigashi 已提交
844 845 846 847 848 849 850 851 852 853
				// for special case,
				//
				//	  Vertices: *8670 {
				//		  a: 0.0356229953467846,13.9599733352661,-0.399196773.....(snip)
				// -0.0612030513584614,13.960485458374,-0.409748703241348,-0.10.....
				// 0.12490539252758,13.7450733184814,-0.454119384288788,0.09272.....
				// 0.0836158767342567,13.5432004928589,-0.435397416353226,0.028.....
				//
				// these case the lines must contiue with previous line
				if ( l.match( /^[^\s\t}]/ ) ) {
Y
yamahigashi 已提交
854

Y
yamahigashi 已提交
855
					this.parseNodePropertyContinued( l );
Y
yamahigashi 已提交
856

Y
yamahigashi 已提交
857
				}
Y
yamahigashi 已提交
858

Y
yamahigashi 已提交
859
			}
Y
yamahigashi 已提交
860

Y
yamahigashi 已提交
861
			return this.allNodes;
Y
yamahigashi 已提交
862

Y
yamahigashi 已提交
863
		},
Y
yamahigashi 已提交
864

Y
yamahigashi 已提交
865
		parseNodeBegin: function ( line, nodeName, nodeAttrs ) {
Y
yamahigashi 已提交
866

Y
yamahigashi 已提交
867 868 869 870
			// var nodeName = match[1];
			var node = { 'name': nodeName, properties: {}, 'subNodes': {} };
			var attrs = this.parseNodeAttr( nodeAttrs );
			var currentNode = this.getCurrentNode();
Y
yamahigashi 已提交
871

Y
yamahigashi 已提交
872 873
			// a top node
			if ( this.currentIndent === 0 ) {
Y
yamahigashi 已提交
874

Y
yamahigashi 已提交
875
				this.allNodes.add( nodeName, node );
Y
yamahigashi 已提交
876

Y
yamahigashi 已提交
877
			} else {
Y
yamahigashi 已提交
878

Y
yamahigashi 已提交
879
				// a subnode
Y
yamahigashi 已提交
880

Y
yamahigashi 已提交
881 882
				// already exists subnode, then append it
				if ( nodeName in currentNode.subNodes ) {
Y
yamahigashi 已提交
883

Y
yamahigashi 已提交
884
					var tmp = currentNode.subNodes[ nodeName ];
Y
yamahigashi 已提交
885

Y
yamahigashi 已提交
886 887
					// console.log( "duped entry found\nkey: " + nodeName + "\nvalue: " + propValue );
					if ( this.isFlattenNode( currentNode.subNodes[ nodeName ] ) ) {
Y
yamahigashi 已提交
888 889


Y
yamahigashi 已提交
890
						if ( attrs.id === '' ) {
Y
yamahigashi 已提交
891

Y
yamahigashi 已提交
892 893
							currentNode.subNodes[ nodeName ] = [];
							currentNode.subNodes[ nodeName ].push( tmp );
Y
yamahigashi 已提交
894

Y
yamahigashi 已提交
895
						} else {
Y
yamahigashi 已提交
896

Y
yamahigashi 已提交
897 898
							currentNode.subNodes[ nodeName ] = {};
							currentNode.subNodes[ nodeName ][ tmp.id ] = tmp;
Y
yamahigashi 已提交
899

Y
yamahigashi 已提交
900
						}
Y
yamahigashi 已提交
901

Y
yamahigashi 已提交
902
					}
Y
yamahigashi 已提交
903

Y
yamahigashi 已提交
904
					if ( attrs.id === '' ) {
Y
yamahigashi 已提交
905

Y
yamahigashi 已提交
906
						currentNode.subNodes[ nodeName ].push( node );
Y
yamahigashi 已提交
907

Y
yamahigashi 已提交
908
					} else {
Y
yamahigashi 已提交
909

Y
yamahigashi 已提交
910
						currentNode.subNodes[ nodeName ][ attrs.id ] = node;
Y
yamahigashi 已提交
911

Y
yamahigashi 已提交
912
					}
Y
yamahigashi 已提交
913

914 915 916 917 918
				} else if ( typeof attrs.id === 'number' || attrs.id.match( /^\d+$/ ) ) {

					currentNode.subNodes[ nodeName ] = {};
					currentNode.subNodes[ nodeName ][ attrs.id ] = node;

Y
yamahigashi 已提交
919
				} else {
Y
yamahigashi 已提交
920

Y
yamahigashi 已提交
921
					currentNode.subNodes[ nodeName ] = node;
Y
yamahigashi 已提交
922

Y
yamahigashi 已提交
923
				}
Y
yamahigashi 已提交
924

Y
yamahigashi 已提交
925
			}
Y
yamahigashi 已提交
926

Y
yamahigashi 已提交
927 928 929
			// for this		  ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
			// NodeAttribute: 1001463072, "NodeAttribute::", "LimbNode" {
			if ( nodeAttrs ) {
Y
yamahigashi 已提交
930

Y
yamahigashi 已提交
931 932 933
				node.id = attrs.id;
				node.attrName = attrs.name;
				node.attrType = attrs.type;
Y
yamahigashi 已提交
934

Y
yamahigashi 已提交
935
			}
Y
yamahigashi 已提交
936

Y
yamahigashi 已提交
937
			this.pushStack( node );
Y
yamahigashi 已提交
938

Y
yamahigashi 已提交
939
		},
Y
yamahigashi 已提交
940

Y
yamahigashi 已提交
941
		parseNodeAttr: function ( attrs ) {
Y
yamahigashi 已提交
942

Y
yamahigashi 已提交
943
			var id = attrs[ 0 ];
Y
yamahigashi 已提交
944

Y
yamahigashi 已提交
945
			if ( attrs[ 0 ] !== "" ) {
Y
yamahigashi 已提交
946

Y
yamahigashi 已提交
947
				id = parseInt( attrs[ 0 ] );
Y
yamahigashi 已提交
948

Y
yamahigashi 已提交
949
				if ( isNaN( id ) ) {
Y
yamahigashi 已提交
950

Y
yamahigashi 已提交
951 952
					// PolygonVertexIndex: *16380 {
					id = attrs[ 0 ];
Y
yamahigashi 已提交
953

Y
yamahigashi 已提交
954
				}
Y
yamahigashi 已提交
955

Y
yamahigashi 已提交
956
			}
Y
yamahigashi 已提交
957

Y
yamahigashi 已提交
958 959 960
			var name;
			var type;
			if ( attrs.length > 1 ) {
Y
yamahigashi 已提交
961

Y
yamahigashi 已提交
962 963
				name = attrs[ 1 ].replace( /^(\w+)::/, '' );
				type = attrs[ 2 ];
Y
yamahigashi 已提交
964

Y
yamahigashi 已提交
965
			}
Y
yamahigashi 已提交
966

Y
yamahigashi 已提交
967
			return { id: id, name: name || '', type: type || '' };
Y
yamahigashi 已提交
968

Y
yamahigashi 已提交
969
		},
Y
yamahigashi 已提交
970

Y
yamahigashi 已提交
971
		parseNodeProperty: function ( line, propName, propValue ) {
Y
yamahigashi 已提交
972

Y
yamahigashi 已提交
973 974
			var currentNode = this.getCurrentNode();
			var parentName = currentNode.name;
Y
yamahigashi 已提交
975

Y
yamahigashi 已提交
976 977 978
			// special case parent node's is like "Properties70"
			// these chilren nodes must treat with careful
			if ( parentName !== undefined ) {
Y
yamahigashi 已提交
979

Y
yamahigashi 已提交
980 981
				var propMatch = parentName.match( /Properties(\d)+/ );
				if ( propMatch ) {
Y
yamahigashi 已提交
982

Y
yamahigashi 已提交
983 984
					this.parseNodeSpecialProperty( line, propName, propValue );
					return;
Y
yamahigashi 已提交
985

Y
yamahigashi 已提交
986
				}
Y
yamahigashi 已提交
987

Y
yamahigashi 已提交
988
			}
Y
yamahigashi 已提交
989

Y
yamahigashi 已提交
990 991
			// special case Connections
			if ( propName == 'C' ) {
Y
yamahigashi 已提交
992

Y
yamahigashi 已提交
993 994 995
				var connProps = propValue.split( ',' ).slice( 1 );
				var from = parseInt( connProps[ 0 ] );
				var to = parseInt( connProps[ 1 ] );
Y
yamahigashi 已提交
996

Y
yamahigashi 已提交
997
				var rest = propValue.split( ',' ).slice( 3 );
Y
yamahigashi 已提交
998

Y
yamahigashi 已提交
999 1000 1001
				propName = 'connections';
				propValue = [ from, to ];
				propValue = propValue.concat( rest );
Y
yamahigashi 已提交
1002

Y
yamahigashi 已提交
1003
				if ( currentNode.properties[ propName ] === undefined ) {
Y
yamahigashi 已提交
1004

Y
yamahigashi 已提交
1005
					currentNode.properties[ propName ] = [];
Y
yamahigashi 已提交
1006

Y
yamahigashi 已提交
1007
				}
Y
yamahigashi 已提交
1008

Y
yamahigashi 已提交
1009
			}
Y
yamahigashi 已提交
1010

Y
yamahigashi 已提交
1011 1012
			// special case Connections
			if ( propName == 'Node' ) {
Y
yamahigashi 已提交
1013

Y
yamahigashi 已提交
1014 1015 1016
				var id = parseInt( propValue );
				currentNode.properties.id = id;
				currentNode.id = id;
Y
yamahigashi 已提交
1017

Y
yamahigashi 已提交
1018
			}
Y
yamahigashi 已提交
1019

Y
yamahigashi 已提交
1020 1021
			// already exists in properties, then append this
			if ( propName in currentNode.properties ) {
Y
yamahigashi 已提交
1022

Y
yamahigashi 已提交
1023 1024
				// console.log( "duped entry found\nkey: " + propName + "\nvalue: " + propValue );
				if ( Array.isArray( currentNode.properties[ propName ] ) ) {
Y
yamahigashi 已提交
1025

Y
yamahigashi 已提交
1026
					currentNode.properties[ propName ].push( propValue );
Y
yamahigashi 已提交
1027

Y
yamahigashi 已提交
1028
				} else {
Y
yamahigashi 已提交
1029

Y
yamahigashi 已提交
1030
					currentNode.properties[ propName ] += propValue;
Y
yamahigashi 已提交
1031

Y
yamahigashi 已提交
1032
				}
Y
yamahigashi 已提交
1033

Y
yamahigashi 已提交
1034
			} else {
Y
yamahigashi 已提交
1035

Y
yamahigashi 已提交
1036 1037
				// console.log( propName + ":  " + propValue );
				if ( Array.isArray( currentNode.properties[ propName ] ) ) {
Y
yamahigashi 已提交
1038

Y
yamahigashi 已提交
1039
					currentNode.properties[ propName ].push( propValue );
Y
yamahigashi 已提交
1040

Y
yamahigashi 已提交
1041
				} else {
Y
yamahigashi 已提交
1042

Y
yamahigashi 已提交
1043
					currentNode.properties[ propName ] = propValue;
Y
yamahigashi 已提交
1044

Y
yamahigashi 已提交
1045
				}
Y
yamahigashi 已提交
1046

Y
yamahigashi 已提交
1047
			}
Y
yamahigashi 已提交
1048

Y
yamahigashi 已提交
1049
			this.setCurrentProp( currentNode.properties, propName );
Y
yamahigashi 已提交
1050

Y
yamahigashi 已提交
1051
		},
Y
yamahigashi 已提交
1052

Y
yamahigashi 已提交
1053 1054
		// TODO:
		parseNodePropertyContinued: function ( line ) {
Y
yamahigashi 已提交
1055

Y
yamahigashi 已提交
1056
			this.currentProp[ this.currentPropName ] += line;
Y
yamahigashi 已提交
1057

Y
yamahigashi 已提交
1058
		},
Y
yamahigashi 已提交
1059

Y
yamahigashi 已提交
1060
		parseNodeSpecialProperty: function ( line, propName, propValue ) {
Y
yamahigashi 已提交
1061

Y
yamahigashi 已提交
1062 1063 1064 1065 1066
			// split this
			// P: "Lcl Scaling", "Lcl Scaling", "", "A",1,1,1
			// into array like below
			// ["Lcl Scaling", "Lcl Scaling", "", "A", "1,1,1" ]
			var props = propValue.split( '",' ).map( function ( element ) {
Y
yamahigashi 已提交
1067

Y
yamahigashi 已提交
1068
				return element.trim().replace( /^\"/, '' ).replace( /\s/, '_' );
Y
yamahigashi 已提交
1069

Y
yamahigashi 已提交
1070
			} );
Y
yamahigashi 已提交
1071

Y
yamahigashi 已提交
1072 1073 1074 1075 1076
			var innerPropName = props[ 0 ];
			var innerPropType1 = props[ 1 ];
			var innerPropType2 = props[ 2 ];
			var innerPropFlag = props[ 3 ];
			var innerPropValue = props[ 4 ];
Y
yamahigashi 已提交
1077

Y
yamahigashi 已提交
1078 1079 1080 1081 1082
			/*
			if ( innerPropValue === undefined ) {
				innerPropValue = props[3];
			}
			*/
Y
yamahigashi 已提交
1083

Y
yamahigashi 已提交
1084 1085
			// cast value in its type
			switch ( innerPropType1 ) {
Y
yamahigashi 已提交
1086

Y
yamahigashi 已提交
1087 1088 1089
				case "int":
					innerPropValue = parseInt( innerPropValue );
					break;
Y
yamahigashi 已提交
1090

Y
yamahigashi 已提交
1091 1092 1093
				case "double":
					innerPropValue = parseFloat( innerPropValue );
					break;
Y
yamahigashi 已提交
1094

Y
yamahigashi 已提交
1095 1096 1097 1098 1099
				case "ColorRGB":
				case "Vector3D":
					var tmp = innerPropValue.split( ',' );
					innerPropValue = new THREE.Vector3( tmp[ 0 ], tmp[ 1 ], tmp[ 2 ] );
					break;
Y
yamahigashi 已提交
1100

Y
yamahigashi 已提交
1101
			}
Y
yamahigashi 已提交
1102

Y
yamahigashi 已提交
1103 1104
			// CAUTION: these props must append to parent's parent
			this.getPrevNode().properties[ innerPropName ] = {
Y
yamahigashi 已提交
1105

Y
yamahigashi 已提交
1106 1107 1108 1109
				'type': innerPropType1,
				'type2': innerPropType2,
				'flag': innerPropFlag,
				'value': innerPropValue
Y
yamahigashi 已提交
1110

Y
yamahigashi 已提交
1111
			};
Y
yamahigashi 已提交
1112

Y
yamahigashi 已提交
1113
			this.setCurrentProp( this.getPrevNode().properties, innerPropName );
Y
yamahigashi 已提交
1114

Y
yamahigashi 已提交
1115
		},
Y
yamahigashi 已提交
1116

1117
		nodeEnd: function () {
Y
yamahigashi 已提交
1118

Y
yamahigashi 已提交
1119
			this.popStack();
Y
yamahigashi 已提交
1120

Y
yamahigashi 已提交
1121
		},
Y
yamahigashi 已提交
1122

Y
yamahigashi 已提交
1123 1124 1125
		/* ---------------------------------------------------------------- */
		/*		util													  */
		isFlattenNode: function ( node ) {
Y
yamahigashi 已提交
1126

Y
yamahigashi 已提交
1127
			return ( 'subNodes' in node && 'properties' in node ) ? true : false;
Y
yamahigashi 已提交
1128

Y
yamahigashi 已提交
1129
		}
Y
yamahigashi 已提交
1130

1131
	} );
Y
yamahigashi 已提交
1132 1133


Y
yamahigashi 已提交
1134 1135 1136 1137 1138
	// generate skinIndices, skinWeights
	//	  @skinIndices: per vertex data, this represents the bone indexes affects that vertex
	//	  @skinWeights: per vertex data, this represents the Weight Values affects that vertex
	//	  @matrices:	per `bones` data
	function Weights() {
Y
yamahigashi 已提交
1139

Y
yamahigashi 已提交
1140 1141
		this.skinIndices = [];
		this.skinWeights = [];
Y
yamahigashi 已提交
1142

Y
yamahigashi 已提交
1143
		this.matrices	= [];
Y
yamahigashi 已提交
1144

Y
yamahigashi 已提交
1145
	}
Y
yamahigashi 已提交
1146 1147


Y
yamahigashi 已提交
1148
	Weights.prototype.parseCluster = function ( node, id, entry ) {
Y
yamahigashi 已提交
1149

Y
yamahigashi 已提交
1150
		var _p = node.searchConnectionParent( id );
1151 1152 1153 1154
		var _indices = parseArrayToInt( entry.subNodes.Indexes.properties.a );
		var _weights = parseArrayToFloat( entry.subNodes.Weights.properties.a );
		var _transform = parseArrayToMatrix( entry.subNodes.Transform.properties.a );
		var _link = parseArrayToMatrix( entry.subNodes.TransformLink.properties.a );
Y
yamahigashi 已提交
1155

Y
yamahigashi 已提交
1156
		return {
Y
yamahigashi 已提交
1157

Y
yamahigashi 已提交
1158 1159 1160 1161 1162 1163 1164
			'parent': _p,
			'id': parseInt( id ),
			'indices': _indices,
			'weights': _weights,
			'transform': _transform,
			'transformlink': _link,
			'linkMode': entry.properties.Mode
Y
yamahigashi 已提交
1165

Y
yamahigashi 已提交
1166
		};
Y
yamahigashi 已提交
1167

Y
yamahigashi 已提交
1168
	};
Y
yamahigashi 已提交
1169

Y
yamahigashi 已提交
1170
	Weights.prototype.parse = function ( node, bones ) {
Y
yamahigashi 已提交
1171

Y
yamahigashi 已提交
1172 1173
		this.skinIndices = [];
		this.skinWeights = [];
Y
yamahigashi 已提交
1174

Y
yamahigashi 已提交
1175
		this.matrices = [];
Y
yamahigashi 已提交
1176

Y
yamahigashi 已提交
1177
		var deformers = node.Objects.subNodes.Deformer;
Y
yamahigashi 已提交
1178

Y
yamahigashi 已提交
1179 1180
		var clusters = {};
		for ( var id in deformers ) {
Y
yamahigashi 已提交
1181

Y
yamahigashi 已提交
1182
			if ( deformers[ id ].attrType === 'Cluster' ) {
Y
yamahigashi 已提交
1183

Y
yamahigashi 已提交
1184
				if ( ! ( 'Indexes' in deformers[ id ].subNodes ) ) {
Y
yamahigashi 已提交
1185

Y
yamahigashi 已提交
1186
					continue;
Y
yamahigashi 已提交
1187

Y
yamahigashi 已提交
1188
				}
Y
yamahigashi 已提交
1189

Y
yamahigashi 已提交
1190 1191 1192 1193
				//clusters.push( this.parseCluster( node, id, deformers[id] ) );
				var cluster = this.parseCluster( node, id, deformers[ id ] );
				var boneId = node.searchConnectionChildren( cluster.id )[ 0 ];
				clusters[ boneId ] = cluster;
Y
yamahigashi 已提交
1194

Y
yamahigashi 已提交
1195
			}
Y
yamahigashi 已提交
1196

Y
yamahigashi 已提交
1197
		}
Y
yamahigashi 已提交
1198 1199


Y
yamahigashi 已提交
1200 1201 1202 1203
		// this clusters is per Bone data, thus we make this into per vertex data
		var weights = [];
		var hi = bones.hierarchy;
		for ( var b = 0; b < hi.length; ++ b ) {
Y
yamahigashi 已提交
1204

Y
yamahigashi 已提交
1205 1206
			var bid = hi[ b ].internalId;
			if ( clusters[ bid ] === undefined ) {
Y
yamahigashi 已提交
1207

Y
yamahigashi 已提交
1208 1209 1210
				//console.log( bid );
				this.matrices.push( new THREE.Matrix4() );
				continue;
Y
yamahigashi 已提交
1211

Y
yamahigashi 已提交
1212
			}
Y
yamahigashi 已提交
1213

Y
yamahigashi 已提交
1214 1215 1216 1217 1218
			var clst = clusters[ bid ];
			// store transform matrix per bones
			this.matrices.push( clst.transform );
			//this.matrices.push( clst.transformlink );
			for ( var v = 0; v < clst.indices.length; ++ v ) {
Y
yamahigashi 已提交
1219

Y
yamahigashi 已提交
1220
				if ( weights[ clst.indices[ v ] ] === undefined ) {
Y
yamahigashi 已提交
1221

Y
yamahigashi 已提交
1222 1223 1224
					weights[ clst.indices[ v ] ] = {};
					weights[ clst.indices[ v ] ].joint = [];
					weights[ clst.indices[ v ] ].weight = [];
Y
yamahigashi 已提交
1225

Y
yamahigashi 已提交
1226
				}
Y
yamahigashi 已提交
1227

Y
yamahigashi 已提交
1228 1229
				// indices
				var affect = node.searchConnectionChildren( clst.id );
Y
yamahigashi 已提交
1230

Y
yamahigashi 已提交
1231
				if ( affect.length > 1 ) {
Y
yamahigashi 已提交
1232

Y
yamahigashi 已提交
1233
					console.warn( "FBXLoader: node " + clst.id + " have many weight kids: " + affect );
Y
yamahigashi 已提交
1234

Y
yamahigashi 已提交
1235 1236
				}
				weights[ clst.indices[ v ] ].joint.push( bones.getBoneIdfromInternalId( node, affect[ 0 ] ) );
Y
yamahigashi 已提交
1237

Y
yamahigashi 已提交
1238 1239
				// weight value
				weights[ clst.indices[ v ] ].weight.push( clst.weights[ v ] );
Y
yamahigashi 已提交
1240

Y
yamahigashi 已提交
1241
			}
Y
yamahigashi 已提交
1242

Y
yamahigashi 已提交
1243
		}
Y
yamahigashi 已提交
1244

Y
yamahigashi 已提交
1245 1246 1247
		// normalize the skin weights
		// TODO -  this might be a good place to choose greatest 4 weights
		for ( var i = 0; i < weights.length; i ++ ) {
Y
yamahigashi 已提交
1248

1249 1250 1251 1252 1253 1254 1255 1256
			if ( weights[ i ] === undefined ) {

				this.skinIndices.push( new THREE.Vector4( 0, 0, 0, 0 ) );
				this.skinWeights.push( new THREE.Vector4( 0, 0, 0, 0 ) );
				continue;

			}

Y
yamahigashi 已提交
1257 1258 1259 1260 1261
			var indicies = new THREE.Vector4(
				weights[ i ].joint[ 0 ] ? weights[ i ].joint[ 0 ] : 0,
				weights[ i ].joint[ 1 ] ? weights[ i ].joint[ 1 ] : 0,
				weights[ i ].joint[ 2 ] ? weights[ i ].joint[ 2 ] : 0,
				weights[ i ].joint[ 3 ] ? weights[ i ].joint[ 3 ] : 0 );
Y
yamahigashi 已提交
1262

Y
yamahigashi 已提交
1263 1264 1265 1266 1267
			var weight = new THREE.Vector4(
				weights[ i ].weight[ 0 ] ? weights[ i ].weight[ 0 ] : 0,
				weights[ i ].weight[ 1 ] ? weights[ i ].weight[ 1 ] : 0,
				weights[ i ].weight[ 2 ] ? weights[ i ].weight[ 2 ] : 0,
				weights[ i ].weight[ 3 ] ? weights[ i ].weight[ 3 ] : 0 );
Y
yamahigashi 已提交
1268

Y
yamahigashi 已提交
1269 1270
			this.skinIndices.push( indicies );
			this.skinWeights.push( weight );
Y
yamahigashi 已提交
1271

Y
yamahigashi 已提交
1272
		}
Y
yamahigashi 已提交
1273

Y
yamahigashi 已提交
1274 1275
		//console.log( this );
		return this;
Y
yamahigashi 已提交
1276

Y
yamahigashi 已提交
1277
	};
Y
yamahigashi 已提交
1278

Y
yamahigashi 已提交
1279
	function Bones() {
Y
yamahigashi 已提交
1280

Y
yamahigashi 已提交
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
		// returns bones hierarchy tree.
		//	  [
		//		  {
		//			  "parent": id,
		//			  "name": name,
		//			  "pos": pos,
		//			  "rotq": quat
		//		  },
		//		  ...
		//		  {},
		//		  ...
		//	  ]
		//
		/* sample response
Y
yamahigashi 已提交
1295

Y
yamahigashi 已提交
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
		   "bones" : [
			{"parent":-1, "name":"Fbx01",			"pos":[-0.002,	 98.739,   1.6e-05],	 "rotq":[0, 0, 0, 1]},
			{"parent":0,  "name":"Fbx01_Pelvis",	 "pos":[0.00015963, 0,		7.33107e-08], "rotq":[0, 0, 0, 1]},
			{"parent":1,  "name":"Fbx01_Spine",	  "pos":[6.577e-06,  10.216,   0.0106811],   "rotq":[0, 0, 0, 1]},
			{"parent":2,  "name":"Fbx01_R_Thigh",	"pos":[14.6537,	-10.216,  -0.00918758], "rotq":[0, 0, 0, 1]},
			{"parent":3,  "name":"Fbx01_R_Calf",	 "pos":[-3.70047,	 -42.9681,	 -7.78158],	 "rotq":[0, 0, 0, 1]},
			{"parent":4,  "name":"Fbx01_R_Foot",	 "pos":[-2.0696,	  -46.0488,	 9.42052],	  "rotq":[0, 0, 0, 1]},
			{"parent":5,  "name":"Fbx01_R_Toe0",	 "pos":[-0.0234785,   -9.46233,	 -15.3187],	 "rotq":[0, 0, 0, 1]},
			{"parent":2,  "name":"Fbx01_L_Thigh",	"pos":[-14.6537,	 -10.216,	  -0.00918314],  "rotq":[0, 0, 0, 1]},
			{"parent":7,  "name":"Fbx01_L_Calf",	 "pos":[3.70037,	  -42.968,	  -7.78155],	 "rotq":[0, 0, 0, 1]},
			{"parent":8,  "name":"Fbx01_L_Foot",	 "pos":[2.06954,	  -46.0488,	 9.42052],	  "rotq":[0, 0, 0, 1]},
			{"parent":9,  "name":"Fbx01_L_Toe0",	 "pos":[0.0234566,	-9.46235,	 -15.3187],	 "rotq":[0, 0, 0, 1]},
			{"parent":2,  "name":"Fbx01_Spine1",	 "pos":[-2.97523e-05, 11.5892,	  -9.81027e-05], "rotq":[0, 0, 0, 1]},
			{"parent":11, "name":"Fbx01_Spine2",	 "pos":[-2.91292e-05, 11.4685,	  8.27126e-05],  "rotq":[0, 0, 0, 1]},
			{"parent":12, "name":"Fbx01_Spine3",	 "pos":[-4.48857e-05, 11.5783,	  8.35108e-05],  "rotq":[0, 0, 0, 1]},
			{"parent":13, "name":"Fbx01_Neck",	   "pos":[1.22987e-05,  11.5582,	  -0.0044775],   "rotq":[0, 0, 0, 1]},
			{"parent":14, "name":"Fbx01_Head",	   "pos":[-3.50709e-05, 6.62915,	  -0.00523254],  "rotq":[0, 0, 0, 1]},
			{"parent":15, "name":"Fbx01_R_Eye",	  "pos":[3.31681,	  12.739,	   -10.5267],	 "rotq":[0, 0, 0, 1]},
			{"parent":15, "name":"Fbx01_L_Eye",	  "pos":[-3.32038,	 12.7391,	  -10.5267],	 "rotq":[0, 0, 0, 1]},
			{"parent":15, "name":"Jaw",			  "pos":[-0.0017738,   7.43481,	  -4.08114],	 "rotq":[0, 0, 0, 1]},
			{"parent":14, "name":"Fbx01_R_Clavicle", "pos":[3.10919,	  2.46577,	  -0.0115284],   "rotq":[0, 0, 0, 1]},
			{"parent":19, "name":"Fbx01_R_UpperArm", "pos":[16.014,	   4.57764e-05,  3.10405],	  "rotq":[0, 0, 0, 1]},
			{"parent":20, "name":"Fbx01_R_Forearm",  "pos":[22.7068,	  -1.66322,	 -2.13803],	 "rotq":[0, 0, 0, 1]},
			{"parent":21, "name":"Fbx01_R_Hand",	 "pos":[25.5881,	  -0.80249,	 -6.37307],	 "rotq":[0, 0, 0, 1]},
			...
			{"parent":27, "name":"Fbx01_R_Finger32", "pos":[2.15572,	  -0.548737,	-0.539604],	"rotq":[0, 0, 0, 1]},
			{"parent":22, "name":"Fbx01_R_Finger2",  "pos":[9.79318,	  0.132553,	 -2.97845],	 "rotq":[0, 0, 0, 1]},
			{"parent":29, "name":"Fbx01_R_Finger21", "pos":[2.74037,	  0.0483093,	-0.650531],	"rotq":[0, 0, 0, 1]},
			{"parent":55, "name":"Fbx01_L_Finger02", "pos":[-1.65308,	 -1.43208,	 -1.82885],	 "rotq":[0, 0, 0, 1]}
			]
		*/
		this.hierarchy = [];
Y
yamahigashi 已提交
1328

Y
yamahigashi 已提交
1329
	}
Y
yamahigashi 已提交
1330

Y
yamahigashi 已提交
1331
	Bones.prototype.parseHierarchy = function ( node ) {
Y
yamahigashi 已提交
1332

Y
yamahigashi 已提交
1333 1334
		var objects = node.Objects;
		var models = objects.subNodes.Model;
Y
yamahigashi 已提交
1335

Y
yamahigashi 已提交
1336 1337
		var bones = [];
		for ( var id in models ) {
Y
yamahigashi 已提交
1338

Y
yamahigashi 已提交
1339
			if ( models[ id ].attrType === undefined ) {
Y
yamahigashi 已提交
1340

Y
yamahigashi 已提交
1341
				continue;
Y
yamahigashi 已提交
1342

Y
yamahigashi 已提交
1343 1344
			}
			bones.push( models[ id ] );
Y
yamahigashi 已提交
1345

Y
yamahigashi 已提交
1346
		}
Y
yamahigashi 已提交
1347

Y
yamahigashi 已提交
1348 1349
		this.hierarchy = [];
		for ( var i = 0; i < bones.length; ++ i ) {
Y
yamahigashi 已提交
1350

Y
yamahigashi 已提交
1351
			var bone = bones[ i ];
Y
yamahigashi 已提交
1352

Y
yamahigashi 已提交
1353 1354 1355 1356
			var p = node.searchConnectionParent( bone.id )[ 0 ];
			var t = [ 0.0, 0.0, 0.0 ];
			var r = [ 0.0, 0.0, 0.0, 1.0 ];
			var s = [ 1.0, 1.0, 1.0 ];
Y
yamahigashi 已提交
1357

Y
yamahigashi 已提交
1358
			if ( 'Lcl_Translation' in bone.properties ) {
Y
yamahigashi 已提交
1359

1360
				t = parseArrayToFloat( bone.properties.Lcl_Translation.value );
Y
yamahigashi 已提交
1361

Y
yamahigashi 已提交
1362
			}
Y
yamahigashi 已提交
1363

Y
yamahigashi 已提交
1364
			if ( 'Lcl_Rotation' in bone.properties ) {
Y
yamahigashi 已提交
1365

1366
				r = parseArrayToRadians( bone.properties.Lcl_Rotation.value );
Y
yamahigashi 已提交
1367 1368 1369
				var q = new THREE.Quaternion();
				q.setFromEuler( new THREE.Euler( r[ 0 ], r[ 1 ], r[ 2 ], 'ZYX' ) );
				r = [ q.x, q.y, q.z, q.w ];
Y
yamahigashi 已提交
1370

Y
yamahigashi 已提交
1371
			}
Y
yamahigashi 已提交
1372

Y
yamahigashi 已提交
1373
			if ( 'Lcl_Scaling' in bone.properties ) {
Y
yamahigashi 已提交
1374

1375
				s = parseArrayToFloat( bone.properties.Lcl_Scaling.value );
Y
yamahigashi 已提交
1376

Y
yamahigashi 已提交
1377
			}
Y
yamahigashi 已提交
1378

Y
yamahigashi 已提交
1379 1380 1381 1382 1383 1384
			// replace unsafe character
			var name = bone.attrName;
			name = name.replace( /:/, '' );
			name = name.replace( /_/, '' );
			name = name.replace( /-/, '' );
			this.hierarchy.push( { "parent": p, "name": name, "pos": t, "rotq": r, "scl": s, "internalId": bone.id } );
Y
yamahigashi 已提交
1385

Y
yamahigashi 已提交
1386
		}
Y
yamahigashi 已提交
1387

Y
yamahigashi 已提交
1388
		this.reindexParentId();
Y
yamahigashi 已提交
1389

Y
yamahigashi 已提交
1390
		this.restoreBindPose( node );
Y
yamahigashi 已提交
1391

Y
yamahigashi 已提交
1392
		return this;
Y
yamahigashi 已提交
1393

Y
yamahigashi 已提交
1394
	};
Y
yamahigashi 已提交
1395

Y
yamahigashi 已提交
1396
	Bones.prototype.reindexParentId = function () {
Y
yamahigashi 已提交
1397

Y
yamahigashi 已提交
1398
		for ( var h = 0; h < this.hierarchy.length; h ++ ) {
Y
yamahigashi 已提交
1399

Y
yamahigashi 已提交
1400
			for ( var ii = 0; ii < this.hierarchy.length; ++ ii ) {
Y
yamahigashi 已提交
1401

Y
yamahigashi 已提交
1402
				if ( this.hierarchy[ h ].parent == this.hierarchy[ ii ].internalId ) {
Y
yamahigashi 已提交
1403

Y
yamahigashi 已提交
1404 1405
					this.hierarchy[ h ].parent = ii;
					break;
Y
yamahigashi 已提交
1406

Y
yamahigashi 已提交
1407
				}
Y
yamahigashi 已提交
1408

Y
yamahigashi 已提交
1409
			}
Y
yamahigashi 已提交
1410

Y
yamahigashi 已提交
1411
		}
Y
yamahigashi 已提交
1412

Y
yamahigashi 已提交
1413
	};
Y
yamahigashi 已提交
1414

Y
yamahigashi 已提交
1415
	Bones.prototype.restoreBindPose = function ( node ) {
Y
yamahigashi 已提交
1416

Y
yamahigashi 已提交
1417 1418
		var bindPoseNode = node.Objects.subNodes.Pose;
		if ( bindPoseNode === undefined ) {
Y
yamahigashi 已提交
1419

Y
yamahigashi 已提交
1420
			return;
Y
yamahigashi 已提交
1421

Y
yamahigashi 已提交
1422
		}
Y
yamahigashi 已提交
1423

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
		for ( var key in bindPoseNode ) {

			if ( bindPoseNode[ key ].attrType === 'BindPose' ) {

				bindPoseNode = bindPoseNode[ key ];
				break;

			}

		}

Y
yamahigashi 已提交
1435 1436 1437
		var poseNode = bindPoseNode.subNodes.PoseNode;
		var localMatrices = {}; // store local matrices, modified later( initialy world space )
		var worldMatrices = {}; // store world matrices
Y
yamahigashi 已提交
1438

Y
yamahigashi 已提交
1439
		for ( var i = 0; i < poseNode.length; ++ i ) {
Y
yamahigashi 已提交
1440

1441 1442
			var rawMatLcl = parseArrayToMatrix( poseNode[ i ].subNodes.Matrix.properties.a );
			var rawMatWrd = parseArrayToMatrix( poseNode[ i ].subNodes.Matrix.properties.a );
Y
yamahigashi 已提交
1443

Y
yamahigashi 已提交
1444 1445
			localMatrices[ poseNode[ i ].id ] = rawMatLcl;
			worldMatrices[ poseNode[ i ].id ] = rawMatWrd;
Y
yamahigashi 已提交
1446

Y
yamahigashi 已提交
1447
		}
Y
yamahigashi 已提交
1448

Y
yamahigashi 已提交
1449
		for ( var h = 0; h < this.hierarchy.length; ++ h ) {
Y
yamahigashi 已提交
1450

Y
yamahigashi 已提交
1451 1452
			var bone = this.hierarchy[ h ];
			var inId = bone.internalId;
Y
yamahigashi 已提交
1453

Y
yamahigashi 已提交
1454
			if ( worldMatrices[ inId ] === undefined ) {
Y
yamahigashi 已提交
1455

Y
yamahigashi 已提交
1456 1457 1458
				// has no bind pose node, possibly be mesh
				// console.log( bone );
				continue;
Y
yamahigashi 已提交
1459

Y
yamahigashi 已提交
1460
			}
Y
yamahigashi 已提交
1461

Y
yamahigashi 已提交
1462 1463 1464
			var t = new THREE.Vector3( 0, 0, 0 );
			var r = new THREE.Quaternion();
			var s = new THREE.Vector3( 1, 1, 1 );
Y
yamahigashi 已提交
1465

Y
yamahigashi 已提交
1466 1467 1468
			var parentId;
			var parentNodes = node.searchConnectionParent( inId );
			for ( var pn = 0; pn < parentNodes.length; ++ pn ) {
Y
yamahigashi 已提交
1469

Y
yamahigashi 已提交
1470
				if ( this.isBoneNode( parentNodes[ pn ] ) ) {
Y
yamahigashi 已提交
1471

Y
yamahigashi 已提交
1472 1473
					parentId = parentNodes[ pn ];
					break;
Y
yamahigashi 已提交
1474

Y
yamahigashi 已提交
1475
				}
Y
yamahigashi 已提交
1476

Y
yamahigashi 已提交
1477
			}
Y
yamahigashi 已提交
1478

Y
yamahigashi 已提交
1479
			if ( parentId !== undefined && localMatrices[ parentId ] !== undefined ) {
Y
yamahigashi 已提交
1480

Y
yamahigashi 已提交
1481 1482 1483 1484 1485
				// convert world space matrix into local space
				var inv = new THREE.Matrix4();
				inv.getInverse( worldMatrices[ parentId ] );
				inv.multiply( localMatrices[ inId ] );
				localMatrices[ inId ] = inv;
Y
yamahigashi 已提交
1486

Y
yamahigashi 已提交
1487 1488 1489
			} else {
				//console.log( bone );
			}
Y
yamahigashi 已提交
1490

Y
yamahigashi 已提交
1491 1492 1493 1494
			localMatrices[ inId ].decompose( t, r, s );
			bone.pos = [ t.x, t.y, t.z ];
			bone.rotq = [ r.x, r.y, r.z, r.w ];
			bone.scl = [ s.x, s.y, s.z ];
Y
yamahigashi 已提交
1495

Y
yamahigashi 已提交
1496
		}
Y
yamahigashi 已提交
1497

Y
yamahigashi 已提交
1498
	};
Y
yamahigashi 已提交
1499

Y
yamahigashi 已提交
1500
	Bones.prototype.searchRealId = function ( internalId ) {
Y
yamahigashi 已提交
1501

Y
yamahigashi 已提交
1502
		for ( var h = 0; h < this.hierarchy.length; h ++ ) {
Y
yamahigashi 已提交
1503

Y
yamahigashi 已提交
1504
			if ( internalId == this.hierarchy[ h ].internalId ) {
Y
yamahigashi 已提交
1505

Y
yamahigashi 已提交
1506
				return h;
Y
yamahigashi 已提交
1507

Y
yamahigashi 已提交
1508
			}
Y
yamahigashi 已提交
1509

Y
yamahigashi 已提交
1510
		}
Y
yamahigashi 已提交
1511

Y
yamahigashi 已提交
1512 1513
		// console.warn( 'FBXLoader: notfound internalId in bones: ' + internalId);
		return - 1;
Y
yamahigashi 已提交
1514

Y
yamahigashi 已提交
1515
	};
Y
yamahigashi 已提交
1516

Y
yamahigashi 已提交
1517
	Bones.prototype.getByInternalId = function ( internalId ) {
Y
yamahigashi 已提交
1518

Y
yamahigashi 已提交
1519
		for ( var h = 0; h < this.hierarchy.length; h ++ ) {
Y
yamahigashi 已提交
1520

Y
yamahigashi 已提交
1521
			if ( internalId == this.hierarchy[ h ].internalId ) {
Y
yamahigashi 已提交
1522

Y
yamahigashi 已提交
1523
				return this.hierarchy[ h ];
Y
yamahigashi 已提交
1524

Y
yamahigashi 已提交
1525
			}
Y
yamahigashi 已提交
1526

Y
yamahigashi 已提交
1527
		}
Y
yamahigashi 已提交
1528

Y
yamahigashi 已提交
1529
		return null;
Y
yamahigashi 已提交
1530

Y
yamahigashi 已提交
1531
	};
Y
yamahigashi 已提交
1532

Y
yamahigashi 已提交
1533
	Bones.prototype.isBoneNode = function ( id ) {
Y
yamahigashi 已提交
1534

Y
yamahigashi 已提交
1535
		for ( var i = 0; i < this.hierarchy.length; ++ i ) {
Y
yamahigashi 已提交
1536

Y
yamahigashi 已提交
1537
			if ( id === this.hierarchy[ i ].internalId ) {
Y
yamahigashi 已提交
1538

Y
yamahigashi 已提交
1539
				return true;
Y
yamahigashi 已提交
1540

Y
yamahigashi 已提交
1541
			}
Y
yamahigashi 已提交
1542

Y
yamahigashi 已提交
1543 1544
		}
		return false;
Y
yamahigashi 已提交
1545

Y
yamahigashi 已提交
1546
	};
Y
yamahigashi 已提交
1547

Y
yamahigashi 已提交
1548
	Bones.prototype.getBoneIdfromInternalId = function ( node, id ) {
Y
yamahigashi 已提交
1549

Y
yamahigashi 已提交
1550
		if ( node.__cache_get_boneid_from_internalid === undefined ) {
Y
yamahigashi 已提交
1551

Y
yamahigashi 已提交
1552
			node.__cache_get_boneid_from_internalid = [];
Y
yamahigashi 已提交
1553

Y
yamahigashi 已提交
1554
		}
Y
yamahigashi 已提交
1555

Y
yamahigashi 已提交
1556
		if ( node.__cache_get_boneid_from_internalid[ id ] !== undefined ) {
Y
yamahigashi 已提交
1557

Y
yamahigashi 已提交
1558
			return node.__cache_get_boneid_from_internalid[ id ];
Y
yamahigashi 已提交
1559

Y
yamahigashi 已提交
1560
		}
Y
yamahigashi 已提交
1561

Y
yamahigashi 已提交
1562
		for ( var i = 0; i < this.hierarchy.length; ++ i ) {
Y
yamahigashi 已提交
1563

Y
yamahigashi 已提交
1564
			if ( this.hierarchy[ i ].internalId == id ) {
Y
yamahigashi 已提交
1565

Y
yamahigashi 已提交
1566 1567
				node.__cache_get_boneid_from_internalid[ id ] = i;
				return i;
Y
yamahigashi 已提交
1568

Y
yamahigashi 已提交
1569
			}
Y
yamahigashi 已提交
1570

Y
yamahigashi 已提交
1571
		}
Y
yamahigashi 已提交
1572

Y
yamahigashi 已提交
1573 1574
		// console.warn( 'FBXLoader: bone internalId(' + id + ') not found in bone hierarchy' );
		return - 1;
Y
yamahigashi 已提交
1575

Y
yamahigashi 已提交
1576
	};
Y
yamahigashi 已提交
1577 1578


Y
yamahigashi 已提交
1579
	function Geometry() {
Y
yamahigashi 已提交
1580

Y
yamahigashi 已提交
1581 1582 1583
		this.node = null;
		this.name = null;
		this.id = null;
Y
yamahigashi 已提交
1584

Y
yamahigashi 已提交
1585 1586 1587 1588
		this.vertices = [];
		this.indices = [];
		this.normals = [];
		this.uvs = [];
Y
yamahigashi 已提交
1589

Y
yamahigashi 已提交
1590 1591
		this.bones = [];
		this.skins = null;
Y
yamahigashi 已提交
1592

Y
yamahigashi 已提交
1593
	}
Y
yamahigashi 已提交
1594

Y
yamahigashi 已提交
1595
	Geometry.prototype.parse = function ( geoNode ) {
Y
yamahigashi 已提交
1596

Y
yamahigashi 已提交
1597 1598 1599
		this.node = geoNode;
		this.name = geoNode.attrName;
		this.id = geoNode.id;
Y
yamahigashi 已提交
1600

Y
yamahigashi 已提交
1601
		this.vertices = this.getVertices();
Y
yamahigashi 已提交
1602

Y
yamahigashi 已提交
1603
		if ( this.vertices === undefined ) {
Y
yamahigashi 已提交
1604

Y
yamahigashi 已提交
1605 1606
			console.log( 'FBXLoader: Geometry.parse(): pass' + this.node.id );
			return;
Y
yamahigashi 已提交
1607

Y
yamahigashi 已提交
1608
		}
Y
yamahigashi 已提交
1609

Y
yamahigashi 已提交
1610 1611 1612
		this.indices = this.getPolygonVertexIndices();
		this.uvs = ( new UV() ).parse( this.node, this );
		this.normals = ( new Normal() ).parse( this.node, this );
Y
yamahigashi 已提交
1613

Y
yamahigashi 已提交
1614
		if ( this.getPolygonTopologyMax() > 3 ) {
Y
yamahigashi 已提交
1615

1616
			var indexInfo = this.convertPolyIndicesToTri(
Y
yamahigashi 已提交
1617
								this.indices, this.getPolygonTopologyArray() );
1618 1619
			this.indices = indexInfo.res;
			this.polyIndices = indexInfo.polyIndices;
Y
yamahigashi 已提交
1620

Y
yamahigashi 已提交
1621
		}
Y
yamahigashi 已提交
1622

Y
yamahigashi 已提交
1623
		return this;
Y
yamahigashi 已提交
1624

Y
yamahigashi 已提交
1625
	};
Y
yamahigashi 已提交
1626 1627


Y
yamahigashi 已提交
1628
	Geometry.prototype.getVertices = function () {
Y
yamahigashi 已提交
1629

Y
yamahigashi 已提交
1630
		if ( this.node.__cache_vertices ) {
Y
yamahigashi 已提交
1631

Y
yamahigashi 已提交
1632
			return this.node.__cache_vertices;
Y
yamahigashi 已提交
1633

Y
yamahigashi 已提交
1634
		}
Y
yamahigashi 已提交
1635

Y
yamahigashi 已提交
1636
		if ( this.node.subNodes.Vertices === undefined ) {
Y
yamahigashi 已提交
1637

Y
yamahigashi 已提交
1638 1639 1640
			console.warn( 'this.node: ' + this.node.attrName + "(" + this.node.id + ") does not have Vertices" );
			this.node.__cache_vertices = undefined;
			return null;
Y
yamahigashi 已提交
1641

Y
yamahigashi 已提交
1642
		}
Y
yamahigashi 已提交
1643

Y
yamahigashi 已提交
1644 1645
		var rawTextVert	= this.node.subNodes.Vertices.properties.a;
		var vertices = rawTextVert.split( ',' ).map( function ( element ) {
Y
yamahigashi 已提交
1646

Y
yamahigashi 已提交
1647
			return parseFloat( element );
Y
yamahigashi 已提交
1648

Y
yamahigashi 已提交
1649
		} );
Y
yamahigashi 已提交
1650

Y
yamahigashi 已提交
1651 1652
		this.node.__cache_vertices = vertices;
		return this.node.__cache_vertices;
Y
yamahigashi 已提交
1653

Y
yamahigashi 已提交
1654
	};
Y
yamahigashi 已提交
1655

Y
yamahigashi 已提交
1656
	Geometry.prototype.getPolygonVertexIndices = function () {
Y
yamahigashi 已提交
1657

Y
yamahigashi 已提交
1658
		if ( this.node.__cache_indices && this.node.__cache_poly_topology_max ) {
Y
yamahigashi 已提交
1659

Y
yamahigashi 已提交
1660
			return this.node.__cache_indices;
Y
yamahigashi 已提交
1661

Y
yamahigashi 已提交
1662
		}
Y
yamahigashi 已提交
1663

Y
yamahigashi 已提交
1664
		if ( this.node.subNodes === undefined ) {
Y
yamahigashi 已提交
1665

Y
yamahigashi 已提交
1666 1667 1668
			console.error( 'this.node.subNodes undefined' );
			console.log( this.node );
			return;
Y
yamahigashi 已提交
1669

Y
yamahigashi 已提交
1670
		}
Y
yamahigashi 已提交
1671

Y
yamahigashi 已提交
1672
		if ( this.node.subNodes.PolygonVertexIndex === undefined ) {
Y
yamahigashi 已提交
1673

Y
yamahigashi 已提交
1674 1675 1676
			console.warn( 'this.node: ' + this.node.attrName + "(" + this.node.id + ") does not have PolygonVertexIndex " );
			this.node.__cache_indices = undefined;
			return;
Y
yamahigashi 已提交
1677

Y
yamahigashi 已提交
1678
		}
Y
yamahigashi 已提交
1679

Y
yamahigashi 已提交
1680 1681
		var rawTextIndices = this.node.subNodes.PolygonVertexIndex.properties.a;
		var indices = rawTextIndices.split( ',' );
Y
yamahigashi 已提交
1682

Y
yamahigashi 已提交
1683 1684 1685
		var currentTopo = 1;
		var topologyN = null;
		var topologyArr = [];
Y
yamahigashi 已提交
1686

Y
yamahigashi 已提交
1687 1688 1689 1690
		// The indices that make up the polygon are in order and a negative index
		// means that it’s the last index of the polygon. That index needs
		// to be made positive and then you have to subtract 1 from it!
		for ( var i = 0; i < indices.length; ++ i ) {
Y
yamahigashi 已提交
1691

Y
yamahigashi 已提交
1692 1693 1694
			var tmpI = parseInt( indices[ i ] );
			// found n
			if ( tmpI < 0 ) {
Y
yamahigashi 已提交
1695

Y
yamahigashi 已提交
1696
				if ( currentTopo > topologyN ) {
Y
yamahigashi 已提交
1697

Y
yamahigashi 已提交
1698
					topologyN = currentTopo;
Y
yamahigashi 已提交
1699

Y
yamahigashi 已提交
1700
				}
Y
yamahigashi 已提交
1701

Y
yamahigashi 已提交
1702 1703 1704
				indices[ i ] = tmpI ^ - 1;
				topologyArr.push( currentTopo );
				currentTopo = 1;
Y
yamahigashi 已提交
1705

Y
yamahigashi 已提交
1706
			} else {
Y
yamahigashi 已提交
1707

Y
yamahigashi 已提交
1708 1709
				indices[ i ] = tmpI;
				currentTopo ++;
Y
yamahigashi 已提交
1710

Y
yamahigashi 已提交
1711
			}
Y
yamahigashi 已提交
1712

Y
yamahigashi 已提交
1713
		}
Y
yamahigashi 已提交
1714

Y
yamahigashi 已提交
1715
		if ( topologyN === null ) {
Y
yamahigashi 已提交
1716

Y
yamahigashi 已提交
1717 1718 1719
			console.warn( "FBXLoader: topology N not found: " + this.node.attrName );
			console.warn( this.node );
			topologyN = 3;
Y
yamahigashi 已提交
1720

Y
yamahigashi 已提交
1721
		}
Y
yamahigashi 已提交
1722

Y
yamahigashi 已提交
1723 1724 1725
		this.node.__cache_poly_topology_max = topologyN;
		this.node.__cache_poly_topology_arr = topologyArr;
		this.node.__cache_indices = indices;
Y
yamahigashi 已提交
1726

Y
yamahigashi 已提交
1727
		return this.node.__cache_indices;
Y
yamahigashi 已提交
1728

Y
yamahigashi 已提交
1729
	};
Y
yamahigashi 已提交
1730

Y
yamahigashi 已提交
1731
	Geometry.prototype.getPolygonTopologyMax = function () {
Y
yamahigashi 已提交
1732

Y
yamahigashi 已提交
1733
		if ( this.node.__cache_indices && this.node.__cache_poly_topology_max ) {
Y
yamahigashi 已提交
1734

Y
yamahigashi 已提交
1735
			return this.node.__cache_poly_topology_max;
Y
yamahigashi 已提交
1736

Y
yamahigashi 已提交
1737
		}
Y
yamahigashi 已提交
1738

Y
yamahigashi 已提交
1739 1740
		this.getPolygonVertexIndices( this.node );
		return this.node.__cache_poly_topology_max;
Y
yamahigashi 已提交
1741

Y
yamahigashi 已提交
1742
	};
Y
yamahigashi 已提交
1743

Y
yamahigashi 已提交
1744
	Geometry.prototype.getPolygonTopologyArray = function () {
Y
yamahigashi 已提交
1745

Y
yamahigashi 已提交
1746
		if ( this.node.__cache_indices && this.node.__cache_poly_topology_max ) {
Y
yamahigashi 已提交
1747

Y
yamahigashi 已提交
1748
			return this.node.__cache_poly_topology_arr;
Y
yamahigashi 已提交
1749

Y
yamahigashi 已提交
1750
		}
Y
yamahigashi 已提交
1751

Y
yamahigashi 已提交
1752 1753
		this.getPolygonVertexIndices( this.node );
		return this.node.__cache_poly_topology_arr;
Y
yamahigashi 已提交
1754

Y
yamahigashi 已提交
1755
	};
Y
yamahigashi 已提交
1756

Y
yamahigashi 已提交
1757 1758 1759 1760 1761 1762
	// a - d
	// |   |
	// b - c
	//
	// [( a, b, c, d ) ...........
	// [( a, b, c ), (a, c, d )....
1763 1764

	// Also keep track of original poly index.
Y
yamahigashi 已提交
1765
	Geometry.prototype.convertPolyIndicesToTri = function ( indices, strides ) {
Y
yamahigashi 已提交
1766

Y
yamahigashi 已提交
1767
		var res = [];
Y
yamahigashi 已提交
1768

Y
yamahigashi 已提交
1769 1770 1771
		var i = 0;
		var currentPolyNum = 0;
		var currentStride = 0;
1772
		var polyIndices = [];
Y
yamahigashi 已提交
1773

Y
yamahigashi 已提交
1774
		while ( i < indices.length ) {
Y
yamahigashi 已提交
1775

Y
yamahigashi 已提交
1776
			currentStride = strides[ currentPolyNum ];
Y
yamahigashi 已提交
1777

Y
yamahigashi 已提交
1778 1779
			// CAUTIN: NG over 6gon
			for ( var j = 0; j <= ( currentStride - 3 ); j ++ ) {
Y
yamahigashi 已提交
1780

Y
yamahigashi 已提交
1781 1782 1783
				res.push( indices[ i ] );
				res.push( indices[ i + ( currentStride - 2 - j ) ] );
				res.push( indices[ i + ( currentStride - 1 - j ) ] );
Y
yamahigashi 已提交
1784

K
Kyle Larson 已提交
1785 1786
				polyIndices.push( currentPolyNum );

Y
yamahigashi 已提交
1787
			}
Y
yamahigashi 已提交
1788

Y
yamahigashi 已提交
1789 1790
			currentPolyNum ++;
			i += currentStride;
Y
yamahigashi 已提交
1791

Y
yamahigashi 已提交
1792
		}
Y
yamahigashi 已提交
1793

K
Kyle Larson 已提交
1794
		return { res: res, polyIndices: polyIndices };
Y
yamahigashi 已提交
1795

Y
yamahigashi 已提交
1796
	};
Y
yamahigashi 已提交
1797

Y
yamahigashi 已提交
1798
	Geometry.prototype.addBones = function ( bones ) {
Y
yamahigashi 已提交
1799

Y
yamahigashi 已提交
1800
		this.bones = bones;
Y
yamahigashi 已提交
1801

Y
yamahigashi 已提交
1802
	};
Y
yamahigashi 已提交
1803 1804


Y
yamahigashi 已提交
1805
	function UV() {
Y
yamahigashi 已提交
1806

Y
yamahigashi 已提交
1807 1808 1809 1810 1811
		this.uv = null;
		this.map = null;
		this.ref = null;
		this.node = null;
		this.index = null;
Y
yamahigashi 已提交
1812

Y
yamahigashi 已提交
1813
	}
Y
yamahigashi 已提交
1814

Y
yamahigashi 已提交
1815
	UV.prototype.getUV = function ( node ) {
Y
yamahigashi 已提交
1816

Y
yamahigashi 已提交
1817
		if ( this.node && this.uv && this.map && this.ref ) {
Y
yamahigashi 已提交
1818

Y
yamahigashi 已提交
1819
			return this.uv;
Y
yamahigashi 已提交
1820

Y
yamahigashi 已提交
1821
		} else {
Y
yamahigashi 已提交
1822

Y
yamahigashi 已提交
1823
			return this._parseText( node );
Y
yamahigashi 已提交
1824

Y
yamahigashi 已提交
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
		}

	};

	UV.prototype.getMap = function ( node ) {

		if ( this.node && this.uv && this.map && this.ref ) {

			return this.map;

		} else {

			this._parseText( node );
			return this.map;

		}

	};

	UV.prototype.getRef = function ( node ) {

		if ( this.node && this.uv && this.map && this.ref ) {

			return this.ref;

		} else {

			this._parseText( node );
			return this.ref;

		}

	};

	UV.prototype.getIndex = function ( node ) {

		if ( this.node && this.uv && this.map && this.ref ) {

			return this.index;

		} else {

			this._parseText( node );
			return this.index;

		}

	};

	UV.prototype.getNode = function ( topnode ) {

		if ( this.node !== null ) {

			return this.node;

		}

		this.node = topnode.subNodes.LayerElementUV;
		return this.node;

	};

	UV.prototype._parseText = function ( node ) {

1889
		var uvNode = this.getNode( node )[ 0 ];
Y
yamahigashi 已提交
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
		if ( uvNode === undefined ) {

			// console.log( node.attrName + "(" + node.id + ")" + " has no LayerElementUV." );
			return [];

		}

		var count = 0;
		for ( var n in uvNode ) {

			if ( n.match( /^\d+$/ ) ) {

				count ++;

			}

		}

		if ( count > 0 ) {

			console.warn( 'multi uv not supported' );
			uvNode = uvNode[ n ];

		}

		var uvIndex = uvNode.subNodes.UVIndex.properties.a;
		var uvs = uvNode.subNodes.UV.properties.a;
		var uvMap = uvNode.properties.MappingInformationType;
		var uvRef = uvNode.properties.ReferenceInformationType;


1921 1922
		this.uv	= parseArrayToFloat( uvs );
		this.index = parseArrayToInt( uvIndex );
Y
yamahigashi 已提交
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932

		this.map = uvMap; // TODO: normalize notation shaking... FOR BLENDER
		this.ref = uvRef;

		return this.uv;

	};

	UV.prototype.parse = function ( node, geo ) {

1933 1934 1935 1936 1937 1938
		if ( ! ( 'LayerElementUV' in node.subNodes ) ) {

			return;

		}

Y
yamahigashi 已提交
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
		this.uvNode = this.getNode( node );

		this.uv = this.getUV( node );
		var mappingType = this.getMap( node );
		var refType = this.getRef( node );
		var indices = this.getIndex( node );

		var strides = geo.getPolygonTopologyArray();

		// it means that there is a normal for every vertex of every polygon of the model.
		// For example, if the models has 8 vertices that make up four quads, then there
		// will be 16 normals (one normal * 4 polygons * 4 vertices of the polygon). Note
		// that generally a game engine needs the vertices to have only one normal defined.
		// So, if you find a vertex has more tha one normal, you can either ignore the normals
		// you find after the first, or calculate the mean from all of them (normal smoothing).
		//if ( mappingType == "ByPolygonVertex" ){
		switch ( mappingType ) {

			case "ByPolygonVertex":

				switch ( refType ) {

					// Direct
					// The this.uv are in order.
					case "Direct":
						this.uv = this.parseUV_ByPolygonVertex_Direct( this.uv, indices, strides, 2 );
						break;

					// IndexToDirect
					// The order of the this.uv is given by the uvsIndex property.
					case "IndexToDirect":
						this.uv = this.parseUV_ByPolygonVertex_IndexToDirect( this.uv, indices );
						break;

				}

				// convert from by polygon(vert) data into by verts data
				this.uv = mapByPolygonVertexToByVertex( this.uv, geo.getPolygonVertexIndices( node ), 2 );
				break;

			case "ByPolygon":

				switch ( refType ) {

					// Direct
					// The this.uv are in order.
					case "Direct":
						this.uv = this.parseUV_ByPolygon_Direct();
						break;

					// IndexToDirect
					// The order of the this.uv is given by the uvsIndex property.
					case "IndexToDirect":
						this.uv = this.parseUV_ByPolygon_IndexToDirect();
						break;

				}
				break;
1997

Y
yamahigashi 已提交
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
		}

		return this.uv;

	};

	UV.prototype.parseUV_ByPolygonVertex_Direct = function ( node, indices, strides, itemSize ) {

		return parse_Data_ByPolygonVertex_Direct( node, indices, strides, itemSize );

	};

	UV.prototype.parseUV_ByPolygonVertex_IndexToDirect = function ( node, indices ) {

		return parse_Data_ByPolygonVertex_IndexToDirect( node, indices, 2 );

	};

	UV.prototype.parseUV_ByPolygon_Direct = function ( node ) {

		console.warn( "not implemented" );
		return node;

	};

	UV.prototype.parseUV_ByPolygon_IndexToDirect = function ( node ) {

		console.warn( "not implemented" );
		return node;

	};

	UV.prototype.parseUV_ByVertex_Direct = function ( node ) {

		console.warn( "not implemented" );
		return node;

	};


	function Normal() {

		this.normal = null;
		this.map	= null;
		this.ref	= null;
		this.node = null;
		this.index = null;

	}

	Normal.prototype.getNormal = function ( node ) {

		if ( this.node && this.normal && this.map && this.ref ) {

			return this.normal;

		} else {

			this._parseText( node );
			return this.normal;

		}

	};

	// mappingType: possible variant
	//	  ByPolygon
	//	  ByPolygonVertex
	//	  ByVertex (or also ByVertice, as the Blender exporter writes)
	//	  ByEdge
	//	  AllSame
	//	var mappingType = node.properties.MappingInformationType;
	Normal.prototype.getMap = function ( node ) {

		if ( this.node && this.normal && this.map && this.ref ) {

			return this.map;

		} else {

			this._parseText( node );
			return this.map;

		}

	};

	// refType: possible variants
	//	  Direct
	//	  IndexToDirect (or Index for older versions)
	// var refType	 = node.properties.ReferenceInformationType;
	Normal.prototype.getRef = function ( node ) {

		if ( this.node && this.normal && this.map && this.ref ) {

			return this.ref;

		} else {

			this._parseText( node );
			return this.ref;

		}

	};

	Normal.prototype.getNode = function ( node ) {

		if ( this.node ) {

			return this.node;

		}

		this.node = node.subNodes.LayerElementNormal;
		return this.node;

	};

	Normal.prototype._parseText = function ( node ) {

2119
		var normalNode = this.getNode( node )[ 0 ];
Y
yamahigashi 已提交
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131

		if ( normalNode === undefined ) {

			console.warn( 'node: ' + node.attrName + "(" + node.id + ") does not have LayerElementNormal" );
			return;

		}

		var mappingType = normalNode.properties.MappingInformationType;
		var refType = normalNode.properties.ReferenceInformationType;

		var rawTextNormals = normalNode.subNodes.Normals.properties.a;
2132
		this.normal = parseArrayToFloat( rawTextNormals );
Y
yamahigashi 已提交
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142

		// TODO: normalize notation shaking, vertex / vertice... blender...
		this.map	= mappingType;
		this.ref	= refType;

	};

	Normal.prototype.parse = function ( topnode, geo ) {

		var normals = this.getNormal( topnode );
K
Kyle Larson 已提交
2143
		//var normalNode = this.getNode( topnode );
Y
yamahigashi 已提交
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
		var mappingType = this.getMap( topnode );
		var refType = this.getRef( topnode );

		var indices = geo.getPolygonVertexIndices( topnode );
		var strides = geo.getPolygonTopologyArray( topnode );

		// it means that there is a normal for every vertex of every polygon of the model.
		// For example, if the models has 8 vertices that make up four quads, then there
		// will be 16 normals (one normal * 4 polygons * 4 vertices of the polygon). Note
		// that generally a game engine needs the vertices to have only one normal defined.
		// So, if you find a vertex has more tha one normal, you can either ignore the normals
		// you find after the first, or calculate the mean from all of them (normal smoothing).
		//if ( mappingType == "ByPolygonVertex" ){
		switch ( mappingType ) {

			case "ByPolygonVertex":

				switch ( refType ) {

					// Direct
					// The normals are in order.
					case "Direct":
						normals = this.parseNormal_ByPolygonVertex_Direct( normals, indices, strides, 3 );
						break;

					// IndexToDirect
					// The order of the normals is given by the NormalsIndex property.
					case "IndexToDirect":
						normals = this.parseNormal_ByPolygonVertex_IndexToDirect();
						break;

				}
				break;

			case "ByPolygon":

				switch ( refType ) {

					// Direct
					// The normals are in order.
					case "Direct":
						normals = this.parseNormal_ByPolygon_Direct();
						break;

					// IndexToDirect
					// The order of the normals is given by the NormalsIndex property.
					case "IndexToDirect":
						normals = this.parseNormal_ByPolygon_IndexToDirect();
						break;

				}
				break;
K
Kyle Larson 已提交
2196

Y
yamahigashi 已提交
2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260
		}

		return normals;

	};

	Normal.prototype.parseNormal_ByPolygonVertex_Direct = function ( node, indices, strides, itemSize ) {

		return parse_Data_ByPolygonVertex_Direct( node, indices, strides, itemSize );

	};

	Normal.prototype.parseNormal_ByPolygonVertex_IndexToDirect = function ( node ) {

		console.warn( "not implemented" );
		return node;

	};

	Normal.prototype.parseNormal_ByPolygon_Direct = function ( node ) {

		console.warn( "not implemented" );
		return node;

	};

	Normal.prototype.parseNormal_ByPolygon_IndexToDirect = function ( node ) {

		console.warn( "not implemented" );
		return node;

	};

	Normal.prototype.parseNormal_ByVertex_Direct = function ( node ) {

		console.warn( "not implemented" );
		return node;

	};

	function AnimationCurve() {

		this.version = null;

		this.id = null;
		this.internalId = null;
		this.times = null;
		this.values = null;

		this.attrFlag = null; // tangeant
		this.attrData = null; // slope, weight

	}

	AnimationCurve.prototype.fromNode = function ( curveNode ) {

		this.id = curveNode.id;
		this.internalId = curveNode.id;
		this.times = curveNode.subNodes.KeyTime.properties.a;
		this.values = curveNode.subNodes.KeyValueFloat.properties.a;

		this.attrFlag = curveNode.subNodes.KeyAttrFlags.properties.a;
		this.attrData = curveNode.subNodes.KeyAttrDataFloat.properties.a;

2261 2262 2263 2264
		this.times = parseArrayToFloat( this.times );
		this.values = parseArrayToFloat( this.values );
		this.attrData = parseArrayToFloat( this.attrData );
		this.attrFlag = parseArrayToInt( this.attrFlag );
Y
yamahigashi 已提交
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292

		this.times = this.times.map( function ( element ) {

			return FBXTimeToSeconds( element );

		} );

		return this;

	};

	AnimationCurve.prototype.getLength = function () {

		return this.times[ this.times.length - 1 ];

	};

	function AnimationNode() {

		this.id = null;
		this.attr = null; // S, R, T
		this.attrX = false;
		this.attrY = false;
		this.attrZ = false;
		this.internalId = null;
		this.containerInternalId = null; // bone, null etc Id
		this.containerBoneId = null; // bone, null etc Id
		this.curveIdx = null; // AnimationCurve's indices
2293
		this.curves = {};	// AnimationCurve refs
Y
yamahigashi 已提交
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340

	}

	AnimationNode.prototype.fromNode = function ( allNodes, node, bones ) {

		this.id = node.id;
		this.attr = node.attrName;
		this.internalId = node.id;

		if ( this.attr.match( /S|R|T/ ) ) {

			for ( var attrKey in node.properties ) {

				if ( attrKey.match( /X/ ) ) {

					this.attrX = true;

				}
				if ( attrKey.match( /Y/ ) ) {

					this.attrY = true;

				}
				if ( attrKey.match( /Z/ ) ) {

					this.attrZ = true;

				}

			}

		} else {

			// may be deform percent nodes
			return null;

		}

		this.containerIndices = allNodes.searchConnectionParent( this.id );
		this.curveIdx	= allNodes.searchConnectionChildren( this.id );

		for ( var i = this.containerIndices.length - 1; i >= 0; -- i ) {

			var boneId = bones.searchRealId( this.containerIndices[ i ] );
			if ( boneId >= 0 ) {

				this.containerBoneId = boneId;
K
Kyle Larson 已提交
2341
				this.containerId = this.containerIndices[ i ];
Y
yamahigashi 已提交
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376

			}

			if ( boneId >= 0 ) {

				break;

			}

		}
		// this.containerBoneId = bones.searchRealId( this.containerIndices );

		return this;

	};

	AnimationNode.prototype.setCurve = function ( curve ) {

		this.curves.push( curve );

	};

	function Animation() {

		this.curves = {};
		this.length = 0.0;
		this.fps	= 30.0;
		this.frames = 0.0;

	}

	Animation.prototype.parse = function ( node, bones ) {

		var rawNodes = node.Objects.subNodes.AnimationCurveNode;
		var rawCurves = node.Objects.subNodes.AnimationCurve;
2377 2378
		var rawLayers = node.Objects.subNodes.AnimationLayer;
		var rawStacks = node.Objects.subNodes.AnimationStack;
Y
yamahigashi 已提交
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422

		// first: expand AnimationCurveNode into curve nodes
		var curveNodes = [];
		for ( var key in rawNodes ) {

			if ( key.match( /\d+/ ) ) {

				var a = ( new AnimationNode() ).fromNode( node, rawNodes[ key ], bones );
				curveNodes.push( a );

			}

		}

		// second: gen dict, mapped by internalId
		var tmp = {};
		for ( var i = 0; i < curveNodes.length; ++ i ) {

			if ( curveNodes[ i ] === null ) {

				continue;

			}

			tmp[ curveNodes[ i ].id ] = curveNodes[ i ];

		}

		// third: insert curves into the dict
		var ac = [];
		for ( key in rawCurves ) {

			if ( key.match( /\d+/ ) ) {

				var c = ( new AnimationCurve() ).fromNode( rawCurves[ key ] );
				ac.push( c );

				var parentId = node.searchConnectionParent( c.id )[ 0 ];
				var axis = node.searchConnectionType( c.id, parentId );

				if ( axis.match( /X/ ) ) {

					axis = 'x';

2423
				} else if ( axis.match( /Y/ ) ) {
Y
yamahigashi 已提交
2424 2425 2426

					axis = 'y';

2427
				} else if ( axis.match( /Z/ ) ) {
Y
yamahigashi 已提交
2428 2429 2430

					axis = 'z';

2431 2432 2433 2434
				} else {

					continue;

Y
yamahigashi 已提交
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448
				}

				tmp[ parentId ].curves[ axis ] = c;

			}

		}

		// forth:
		for ( var t in tmp ) {

			var id = tmp[ t ].containerBoneId;
			if ( this.curves[ id ] === undefined ) {

2449 2450 2451 2452 2453
				this.curves[ id ] = {
					T: null,
					R: null,
					S: null
				};
Y
yamahigashi 已提交
2454 2455 2456 2457 2458 2459 2460

			}

			this.curves[ id ][ tmp[ t ].attr ] = tmp[ t ];

		}

2461 2462 2463 2464 2465 2466 2467 2468
		//Layers
		this.layers = {};
		for ( var key in rawLayers ) {

			var layer = [];
			var children = node.searchConnectionChildren( key );
			for ( var i = 0; i < children.length; ++ i ) {

2469 2470
				//Skip lockInfluenceWeights
				if ( tmp[ children[ i ] ] ) {
2471

2472
					if ( layer[ tmp[ children[ i ] ].containerBoneId ] === undefined ) {
2473

2474 2475 2476 2477 2478 2479 2480
						layer[ tmp[ children[ i ] ].containerBoneId ] = {
							T: null,
							R: null,
							S: null
						};

					}
2481

K
Kyle Larson 已提交
2482
					layer[ tmp[ children[ i ] ].containerBoneId ][ tmp[ children[ i ] ].attr ] = tmp[ children[ i ] ];
2483 2484

				}
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497

			}

			this.layers[ key ] = layer;

		}

		//Takes
		this.stacks = {};
		for ( var key in rawStacks ) {

			var layers = [];
			var children = node.searchConnectionChildren( key );
2498 2499
			var max = 0.0;
			var min = Number.MAX_VALUE;
2500 2501 2502 2503 2504 2505
			for ( var i = 0; i < children.length; ++ i ) {

				if ( children[ i ] in this.layers ) {

					layers.push( this.layers[ children[ i ] ] );

2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559
					for ( var j = 0; j < this.layers[ children[ i ] ].length; ++ j ) {

						function getMaxMin( layer ) {

							function _getMaxMin( curves ) {

								if ( curves.x ) {

									max = curves.x.getLength() > max ? curves.x.getLength() : max;
									min = curves.x.times[ 0 ] < min ? curves.x.times[ 0 ] : min;

								}
								if ( curves.y ) {

									max = curves.y.getLength() > max ? curves.y.getLength() : max;
									min = curves.y.times[ 0 ] < min ? curves.y.times[ 0 ] : min;

								}
								if ( curves.z ) {

									max = curves.z.getLength() > max ? curves.z.getLength() : max;
									min = curves.z.times[ 0 ] < min ? curves.z.times[ 0 ] : min;

								}

							}

							if ( layer.R ) {

								_getMaxMin( layer.R.curves );

							}
							if ( layer.S ) {

								_getMaxMin( layer.S.curves );

							}
							if ( layer.T ) {

								_getMaxMin( layer.T.curves );

							}

						}

						var layer = this.layers[ children[ i ] ][ j ];
						if ( layer ) {

							getMaxMin( layer );

						}

					}

2560 2561 2562 2563
				}

			}

2564 2565
			//Do we have an animation clip with an actual length?
			if ( max > min ) {
2566

2567
				this.stacks[ key ] = {
2568

2569 2570 2571 2572 2573 2574 2575 2576
					name: rawStacks[ key ].attrName,
					layers: layers,
					length: max - min,
					frames: ( max - min ) * 30,

				};

			}
2577 2578 2579

		}

Y
yamahigashi 已提交
2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615
		return this;

	};


	function Textures() {

		this.textures = [];
		this.perGeoMap = {};

	}

	Textures.prototype.add = function ( tex ) {

		if ( this.textures === undefined ) {

			this.textures = [];

		}

		this.textures.push( tex );

		for ( var i = 0; i < tex.parentIds.length; ++ i ) {

			if ( this.perGeoMap[ tex.parentIds[ i ] ] === undefined ) {

				this.perGeoMap[ tex.parentIds[ i ] ] = [];

			}

			this.perGeoMap[ tex.parentIds[ i ] ].push( this.textures[ this.textures.length - 1 ] );

		}

	};

K
Kyle Larson 已提交
2616
	Textures.prototype.parse = function ( node ) {
Y
yamahigashi 已提交
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688

		var rawNodes = node.Objects.subNodes.Texture;

		for ( var n in rawNodes ) {

			var tex = ( new Texture() ).parse( rawNodes[ n ], node );
			this.add( tex );

		}

		return this;

	};

	Textures.prototype.getById = function ( id ) {

		return this.perGeoMap[ id ];

	};

	function Texture() {

		this.fileName = "";
		this.name = "";
		this.id = null;
		this.parentIds = [];

	}

	Texture.prototype.parse = function ( node, nodes ) {

		this.id = node.id;
		this.name = node.attrName;
		this.fileName = this.parseFileName( node.properties.FileName );

		this.parentIds = this.searchParents( this.id, nodes );

		return this;

	};

	// TODO: support directory
	Texture.prototype.parseFileName = function ( fname ) {

		if ( fname === undefined ) {

			return "";

		}

		// ignore directory structure, flatten path
		var splitted = fname.split( /[\\\/]/ );
		if ( splitted.length > 0 ) {

			return splitted[ splitted.length - 1 ];

		} else {

			return fname;

		}

	};

	Texture.prototype.searchParents = function ( id, nodes ) {

		var p = nodes.searchConnectionParent( id );

		return p;

	};

2689
	function Materials() {
K
Kyle Larson 已提交
2690

2691 2692
		this.materials = [];
		this.perGeoMap = {};
K
Kyle Larson 已提交
2693

2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
	}

	Materials.prototype.add = function ( mat ) {

		if ( this.materials === undefined ) {

			this.materials = [];

		}

		this.materials.push( mat );

		for ( var i = 0; i < mat.parentIds.length; ++ i ) {

			if ( this.perGeoMap[ mat.parentIds[ i ] ] === undefined ) {

				this.perGeoMap[ mat.parentIds[ i ] ] = [];

			}

			this.perGeoMap[ mat.parentIds[ i ] ].push( this.materials[ this.materials.length - 1 ] );

		}

	};

K
Kyle Larson 已提交
2720
	Materials.prototype.parse = function ( node ) {
2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763

		var rawNodes = node.Objects.subNodes.Material;

		for ( var n in rawNodes ) {

			var mat = ( new Material() ).parse( rawNodes[ n ], node );
			this.add( mat );

		}

		return this;

	};

	Materials.prototype.getById = function ( id ) {

		return this.perGeoMap[ id ];

	};

	function Material() {

		this.fileName = "";
		this.name = "";
		this.id = null;
		this.parentIds = [];

	}

	Material.prototype.parse = function ( node, nodes ) {

		this.id = node.id;
		this.name = node.attrName;
		this.type = node.properties.ShadingModel;

		this.parameters = this.getParameters( node.properties );

		this.parentIds = this.searchParents( this.id, nodes );

		return this;

	};

K
Kyle Larson 已提交
2764 2765
	Material.prototype.getParameters = function ( properties ) {

2766 2767 2768 2769 2770 2771 2772 2773 2774
		var parameters = {};

		//TODO: Missing parameters:
		// - Ambient
		// - MultiLayer
		// - ShininessExponent (Same vals as Shininess)
		// - Specular (Same vals as SpecularColor)
		// - TransparencyFactor (Maybe same as Opacity?).

2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
		if ( properties.Diffuse ) {

			parameters.color = new THREE.Color().fromArray( [ parseFloat( properties.Diffuse.value.x ), parseFloat( properties.Diffuse.value.y ), parseFloat( properties.Diffuse.value.z ) ] );

		}
		if ( properties.Specular ) {

			parameters.specular = new THREE.Color().fromArray( [ parseFloat( properties.Specular.value.x ), parseFloat( properties.Specular.value.y ), parseFloat( properties.Specular.value.z ) ] );

		}
		if ( properties.Shininess ) {

			parameters.shininess = properties.Shininess.value;

		}
		if ( properties.Emissive ) {

			parameters.emissive = new THREE.Color().fromArray( [ parseFloat( properties.Emissive.value.x ), parseFloat( properties.Emissive.value.y ), parseFloat( properties.Emissive.value.z ) ] );

		}
		if ( properties.EmissiveFactor ) {

			parameters.emissiveIntensity = properties.EmissiveFactor.value;

		}
		if ( properties.Reflectivity ) {

			parameters.reflectivity = properties.Reflectivity.value;

		}
		if ( properties.Opacity ) {

			parameters.opacity = properties.Opacity.value;

		}
K
Kyle Larson 已提交
2810 2811
		if ( parameters.opacity < 1.0 ) {

2812
			parameters.transparent = true;
K
Kyle Larson 已提交
2813

2814 2815 2816
		}

		return parameters;
K
Kyle Larson 已提交
2817

2818 2819 2820 2821 2822 2823 2824 2825 2826 2827
	};

	Material.prototype.searchParents = function ( id, nodes ) {

		var p = nodes.searchConnectionParent( id );

		return p;

	};

Y
yamahigashi 已提交
2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864

	/* --------------------------------------------------------------------- */
	/* --------------------------------------------------------------------- */
	/* --------------------------------------------------------------------- */
	/* --------------------------------------------------------------------- */

	// LayerElementUV: 0 {
	// 	Version: 101
	//	Name: "Texture_Projection"
	//	MappingInformationType: "ByPolygonVertex"
	//	ReferenceInformationType: "IndexToDirect"
	//	UV: *1746 {
	//	UVIndex: *7068 {
	//
	//	The order of the uvs is given by the UVIndex property.
	function parse_Data_ByPolygonVertex_IndexToDirect( node, indices, itemSize ) {

		var res = [];

		for ( var i = 0; i < indices.length; ++ i ) {

			for ( var j = 0; j < itemSize; ++ j ) {

				res.push( node[ ( indices[ i ] * itemSize ) + j ] );

			}

		}

		return res;

	}


	// what want: normal per vertex, order vertice
	// i have: normal per polygon
	// i have: indice per polygon
K
Kyle Larson 已提交
2865
	var parse_Data_ByPolygonVertex_Direct = function ( node, indices, strides, itemSize ) {
Y
yamahigashi 已提交
2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968

		// *21204 > 3573
		// Geometry: 690680816, "Geometry::", "Mesh" {
		//  Vertices: *3573 {
		//  PolygonVertexIndex: *7068 {

		var tmp = [];
		var currentIndex = 0;

		// first: sort to per vertex
		for ( var i = 0; i < indices.length; ++ i ) {

			tmp[ indices[ i ] ] = [];

			// TODO: duped entry? blend or something?
			for ( var s = 0; s < itemSize; ++ s ) {

				tmp[ indices[ i ] ][ s ] = node[ currentIndex + s ];

			}

			currentIndex += itemSize;

		}

		// second: expand x,y,z into serial array
		var res = [];
		for ( var jj = 0; jj < tmp.length; ++ jj ) {

			if ( tmp[ jj ] === undefined ) {

				continue;

			}

			for ( var t = 0; t < itemSize; ++ t ) {

				if ( tmp[ jj ][ t ] === undefined ) {

					continue;

				}
				res.push( tmp[ jj ][ t ] );

			}

		}

		return res;

	};

	// convert from by polygon(vert) data into by verts data
	function mapByPolygonVertexToByVertex( data, indices, stride ) {

		var tmp = {};
		var res = [];
		var max = 0;

		for ( var i = 0; i < indices.length; ++ i ) {

			if ( indices[ i ] in tmp ) {

				continue;

			}

			tmp[ indices[ i ] ] = {};

			for ( var j = 0; j < stride; ++ j ) {

				tmp[ indices[ i ] ][ j ] = data[ i * stride + j ];

			}

			max = max < indices[ i ] ? indices[ i ] : max;

		}

		try {

			for ( i = 0; i <= max; i ++ ) {

				for ( var s = 0; s < stride; s ++ ) {

					res.push( tmp[ i ][ s ] );

				}

			}

		} catch ( e ) {
			//console.log( max );
			//console.log( tmp );
			//console.log( i );
			//console.log( e );
		}

		return res;

	}

	// AUTODESK uses broken clock. i guess
2969
	function FBXTimeToSeconds( adskTime ) {
Y
yamahigashi 已提交
2970 2971 2972

		return adskTime / 46186158000;

2973
	}
Y
yamahigashi 已提交
2974

2975
	function degToRad( degrees ) {
Y
yamahigashi 已提交
2976 2977 2978

		return degrees * Math.PI / 180;

2979
	}
Y
yamahigashi 已提交
2980

2981
	function quatFromVec( x, y, z ) {
Y
yamahigashi 已提交
2982 2983 2984 2985 2986 2987 2988

		var euler = new THREE.Euler( x, y, z, 'ZYX' );
		var quat = new THREE.Quaternion();
		quat.setFromEuler( euler );

		return quat;

2989
	}
Y
yamahigashi 已提交
2990

2991
	function parseArrayToInt( string ) {
Y
yamahigashi 已提交
2992

2993
		return string.split( ',' ).map( function ( element ) {
Y
yamahigashi 已提交
2994 2995 2996 2997 2998

			return parseInt( element );

		} );

2999
	}
Y
yamahigashi 已提交
3000

3001
	function parseArrayToFloat( string ) {
Y
yamahigashi 已提交
3002

3003
		return string.split( ',' ).map( function ( element ) {
Y
yamahigashi 已提交
3004 3005 3006 3007 3008

			return parseFloat( element );

		} );

3009
	}
Y
yamahigashi 已提交
3010

3011
	function parseArrayToRadians( string ) {
Y
yamahigashi 已提交
3012

3013
		return string.split( ',' ).map( function ( element ) {
Y
yamahigashi 已提交
3014

3015
			return degToRad( parseFloat( element ) );
Y
yamahigashi 已提交
3016 3017 3018

		} );

3019
	}
Y
yamahigashi 已提交
3020

3021
	function parseArrayToMatrix( string ) {
Y
yamahigashi 已提交
3022

3023 3024
		var arr = parseArrayToFloat( string );
		return new THREE.Matrix4().fromArray( arr );
Y
yamahigashi 已提交
3025

3026
	}
Y
yamahigashi 已提交
3027 3028

} )();