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

( function() {

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

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

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

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

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

Y
yamahigashi 已提交
32
	THREE.FBXLoader.prototype.load = function ( url, onLoad, onProgress, onError ) {
Y
yamahigashi 已提交
33

Y
yamahigashi 已提交
34
		var scope = this;
Y
yamahigashi 已提交
35

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

Y
yamahigashi 已提交
40
			if ( ! scope.isFbxFormatASCII( text ) ) {
Y
yamahigashi 已提交
41

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

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

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

Y
yamahigashi 已提交
48
			} else {
Y
yamahigashi 已提交
49

Y
yamahigashi 已提交
50
				scope.textureBasePath = scope.extractUrlBase( url );
M
Mr.doob 已提交
51
				onLoad( scope.parse( text ) );
Y
yamahigashi 已提交
52

Y
yamahigashi 已提交
53
			}
Y
yamahigashi 已提交
54

Y
yamahigashi 已提交
55
		}, onProgress, onError );
Y
yamahigashi 已提交
56

Y
yamahigashi 已提交
57
	};
Y
yamahigashi 已提交
58

Y
yamahigashi 已提交
59
	THREE.FBXLoader.prototype.setCrossOrigin = function ( value ) {
Y
yamahigashi 已提交
60

Y
yamahigashi 已提交
61
		this.crossOrigin = value;
Y
yamahigashi 已提交
62

Y
yamahigashi 已提交
63
	};
Y
yamahigashi 已提交
64

Y
yamahigashi 已提交
65
	THREE.FBXLoader.prototype.isFbxFormatASCII = function ( body ) {
Y
yamahigashi 已提交
66

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

Y
yamahigashi 已提交
69 70
		var cursor = 0;
		var read = function ( offset ) {
Y
yamahigashi 已提交
71

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

Y
yamahigashi 已提交
77
		};
Y
yamahigashi 已提交
78

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

K
Kyle Larson 已提交
81
			var num = read( 1 );
Y
yamahigashi 已提交
82
			if ( num == CORRECT[ i ] ) {
Y
yamahigashi 已提交
83

Y
yamahigashi 已提交
84
				return false;
Y
yamahigashi 已提交
85

Y
yamahigashi 已提交
86
			}
Y
yamahigashi 已提交
87

Y
yamahigashi 已提交
88
		}
Y
yamahigashi 已提交
89

Y
yamahigashi 已提交
90
		return true;
Y
yamahigashi 已提交
91

Y
yamahigashi 已提交
92
	};
Y
yamahigashi 已提交
93

Y
yamahigashi 已提交
94
	THREE.FBXLoader.prototype.isFbxVersionSupported = function ( body ) {
Y
yamahigashi 已提交
95

Y
yamahigashi 已提交
96
		var versionExp = /FBXVersion: (\d+)/;
K
Kyle Larson 已提交
97
		var match = body.match( versionExp );
Y
yamahigashi 已提交
98
		if ( match ) {
Y
yamahigashi 已提交
99

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

Y
yamahigashi 已提交
104 105
		}
		return false;
Y
yamahigashi 已提交
106

Y
yamahigashi 已提交
107
	};
Y
yamahigashi 已提交
108

M
Mr.doob 已提交
109
	THREE.FBXLoader.prototype.parse = function ( text ) {
Y
yamahigashi 已提交
110

Y
yamahigashi 已提交
111
		var scope = this;
Y
yamahigashi 已提交
112

M
Mr.doob 已提交
113 114 115
		console.time( 'FBXLoader' );

		console.time( 'FBXLoader: TextParser' );
M
Mr.doob 已提交
116
		var nodes = new FBXParser().parse( text );
M
Mr.doob 已提交
117 118
		console.timeEnd( 'FBXLoader: TextParser' );

Y
yamahigashi 已提交
119
		console.time( 'FBXLoader: ObjectParser' );
M
Mr.doob 已提交
120 121 122 123
		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 );
124
		scope.materials = ( new Materials() ).parse( nodes, scope.hierarchy );
Y
yamahigashi 已提交
125
		console.timeEnd( 'FBXLoader: ObjectParser' );
Y
yamahigashi 已提交
126

Y
yamahigashi 已提交
127
		console.time( 'FBXLoader: GeometryParser' );
K
Kyle Larson 已提交
128
		var geometries = this.parseGeometries( nodes );
Y
yamahigashi 已提交
129
		console.timeEnd( 'FBXLoader: GeometryParser' );
Y
yamahigashi 已提交
130

M
Mr.doob 已提交
131 132
		var container = new THREE.Group();

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

Y
yamahigashi 已提交
135
			if ( geometries[ i ] === undefined ) {
Y
yamahigashi 已提交
136

Y
yamahigashi 已提交
137
				continue;
Y
yamahigashi 已提交
138

Y
yamahigashi 已提交
139
			}
Y
yamahigashi 已提交
140

Y
yamahigashi 已提交
141
			container.add( geometries[ i ] );
Y
yamahigashi 已提交
142

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

Y
yamahigashi 已提交
146 147
			//vnh = new THREE.VertexNormalsHelper( geometries[i], 0.6 );
			//container.add( vnh );
Y
yamahigashi 已提交
148

Y
yamahigashi 已提交
149 150
			//skh = new THREE.SkeletonHelper( geometries[i] );
			//container.add( skh );
Y
yamahigashi 已提交
151

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

Y
yamahigashi 已提交
154
		}
Y
yamahigashi 已提交
155

M
Mr.doob 已提交
156
		console.timeEnd( 'FBXLoader' );
Y
yamahigashi 已提交
157
		return container;
Y
yamahigashi 已提交
158

Y
yamahigashi 已提交
159
	};
Y
yamahigashi 已提交
160

Y
yamahigashi 已提交
161
	THREE.FBXLoader.prototype.parseGeometries = function ( node ) {
Y
yamahigashi 已提交
162

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

Y
yamahigashi 已提交
166
			return [];
Y
yamahigashi 已提交
167

Y
yamahigashi 已提交
168
		}
Y
yamahigashi 已提交
169

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

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

Y
yamahigashi 已提交
176
				geoCount ++;
Y
yamahigashi 已提交
177

Y
yamahigashi 已提交
178
			}
Y
yamahigashi 已提交
179

Y
yamahigashi 已提交
180
		}
Y
yamahigashi 已提交
181

Y
yamahigashi 已提交
182 183
		var res = [];
		if ( geoCount > 0 ) {
Y
yamahigashi 已提交
184

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

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

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

Y
yamahigashi 已提交
191
				}
Y
yamahigashi 已提交
192

Y
yamahigashi 已提交
193
			}
Y
yamahigashi 已提交
194

Y
yamahigashi 已提交
195
		} else {
Y
yamahigashi 已提交
196

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

Y
yamahigashi 已提交
199
		}
Y
yamahigashi 已提交
200

Y
yamahigashi 已提交
201
		return res;
Y
yamahigashi 已提交
202

Y
yamahigashi 已提交
203
	};
Y
yamahigashi 已提交
204

Y
yamahigashi 已提交
205
	THREE.FBXLoader.prototype.parseGeometry = function ( node, nodes ) {
Y
yamahigashi 已提交
206

K
Kyle Larson 已提交
207
		var geo = ( new Geometry() ).parse( node );
Y
yamahigashi 已提交
208
		geo.addBones( this.hierarchy.hierarchy );
Y
yamahigashi 已提交
209

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

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

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

Y
yamahigashi 已提交
219
		}
Y
yamahigashi 已提交
220

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

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

Y
yamahigashi 已提交
225
		}
Y
yamahigashi 已提交
226

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

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

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

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

Y
yamahigashi 已提交
235
		}
Y
yamahigashi 已提交
236

Y
yamahigashi 已提交
237 238 239
		geometry.verticesNeedUpdate = true;
		geometry.computeBoundingSphere();
		geometry.computeBoundingBox();
Y
yamahigashi 已提交
240

Y
yamahigashi 已提交
241 242 243 244
		// TODO: texture & material support
		var texture;
		var texs = this.textures.getById( nodes.searchConnectionParent( geo.id ) );
		if ( texs !== undefined && texs.length > 0 ) {
Y
yamahigashi 已提交
245

Y
yamahigashi 已提交
246
			if ( this.textureLoader === null ) {
Y
yamahigashi 已提交
247

Y
yamahigashi 已提交
248
				this.textureLoader = new THREE.TextureLoader();
Y
yamahigashi 已提交
249

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

Y
yamahigashi 已提交
253
		}
Y
yamahigashi 已提交
254

Y
yamahigashi 已提交
255
		var material;
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
		var mats = this.materials.getById( nodes.searchConnectionParent( geo.id ) );
		if ( mats !== undefined && mats.length > 0) {
			var mat_data = mats[0];

			// 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":
				material = new THREE.MeshPhongMaterial();
				break;
				default:
				console.warn("No implementation given for material type " + mat_data.type + " in FBXLoader.js.  Defaulting to basic material")
				material = new THREE.MeshBasicMaterial({ color: 0x3300ff });
				break;
			}
			if (texture !== undefined) {
				mat_data.parameters.map = texture;
			}
			material.setValues(mat_data.parameters);
		} else {
			//No material found for this geometry, create default
			if (texture !== undefined) {
Y
yamahigashi 已提交
280

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

283
			} else {
Y
yamahigashi 已提交
284

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

287
			}
Y
yamahigashi 已提交
288
		}
Y
yamahigashi 已提交
289

Y
yamahigashi 已提交
290 291 292 293
		geometry = new THREE.Geometry().fromBufferGeometry( geometry );
		geometry.bones = geo.bones;
		geometry.skinIndices = this.weights.skinIndices;
		geometry.skinWeights = this.weights.skinWeights;
Y
yamahigashi 已提交
294

Y
yamahigashi 已提交
295 296
		var mesh = null;
		if ( geo.bones === undefined || geo.skins === undefined || this.animations === undefined || this.animations.length === 0 ) {
Y
yamahigashi 已提交
297

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

Y
yamahigashi 已提交
300
		} else {
Y
yamahigashi 已提交
301

Y
yamahigashi 已提交
302 303 304
			material.skinning = true;
			mesh = new THREE.SkinnedMesh( geometry, material );
			this.addAnimation( mesh, this.weights.matrices, this.animations );
Y
yamahigashi 已提交
305

Y
yamahigashi 已提交
306
		}
Y
yamahigashi 已提交
307

Y
yamahigashi 已提交
308
		return mesh;
Y
yamahigashi 已提交
309

Y
yamahigashi 已提交
310
	};
Y
yamahigashi 已提交
311

Y
yamahigashi 已提交
312
	THREE.FBXLoader.prototype.addAnimation = function ( mesh, matrices, animations ) {
Y
yamahigashi 已提交
313

Y
yamahigashi 已提交
314
		var animationdata = { "name": 'animationtest', "fps": 30, "length": animations.length, "hierarchy": [] };
Y
yamahigashi 已提交
315

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

Y
yamahigashi 已提交
318 319 320
			var name = mesh.geometry.bones[ i ].name;
			name = name.replace( /.*:/, '' );
			animationdata.hierarchy.push( { parent: mesh.geometry.bones[ i ].parent, name: name, keys: [] } );
Y
yamahigashi 已提交
321

Y
yamahigashi 已提交
322
		}
Y
yamahigashi 已提交
323

Y
yamahigashi 已提交
324
		var hasCurve = function ( animNode, attr ) {
Y
yamahigashi 已提交
325

Y
yamahigashi 已提交
326
			if ( animNode === undefined ) {
Y
yamahigashi 已提交
327

Y
yamahigashi 已提交
328
				return false;
Y
yamahigashi 已提交
329

Y
yamahigashi 已提交
330
			}
Y
yamahigashi 已提交
331

Y
yamahigashi 已提交
332 333
			var attrNode;
			switch ( attr ) {
Y
yamahigashi 已提交
334

Y
yamahigashi 已提交
335 336
				case 'S':
					if ( animNode.S === undefined ) {
Y
yamahigashi 已提交
337

Y
yamahigashi 已提交
338
						return false;
Y
yamahigashi 已提交
339

Y
yamahigashi 已提交
340 341 342
					}
					attrNode = animNode.S;
					break;
Y
yamahigashi 已提交
343

Y
yamahigashi 已提交
344 345
				case 'R':
					if ( animNode.R === undefined ) {
Y
yamahigashi 已提交
346

Y
yamahigashi 已提交
347
						return false;
Y
yamahigashi 已提交
348

Y
yamahigashi 已提交
349 350 351
					}
					attrNode = animNode.R;
					break;
Y
yamahigashi 已提交
352

Y
yamahigashi 已提交
353 354
				case 'T':
					if ( animNode.T === undefined ) {
Y
yamahigashi 已提交
355

Y
yamahigashi 已提交
356
						return false;
Y
yamahigashi 已提交
357

Y
yamahigashi 已提交
358 359 360 361
					}
					attrNode = animNode.T;
					break;
			}
Y
yamahigashi 已提交
362

Y
yamahigashi 已提交
363
			if ( attrNode.curves.x === undefined ) {
Y
yamahigashi 已提交
364

Y
yamahigashi 已提交
365
				return false;
Y
yamahigashi 已提交
366

Y
yamahigashi 已提交
367
			}
Y
yamahigashi 已提交
368

Y
yamahigashi 已提交
369
			if ( attrNode.curves.y === undefined ) {
Y
yamahigashi 已提交
370

Y
yamahigashi 已提交
371
				return false;
Y
yamahigashi 已提交
372

Y
yamahigashi 已提交
373
			}
Y
yamahigashi 已提交
374

Y
yamahigashi 已提交
375
			if ( attrNode.curves.z === undefined ) {
Y
yamahigashi 已提交
376

Y
yamahigashi 已提交
377
				return false;
Y
yamahigashi 已提交
378

Y
yamahigashi 已提交
379
			}
Y
yamahigashi 已提交
380

Y
yamahigashi 已提交
381
			return true;
Y
yamahigashi 已提交
382

Y
yamahigashi 已提交
383
		};
Y
yamahigashi 已提交
384

Y
yamahigashi 已提交
385
		var hasKeyOnFrame = function ( attrNode, frame ) {
Y
yamahigashi 已提交
386

Y
yamahigashi 已提交
387 388 389
			var x = isKeyExistOnFrame( attrNode.curves.x, frame );
			var y = isKeyExistOnFrame( attrNode.curves.y, frame );
			var z = isKeyExistOnFrame( attrNode.curves.z, frame );
Y
yamahigashi 已提交
390

Y
yamahigashi 已提交
391
			return x && y && z;
Y
yamahigashi 已提交
392

Y
yamahigashi 已提交
393
		};
Y
yamahigashi 已提交
394

Y
yamahigashi 已提交
395
		var isKeyExistOnFrame = function ( curve, frame ) {
Y
yamahigashi 已提交
396

Y
yamahigashi 已提交
397 398
			var value = curve.values[ frame ];
			return value !== undefined;
Y
yamahigashi 已提交
399

Y
yamahigashi 已提交
400
		};
Y
yamahigashi 已提交
401 402


Y
yamahigashi 已提交
403
		var genKey = function ( animNode, bone ) {
Y
yamahigashi 已提交
404

Y
yamahigashi 已提交
405 406 407 408 409 410
			// 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 已提交
411

Y
yamahigashi 已提交
412
			if ( animNode === undefined ) {
Y
yamahigashi 已提交
413

Y
yamahigashi 已提交
414
				return key;
Y
yamahigashi 已提交
415

Y
yamahigashi 已提交
416
			}
Y
yamahigashi 已提交
417

Y
yamahigashi 已提交
418
			try {
Y
yamahigashi 已提交
419

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

Y
yamahigashi 已提交
422 423 424 425 426
					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 已提交
427

Y
yamahigashi 已提交
428
				} else {
Y
yamahigashi 已提交
429

Y
yamahigashi 已提交
430
					delete key.pos;
Y
yamahigashi 已提交
431

Y
yamahigashi 已提交
432
				}
Y
yamahigashi 已提交
433

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

Y
yamahigashi 已提交
436 437 438 439 440 441
					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 已提交
442

Y
yamahigashi 已提交
443
				} else {
Y
yamahigashi 已提交
444

Y
yamahigashi 已提交
445
					delete key.rot;
Y
yamahigashi 已提交
446

Y
yamahigashi 已提交
447
				}
Y
yamahigashi 已提交
448

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

Y
yamahigashi 已提交
451 452 453 454 455
					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 已提交
456

Y
yamahigashi 已提交
457
				} else {
Y
yamahigashi 已提交
458

Y
yamahigashi 已提交
459
					delete key.scl;
Y
yamahigashi 已提交
460

Y
yamahigashi 已提交
461
				}
Y
yamahigashi 已提交
462

Y
yamahigashi 已提交
463
			} catch ( e ) {
Y
yamahigashi 已提交
464

Y
yamahigashi 已提交
465 466 467
				// curve is not full plotted
				console.log( bone );
				console.log( e );
Y
yamahigashi 已提交
468

Y
yamahigashi 已提交
469
			}
Y
yamahigashi 已提交
470

Y
yamahigashi 已提交
471
			return key;
Y
yamahigashi 已提交
472

Y
yamahigashi 已提交
473
		};
Y
yamahigashi 已提交
474

Y
yamahigashi 已提交
475
		var bones = mesh.geometry.bones;
K
Kyle Larson 已提交
476
		for ( var frame = 0; frame < animations.frames; frame ++ ) {
Y
yamahigashi 已提交
477 478


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

Y
yamahigashi 已提交
481 482
				var bone = bones[ i ];
				var animNode = animations.curves[ i ];
Y
yamahigashi 已提交
483

Y
yamahigashi 已提交
484
				for ( var j = 0; j < animationdata.hierarchy.length; j ++ ) {
Y
yamahigashi 已提交
485

Y
yamahigashi 已提交
486
					if ( animationdata.hierarchy[ j ].name === bone.name ) {
Y
yamahigashi 已提交
487

Y
yamahigashi 已提交
488
						animationdata.hierarchy[ j ].keys.push( genKey( animNode, bone ) );
Y
yamahigashi 已提交
489

Y
yamahigashi 已提交
490
					}
Y
yamahigashi 已提交
491

Y
yamahigashi 已提交
492
				}
Y
yamahigashi 已提交
493

Y
yamahigashi 已提交
494
			}
Y
yamahigashi 已提交
495

Y
yamahigashi 已提交
496
		}
Y
yamahigashi 已提交
497

Y
yamahigashi 已提交
498
		if ( mesh.geometry.animations === undefined ) {
Y
yamahigashi 已提交
499

Y
yamahigashi 已提交
500
			mesh.geometry.animations = [];
Y
yamahigashi 已提交
501

Y
yamahigashi 已提交
502
		}
Y
yamahigashi 已提交
503

Y
yamahigashi 已提交
504
		mesh.geometry.animations.push( THREE.AnimationClip.parseAnimation( animationdata, mesh.geometry.bones ) );
Y
yamahigashi 已提交
505

Y
yamahigashi 已提交
506
	};
Y
yamahigashi 已提交
507

Y
yamahigashi 已提交
508
	THREE.FBXLoader.prototype.parseMaterials = function ( node ) {
Y
yamahigashi 已提交
509

Y
yamahigashi 已提交
510 511
		// has not mat, return []
		if ( ! ( 'Material' in node.subNodes ) ) {
Y
yamahigashi 已提交
512

Y
yamahigashi 已提交
513
			return [];
Y
yamahigashi 已提交
514

Y
yamahigashi 已提交
515
		}
Y
yamahigashi 已提交
516

Y
yamahigashi 已提交
517 518 519
		// has many
		var matCount = 0;
		for ( var mat in node.subNodes.Materials ) {
Y
yamahigashi 已提交
520

Y
yamahigashi 已提交
521
			if ( mat.match( /^\d+$/ ) ) {
Y
yamahigashi 已提交
522

Y
yamahigashi 已提交
523
				matCount ++;
Y
yamahigashi 已提交
524

Y
yamahigashi 已提交
525
			}
Y
yamahigashi 已提交
526

Y
yamahigashi 已提交
527
		}
Y
yamahigashi 已提交
528

Y
yamahigashi 已提交
529 530
		var res = [];
		if ( matCount > 0 ) {
Y
yamahigashi 已提交
531

Y
yamahigashi 已提交
532
			for ( mat in node.subNodes.Material ) {
Y
yamahigashi 已提交
533

Y
yamahigashi 已提交
534
				res.push( parseMaterial( node.subNodes.Material[ mat ] ) );
Y
yamahigashi 已提交
535

Y
yamahigashi 已提交
536
			}
Y
yamahigashi 已提交
537

Y
yamahigashi 已提交
538
		} else {
Y
yamahigashi 已提交
539

Y
yamahigashi 已提交
540
			res.push( parseMaterial( node.subNodes.Material ) );
Y
yamahigashi 已提交
541

Y
yamahigashi 已提交
542
		}
Y
yamahigashi 已提交
543

Y
yamahigashi 已提交
544
		return res;
Y
yamahigashi 已提交
545

Y
yamahigashi 已提交
546
	};
Y
yamahigashi 已提交
547

Y
yamahigashi 已提交
548 549
	// TODO
	THREE.FBXLoader.prototype.parseMaterial = function ( node ) {
550
		
Y
yamahigashi 已提交
551
	};
Y
yamahigashi 已提交
552 553


Y
yamahigashi 已提交
554
	THREE.FBXLoader.prototype.loadFile = function ( url, onLoad, onProgress, onError, responseType ) {
Y
yamahigashi 已提交
555

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

Y
yamahigashi 已提交
558
		loader.setResponseType( responseType );
Y
yamahigashi 已提交
559

Y
yamahigashi 已提交
560
		var request = loader.load( url, function ( result ) {
Y
yamahigashi 已提交
561

Y
yamahigashi 已提交
562
			onLoad( result );
Y
yamahigashi 已提交
563

Y
yamahigashi 已提交
564
		}, onProgress, onError );
Y
yamahigashi 已提交
565

Y
yamahigashi 已提交
566
		return request;
Y
yamahigashi 已提交
567

Y
yamahigashi 已提交
568
	};
Y
yamahigashi 已提交
569

Y
yamahigashi 已提交
570
	THREE.FBXLoader.prototype.loadFileAsBuffer = function ( url, onload, onProgress, onError ) {
Y
yamahigashi 已提交
571

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

Y
yamahigashi 已提交
574
	};
Y
yamahigashi 已提交
575

Y
yamahigashi 已提交
576
	THREE.FBXLoader.prototype.loadFileAsText = function ( url, onLoad, onProgress, onError ) {
Y
yamahigashi 已提交
577

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

Y
yamahigashi 已提交
580
	};
Y
yamahigashi 已提交
581 582


Y
yamahigashi 已提交
583
	/* ----------------------------------------------------------------- */
Y
yamahigashi 已提交
584

Y
yamahigashi 已提交
585
	function FBXNodes() {}
Y
yamahigashi 已提交
586

Y
yamahigashi 已提交
587
	FBXNodes.prototype.add = function ( key, val ) {
Y
yamahigashi 已提交
588

Y
yamahigashi 已提交
589
		this[ key ] = val;
Y
yamahigashi 已提交
590

Y
yamahigashi 已提交
591
	};
Y
yamahigashi 已提交
592

Y
yamahigashi 已提交
593
	FBXNodes.prototype.searchConnectionParent = function ( id ) {
Y
yamahigashi 已提交
594

Y
yamahigashi 已提交
595
		if ( this.__cache_search_connection_parent === undefined ) {
Y
yamahigashi 已提交
596

Y
yamahigashi 已提交
597
			this.__cache_search_connection_parent = [];
Y
yamahigashi 已提交
598

Y
yamahigashi 已提交
599
		}
Y
yamahigashi 已提交
600

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

Y
yamahigashi 已提交
603
			return this.__cache_search_connection_parent[ id ];
Y
yamahigashi 已提交
604

Y
yamahigashi 已提交
605
		} else {
Y
yamahigashi 已提交
606

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

Y
yamahigashi 已提交
609
		}
Y
yamahigashi 已提交
610

Y
yamahigashi 已提交
611
		var conns = this.Connections.properties.connections;
Y
yamahigashi 已提交
612

Y
yamahigashi 已提交
613 614
		var results = [];
		for ( var i = 0; i < conns.length; ++ i ) {
Y
yamahigashi 已提交
615

Y
yamahigashi 已提交
616
			if ( conns[ i ][ 0 ] == id ) {
Y
yamahigashi 已提交
617

Y
yamahigashi 已提交
618 619 620
				// 0 means scene root
				var res = conns[ i ][ 1 ] === 0 ? - 1 : conns[ i ][ 1 ];
				results.push( res );
Y
yamahigashi 已提交
621

Y
yamahigashi 已提交
622
			}
Y
yamahigashi 已提交
623

Y
yamahigashi 已提交
624
		}
Y
yamahigashi 已提交
625

Y
yamahigashi 已提交
626
		if ( results.length > 0 ) {
Y
yamahigashi 已提交
627

Y
yamahigashi 已提交
628 629
			this.__cache_search_connection_parent[ id ] = this.__cache_search_connection_parent[ id ].concat( results );
			return results;
Y
yamahigashi 已提交
630

Y
yamahigashi 已提交
631
		} else {
Y
yamahigashi 已提交
632

Y
yamahigashi 已提交
633 634
			this.__cache_search_connection_parent[ id ] = [ - 1 ];
			return [ - 1 ];
Y
yamahigashi 已提交
635

Y
yamahigashi 已提交
636
		}
Y
yamahigashi 已提交
637

Y
yamahigashi 已提交
638
	};
Y
yamahigashi 已提交
639

Y
yamahigashi 已提交
640
	FBXNodes.prototype.searchConnectionChildren = function ( id ) {
Y
yamahigashi 已提交
641

Y
yamahigashi 已提交
642
		if ( this.__cache_search_connection_children === undefined ) {
Y
yamahigashi 已提交
643

Y
yamahigashi 已提交
644
			this.__cache_search_connection_children = [];
Y
yamahigashi 已提交
645

Y
yamahigashi 已提交
646
		}
Y
yamahigashi 已提交
647

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

Y
yamahigashi 已提交
650
			return this.__cache_search_connection_children[ id ];
Y
yamahigashi 已提交
651

Y
yamahigashi 已提交
652
		} else {
Y
yamahigashi 已提交
653

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

Y
yamahigashi 已提交
656
		}
Y
yamahigashi 已提交
657

Y
yamahigashi 已提交
658
		var conns = this.Connections.properties.connections;
Y
yamahigashi 已提交
659

Y
yamahigashi 已提交
660 661
		var res = [];
		for ( var i = 0; i < conns.length; ++ i ) {
Y
yamahigashi 已提交
662

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

Y
yamahigashi 已提交
665 666 667
				// 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 已提交
668

Y
yamahigashi 已提交
669
			}
Y
yamahigashi 已提交
670

Y
yamahigashi 已提交
671
		}
Y
yamahigashi 已提交
672

Y
yamahigashi 已提交
673
		if ( res.length > 0 ) {
Y
yamahigashi 已提交
674

Y
yamahigashi 已提交
675 676
			this.__cache_search_connection_children[ id ] = this.__cache_search_connection_children[ id ].concat( res );
			return res;
Y
yamahigashi 已提交
677

Y
yamahigashi 已提交
678
		} else {
Y
yamahigashi 已提交
679

Y
yamahigashi 已提交
680 681
			this.__cache_search_connection_children[ id ] = [ - 1 ];
			return [ - 1 ];
Y
yamahigashi 已提交
682

Y
yamahigashi 已提交
683
		}
Y
yamahigashi 已提交
684

Y
yamahigashi 已提交
685
	};
Y
yamahigashi 已提交
686

Y
yamahigashi 已提交
687
	FBXNodes.prototype.searchConnectionType = function ( id, to ) {
Y
yamahigashi 已提交
688

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

K
Kyle Larson 已提交
692
			this.__cache_search_connection_type = {};
Y
yamahigashi 已提交
693

Y
yamahigashi 已提交
694
		}
Y
yamahigashi 已提交
695

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

Y
yamahigashi 已提交
698
			return this.__cache_search_connection_type[ key ];
Y
yamahigashi 已提交
699

Y
yamahigashi 已提交
700
		} else {
Y
yamahigashi 已提交
701

Y
yamahigashi 已提交
702
			this.__cache_search_connection_type[ key ] = '';
Y
yamahigashi 已提交
703

Y
yamahigashi 已提交
704
		}
Y
yamahigashi 已提交
705

Y
yamahigashi 已提交
706
		var conns = this.Connections.properties.connections;
Y
yamahigashi 已提交
707

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

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

Y
yamahigashi 已提交
712 713 714
				// 0 means scene root
				this.__cache_search_connection_type[ key ] = conns[ i ][ 2 ];
				return conns[ i ][ 2 ];
Y
yamahigashi 已提交
715

Y
yamahigashi 已提交
716
			}
Y
yamahigashi 已提交
717

Y
yamahigashi 已提交
718
		}
Y
yamahigashi 已提交
719

Y
yamahigashi 已提交
720 721
		this.__cache_search_connection_type[ id ] = null;
		return null;
Y
yamahigashi 已提交
722

Y
yamahigashi 已提交
723
	};
Y
yamahigashi 已提交
724

Y
yamahigashi 已提交
725
	function FBXParser() {}
Y
yamahigashi 已提交
726

Y
yamahigashi 已提交
727
	FBXParser.prototype = {
Y
yamahigashi 已提交
728

Y
yamahigashi 已提交
729
		// constructor: FBXParser,
Y
yamahigashi 已提交
730

Y
yamahigashi 已提交
731
		// ------------ node stack manipulations ----------------------------------
Y
yamahigashi 已提交
732

Y
yamahigashi 已提交
733
		getPrevNode: function () {
Y
yamahigashi 已提交
734

Y
yamahigashi 已提交
735
			return this.nodeStack[ this.currentIndent - 2 ];
Y
yamahigashi 已提交
736

Y
yamahigashi 已提交
737
		},
Y
yamahigashi 已提交
738

Y
yamahigashi 已提交
739
		getCurrentNode: function () {
Y
yamahigashi 已提交
740

Y
yamahigashi 已提交
741
			return this.nodeStack[ this.currentIndent - 1 ];
Y
yamahigashi 已提交
742

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

Y
yamahigashi 已提交
745
		getCurrentProp: function () {
Y
yamahigashi 已提交
746

Y
yamahigashi 已提交
747
			return this.currentProp;
Y
yamahigashi 已提交
748

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

Y
yamahigashi 已提交
751
		pushStack: function ( node ) {
Y
yamahigashi 已提交
752

Y
yamahigashi 已提交
753 754
			this.nodeStack.push( node );
			this.currentIndent += 1;
Y
yamahigashi 已提交
755

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

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

Y
yamahigashi 已提交
760 761
			this.nodeStack.pop();
			this.currentIndent -= 1;
Y
yamahigashi 已提交
762

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

Y
yamahigashi 已提交
765
		setCurrentProp: function ( val, name ) {
Y
yamahigashi 已提交
766

Y
yamahigashi 已提交
767 768
			this.currentProp = val;
			this.currentPropName = name;
Y
yamahigashi 已提交
769

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

Y
yamahigashi 已提交
772 773
		// ----------parse ---------------------------------------------------
		parse: function ( text ) {
Y
yamahigashi 已提交
774

Y
yamahigashi 已提交
775 776 777 778 779
			this.currentIndent = 0;
			this.allNodes = new FBXNodes();
			this.nodeStack = [];
			this.currentProp = [];
			this.currentPropName = '';
Y
yamahigashi 已提交
780

Y
yamahigashi 已提交
781 782
			var split = text.split( "\n" );
			for ( var line in split ) {
Y
yamahigashi 已提交
783

Y
yamahigashi 已提交
784
				var l = split[ line ];
Y
yamahigashi 已提交
785

Y
yamahigashi 已提交
786 787
				// short cut
				if ( l.match( /^[\s\t]*;/ ) ) {
Y
yamahigashi 已提交
788

Y
yamahigashi 已提交
789
					continue;
Y
yamahigashi 已提交
790

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

Y
yamahigashi 已提交
794
					continue;
Y
yamahigashi 已提交
795

Y
yamahigashi 已提交
796
				} // skip empty line
Y
yamahigashi 已提交
797

Y
yamahigashi 已提交
798 799
				// beginning of node
				var beginningOfNodeExp = new RegExp( "^\\t{" + this.currentIndent + "}(\\w+):(.*){", '' );
K
Kyle Larson 已提交
800
				var match = l.match( beginningOfNodeExp );
Y
yamahigashi 已提交
801
				if ( match ) {
Y
yamahigashi 已提交
802

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

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

Y
yamahigashi 已提交
808
					} );
Y
yamahigashi 已提交
809

Y
yamahigashi 已提交
810 811
					this.parseNodeBegin( l, nodeName, nodeAttrs || null );
					continue;
Y
yamahigashi 已提交
812

Y
yamahigashi 已提交
813
				}
Y
yamahigashi 已提交
814

Y
yamahigashi 已提交
815 816
				// node's property
				var propExp = new RegExp( "^\\t{" + ( this.currentIndent ) + "}(\\w+):[\\s\\t\\r\\n](.*)" );
817
				var match = l.match( propExp );
Y
yamahigashi 已提交
818
				if ( match ) {
Y
yamahigashi 已提交
819

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

Y
yamahigashi 已提交
823 824
					this.parseNodeProperty( l, propName, propValue );
					continue;
Y
yamahigashi 已提交
825

Y
yamahigashi 已提交
826
				}
Y
yamahigashi 已提交
827

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

Y
yamahigashi 已提交
832 833
					this.nodeEnd();
					continue;
Y
yamahigashi 已提交
834

Y
yamahigashi 已提交
835
				}
Y
yamahigashi 已提交
836

Y
yamahigashi 已提交
837 838 839 840 841 842 843 844 845 846
				// 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 已提交
847

Y
yamahigashi 已提交
848
					this.parseNodePropertyContinued( l );
Y
yamahigashi 已提交
849

Y
yamahigashi 已提交
850
				}
Y
yamahigashi 已提交
851

Y
yamahigashi 已提交
852
			}
Y
yamahigashi 已提交
853

Y
yamahigashi 已提交
854
			return this.allNodes;
Y
yamahigashi 已提交
855

Y
yamahigashi 已提交
856
		},
Y
yamahigashi 已提交
857

Y
yamahigashi 已提交
858
		parseNodeBegin: function ( line, nodeName, nodeAttrs ) {
Y
yamahigashi 已提交
859

Y
yamahigashi 已提交
860 861 862 863
			// var nodeName = match[1];
			var node = { 'name': nodeName, properties: {}, 'subNodes': {} };
			var attrs = this.parseNodeAttr( nodeAttrs );
			var currentNode = this.getCurrentNode();
Y
yamahigashi 已提交
864

Y
yamahigashi 已提交
865 866
			// a top node
			if ( this.currentIndent === 0 ) {
Y
yamahigashi 已提交
867

Y
yamahigashi 已提交
868
				this.allNodes.add( nodeName, node );
Y
yamahigashi 已提交
869

Y
yamahigashi 已提交
870
			} else {
Y
yamahigashi 已提交
871

Y
yamahigashi 已提交
872
				// a subnode
Y
yamahigashi 已提交
873

Y
yamahigashi 已提交
874 875
				// already exists subnode, then append it
				if ( nodeName in currentNode.subNodes ) {
Y
yamahigashi 已提交
876

Y
yamahigashi 已提交
877
					var tmp = currentNode.subNodes[ nodeName ];
Y
yamahigashi 已提交
878

Y
yamahigashi 已提交
879 880
					// console.log( "duped entry found\nkey: " + nodeName + "\nvalue: " + propValue );
					if ( this.isFlattenNode( currentNode.subNodes[ nodeName ] ) ) {
Y
yamahigashi 已提交
881 882


Y
yamahigashi 已提交
883
						if ( attrs.id === '' ) {
Y
yamahigashi 已提交
884

Y
yamahigashi 已提交
885 886
							currentNode.subNodes[ nodeName ] = [];
							currentNode.subNodes[ nodeName ].push( tmp );
Y
yamahigashi 已提交
887

Y
yamahigashi 已提交
888
						} else {
Y
yamahigashi 已提交
889

Y
yamahigashi 已提交
890 891
							currentNode.subNodes[ nodeName ] = {};
							currentNode.subNodes[ nodeName ][ tmp.id ] = tmp;
Y
yamahigashi 已提交
892

Y
yamahigashi 已提交
893
						}
Y
yamahigashi 已提交
894

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

Y
yamahigashi 已提交
897
					if ( attrs.id === '' ) {
Y
yamahigashi 已提交
898

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

Y
yamahigashi 已提交
901
					} else {
Y
yamahigashi 已提交
902

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

Y
yamahigashi 已提交
905
					}
Y
yamahigashi 已提交
906

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

Y
yamahigashi 已提交
909
					currentNode.subNodes[ nodeName ] = node;
Y
yamahigashi 已提交
910

Y
yamahigashi 已提交
911
				}
Y
yamahigashi 已提交
912

Y
yamahigashi 已提交
913
			}
Y
yamahigashi 已提交
914

Y
yamahigashi 已提交
915 916 917
			// for this		  ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
			// NodeAttribute: 1001463072, "NodeAttribute::", "LimbNode" {
			if ( nodeAttrs ) {
Y
yamahigashi 已提交
918

Y
yamahigashi 已提交
919 920 921
				node.id = attrs.id;
				node.attrName = attrs.name;
				node.attrType = attrs.type;
Y
yamahigashi 已提交
922

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

Y
yamahigashi 已提交
925
			this.pushStack( node );
Y
yamahigashi 已提交
926

Y
yamahigashi 已提交
927
		},
Y
yamahigashi 已提交
928

Y
yamahigashi 已提交
929
		parseNodeAttr: function ( attrs ) {
Y
yamahigashi 已提交
930

Y
yamahigashi 已提交
931
			var id = attrs[ 0 ];
Y
yamahigashi 已提交
932

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

Y
yamahigashi 已提交
935
				id = parseInt( attrs[ 0 ] );
Y
yamahigashi 已提交
936

Y
yamahigashi 已提交
937
				if ( isNaN( id ) ) {
Y
yamahigashi 已提交
938

Y
yamahigashi 已提交
939 940
					// PolygonVertexIndex: *16380 {
					id = attrs[ 0 ];
Y
yamahigashi 已提交
941

Y
yamahigashi 已提交
942
				}
Y
yamahigashi 已提交
943

Y
yamahigashi 已提交
944
			}
Y
yamahigashi 已提交
945

Y
yamahigashi 已提交
946 947 948
			var name;
			var type;
			if ( attrs.length > 1 ) {
Y
yamahigashi 已提交
949

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

Y
yamahigashi 已提交
953
			}
Y
yamahigashi 已提交
954

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

Y
yamahigashi 已提交
957
		},
Y
yamahigashi 已提交
958

Y
yamahigashi 已提交
959
		parseNodeProperty: function ( line, propName, propValue ) {
Y
yamahigashi 已提交
960

Y
yamahigashi 已提交
961 962
			var currentNode = this.getCurrentNode();
			var parentName = currentNode.name;
Y
yamahigashi 已提交
963

Y
yamahigashi 已提交
964 965 966
			// special case parent node's is like "Properties70"
			// these chilren nodes must treat with careful
			if ( parentName !== undefined ) {
Y
yamahigashi 已提交
967

Y
yamahigashi 已提交
968 969
				var propMatch = parentName.match( /Properties(\d)+/ );
				if ( propMatch ) {
Y
yamahigashi 已提交
970

Y
yamahigashi 已提交
971 972
					this.parseNodeSpecialProperty( line, propName, propValue );
					return;
Y
yamahigashi 已提交
973

Y
yamahigashi 已提交
974
				}
Y
yamahigashi 已提交
975

Y
yamahigashi 已提交
976
			}
Y
yamahigashi 已提交
977

Y
yamahigashi 已提交
978 979
			// special case Connections
			if ( propName == 'C' ) {
Y
yamahigashi 已提交
980

Y
yamahigashi 已提交
981 982 983
				var connProps = propValue.split( ',' ).slice( 1 );
				var from = parseInt( connProps[ 0 ] );
				var to = parseInt( connProps[ 1 ] );
Y
yamahigashi 已提交
984

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

Y
yamahigashi 已提交
987 988 989
				propName = 'connections';
				propValue = [ from, to ];
				propValue = propValue.concat( rest );
Y
yamahigashi 已提交
990

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

Y
yamahigashi 已提交
993
					currentNode.properties[ propName ] = [];
Y
yamahigashi 已提交
994

Y
yamahigashi 已提交
995
				}
Y
yamahigashi 已提交
996

Y
yamahigashi 已提交
997
			}
Y
yamahigashi 已提交
998

Y
yamahigashi 已提交
999 1000
			// special case Connections
			if ( propName == 'Node' ) {
Y
yamahigashi 已提交
1001

Y
yamahigashi 已提交
1002 1003 1004
				var id = parseInt( propValue );
				currentNode.properties.id = id;
				currentNode.id = id;
Y
yamahigashi 已提交
1005

Y
yamahigashi 已提交
1006
			}
Y
yamahigashi 已提交
1007

Y
yamahigashi 已提交
1008 1009
			// already exists in properties, then append this
			if ( propName in currentNode.properties ) {
Y
yamahigashi 已提交
1010

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

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

Y
yamahigashi 已提交
1016
				} else {
Y
yamahigashi 已提交
1017

Y
yamahigashi 已提交
1018
					currentNode.properties[ propName ] += propValue;
Y
yamahigashi 已提交
1019

Y
yamahigashi 已提交
1020
				}
Y
yamahigashi 已提交
1021

Y
yamahigashi 已提交
1022
			} else {
Y
yamahigashi 已提交
1023

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

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

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

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

Y
yamahigashi 已提交
1033
				}
Y
yamahigashi 已提交
1034

Y
yamahigashi 已提交
1035
			}
Y
yamahigashi 已提交
1036

Y
yamahigashi 已提交
1037
			this.setCurrentProp( currentNode.properties, propName );
Y
yamahigashi 已提交
1038

Y
yamahigashi 已提交
1039
		},
Y
yamahigashi 已提交
1040

Y
yamahigashi 已提交
1041 1042
		// TODO:
		parseNodePropertyContinued: function ( line ) {
Y
yamahigashi 已提交
1043

Y
yamahigashi 已提交
1044
			this.currentProp[ this.currentPropName ] += line;
Y
yamahigashi 已提交
1045

Y
yamahigashi 已提交
1046
		},
Y
yamahigashi 已提交
1047

Y
yamahigashi 已提交
1048
		parseNodeSpecialProperty: function ( line, propName, propValue ) {
Y
yamahigashi 已提交
1049

Y
yamahigashi 已提交
1050 1051 1052 1053 1054
			// 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 已提交
1055

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

Y
yamahigashi 已提交
1058
			} );
Y
yamahigashi 已提交
1059

Y
yamahigashi 已提交
1060 1061 1062 1063 1064
			var innerPropName = props[ 0 ];
			var innerPropType1 = props[ 1 ];
			var innerPropType2 = props[ 2 ];
			var innerPropFlag = props[ 3 ];
			var innerPropValue = props[ 4 ];
Y
yamahigashi 已提交
1065

Y
yamahigashi 已提交
1066 1067 1068 1069 1070
			/*
			if ( innerPropValue === undefined ) {
				innerPropValue = props[3];
			}
			*/
Y
yamahigashi 已提交
1071

Y
yamahigashi 已提交
1072 1073
			// cast value in its type
			switch ( innerPropType1 ) {
Y
yamahigashi 已提交
1074

Y
yamahigashi 已提交
1075 1076 1077
				case "int":
					innerPropValue = parseInt( innerPropValue );
					break;
Y
yamahigashi 已提交
1078

Y
yamahigashi 已提交
1079 1080 1081
				case "double":
					innerPropValue = parseFloat( innerPropValue );
					break;
Y
yamahigashi 已提交
1082

Y
yamahigashi 已提交
1083 1084 1085 1086 1087
				case "ColorRGB":
				case "Vector3D":
					var tmp = innerPropValue.split( ',' );
					innerPropValue = new THREE.Vector3( tmp[ 0 ], tmp[ 1 ], tmp[ 2 ] );
					break;
Y
yamahigashi 已提交
1088

Y
yamahigashi 已提交
1089
			}
Y
yamahigashi 已提交
1090

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

Y
yamahigashi 已提交
1094 1095 1096 1097
				'type': innerPropType1,
				'type2': innerPropType2,
				'flag': innerPropFlag,
				'value': innerPropValue
Y
yamahigashi 已提交
1098

Y
yamahigashi 已提交
1099
			};
Y
yamahigashi 已提交
1100

Y
yamahigashi 已提交
1101
			this.setCurrentProp( this.getPrevNode().properties, innerPropName );
Y
yamahigashi 已提交
1102

Y
yamahigashi 已提交
1103
		},
Y
yamahigashi 已提交
1104

Y
yamahigashi 已提交
1105
		nodeEnd: function ( line ) {
Y
yamahigashi 已提交
1106

Y
yamahigashi 已提交
1107
			this.popStack();
Y
yamahigashi 已提交
1108

Y
yamahigashi 已提交
1109
		},
Y
yamahigashi 已提交
1110

Y
yamahigashi 已提交
1111 1112 1113
		/* ---------------------------------------------------------------- */
		/*		util													  */
		isFlattenNode: function ( node ) {
Y
yamahigashi 已提交
1114

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

Y
yamahigashi 已提交
1117
		}
Y
yamahigashi 已提交
1118

Y
yamahigashi 已提交
1119
	};
Y
yamahigashi 已提交
1120

Y
yamahigashi 已提交
1121
	function FBXAnalyzer() {}
Y
yamahigashi 已提交
1122

Y
yamahigashi 已提交
1123
	FBXAnalyzer.prototype = {
Y
yamahigashi 已提交
1124

Y
yamahigashi 已提交
1125
	};
Y
yamahigashi 已提交
1126 1127


Y
yamahigashi 已提交
1128 1129 1130 1131 1132
	// 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 已提交
1133

Y
yamahigashi 已提交
1134 1135
		this.skinIndices = [];
		this.skinWeights = [];
Y
yamahigashi 已提交
1136

Y
yamahigashi 已提交
1137
		this.matrices	= [];
Y
yamahigashi 已提交
1138

Y
yamahigashi 已提交
1139
	}
Y
yamahigashi 已提交
1140 1141


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

Y
yamahigashi 已提交
1144 1145 1146 1147 1148
		var _p = node.searchConnectionParent( id );
		var _indices = toInt( entry.subNodes.Indexes.properties.a.split( ',' ) );
		var _weights = toFloat( entry.subNodes.Weights.properties.a.split( ',' ) );
		var _transform = toMat44( toFloat( entry.subNodes.Transform.properties.a.split( ',' ) ) );
		var _link = toMat44( toFloat( entry.subNodes.TransformLink.properties.a.split( ',' ) ) );
Y
yamahigashi 已提交
1149

Y
yamahigashi 已提交
1150
		return {
Y
yamahigashi 已提交
1151

Y
yamahigashi 已提交
1152 1153 1154 1155 1156 1157 1158
			'parent': _p,
			'id': parseInt( id ),
			'indices': _indices,
			'weights': _weights,
			'transform': _transform,
			'transformlink': _link,
			'linkMode': entry.properties.Mode
Y
yamahigashi 已提交
1159

Y
yamahigashi 已提交
1160
		};
Y
yamahigashi 已提交
1161

Y
yamahigashi 已提交
1162
	};
Y
yamahigashi 已提交
1163

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

Y
yamahigashi 已提交
1166 1167
		this.skinIndices = [];
		this.skinWeights = [];
Y
yamahigashi 已提交
1168

Y
yamahigashi 已提交
1169
		this.matrices = [];
Y
yamahigashi 已提交
1170

Y
yamahigashi 已提交
1171
		var deformers = node.Objects.subNodes.Deformer;
Y
yamahigashi 已提交
1172

Y
yamahigashi 已提交
1173 1174
		var clusters = {};
		for ( var id in deformers ) {
Y
yamahigashi 已提交
1175

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

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

Y
yamahigashi 已提交
1180
					continue;
Y
yamahigashi 已提交
1181

Y
yamahigashi 已提交
1182
				}
Y
yamahigashi 已提交
1183

Y
yamahigashi 已提交
1184 1185 1186 1187
				//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 已提交
1188

Y
yamahigashi 已提交
1189
			}
Y
yamahigashi 已提交
1190

Y
yamahigashi 已提交
1191
		}
Y
yamahigashi 已提交
1192 1193


Y
yamahigashi 已提交
1194 1195 1196 1197
		// 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 已提交
1198

Y
yamahigashi 已提交
1199 1200
			var bid = hi[ b ].internalId;
			if ( clusters[ bid ] === undefined ) {
Y
yamahigashi 已提交
1201

Y
yamahigashi 已提交
1202 1203 1204
				//console.log( bid );
				this.matrices.push( new THREE.Matrix4() );
				continue;
Y
yamahigashi 已提交
1205

Y
yamahigashi 已提交
1206
			}
Y
yamahigashi 已提交
1207

Y
yamahigashi 已提交
1208 1209 1210 1211 1212
			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 已提交
1213

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

Y
yamahigashi 已提交
1216 1217 1218
					weights[ clst.indices[ v ] ] = {};
					weights[ clst.indices[ v ] ].joint = [];
					weights[ clst.indices[ v ] ].weight = [];
Y
yamahigashi 已提交
1219

Y
yamahigashi 已提交
1220
				}
Y
yamahigashi 已提交
1221

Y
yamahigashi 已提交
1222 1223
				// indices
				var affect = node.searchConnectionChildren( clst.id );
Y
yamahigashi 已提交
1224

Y
yamahigashi 已提交
1225
				if ( affect.length > 1 ) {
Y
yamahigashi 已提交
1226

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

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

Y
yamahigashi 已提交
1232 1233
				// weight value
				weights[ clst.indices[ v ] ].weight.push( clst.weights[ v ] );
Y
yamahigashi 已提交
1234

Y
yamahigashi 已提交
1235
			}
Y
yamahigashi 已提交
1236

Y
yamahigashi 已提交
1237
		}
Y
yamahigashi 已提交
1238

Y
yamahigashi 已提交
1239 1240 1241
		// 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 已提交
1242

Y
yamahigashi 已提交
1243 1244 1245 1246 1247
			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 已提交
1248

Y
yamahigashi 已提交
1249 1250 1251 1252 1253
			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 已提交
1254

Y
yamahigashi 已提交
1255 1256
			this.skinIndices.push( indicies );
			this.skinWeights.push( weight );
Y
yamahigashi 已提交
1257

Y
yamahigashi 已提交
1258
		}
Y
yamahigashi 已提交
1259

Y
yamahigashi 已提交
1260 1261
		//console.log( this );
		return this;
Y
yamahigashi 已提交
1262

Y
yamahigashi 已提交
1263
	};
Y
yamahigashi 已提交
1264

Y
yamahigashi 已提交
1265
	function Bones() {
Y
yamahigashi 已提交
1266

Y
yamahigashi 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
		// returns bones hierarchy tree.
		//	  [
		//		  {
		//			  "parent": id,
		//			  "name": name,
		//			  "pos": pos,
		//			  "rotq": quat
		//		  },
		//		  ...
		//		  {},
		//		  ...
		//	  ]
		//
		/* sample response
Y
yamahigashi 已提交
1281

Y
yamahigashi 已提交
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
		   "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 已提交
1314

Y
yamahigashi 已提交
1315
	}
Y
yamahigashi 已提交
1316

Y
yamahigashi 已提交
1317
	Bones.prototype.parseHierarchy = function ( node ) {
Y
yamahigashi 已提交
1318

Y
yamahigashi 已提交
1319 1320
		var objects = node.Objects;
		var models = objects.subNodes.Model;
Y
yamahigashi 已提交
1321

Y
yamahigashi 已提交
1322 1323
		var bones = [];
		for ( var id in models ) {
Y
yamahigashi 已提交
1324

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

Y
yamahigashi 已提交
1327
				continue;
Y
yamahigashi 已提交
1328

Y
yamahigashi 已提交
1329 1330
			}
			bones.push( models[ id ] );
Y
yamahigashi 已提交
1331

Y
yamahigashi 已提交
1332
		}
Y
yamahigashi 已提交
1333

Y
yamahigashi 已提交
1334 1335
		this.hierarchy = [];
		for ( var i = 0; i < bones.length; ++ i ) {
Y
yamahigashi 已提交
1336

Y
yamahigashi 已提交
1337
			var bone = bones[ i ];
Y
yamahigashi 已提交
1338

Y
yamahigashi 已提交
1339 1340 1341 1342
			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 已提交
1343

Y
yamahigashi 已提交
1344
			if ( 'Lcl_Translation' in bone.properties ) {
Y
yamahigashi 已提交
1345

Y
yamahigashi 已提交
1346
				t = toFloat( bone.properties.Lcl_Translation.value.split( ',' ) );
Y
yamahigashi 已提交
1347

Y
yamahigashi 已提交
1348
			}
Y
yamahigashi 已提交
1349

Y
yamahigashi 已提交
1350
			if ( 'Lcl_Rotation' in bone.properties ) {
Y
yamahigashi 已提交
1351

Y
yamahigashi 已提交
1352 1353 1354 1355
				r = toRad( toFloat( bone.properties.Lcl_Rotation.value.split( ',' ) ) );
				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 已提交
1356

Y
yamahigashi 已提交
1357
			}
Y
yamahigashi 已提交
1358

Y
yamahigashi 已提交
1359
			if ( 'Lcl_Scaling' in bone.properties ) {
Y
yamahigashi 已提交
1360

Y
yamahigashi 已提交
1361
				s = toFloat( bone.properties.Lcl_Scaling.value.split( ',' ) );
Y
yamahigashi 已提交
1362

Y
yamahigashi 已提交
1363
			}
Y
yamahigashi 已提交
1364

Y
yamahigashi 已提交
1365 1366 1367 1368 1369 1370
			// 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 已提交
1371

Y
yamahigashi 已提交
1372
		}
Y
yamahigashi 已提交
1373

Y
yamahigashi 已提交
1374
		this.reindexParentId();
Y
yamahigashi 已提交
1375

Y
yamahigashi 已提交
1376
		this.restoreBindPose( node );
Y
yamahigashi 已提交
1377

Y
yamahigashi 已提交
1378
		return this;
Y
yamahigashi 已提交
1379

Y
yamahigashi 已提交
1380
	};
Y
yamahigashi 已提交
1381

Y
yamahigashi 已提交
1382
	Bones.prototype.reindexParentId = function () {
Y
yamahigashi 已提交
1383

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

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

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

Y
yamahigashi 已提交
1390 1391
					this.hierarchy[ h ].parent = ii;
					break;
Y
yamahigashi 已提交
1392

Y
yamahigashi 已提交
1393
				}
Y
yamahigashi 已提交
1394

Y
yamahigashi 已提交
1395
			}
Y
yamahigashi 已提交
1396

Y
yamahigashi 已提交
1397
		}
Y
yamahigashi 已提交
1398

Y
yamahigashi 已提交
1399
	};
Y
yamahigashi 已提交
1400

Y
yamahigashi 已提交
1401
	Bones.prototype.restoreBindPose = function ( node ) {
Y
yamahigashi 已提交
1402

Y
yamahigashi 已提交
1403 1404
		var bindPoseNode = node.Objects.subNodes.Pose;
		if ( bindPoseNode === undefined ) {
Y
yamahigashi 已提交
1405

Y
yamahigashi 已提交
1406
			return;
Y
yamahigashi 已提交
1407

Y
yamahigashi 已提交
1408
		}
Y
yamahigashi 已提交
1409

Y
yamahigashi 已提交
1410 1411 1412
		var poseNode = bindPoseNode.subNodes.PoseNode;
		var localMatrices = {}; // store local matrices, modified later( initialy world space )
		var worldMatrices = {}; // store world matrices
Y
yamahigashi 已提交
1413

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

Y
yamahigashi 已提交
1416 1417
			var rawMatLcl = toMat44( poseNode[ i ].subNodes.Matrix.properties.a.split( ',' ) );
			var rawMatWrd = toMat44( poseNode[ i ].subNodes.Matrix.properties.a.split( ',' ) );
Y
yamahigashi 已提交
1418

Y
yamahigashi 已提交
1419 1420
			localMatrices[ poseNode[ i ].id ] = rawMatLcl;
			worldMatrices[ poseNode[ i ].id ] = rawMatWrd;
Y
yamahigashi 已提交
1421

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

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

Y
yamahigashi 已提交
1426 1427
			var bone = this.hierarchy[ h ];
			var inId = bone.internalId;
Y
yamahigashi 已提交
1428

Y
yamahigashi 已提交
1429
			if ( worldMatrices[ inId ] === undefined ) {
Y
yamahigashi 已提交
1430

Y
yamahigashi 已提交
1431 1432 1433
				// has no bind pose node, possibly be mesh
				// console.log( bone );
				continue;
Y
yamahigashi 已提交
1434

Y
yamahigashi 已提交
1435
			}
Y
yamahigashi 已提交
1436

Y
yamahigashi 已提交
1437 1438 1439
			var t = new THREE.Vector3( 0, 0, 0 );
			var r = new THREE.Quaternion();
			var s = new THREE.Vector3( 1, 1, 1 );
Y
yamahigashi 已提交
1440

Y
yamahigashi 已提交
1441 1442 1443
			var parentId;
			var parentNodes = node.searchConnectionParent( inId );
			for ( var pn = 0; pn < parentNodes.length; ++ pn ) {
Y
yamahigashi 已提交
1444

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

Y
yamahigashi 已提交
1447 1448
					parentId = parentNodes[ pn ];
					break;
Y
yamahigashi 已提交
1449

Y
yamahigashi 已提交
1450
				}
Y
yamahigashi 已提交
1451

Y
yamahigashi 已提交
1452
			}
Y
yamahigashi 已提交
1453

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

Y
yamahigashi 已提交
1456 1457 1458 1459 1460
				// 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 已提交
1461

Y
yamahigashi 已提交
1462 1463 1464
			} else {
				//console.log( bone );
			}
Y
yamahigashi 已提交
1465

Y
yamahigashi 已提交
1466 1467 1468 1469
			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 已提交
1470

Y
yamahigashi 已提交
1471
		}
Y
yamahigashi 已提交
1472

Y
yamahigashi 已提交
1473
	};
Y
yamahigashi 已提交
1474

Y
yamahigashi 已提交
1475
	Bones.prototype.searchRealId = function ( internalId ) {
Y
yamahigashi 已提交
1476

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

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

Y
yamahigashi 已提交
1481
				return h;
Y
yamahigashi 已提交
1482

Y
yamahigashi 已提交
1483
			}
Y
yamahigashi 已提交
1484

Y
yamahigashi 已提交
1485
		}
Y
yamahigashi 已提交
1486

Y
yamahigashi 已提交
1487 1488
		// console.warn( 'FBXLoader: notfound internalId in bones: ' + internalId);
		return - 1;
Y
yamahigashi 已提交
1489

Y
yamahigashi 已提交
1490
	};
Y
yamahigashi 已提交
1491

Y
yamahigashi 已提交
1492
	Bones.prototype.getByInternalId = function ( internalId ) {
Y
yamahigashi 已提交
1493

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

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

Y
yamahigashi 已提交
1498
				return this.hierarchy[ h ];
Y
yamahigashi 已提交
1499

Y
yamahigashi 已提交
1500
			}
Y
yamahigashi 已提交
1501

Y
yamahigashi 已提交
1502
		}
Y
yamahigashi 已提交
1503

Y
yamahigashi 已提交
1504
		return null;
Y
yamahigashi 已提交
1505

Y
yamahigashi 已提交
1506
	};
Y
yamahigashi 已提交
1507

Y
yamahigashi 已提交
1508
	Bones.prototype.isBoneNode = function ( id ) {
Y
yamahigashi 已提交
1509

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

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

Y
yamahigashi 已提交
1514
				return true;
Y
yamahigashi 已提交
1515

Y
yamahigashi 已提交
1516
			}
Y
yamahigashi 已提交
1517

Y
yamahigashi 已提交
1518 1519
		}
		return false;
Y
yamahigashi 已提交
1520

Y
yamahigashi 已提交
1521
	};
Y
yamahigashi 已提交
1522

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

Y
yamahigashi 已提交
1525
		if ( node.__cache_get_boneid_from_internalid === undefined ) {
Y
yamahigashi 已提交
1526

Y
yamahigashi 已提交
1527
			node.__cache_get_boneid_from_internalid = [];
Y
yamahigashi 已提交
1528

Y
yamahigashi 已提交
1529
		}
Y
yamahigashi 已提交
1530

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

Y
yamahigashi 已提交
1533
			return node.__cache_get_boneid_from_internalid[ id ];
Y
yamahigashi 已提交
1534

Y
yamahigashi 已提交
1535
		}
Y
yamahigashi 已提交
1536

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

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

Y
yamahigashi 已提交
1541 1542 1543
				var res = i;
				node.__cache_get_boneid_from_internalid[ id ] = i;
				return i;
Y
yamahigashi 已提交
1544

Y
yamahigashi 已提交
1545
			}
Y
yamahigashi 已提交
1546

Y
yamahigashi 已提交
1547
		}
Y
yamahigashi 已提交
1548

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

Y
yamahigashi 已提交
1552
	};
Y
yamahigashi 已提交
1553 1554


Y
yamahigashi 已提交
1555
	function Geometry() {
Y
yamahigashi 已提交
1556

Y
yamahigashi 已提交
1557 1558 1559
		this.node = null;
		this.name = null;
		this.id = null;
Y
yamahigashi 已提交
1560

Y
yamahigashi 已提交
1561 1562 1563 1564
		this.vertices = [];
		this.indices = [];
		this.normals = [];
		this.uvs = [];
Y
yamahigashi 已提交
1565

Y
yamahigashi 已提交
1566 1567
		this.bones = [];
		this.skins = null;
Y
yamahigashi 已提交
1568

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

Y
yamahigashi 已提交
1571
	Geometry.prototype.parse = function ( geoNode ) {
Y
yamahigashi 已提交
1572

Y
yamahigashi 已提交
1573 1574 1575
		this.node = geoNode;
		this.name = geoNode.attrName;
		this.id = geoNode.id;
Y
yamahigashi 已提交
1576

Y
yamahigashi 已提交
1577
		this.vertices = this.getVertices();
Y
yamahigashi 已提交
1578

Y
yamahigashi 已提交
1579
		if ( this.vertices === undefined ) {
Y
yamahigashi 已提交
1580

Y
yamahigashi 已提交
1581 1582
			console.log( 'FBXLoader: Geometry.parse(): pass' + this.node.id );
			return;
Y
yamahigashi 已提交
1583

Y
yamahigashi 已提交
1584
		}
Y
yamahigashi 已提交
1585

Y
yamahigashi 已提交
1586 1587 1588
		this.indices = this.getPolygonVertexIndices();
		this.uvs = ( new UV() ).parse( this.node, this );
		this.normals = ( new Normal() ).parse( this.node, this );
Y
yamahigashi 已提交
1589

Y
yamahigashi 已提交
1590
		if ( this.getPolygonTopologyMax() > 3 ) {
Y
yamahigashi 已提交
1591

Y
yamahigashi 已提交
1592 1593
			this.indices = this.convertPolyIndicesToTri(
								this.indices, this.getPolygonTopologyArray() );
Y
yamahigashi 已提交
1594

Y
yamahigashi 已提交
1595
		}
Y
yamahigashi 已提交
1596

Y
yamahigashi 已提交
1597
		return this;
Y
yamahigashi 已提交
1598

Y
yamahigashi 已提交
1599
	};
Y
yamahigashi 已提交
1600 1601


Y
yamahigashi 已提交
1602
	Geometry.prototype.getVertices = function () {
Y
yamahigashi 已提交
1603

Y
yamahigashi 已提交
1604
		if ( this.node.__cache_vertices ) {
Y
yamahigashi 已提交
1605

Y
yamahigashi 已提交
1606
			return this.node.__cache_vertices;
Y
yamahigashi 已提交
1607

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

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

Y
yamahigashi 已提交
1612 1613 1614
			console.warn( 'this.node: ' + this.node.attrName + "(" + this.node.id + ") does not have Vertices" );
			this.node.__cache_vertices = undefined;
			return null;
Y
yamahigashi 已提交
1615

Y
yamahigashi 已提交
1616
		}
Y
yamahigashi 已提交
1617

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

Y
yamahigashi 已提交
1621
			return parseFloat( element );
Y
yamahigashi 已提交
1622

Y
yamahigashi 已提交
1623
		} );
Y
yamahigashi 已提交
1624

Y
yamahigashi 已提交
1625 1626
		this.node.__cache_vertices = vertices;
		return this.node.__cache_vertices;
Y
yamahigashi 已提交
1627

Y
yamahigashi 已提交
1628
	};
Y
yamahigashi 已提交
1629

Y
yamahigashi 已提交
1630
	Geometry.prototype.getPolygonVertexIndices = function () {
Y
yamahigashi 已提交
1631

Y
yamahigashi 已提交
1632
		if ( this.node.__cache_indices && this.node.__cache_poly_topology_max ) {
Y
yamahigashi 已提交
1633

Y
yamahigashi 已提交
1634
			return this.node.__cache_indices;
Y
yamahigashi 已提交
1635

Y
yamahigashi 已提交
1636
		}
Y
yamahigashi 已提交
1637

Y
yamahigashi 已提交
1638
		if ( this.node.subNodes === undefined ) {
Y
yamahigashi 已提交
1639

Y
yamahigashi 已提交
1640 1641 1642
			console.error( 'this.node.subNodes undefined' );
			console.log( this.node );
			return;
Y
yamahigashi 已提交
1643

Y
yamahigashi 已提交
1644
		}
Y
yamahigashi 已提交
1645

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

Y
yamahigashi 已提交
1648 1649 1650
			console.warn( 'this.node: ' + this.node.attrName + "(" + this.node.id + ") does not have PolygonVertexIndex " );
			this.node.__cache_indices = undefined;
			return;
Y
yamahigashi 已提交
1651

Y
yamahigashi 已提交
1652
		}
Y
yamahigashi 已提交
1653

Y
yamahigashi 已提交
1654 1655
		var rawTextIndices = this.node.subNodes.PolygonVertexIndex.properties.a;
		var indices = rawTextIndices.split( ',' );
Y
yamahigashi 已提交
1656

Y
yamahigashi 已提交
1657 1658 1659
		var currentTopo = 1;
		var topologyN = null;
		var topologyArr = [];
Y
yamahigashi 已提交
1660

Y
yamahigashi 已提交
1661 1662 1663 1664
		// 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 已提交
1665

Y
yamahigashi 已提交
1666 1667 1668
			var tmpI = parseInt( indices[ i ] );
			// found n
			if ( tmpI < 0 ) {
Y
yamahigashi 已提交
1669

Y
yamahigashi 已提交
1670
				if ( currentTopo > topologyN ) {
Y
yamahigashi 已提交
1671

Y
yamahigashi 已提交
1672
					topologyN = currentTopo;
Y
yamahigashi 已提交
1673

Y
yamahigashi 已提交
1674
				}
Y
yamahigashi 已提交
1675

Y
yamahigashi 已提交
1676 1677 1678
				indices[ i ] = tmpI ^ - 1;
				topologyArr.push( currentTopo );
				currentTopo = 1;
Y
yamahigashi 已提交
1679

Y
yamahigashi 已提交
1680
			} else {
Y
yamahigashi 已提交
1681

Y
yamahigashi 已提交
1682 1683
				indices[ i ] = tmpI;
				currentTopo ++;
Y
yamahigashi 已提交
1684

Y
yamahigashi 已提交
1685
			}
Y
yamahigashi 已提交
1686

Y
yamahigashi 已提交
1687
		}
Y
yamahigashi 已提交
1688

Y
yamahigashi 已提交
1689
		if ( topologyN === null ) {
Y
yamahigashi 已提交
1690

Y
yamahigashi 已提交
1691 1692 1693
			console.warn( "FBXLoader: topology N not found: " + this.node.attrName );
			console.warn( this.node );
			topologyN = 3;
Y
yamahigashi 已提交
1694

Y
yamahigashi 已提交
1695
		}
Y
yamahigashi 已提交
1696

Y
yamahigashi 已提交
1697 1698 1699
		this.node.__cache_poly_topology_max = topologyN;
		this.node.__cache_poly_topology_arr = topologyArr;
		this.node.__cache_indices = indices;
Y
yamahigashi 已提交
1700

Y
yamahigashi 已提交
1701
		return this.node.__cache_indices;
Y
yamahigashi 已提交
1702

Y
yamahigashi 已提交
1703
	};
Y
yamahigashi 已提交
1704

Y
yamahigashi 已提交
1705
	Geometry.prototype.getPolygonTopologyMax = function () {
Y
yamahigashi 已提交
1706

Y
yamahigashi 已提交
1707
		if ( this.node.__cache_indices && this.node.__cache_poly_topology_max ) {
Y
yamahigashi 已提交
1708

Y
yamahigashi 已提交
1709
			return this.node.__cache_poly_topology_max;
Y
yamahigashi 已提交
1710

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

Y
yamahigashi 已提交
1713 1714
		this.getPolygonVertexIndices( this.node );
		return this.node.__cache_poly_topology_max;
Y
yamahigashi 已提交
1715

Y
yamahigashi 已提交
1716
	};
Y
yamahigashi 已提交
1717

Y
yamahigashi 已提交
1718
	Geometry.prototype.getPolygonTopologyArray = function () {
Y
yamahigashi 已提交
1719

Y
yamahigashi 已提交
1720
		if ( this.node.__cache_indices && this.node.__cache_poly_topology_max ) {
Y
yamahigashi 已提交
1721

Y
yamahigashi 已提交
1722
			return this.node.__cache_poly_topology_arr;
Y
yamahigashi 已提交
1723

Y
yamahigashi 已提交
1724
		}
Y
yamahigashi 已提交
1725

Y
yamahigashi 已提交
1726 1727
		this.getPolygonVertexIndices( this.node );
		return this.node.__cache_poly_topology_arr;
Y
yamahigashi 已提交
1728

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

Y
yamahigashi 已提交
1731 1732 1733 1734 1735 1736 1737
	// a - d
	// |   |
	// b - c
	//
	// [( a, b, c, d ) ...........
	// [( a, b, c ), (a, c, d )....
	Geometry.prototype.convertPolyIndicesToTri = function ( indices, strides ) {
Y
yamahigashi 已提交
1738

Y
yamahigashi 已提交
1739
		var res = [];
Y
yamahigashi 已提交
1740

Y
yamahigashi 已提交
1741 1742 1743 1744
		var i = 0;
		var tmp = [];
		var currentPolyNum = 0;
		var currentStride = 0;
Y
yamahigashi 已提交
1745

Y
yamahigashi 已提交
1746
		while ( i < indices.length ) {
Y
yamahigashi 已提交
1747

Y
yamahigashi 已提交
1748
			currentStride = strides[ currentPolyNum ];
Y
yamahigashi 已提交
1749

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

Y
yamahigashi 已提交
1753 1754 1755
				res.push( indices[ i ] );
				res.push( indices[ i + ( currentStride - 2 - j ) ] );
				res.push( indices[ i + ( currentStride - 1 - j ) ] );
Y
yamahigashi 已提交
1756

Y
yamahigashi 已提交
1757
			}
Y
yamahigashi 已提交
1758

Y
yamahigashi 已提交
1759 1760
			currentPolyNum ++;
			i += currentStride;
Y
yamahigashi 已提交
1761

Y
yamahigashi 已提交
1762
		}
Y
yamahigashi 已提交
1763

Y
yamahigashi 已提交
1764
		return res;
Y
yamahigashi 已提交
1765

Y
yamahigashi 已提交
1766
	};
Y
yamahigashi 已提交
1767

Y
yamahigashi 已提交
1768
	Geometry.prototype.addBones = function ( bones ) {
Y
yamahigashi 已提交
1769

Y
yamahigashi 已提交
1770
		this.bones = bones;
Y
yamahigashi 已提交
1771

Y
yamahigashi 已提交
1772
	};
Y
yamahigashi 已提交
1773 1774


Y
yamahigashi 已提交
1775
	function UV() {
Y
yamahigashi 已提交
1776

Y
yamahigashi 已提交
1777 1778 1779 1780 1781
		this.uv = null;
		this.map = null;
		this.ref = null;
		this.node = null;
		this.index = null;
Y
yamahigashi 已提交
1782

Y
yamahigashi 已提交
1783
	}
Y
yamahigashi 已提交
1784

Y
yamahigashi 已提交
1785
	UV.prototype.getUV = function ( node ) {
Y
yamahigashi 已提交
1786

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

Y
yamahigashi 已提交
1789
			return this.uv;
Y
yamahigashi 已提交
1790

Y
yamahigashi 已提交
1791
		} else {
Y
yamahigashi 已提交
1792

Y
yamahigashi 已提交
1793
			return this._parseText( node );
Y
yamahigashi 已提交
1794

Y
yamahigashi 已提交
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 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 1889 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 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 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 1997 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 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 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 2196 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 2261 2262 2263 2264 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 2293 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 2341 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 2377 2378 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 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 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
		}

	};

	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 ) {

		var uvNode = this.getNode( node );
		if ( uvNode === undefined ) {

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

		}

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

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

				count ++;
				x = n;

			}

		}

		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;


		this.uv	= toFloat( uvs.split( ',' ) );
		this.index = toInt( uvIndex.split( ',' ) );

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

		return this.uv;

	};

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

		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;
		}

		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 ) {

		var normalNode = this.getNode( node );

		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;
		this.normal = toFloat( rawTextNormals.split( ',' ) );

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

	};

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

		var normals = this.getNormal( topnode );
		var normalNode = this.getNode( topnode );
		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;
		}

		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;

		this.times = toFloat( this.times.split(	',' ) );
		this.values = toFloat( this.values.split( ',' ) );
		this.attrData = toFloat( this.attrData.split( ',' ) );
		this.attrFlag = toInt( this.attrFlag.split( ',' ) );

		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
		this.curves = [];	// AnimationCurve refs

	}

	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;
				this.containerId = this.containerIndices [ i ];

			}

			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;

		// 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 = [];
		var max = 0.0;
		for ( key in rawCurves ) {

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

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

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

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

					axis = 'x';

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

					axis = 'y';

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

					axis = 'z';

				}

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

			}

		}

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

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

				this.curves[ id ] = {};

			}

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

		}

		this.length = max;
		this.frames = this.length * this.fps;

		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 ] );

		}

	};

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

		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;

	};

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 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 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 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
	function Materials() {
		this.materials = [];
		this.perGeoMap = {};
	}

	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 ] );

		}

	};

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

		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;

	};

	Material.prototype.getParameters = function( properties ) {
		var parameters = {};

		//TODO: Missing parameters:
		// - Ambient
		// - AmbientColor
		// - (Diffuse?) Using DiffuseColor, which has same value, so I dunno.
		// - (Emissive?) Same as above)
		// - MultiLayer
		// - ShininessExponent (Same vals as Shininess)
		// - Specular (Same vals as SpecularColor)
		// - TransparencyFactor (Maybe same as Opacity?).

		parameters.color = new THREE.Color().fromArray(toFloat([properties.DiffuseColor.value.x, properties.DiffuseColor.value.y, properties.DiffuseColor.value.z]));
		parameters.specular = new THREE.Color().fromArray(toFloat([properties.SpecularColor.value.x, properties.SpecularColor.value.y, properties.SpecularColor.value.z]));
		parameters.shininess = properties.Shininess.value;
		parameters.emissive = new THREE.Color().fromArray(toFloat([properties.EmissiveColor.value.x, properties.EmissiveColor.value.y, properties.EmissiveColor.value.z]));
		parameters.emissiveIntensity = properties.EmissiveFactor.value;
		parameters.reflectivity = properties.Reflectivity.value;
		parameters.opacity = properties.Opacity.value;
		if(parameters.opacity < 1.0) {
			parameters.transparent = true;
		}

		return parameters;
	};

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

		var p = nodes.searchConnectionParent( id );

		return p;

	};

Y
yamahigashi 已提交
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 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709

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

	function loadTextureImage( texture, url ) {

		var loader = new THREE.ImageLoader();

		loader.load( url, function ( image ) {


		} );

		loader.load( url, function ( image ) {

			texture.image = image;
			texture.needUpdate = true;
			console.log( 'tex load done' );

		},

		// Function called when download progresses
			function ( xhr ) {

				console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );

			},

			// Function called when download errors
			function ( xhr ) {

				console.log( 'An error happened' );

			}
		);

	}

	// 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 已提交
2710
	var parse_Data_ByPolygonVertex_Direct = function ( node, indices, strides, itemSize ) {
Y
yamahigashi 已提交
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 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 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 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 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819

		// *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
	var FBXTimeToSeconds = function ( adskTime ) {

		return adskTime / 46186158000;

	};

K
Kyle Larson 已提交
2820
	var degToRad = function ( degrees ) {
Y
yamahigashi 已提交
2821 2822 2823 2824 2825

		return degrees * Math.PI / 180;

	};

K
Kyle Larson 已提交
2826
	var radToDeg = function ( radians ) {
Y
yamahigashi 已提交
2827 2828 2829 2830 2831

		return radians * 180 / Math.PI;

	};

K
Kyle Larson 已提交
2832
	var quatFromVec = function ( x, y, z ) {
Y
yamahigashi 已提交
2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843

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

		return quat;

	};


	// extend Array.prototype ?  ....uuuh
K
Kyle Larson 已提交
2844
	var toInt = function ( arr ) {
Y
yamahigashi 已提交
2845 2846 2847 2848 2849 2850 2851 2852 2853

		return arr.map( function ( element ) {

			return parseInt( element );

		} );

	};

K
Kyle Larson 已提交
2854
	var toFloat = function ( arr ) {
Y
yamahigashi 已提交
2855 2856 2857 2858 2859 2860 2861 2862 2863

		return arr.map( function ( element ) {

			return parseFloat( element );

		} );

	};

K
Kyle Larson 已提交
2864
	var toRad = function ( arr ) {
Y
yamahigashi 已提交
2865 2866 2867 2868 2869 2870 2871 2872 2873

		return arr.map( function ( element ) {

			return degToRad( element );

		} );

	};

K
Kyle Larson 已提交
2874
	var toMat44 = function ( arr ) {
Y
yamahigashi 已提交
2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895

		var mat = new THREE.Matrix4();
		mat.set(
			arr[ 0 ], arr[ 4 ], arr[ 8 ], arr[ 12 ],
			arr[ 1 ], arr[ 5 ], arr[ 9 ], arr[ 13 ],
			arr[ 2 ], arr[ 6 ], arr[ 10 ], arr[ 14 ],
			arr[ 3 ], arr[ 7 ], arr[ 11 ], arr[ 15 ]
		);

		/*
		mat.set(
			arr[ 0], arr[ 1], arr[ 2], arr[ 3],
			arr[ 4], arr[ 5], arr[ 6], arr[ 7],
			arr[ 8], arr[ 9], arr[10], arr[11],
			arr[12], arr[13], arr[14], arr[15]
		);
		// */

		return mat;

	};
Y
yamahigashi 已提交
2896 2897

} )();