FBXLoader.js 57.0 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() {

Y
yamahigashi 已提交
19
	THREE.FBXLoader = function ( showStatus, manager ) {
Y
yamahigashi 已提交
20

Y
yamahigashi 已提交
21 22 23 24
		THREE.Loader.call( this, showStatus );
		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

Y
yamahigashi 已提交
36 37 38
		var loader = new THREE.XHRLoader( scope.manager );
		// 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

Y
yamahigashi 已提交
67
		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

Y
yamahigashi 已提交
81 82
			num = read( 1 );
			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 97 98
		var versionExp = /FBXVersion: (\d+)/;
		match = body.match( versionExp );
		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 );
Y
yamahigashi 已提交
124
		console.timeEnd( 'FBXLoader: ObjectParser' );
Y
yamahigashi 已提交
125

Y
yamahigashi 已提交
126
		console.time( 'FBXLoader: GeometryParser' );
M
Mr.doob 已提交
127
		geometries = this.parseGeometries( nodes );
Y
yamahigashi 已提交
128
		console.timeEnd( 'FBXLoader: GeometryParser' );
Y
yamahigashi 已提交
129

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

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

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

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

Y
yamahigashi 已提交
138
			}
Y
yamahigashi 已提交
139

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

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

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

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

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

Y
yamahigashi 已提交
153
		}
Y
yamahigashi 已提交
154

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

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

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

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

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

Y
yamahigashi 已提交
167
		}
Y
yamahigashi 已提交
168

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

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

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

Y
yamahigashi 已提交
177
			}
Y
yamahigashi 已提交
178

Y
yamahigashi 已提交
179
		}
Y
yamahigashi 已提交
180

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

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

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

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

Y
yamahigashi 已提交
190
				}
Y
yamahigashi 已提交
191

Y
yamahigashi 已提交
192
			}
Y
yamahigashi 已提交
193

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

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

Y
yamahigashi 已提交
198
		}
Y
yamahigashi 已提交
199

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

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

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

Y
yamahigashi 已提交
206 207
		geo = ( new Geometry() ).parse( node );
		geo.addBones( this.hierarchy.hierarchy );
Y
yamahigashi 已提交
208

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

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

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

Y
yamahigashi 已提交
218
		}
Y
yamahigashi 已提交
219

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

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

Y
yamahigashi 已提交
224
		}
Y
yamahigashi 已提交
225

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

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

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

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

Y
yamahigashi 已提交
234
		}
Y
yamahigashi 已提交
235

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

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

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

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

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

Y
yamahigashi 已提交
252
		}
Y
yamahigashi 已提交
253

Y
yamahigashi 已提交
254 255
		var material;
		if ( texture !== undefined ) {
Y
yamahigashi 已提交
256

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

Y
yamahigashi 已提交
259
		} else {
Y
yamahigashi 已提交
260

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

Y
yamahigashi 已提交
263
		}
Y
yamahigashi 已提交
264

Y
yamahigashi 已提交
265 266 267 268
		geometry = new THREE.Geometry().fromBufferGeometry( geometry );
		geometry.bones = geo.bones;
		geometry.skinIndices = this.weights.skinIndices;
		geometry.skinWeights = this.weights.skinWeights;
Y
yamahigashi 已提交
269

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

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

Y
yamahigashi 已提交
275
		} else {
Y
yamahigashi 已提交
276

Y
yamahigashi 已提交
277 278 279
			material.skinning = true;
			mesh = new THREE.SkinnedMesh( geometry, material );
			this.addAnimation( mesh, this.weights.matrices, this.animations );
Y
yamahigashi 已提交
280

Y
yamahigashi 已提交
281
		}
Y
yamahigashi 已提交
282

Y
yamahigashi 已提交
283
		return mesh;
Y
yamahigashi 已提交
284

Y
yamahigashi 已提交
285
	};
Y
yamahigashi 已提交
286

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

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

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

Y
yamahigashi 已提交
293 294 295
			var name = mesh.geometry.bones[ i ].name;
			name = name.replace( /.*:/, '' );
			animationdata.hierarchy.push( { parent: mesh.geometry.bones[ i ].parent, name: name, keys: [] } );
Y
yamahigashi 已提交
296

Y
yamahigashi 已提交
297
		}
Y
yamahigashi 已提交
298

Y
yamahigashi 已提交
299
		var hasCurve = function ( animNode, attr ) {
Y
yamahigashi 已提交
300

Y
yamahigashi 已提交
301
			if ( animNode === undefined ) {
Y
yamahigashi 已提交
302

Y
yamahigashi 已提交
303
				return false;
Y
yamahigashi 已提交
304

Y
yamahigashi 已提交
305
			}
Y
yamahigashi 已提交
306

Y
yamahigashi 已提交
307 308
			var attrNode;
			switch ( attr ) {
Y
yamahigashi 已提交
309

Y
yamahigashi 已提交
310 311
				case 'S':
					if ( animNode.S === undefined ) {
Y
yamahigashi 已提交
312

Y
yamahigashi 已提交
313
						return false;
Y
yamahigashi 已提交
314

Y
yamahigashi 已提交
315 316 317
					}
					attrNode = animNode.S;
					break;
Y
yamahigashi 已提交
318

Y
yamahigashi 已提交
319 320
				case 'R':
					if ( animNode.R === undefined ) {
Y
yamahigashi 已提交
321

Y
yamahigashi 已提交
322
						return false;
Y
yamahigashi 已提交
323

Y
yamahigashi 已提交
324 325 326
					}
					attrNode = animNode.R;
					break;
Y
yamahigashi 已提交
327

Y
yamahigashi 已提交
328 329
				case 'T':
					if ( animNode.T === undefined ) {
Y
yamahigashi 已提交
330

Y
yamahigashi 已提交
331
						return false;
Y
yamahigashi 已提交
332

Y
yamahigashi 已提交
333 334 335 336
					}
					attrNode = animNode.T;
					break;
			}
Y
yamahigashi 已提交
337

Y
yamahigashi 已提交
338
			if ( attrNode.curves.x === undefined ) {
Y
yamahigashi 已提交
339

Y
yamahigashi 已提交
340
				return false;
Y
yamahigashi 已提交
341

Y
yamahigashi 已提交
342
			}
Y
yamahigashi 已提交
343

Y
yamahigashi 已提交
344
			if ( attrNode.curves.y === undefined ) {
Y
yamahigashi 已提交
345

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

Y
yamahigashi 已提交
348
			}
Y
yamahigashi 已提交
349

Y
yamahigashi 已提交
350
			if ( attrNode.curves.z === undefined ) {
Y
yamahigashi 已提交
351

Y
yamahigashi 已提交
352
				return false;
Y
yamahigashi 已提交
353

Y
yamahigashi 已提交
354
			}
Y
yamahigashi 已提交
355

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

Y
yamahigashi 已提交
358
		};
Y
yamahigashi 已提交
359

Y
yamahigashi 已提交
360
		var hasKeyOnFrame = function ( attrNode, frame ) {
Y
yamahigashi 已提交
361

Y
yamahigashi 已提交
362 363 364
			var x = isKeyExistOnFrame( attrNode.curves.x, frame );
			var y = isKeyExistOnFrame( attrNode.curves.y, frame );
			var z = isKeyExistOnFrame( attrNode.curves.z, frame );
Y
yamahigashi 已提交
365

Y
yamahigashi 已提交
366
			return x && y && z;
Y
yamahigashi 已提交
367

Y
yamahigashi 已提交
368
		};
Y
yamahigashi 已提交
369

Y
yamahigashi 已提交
370
		var isKeyExistOnFrame = function ( curve, frame ) {
Y
yamahigashi 已提交
371

Y
yamahigashi 已提交
372 373
			var value = curve.values[ frame ];
			return value !== undefined;
Y
yamahigashi 已提交
374

Y
yamahigashi 已提交
375
		};
Y
yamahigashi 已提交
376 377


Y
yamahigashi 已提交
378
		var genKey = function ( animNode, bone ) {
Y
yamahigashi 已提交
379

Y
yamahigashi 已提交
380 381 382 383 384 385
			// 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 已提交
386

Y
yamahigashi 已提交
387
			if ( animNode === undefined ) {
Y
yamahigashi 已提交
388

Y
yamahigashi 已提交
389
				return key;
Y
yamahigashi 已提交
390

Y
yamahigashi 已提交
391
			}
Y
yamahigashi 已提交
392

Y
yamahigashi 已提交
393
			try {
Y
yamahigashi 已提交
394

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

Y
yamahigashi 已提交
397 398 399 400 401
					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 已提交
402

Y
yamahigashi 已提交
403
				} else {
Y
yamahigashi 已提交
404

Y
yamahigashi 已提交
405
					delete key.pos;
Y
yamahigashi 已提交
406

Y
yamahigashi 已提交
407
				}
Y
yamahigashi 已提交
408

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

Y
yamahigashi 已提交
411 412 413 414 415 416
					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 已提交
417

Y
yamahigashi 已提交
418
				} else {
Y
yamahigashi 已提交
419

Y
yamahigashi 已提交
420
					delete key.rot;
Y
yamahigashi 已提交
421

Y
yamahigashi 已提交
422
				}
Y
yamahigashi 已提交
423

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

Y
yamahigashi 已提交
426 427 428 429 430
					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 已提交
431

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

Y
yamahigashi 已提交
434
					delete key.scl;
Y
yamahigashi 已提交
435

Y
yamahigashi 已提交
436
				}
Y
yamahigashi 已提交
437

Y
yamahigashi 已提交
438
			} catch ( e ) {
Y
yamahigashi 已提交
439

Y
yamahigashi 已提交
440 441 442
				// curve is not full plotted
				console.log( bone );
				console.log( e );
Y
yamahigashi 已提交
443

Y
yamahigashi 已提交
444
			}
Y
yamahigashi 已提交
445

Y
yamahigashi 已提交
446
			return key;
Y
yamahigashi 已提交
447

Y
yamahigashi 已提交
448
		};
Y
yamahigashi 已提交
449

Y
yamahigashi 已提交
450 451
		var bones = mesh.geometry.bones;
		for ( frame = 0; frame < animations.frames; frame ++ ) {
Y
yamahigashi 已提交
452 453


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

Y
yamahigashi 已提交
456 457
				var bone = bones[ i ];
				var animNode = animations.curves[ i ];
Y
yamahigashi 已提交
458

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

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

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

Y
yamahigashi 已提交
465
					}
Y
yamahigashi 已提交
466

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

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

Y
yamahigashi 已提交
471
		}
Y
yamahigashi 已提交
472

Y
yamahigashi 已提交
473
		if ( mesh.geometry.animations === undefined ) {
Y
yamahigashi 已提交
474

Y
yamahigashi 已提交
475
			mesh.geometry.animations = [];
Y
yamahigashi 已提交
476

Y
yamahigashi 已提交
477
		}
Y
yamahigashi 已提交
478

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

Y
yamahigashi 已提交
481
	};
Y
yamahigashi 已提交
482

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

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

Y
yamahigashi 已提交
488
			return [];
Y
yamahigashi 已提交
489

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

Y
yamahigashi 已提交
492 493 494
		// has many
		var matCount = 0;
		for ( var mat in node.subNodes.Materials ) {
Y
yamahigashi 已提交
495

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

Y
yamahigashi 已提交
498
				matCount ++;
Y
yamahigashi 已提交
499

Y
yamahigashi 已提交
500
			}
Y
yamahigashi 已提交
501

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

Y
yamahigashi 已提交
504 505
		var res = [];
		if ( matCount > 0 ) {
Y
yamahigashi 已提交
506

Y
yamahigashi 已提交
507
			for ( mat in node.subNodes.Material ) {
Y
yamahigashi 已提交
508

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

Y
yamahigashi 已提交
511
			}
Y
yamahigashi 已提交
512

Y
yamahigashi 已提交
513
		} else {
Y
yamahigashi 已提交
514

Y
yamahigashi 已提交
515
			res.push( parseMaterial( node.subNodes.Material ) );
Y
yamahigashi 已提交
516

Y
yamahigashi 已提交
517
		}
Y
yamahigashi 已提交
518

Y
yamahigashi 已提交
519
		return res;
Y
yamahigashi 已提交
520

Y
yamahigashi 已提交
521
	};
Y
yamahigashi 已提交
522

Y
yamahigashi 已提交
523 524
	// TODO
	THREE.FBXLoader.prototype.parseMaterial = function ( node ) {
Y
yamahigashi 已提交
525

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


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

Y
yamahigashi 已提交
531
		var loader = new THREE.XHRLoader( this.manager );
Y
yamahigashi 已提交
532

Y
yamahigashi 已提交
533
		loader.setResponseType( responseType );
Y
yamahigashi 已提交
534

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

Y
yamahigashi 已提交
537
			onLoad( result );
Y
yamahigashi 已提交
538

Y
yamahigashi 已提交
539
		}, onProgress, onError );
Y
yamahigashi 已提交
540

Y
yamahigashi 已提交
541
		return request;
Y
yamahigashi 已提交
542

Y
yamahigashi 已提交
543
	};
Y
yamahigashi 已提交
544

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

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

Y
yamahigashi 已提交
549
	};
Y
yamahigashi 已提交
550

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

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

Y
yamahigashi 已提交
555
	};
Y
yamahigashi 已提交
556 557


Y
yamahigashi 已提交
558
	/* ----------------------------------------------------------------- */
Y
yamahigashi 已提交
559

Y
yamahigashi 已提交
560
	function FBXNodes() {}
Y
yamahigashi 已提交
561

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

Y
yamahigashi 已提交
564
		this[ key ] = val;
Y
yamahigashi 已提交
565

Y
yamahigashi 已提交
566
	};
Y
yamahigashi 已提交
567

Y
yamahigashi 已提交
568
	FBXNodes.prototype.searchConnectionParent = function ( id ) {
Y
yamahigashi 已提交
569

Y
yamahigashi 已提交
570
		if ( this.__cache_search_connection_parent === undefined ) {
Y
yamahigashi 已提交
571

Y
yamahigashi 已提交
572
			this.__cache_search_connection_parent = [];
Y
yamahigashi 已提交
573

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

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

Y
yamahigashi 已提交
578
			return this.__cache_search_connection_parent[ id ];
Y
yamahigashi 已提交
579

Y
yamahigashi 已提交
580
		} else {
Y
yamahigashi 已提交
581

Y
yamahigashi 已提交
582
			this.__cache_search_connection_parent[ id ] = [];
Y
yamahigashi 已提交
583

Y
yamahigashi 已提交
584
		}
Y
yamahigashi 已提交
585

Y
yamahigashi 已提交
586
		var conns = this.Connections.properties.connections;
Y
yamahigashi 已提交
587

Y
yamahigashi 已提交
588 589
		var results = [];
		for ( var i = 0; i < conns.length; ++ i ) {
Y
yamahigashi 已提交
590

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

Y
yamahigashi 已提交
593 594 595
				// 0 means scene root
				var res = conns[ i ][ 1 ] === 0 ? - 1 : conns[ i ][ 1 ];
				results.push( res );
Y
yamahigashi 已提交
596

Y
yamahigashi 已提交
597
			}
Y
yamahigashi 已提交
598

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

Y
yamahigashi 已提交
601
		if ( results.length > 0 ) {
Y
yamahigashi 已提交
602

Y
yamahigashi 已提交
603 604
			this.__cache_search_connection_parent[ id ] = this.__cache_search_connection_parent[ id ].concat( results );
			return results;
Y
yamahigashi 已提交
605

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

Y
yamahigashi 已提交
608 609
			this.__cache_search_connection_parent[ id ] = [ - 1 ];
			return [ - 1 ];
Y
yamahigashi 已提交
610

Y
yamahigashi 已提交
611
		}
Y
yamahigashi 已提交
612

Y
yamahigashi 已提交
613
	};
Y
yamahigashi 已提交
614

Y
yamahigashi 已提交
615
	FBXNodes.prototype.searchConnectionChildren = function ( id ) {
Y
yamahigashi 已提交
616

Y
yamahigashi 已提交
617
		if ( this.__cache_search_connection_children === undefined ) {
Y
yamahigashi 已提交
618

Y
yamahigashi 已提交
619
			this.__cache_search_connection_children = [];
Y
yamahigashi 已提交
620

Y
yamahigashi 已提交
621
		}
Y
yamahigashi 已提交
622

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

Y
yamahigashi 已提交
625
			return this.__cache_search_connection_children[ id ];
Y
yamahigashi 已提交
626

Y
yamahigashi 已提交
627
		} else {
Y
yamahigashi 已提交
628

Y
yamahigashi 已提交
629
			this.__cache_search_connection_children[ id ] = [];
Y
yamahigashi 已提交
630

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

Y
yamahigashi 已提交
633
		var conns = this.Connections.properties.connections;
Y
yamahigashi 已提交
634

Y
yamahigashi 已提交
635 636
		var res = [];
		for ( var i = 0; i < conns.length; ++ i ) {
Y
yamahigashi 已提交
637

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

Y
yamahigashi 已提交
640 641 642
				// 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 已提交
643

Y
yamahigashi 已提交
644
			}
Y
yamahigashi 已提交
645

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

Y
yamahigashi 已提交
648
		if ( res.length > 0 ) {
Y
yamahigashi 已提交
649

Y
yamahigashi 已提交
650 651
			this.__cache_search_connection_children[ id ] = this.__cache_search_connection_children[ id ].concat( res );
			return res;
Y
yamahigashi 已提交
652

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

Y
yamahigashi 已提交
655 656
			this.__cache_search_connection_children[ id ] = [ - 1 ];
			return [ - 1 ];
Y
yamahigashi 已提交
657

Y
yamahigashi 已提交
658
		}
Y
yamahigashi 已提交
659

Y
yamahigashi 已提交
660
	};
Y
yamahigashi 已提交
661

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

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

Y
yamahigashi 已提交
667
			this.__cache_search_connection_type = '';
Y
yamahigashi 已提交
668

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

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

Y
yamahigashi 已提交
673
			return this.__cache_search_connection_type[ key ];
Y
yamahigashi 已提交
674

Y
yamahigashi 已提交
675
		} else {
Y
yamahigashi 已提交
676

Y
yamahigashi 已提交
677
			this.__cache_search_connection_type[ key ] = '';
Y
yamahigashi 已提交
678

Y
yamahigashi 已提交
679
		}
Y
yamahigashi 已提交
680

Y
yamahigashi 已提交
681
		var conns = this.Connections.properties.connections;
Y
yamahigashi 已提交
682

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

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

Y
yamahigashi 已提交
687 688 689
				// 0 means scene root
				this.__cache_search_connection_type[ key ] = conns[ i ][ 2 ];
				return conns[ i ][ 2 ];
Y
yamahigashi 已提交
690

Y
yamahigashi 已提交
691
			}
Y
yamahigashi 已提交
692

Y
yamahigashi 已提交
693
		}
Y
yamahigashi 已提交
694

Y
yamahigashi 已提交
695 696
		this.__cache_search_connection_type[ id ] = null;
		return null;
Y
yamahigashi 已提交
697

Y
yamahigashi 已提交
698
	};
Y
yamahigashi 已提交
699

Y
yamahigashi 已提交
700
	function FBXParser() {}
Y
yamahigashi 已提交
701

Y
yamahigashi 已提交
702
	FBXParser.prototype = {
Y
yamahigashi 已提交
703

Y
yamahigashi 已提交
704
		// constructor: FBXParser,
Y
yamahigashi 已提交
705

Y
yamahigashi 已提交
706
		// ------------ node stack manipulations ----------------------------------
Y
yamahigashi 已提交
707

Y
yamahigashi 已提交
708
		getPrevNode: function () {
Y
yamahigashi 已提交
709

Y
yamahigashi 已提交
710
			return this.nodeStack[ this.currentIndent - 2 ];
Y
yamahigashi 已提交
711

Y
yamahigashi 已提交
712
		},
Y
yamahigashi 已提交
713

Y
yamahigashi 已提交
714
		getCurrentNode: function () {
Y
yamahigashi 已提交
715

Y
yamahigashi 已提交
716
			return this.nodeStack[ this.currentIndent - 1 ];
Y
yamahigashi 已提交
717

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

Y
yamahigashi 已提交
720
		getCurrentProp: function () {
Y
yamahigashi 已提交
721

Y
yamahigashi 已提交
722
			return this.currentProp;
Y
yamahigashi 已提交
723

Y
yamahigashi 已提交
724
		},
Y
yamahigashi 已提交
725

Y
yamahigashi 已提交
726
		pushStack: function ( node ) {
Y
yamahigashi 已提交
727

Y
yamahigashi 已提交
728 729
			this.nodeStack.push( node );
			this.currentIndent += 1;
Y
yamahigashi 已提交
730

Y
yamahigashi 已提交
731
		},
Y
yamahigashi 已提交
732

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

Y
yamahigashi 已提交
735 736
			this.nodeStack.pop();
			this.currentIndent -= 1;
Y
yamahigashi 已提交
737

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

Y
yamahigashi 已提交
740
		setCurrentProp: function ( val, name ) {
Y
yamahigashi 已提交
741

Y
yamahigashi 已提交
742 743
			this.currentProp = val;
			this.currentPropName = name;
Y
yamahigashi 已提交
744

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

Y
yamahigashi 已提交
747 748
		// ----------parse ---------------------------------------------------
		parse: function ( text ) {
Y
yamahigashi 已提交
749

Y
yamahigashi 已提交
750 751 752 753 754
			this.currentIndent = 0;
			this.allNodes = new FBXNodes();
			this.nodeStack = [];
			this.currentProp = [];
			this.currentPropName = '';
Y
yamahigashi 已提交
755

Y
yamahigashi 已提交
756 757
			var split = text.split( "\n" );
			for ( var line in split ) {
Y
yamahigashi 已提交
758

Y
yamahigashi 已提交
759
				var l = split[ line ];
Y
yamahigashi 已提交
760

Y
yamahigashi 已提交
761 762
				// short cut
				if ( l.match( /^[\s\t]*;/ ) ) {
Y
yamahigashi 已提交
763

Y
yamahigashi 已提交
764
					continue;
Y
yamahigashi 已提交
765

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

Y
yamahigashi 已提交
769
					continue;
Y
yamahigashi 已提交
770

Y
yamahigashi 已提交
771
				} // skip empty line
Y
yamahigashi 已提交
772

Y
yamahigashi 已提交
773 774 775 776
				// beginning of node
				var beginningOfNodeExp = new RegExp( "^\\t{" + this.currentIndent + "}(\\w+):(.*){", '' );
				match = l.match( beginningOfNodeExp );
				if ( match ) {
Y
yamahigashi 已提交
777

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

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

Y
yamahigashi 已提交
783
					} );
Y
yamahigashi 已提交
784

Y
yamahigashi 已提交
785 786
					this.parseNodeBegin( l, nodeName, nodeAttrs || null );
					continue;
Y
yamahigashi 已提交
787

Y
yamahigashi 已提交
788
				}
Y
yamahigashi 已提交
789

Y
yamahigashi 已提交
790 791 792 793
				// node's property
				var propExp = new RegExp( "^\\t{" + ( this.currentIndent ) + "}(\\w+):[\\s\\t\\r\\n](.*)" );
				match = l.match( propExp );
				if ( match ) {
Y
yamahigashi 已提交
794

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

Y
yamahigashi 已提交
798 799
					this.parseNodeProperty( l, propName, propValue );
					continue;
Y
yamahigashi 已提交
800

Y
yamahigashi 已提交
801
				}
Y
yamahigashi 已提交
802

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

Y
yamahigashi 已提交
807 808
					this.nodeEnd();
					continue;
Y
yamahigashi 已提交
809

Y
yamahigashi 已提交
810
				}
Y
yamahigashi 已提交
811

Y
yamahigashi 已提交
812 813 814 815 816 817 818 819 820 821
				// 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 已提交
822

Y
yamahigashi 已提交
823
					this.parseNodePropertyContinued( l );
Y
yamahigashi 已提交
824

Y
yamahigashi 已提交
825
				}
Y
yamahigashi 已提交
826

Y
yamahigashi 已提交
827
			}
Y
yamahigashi 已提交
828

Y
yamahigashi 已提交
829
			return this.allNodes;
Y
yamahigashi 已提交
830

Y
yamahigashi 已提交
831
		},
Y
yamahigashi 已提交
832

Y
yamahigashi 已提交
833
		parseNodeBegin: function ( line, nodeName, nodeAttrs ) {
Y
yamahigashi 已提交
834

Y
yamahigashi 已提交
835 836 837 838
			// var nodeName = match[1];
			var node = { 'name': nodeName, properties: {}, 'subNodes': {} };
			var attrs = this.parseNodeAttr( nodeAttrs );
			var currentNode = this.getCurrentNode();
Y
yamahigashi 已提交
839

Y
yamahigashi 已提交
840 841
			// a top node
			if ( this.currentIndent === 0 ) {
Y
yamahigashi 已提交
842

Y
yamahigashi 已提交
843
				this.allNodes.add( nodeName, node );
Y
yamahigashi 已提交
844

Y
yamahigashi 已提交
845
			} else {
Y
yamahigashi 已提交
846

Y
yamahigashi 已提交
847
				// a subnode
Y
yamahigashi 已提交
848

Y
yamahigashi 已提交
849 850
				// already exists subnode, then append it
				if ( nodeName in currentNode.subNodes ) {
Y
yamahigashi 已提交
851

Y
yamahigashi 已提交
852
					var tmp = currentNode.subNodes[ nodeName ];
Y
yamahigashi 已提交
853

Y
yamahigashi 已提交
854 855
					// console.log( "duped entry found\nkey: " + nodeName + "\nvalue: " + propValue );
					if ( this.isFlattenNode( currentNode.subNodes[ nodeName ] ) ) {
Y
yamahigashi 已提交
856 857


Y
yamahigashi 已提交
858
						if ( attrs.id === '' ) {
Y
yamahigashi 已提交
859

Y
yamahigashi 已提交
860 861
							currentNode.subNodes[ nodeName ] = [];
							currentNode.subNodes[ nodeName ].push( tmp );
Y
yamahigashi 已提交
862

Y
yamahigashi 已提交
863
						} else {
Y
yamahigashi 已提交
864

Y
yamahigashi 已提交
865 866
							currentNode.subNodes[ nodeName ] = {};
							currentNode.subNodes[ nodeName ][ tmp.id ] = tmp;
Y
yamahigashi 已提交
867

Y
yamahigashi 已提交
868
						}
Y
yamahigashi 已提交
869

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

Y
yamahigashi 已提交
872
					if ( attrs.id === '' ) {
Y
yamahigashi 已提交
873

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

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

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

Y
yamahigashi 已提交
880
					}
Y
yamahigashi 已提交
881

Y
yamahigashi 已提交
882
				} else {
Y
yamahigashi 已提交
883

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

Y
yamahigashi 已提交
886
				}
Y
yamahigashi 已提交
887

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

Y
yamahigashi 已提交
890 891 892
			// for this		  ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
			// NodeAttribute: 1001463072, "NodeAttribute::", "LimbNode" {
			if ( nodeAttrs ) {
Y
yamahigashi 已提交
893

Y
yamahigashi 已提交
894 895 896
				node.id = attrs.id;
				node.attrName = attrs.name;
				node.attrType = attrs.type;
Y
yamahigashi 已提交
897

Y
yamahigashi 已提交
898
			}
Y
yamahigashi 已提交
899

Y
yamahigashi 已提交
900
			this.pushStack( node );
Y
yamahigashi 已提交
901

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

Y
yamahigashi 已提交
904
		parseNodeAttr: function ( attrs ) {
Y
yamahigashi 已提交
905

Y
yamahigashi 已提交
906
			var id = attrs[ 0 ];
Y
yamahigashi 已提交
907

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

Y
yamahigashi 已提交
910
				id = parseInt( attrs[ 0 ] );
Y
yamahigashi 已提交
911

Y
yamahigashi 已提交
912
				if ( isNaN( id ) ) {
Y
yamahigashi 已提交
913

Y
yamahigashi 已提交
914 915
					// PolygonVertexIndex: *16380 {
					id = attrs[ 0 ];
Y
yamahigashi 已提交
916

Y
yamahigashi 已提交
917
				}
Y
yamahigashi 已提交
918

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

Y
yamahigashi 已提交
921 922 923
			var name;
			var type;
			if ( attrs.length > 1 ) {
Y
yamahigashi 已提交
924

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

Y
yamahigashi 已提交
928
			}
Y
yamahigashi 已提交
929

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

Y
yamahigashi 已提交
932
		},
Y
yamahigashi 已提交
933

Y
yamahigashi 已提交
934
		parseNodeProperty: function ( line, propName, propValue ) {
Y
yamahigashi 已提交
935

Y
yamahigashi 已提交
936 937
			var currentNode = this.getCurrentNode();
			var parentName = currentNode.name;
Y
yamahigashi 已提交
938

Y
yamahigashi 已提交
939 940 941
			// special case parent node's is like "Properties70"
			// these chilren nodes must treat with careful
			if ( parentName !== undefined ) {
Y
yamahigashi 已提交
942

Y
yamahigashi 已提交
943 944
				var propMatch = parentName.match( /Properties(\d)+/ );
				if ( propMatch ) {
Y
yamahigashi 已提交
945

Y
yamahigashi 已提交
946 947
					this.parseNodeSpecialProperty( line, propName, propValue );
					return;
Y
yamahigashi 已提交
948

Y
yamahigashi 已提交
949
				}
Y
yamahigashi 已提交
950

Y
yamahigashi 已提交
951
			}
Y
yamahigashi 已提交
952

Y
yamahigashi 已提交
953 954
			// special case Connections
			if ( propName == 'C' ) {
Y
yamahigashi 已提交
955

Y
yamahigashi 已提交
956 957 958
				var connProps = propValue.split( ',' ).slice( 1 );
				var from = parseInt( connProps[ 0 ] );
				var to = parseInt( connProps[ 1 ] );
Y
yamahigashi 已提交
959

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

Y
yamahigashi 已提交
962 963 964
				propName = 'connections';
				propValue = [ from, to ];
				propValue = propValue.concat( rest );
Y
yamahigashi 已提交
965

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

Y
yamahigashi 已提交
968
					currentNode.properties[ propName ] = [];
Y
yamahigashi 已提交
969

Y
yamahigashi 已提交
970
				}
Y
yamahigashi 已提交
971

Y
yamahigashi 已提交
972
			}
Y
yamahigashi 已提交
973

Y
yamahigashi 已提交
974 975
			// special case Connections
			if ( propName == 'Node' ) {
Y
yamahigashi 已提交
976

Y
yamahigashi 已提交
977 978 979
				var id = parseInt( propValue );
				currentNode.properties.id = id;
				currentNode.id = id;
Y
yamahigashi 已提交
980

Y
yamahigashi 已提交
981
			}
Y
yamahigashi 已提交
982

Y
yamahigashi 已提交
983 984
			// already exists in properties, then append this
			if ( propName in currentNode.properties ) {
Y
yamahigashi 已提交
985

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

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

Y
yamahigashi 已提交
991
				} else {
Y
yamahigashi 已提交
992

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

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

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

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

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

Y
yamahigashi 已提交
1004
				} else {
Y
yamahigashi 已提交
1005

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

Y
yamahigashi 已提交
1008
				}
Y
yamahigashi 已提交
1009

Y
yamahigashi 已提交
1010
			}
Y
yamahigashi 已提交
1011

Y
yamahigashi 已提交
1012
			this.setCurrentProp( currentNode.properties, propName );
Y
yamahigashi 已提交
1013

Y
yamahigashi 已提交
1014
		},
Y
yamahigashi 已提交
1015

Y
yamahigashi 已提交
1016 1017
		// TODO:
		parseNodePropertyContinued: function ( line ) {
Y
yamahigashi 已提交
1018

Y
yamahigashi 已提交
1019
			this.currentProp[ this.currentPropName ] += line;
Y
yamahigashi 已提交
1020

Y
yamahigashi 已提交
1021
		},
Y
yamahigashi 已提交
1022

Y
yamahigashi 已提交
1023
		parseNodeSpecialProperty: function ( line, propName, propValue ) {
Y
yamahigashi 已提交
1024

Y
yamahigashi 已提交
1025 1026 1027 1028 1029
			// 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 已提交
1030

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

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

Y
yamahigashi 已提交
1035 1036 1037 1038 1039
			var innerPropName = props[ 0 ];
			var innerPropType1 = props[ 1 ];
			var innerPropType2 = props[ 2 ];
			var innerPropFlag = props[ 3 ];
			var innerPropValue = props[ 4 ];
Y
yamahigashi 已提交
1040

Y
yamahigashi 已提交
1041 1042 1043 1044 1045
			/*
			if ( innerPropValue === undefined ) {
				innerPropValue = props[3];
			}
			*/
Y
yamahigashi 已提交
1046

Y
yamahigashi 已提交
1047 1048
			// cast value in its type
			switch ( innerPropType1 ) {
Y
yamahigashi 已提交
1049

Y
yamahigashi 已提交
1050 1051 1052
				case "int":
					innerPropValue = parseInt( innerPropValue );
					break;
Y
yamahigashi 已提交
1053

Y
yamahigashi 已提交
1054 1055 1056
				case "double":
					innerPropValue = parseFloat( innerPropValue );
					break;
Y
yamahigashi 已提交
1057

Y
yamahigashi 已提交
1058 1059 1060 1061 1062
				case "ColorRGB":
				case "Vector3D":
					var tmp = innerPropValue.split( ',' );
					innerPropValue = new THREE.Vector3( tmp[ 0 ], tmp[ 1 ], tmp[ 2 ] );
					break;
Y
yamahigashi 已提交
1063

Y
yamahigashi 已提交
1064
			}
Y
yamahigashi 已提交
1065

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

Y
yamahigashi 已提交
1069 1070 1071 1072
				'type': innerPropType1,
				'type2': innerPropType2,
				'flag': innerPropFlag,
				'value': innerPropValue
Y
yamahigashi 已提交
1073

Y
yamahigashi 已提交
1074
			};
Y
yamahigashi 已提交
1075

Y
yamahigashi 已提交
1076
			this.setCurrentProp( this.getPrevNode().properties, innerPropName );
Y
yamahigashi 已提交
1077

Y
yamahigashi 已提交
1078
		},
Y
yamahigashi 已提交
1079

Y
yamahigashi 已提交
1080
		nodeEnd: function ( line ) {
Y
yamahigashi 已提交
1081

Y
yamahigashi 已提交
1082
			this.popStack();
Y
yamahigashi 已提交
1083

Y
yamahigashi 已提交
1084
		},
Y
yamahigashi 已提交
1085

Y
yamahigashi 已提交
1086 1087 1088
		/* ---------------------------------------------------------------- */
		/*		util													  */
		isFlattenNode: function ( node ) {
Y
yamahigashi 已提交
1089

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

Y
yamahigashi 已提交
1092
		}
Y
yamahigashi 已提交
1093

Y
yamahigashi 已提交
1094
	};
Y
yamahigashi 已提交
1095

Y
yamahigashi 已提交
1096
	function FBXAnalyzer() {}
Y
yamahigashi 已提交
1097

Y
yamahigashi 已提交
1098
	FBXAnalyzer.prototype = {
Y
yamahigashi 已提交
1099

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


Y
yamahigashi 已提交
1103 1104 1105 1106 1107
	// 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 已提交
1108

Y
yamahigashi 已提交
1109 1110
		this.skinIndices = [];
		this.skinWeights = [];
Y
yamahigashi 已提交
1111

Y
yamahigashi 已提交
1112
		this.matrices	= [];
Y
yamahigashi 已提交
1113

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


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

Y
yamahigashi 已提交
1119 1120 1121 1122 1123
		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 已提交
1124

Y
yamahigashi 已提交
1125
		return {
Y
yamahigashi 已提交
1126

Y
yamahigashi 已提交
1127 1128 1129 1130 1131 1132 1133
			'parent': _p,
			'id': parseInt( id ),
			'indices': _indices,
			'weights': _weights,
			'transform': _transform,
			'transformlink': _link,
			'linkMode': entry.properties.Mode
Y
yamahigashi 已提交
1134

Y
yamahigashi 已提交
1135
		};
Y
yamahigashi 已提交
1136

Y
yamahigashi 已提交
1137
	};
Y
yamahigashi 已提交
1138

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

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

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

Y
yamahigashi 已提交
1146
		var deformers = node.Objects.subNodes.Deformer;
Y
yamahigashi 已提交
1147

Y
yamahigashi 已提交
1148 1149
		var clusters = {};
		for ( var id in deformers ) {
Y
yamahigashi 已提交
1150

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

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

Y
yamahigashi 已提交
1155
					continue;
Y
yamahigashi 已提交
1156

Y
yamahigashi 已提交
1157
				}
Y
yamahigashi 已提交
1158

Y
yamahigashi 已提交
1159 1160 1161 1162
				//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 已提交
1163

Y
yamahigashi 已提交
1164
			}
Y
yamahigashi 已提交
1165

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


Y
yamahigashi 已提交
1169 1170 1171 1172
		// 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 已提交
1173

Y
yamahigashi 已提交
1174 1175
			var bid = hi[ b ].internalId;
			if ( clusters[ bid ] === undefined ) {
Y
yamahigashi 已提交
1176

Y
yamahigashi 已提交
1177 1178 1179
				//console.log( bid );
				this.matrices.push( new THREE.Matrix4() );
				continue;
Y
yamahigashi 已提交
1180

Y
yamahigashi 已提交
1181
			}
Y
yamahigashi 已提交
1182

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

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

Y
yamahigashi 已提交
1191 1192 1193
					weights[ clst.indices[ v ] ] = {};
					weights[ clst.indices[ v ] ].joint = [];
					weights[ clst.indices[ v ] ].weight = [];
Y
yamahigashi 已提交
1194

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

Y
yamahigashi 已提交
1197 1198
				// indices
				var affect = node.searchConnectionChildren( clst.id );
Y
yamahigashi 已提交
1199

Y
yamahigashi 已提交
1200
				if ( affect.length > 1 ) {
Y
yamahigashi 已提交
1201

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

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

Y
yamahigashi 已提交
1207 1208
				// weight value
				weights[ clst.indices[ v ] ].weight.push( clst.weights[ v ] );
Y
yamahigashi 已提交
1209

Y
yamahigashi 已提交
1210
			}
Y
yamahigashi 已提交
1211

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

Y
yamahigashi 已提交
1214 1215 1216
		// 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 已提交
1217

Y
yamahigashi 已提交
1218 1219 1220 1221 1222
			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 已提交
1223

Y
yamahigashi 已提交
1224 1225 1226 1227 1228
			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 已提交
1229

Y
yamahigashi 已提交
1230 1231
			this.skinIndices.push( indicies );
			this.skinWeights.push( weight );
Y
yamahigashi 已提交
1232

Y
yamahigashi 已提交
1233
		}
Y
yamahigashi 已提交
1234

Y
yamahigashi 已提交
1235 1236
		//console.log( this );
		return this;
Y
yamahigashi 已提交
1237

Y
yamahigashi 已提交
1238
	};
Y
yamahigashi 已提交
1239

Y
yamahigashi 已提交
1240
	function Bones() {
Y
yamahigashi 已提交
1241

Y
yamahigashi 已提交
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
		// returns bones hierarchy tree.
		//	  [
		//		  {
		//			  "parent": id,
		//			  "name": name,
		//			  "pos": pos,
		//			  "rotq": quat
		//		  },
		//		  ...
		//		  {},
		//		  ...
		//	  ]
		//
		/* sample response
Y
yamahigashi 已提交
1256

Y
yamahigashi 已提交
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
		   "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 已提交
1289

Y
yamahigashi 已提交
1290
	}
Y
yamahigashi 已提交
1291

Y
yamahigashi 已提交
1292
	Bones.prototype.parseHierarchy = function ( node ) {
Y
yamahigashi 已提交
1293

Y
yamahigashi 已提交
1294 1295
		var objects = node.Objects;
		var models = objects.subNodes.Model;
Y
yamahigashi 已提交
1296

Y
yamahigashi 已提交
1297 1298
		var bones = [];
		for ( var id in models ) {
Y
yamahigashi 已提交
1299

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

Y
yamahigashi 已提交
1302
				continue;
Y
yamahigashi 已提交
1303

Y
yamahigashi 已提交
1304 1305
			}
			bones.push( models[ id ] );
Y
yamahigashi 已提交
1306

Y
yamahigashi 已提交
1307
		}
Y
yamahigashi 已提交
1308

Y
yamahigashi 已提交
1309 1310
		this.hierarchy = [];
		for ( var i = 0; i < bones.length; ++ i ) {
Y
yamahigashi 已提交
1311

Y
yamahigashi 已提交
1312
			var bone = bones[ i ];
Y
yamahigashi 已提交
1313

Y
yamahigashi 已提交
1314 1315 1316 1317
			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 已提交
1318

Y
yamahigashi 已提交
1319
			if ( 'Lcl_Translation' in bone.properties ) {
Y
yamahigashi 已提交
1320

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

Y
yamahigashi 已提交
1323
			}
Y
yamahigashi 已提交
1324

Y
yamahigashi 已提交
1325
			if ( 'Lcl_Rotation' in bone.properties ) {
Y
yamahigashi 已提交
1326

Y
yamahigashi 已提交
1327 1328 1329 1330
				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 已提交
1331

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

Y
yamahigashi 已提交
1334
			if ( 'Lcl_Scaling' in bone.properties ) {
Y
yamahigashi 已提交
1335

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

Y
yamahigashi 已提交
1338
			}
Y
yamahigashi 已提交
1339

Y
yamahigashi 已提交
1340 1341 1342 1343 1344 1345
			// 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 已提交
1346

Y
yamahigashi 已提交
1347
		}
Y
yamahigashi 已提交
1348

Y
yamahigashi 已提交
1349
		this.reindexParentId();
Y
yamahigashi 已提交
1350

Y
yamahigashi 已提交
1351
		this.restoreBindPose( node );
Y
yamahigashi 已提交
1352

Y
yamahigashi 已提交
1353
		return this;
Y
yamahigashi 已提交
1354

Y
yamahigashi 已提交
1355
	};
Y
yamahigashi 已提交
1356

Y
yamahigashi 已提交
1357
	Bones.prototype.reindexParentId = function () {
Y
yamahigashi 已提交
1358

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

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

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

Y
yamahigashi 已提交
1365 1366
					this.hierarchy[ h ].parent = ii;
					break;
Y
yamahigashi 已提交
1367

Y
yamahigashi 已提交
1368
				}
Y
yamahigashi 已提交
1369

Y
yamahigashi 已提交
1370
			}
Y
yamahigashi 已提交
1371

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

Y
yamahigashi 已提交
1374
	};
Y
yamahigashi 已提交
1375

Y
yamahigashi 已提交
1376
	Bones.prototype.restoreBindPose = function ( node ) {
Y
yamahigashi 已提交
1377

Y
yamahigashi 已提交
1378 1379
		var bindPoseNode = node.Objects.subNodes.Pose;
		if ( bindPoseNode === undefined ) {
Y
yamahigashi 已提交
1380

Y
yamahigashi 已提交
1381
			return;
Y
yamahigashi 已提交
1382

Y
yamahigashi 已提交
1383
		}
Y
yamahigashi 已提交
1384

Y
yamahigashi 已提交
1385 1386 1387
		var poseNode = bindPoseNode.subNodes.PoseNode;
		var localMatrices = {}; // store local matrices, modified later( initialy world space )
		var worldMatrices = {}; // store world matrices
Y
yamahigashi 已提交
1388

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

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

Y
yamahigashi 已提交
1394 1395
			localMatrices[ poseNode[ i ].id ] = rawMatLcl;
			worldMatrices[ poseNode[ i ].id ] = rawMatWrd;
Y
yamahigashi 已提交
1396

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

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

Y
yamahigashi 已提交
1401 1402
			var bone = this.hierarchy[ h ];
			var inId = bone.internalId;
Y
yamahigashi 已提交
1403

Y
yamahigashi 已提交
1404
			if ( worldMatrices[ inId ] === undefined ) {
Y
yamahigashi 已提交
1405

Y
yamahigashi 已提交
1406 1407 1408
				// has no bind pose node, possibly be mesh
				// console.log( bone );
				continue;
Y
yamahigashi 已提交
1409

Y
yamahigashi 已提交
1410
			}
Y
yamahigashi 已提交
1411

Y
yamahigashi 已提交
1412 1413 1414
			var t = new THREE.Vector3( 0, 0, 0 );
			var r = new THREE.Quaternion();
			var s = new THREE.Vector3( 1, 1, 1 );
Y
yamahigashi 已提交
1415

Y
yamahigashi 已提交
1416 1417 1418
			var parentId;
			var parentNodes = node.searchConnectionParent( inId );
			for ( var pn = 0; pn < parentNodes.length; ++ pn ) {
Y
yamahigashi 已提交
1419

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

Y
yamahigashi 已提交
1422 1423
					parentId = parentNodes[ pn ];
					break;
Y
yamahigashi 已提交
1424

Y
yamahigashi 已提交
1425
				}
Y
yamahigashi 已提交
1426

Y
yamahigashi 已提交
1427
			}
Y
yamahigashi 已提交
1428

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

Y
yamahigashi 已提交
1431 1432 1433 1434 1435
				// 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 已提交
1436

Y
yamahigashi 已提交
1437 1438 1439
			} else {
				//console.log( bone );
			}
Y
yamahigashi 已提交
1440

Y
yamahigashi 已提交
1441 1442 1443 1444
			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 已提交
1445

Y
yamahigashi 已提交
1446
		}
Y
yamahigashi 已提交
1447

Y
yamahigashi 已提交
1448
	};
Y
yamahigashi 已提交
1449

Y
yamahigashi 已提交
1450
	Bones.prototype.searchRealId = function ( internalId ) {
Y
yamahigashi 已提交
1451

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

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

Y
yamahigashi 已提交
1456
				return h;
Y
yamahigashi 已提交
1457

Y
yamahigashi 已提交
1458
			}
Y
yamahigashi 已提交
1459

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

Y
yamahigashi 已提交
1462 1463
		// console.warn( 'FBXLoader: notfound internalId in bones: ' + internalId);
		return - 1;
Y
yamahigashi 已提交
1464

Y
yamahigashi 已提交
1465
	};
Y
yamahigashi 已提交
1466

Y
yamahigashi 已提交
1467
	Bones.prototype.getByInternalId = function ( internalId ) {
Y
yamahigashi 已提交
1468

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

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

Y
yamahigashi 已提交
1473
				return this.hierarchy[ h ];
Y
yamahigashi 已提交
1474

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

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

Y
yamahigashi 已提交
1479
		return null;
Y
yamahigashi 已提交
1480

Y
yamahigashi 已提交
1481
	};
Y
yamahigashi 已提交
1482

Y
yamahigashi 已提交
1483
	Bones.prototype.isBoneNode = function ( id ) {
Y
yamahigashi 已提交
1484

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

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

Y
yamahigashi 已提交
1489
				return true;
Y
yamahigashi 已提交
1490

Y
yamahigashi 已提交
1491
			}
Y
yamahigashi 已提交
1492

Y
yamahigashi 已提交
1493 1494
		}
		return false;
Y
yamahigashi 已提交
1495

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

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

Y
yamahigashi 已提交
1500
		if ( node.__cache_get_boneid_from_internalid === undefined ) {
Y
yamahigashi 已提交
1501

Y
yamahigashi 已提交
1502
			node.__cache_get_boneid_from_internalid = [];
Y
yamahigashi 已提交
1503

Y
yamahigashi 已提交
1504
		}
Y
yamahigashi 已提交
1505

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

Y
yamahigashi 已提交
1508
			return node.__cache_get_boneid_from_internalid[ id ];
Y
yamahigashi 已提交
1509

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

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

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

Y
yamahigashi 已提交
1516 1517 1518
				var res = i;
				node.__cache_get_boneid_from_internalid[ id ] = i;
				return i;
Y
yamahigashi 已提交
1519

Y
yamahigashi 已提交
1520
			}
Y
yamahigashi 已提交
1521

Y
yamahigashi 已提交
1522
		}
Y
yamahigashi 已提交
1523

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

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


Y
yamahigashi 已提交
1530
	function Geometry() {
Y
yamahigashi 已提交
1531

Y
yamahigashi 已提交
1532 1533 1534
		this.node = null;
		this.name = null;
		this.id = null;
Y
yamahigashi 已提交
1535

Y
yamahigashi 已提交
1536 1537 1538 1539
		this.vertices = [];
		this.indices = [];
		this.normals = [];
		this.uvs = [];
Y
yamahigashi 已提交
1540

Y
yamahigashi 已提交
1541 1542
		this.bones = [];
		this.skins = null;
Y
yamahigashi 已提交
1543

Y
yamahigashi 已提交
1544
	}
Y
yamahigashi 已提交
1545

Y
yamahigashi 已提交
1546
	Geometry.prototype.parse = function ( geoNode ) {
Y
yamahigashi 已提交
1547

Y
yamahigashi 已提交
1548 1549 1550
		this.node = geoNode;
		this.name = geoNode.attrName;
		this.id = geoNode.id;
Y
yamahigashi 已提交
1551

Y
yamahigashi 已提交
1552
		this.vertices = this.getVertices();
Y
yamahigashi 已提交
1553

Y
yamahigashi 已提交
1554
		if ( this.vertices === undefined ) {
Y
yamahigashi 已提交
1555

Y
yamahigashi 已提交
1556 1557
			console.log( 'FBXLoader: Geometry.parse(): pass' + this.node.id );
			return;
Y
yamahigashi 已提交
1558

Y
yamahigashi 已提交
1559
		}
Y
yamahigashi 已提交
1560

Y
yamahigashi 已提交
1561 1562 1563
		this.indices = this.getPolygonVertexIndices();
		this.uvs = ( new UV() ).parse( this.node, this );
		this.normals = ( new Normal() ).parse( this.node, this );
Y
yamahigashi 已提交
1564

Y
yamahigashi 已提交
1565
		if ( this.getPolygonTopologyMax() > 3 ) {
Y
yamahigashi 已提交
1566

Y
yamahigashi 已提交
1567 1568
			this.indices = this.convertPolyIndicesToTri(
								this.indices, this.getPolygonTopologyArray() );
Y
yamahigashi 已提交
1569

Y
yamahigashi 已提交
1570
		}
Y
yamahigashi 已提交
1571

Y
yamahigashi 已提交
1572
		return this;
Y
yamahigashi 已提交
1573

Y
yamahigashi 已提交
1574
	};
Y
yamahigashi 已提交
1575 1576


Y
yamahigashi 已提交
1577
	Geometry.prototype.getVertices = function () {
Y
yamahigashi 已提交
1578

Y
yamahigashi 已提交
1579
		if ( this.node.__cache_vertices ) {
Y
yamahigashi 已提交
1580

Y
yamahigashi 已提交
1581
			return this.node.__cache_vertices;
Y
yamahigashi 已提交
1582

Y
yamahigashi 已提交
1583
		}
Y
yamahigashi 已提交
1584

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

Y
yamahigashi 已提交
1587 1588 1589
			console.warn( 'this.node: ' + this.node.attrName + "(" + this.node.id + ") does not have Vertices" );
			this.node.__cache_vertices = undefined;
			return null;
Y
yamahigashi 已提交
1590

Y
yamahigashi 已提交
1591
		}
Y
yamahigashi 已提交
1592

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

Y
yamahigashi 已提交
1596
			return parseFloat( element );
Y
yamahigashi 已提交
1597

Y
yamahigashi 已提交
1598
		} );
Y
yamahigashi 已提交
1599

Y
yamahigashi 已提交
1600 1601
		this.node.__cache_vertices = vertices;
		return this.node.__cache_vertices;
Y
yamahigashi 已提交
1602

Y
yamahigashi 已提交
1603
	};
Y
yamahigashi 已提交
1604

Y
yamahigashi 已提交
1605
	Geometry.prototype.getPolygonVertexIndices = function () {
Y
yamahigashi 已提交
1606

Y
yamahigashi 已提交
1607
		if ( this.node.__cache_indices && this.node.__cache_poly_topology_max ) {
Y
yamahigashi 已提交
1608

Y
yamahigashi 已提交
1609
			return this.node.__cache_indices;
Y
yamahigashi 已提交
1610

Y
yamahigashi 已提交
1611
		}
Y
yamahigashi 已提交
1612

Y
yamahigashi 已提交
1613
		if ( this.node.subNodes === undefined ) {
Y
yamahigashi 已提交
1614

Y
yamahigashi 已提交
1615 1616 1617
			console.error( 'this.node.subNodes undefined' );
			console.log( this.node );
			return;
Y
yamahigashi 已提交
1618

Y
yamahigashi 已提交
1619
		}
Y
yamahigashi 已提交
1620

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

Y
yamahigashi 已提交
1623 1624 1625
			console.warn( 'this.node: ' + this.node.attrName + "(" + this.node.id + ") does not have PolygonVertexIndex " );
			this.node.__cache_indices = undefined;
			return;
Y
yamahigashi 已提交
1626

Y
yamahigashi 已提交
1627
		}
Y
yamahigashi 已提交
1628

Y
yamahigashi 已提交
1629 1630
		var rawTextIndices = this.node.subNodes.PolygonVertexIndex.properties.a;
		var indices = rawTextIndices.split( ',' );
Y
yamahigashi 已提交
1631

Y
yamahigashi 已提交
1632 1633 1634
		var currentTopo = 1;
		var topologyN = null;
		var topologyArr = [];
Y
yamahigashi 已提交
1635

Y
yamahigashi 已提交
1636 1637 1638 1639
		// 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 已提交
1640

Y
yamahigashi 已提交
1641 1642 1643
			var tmpI = parseInt( indices[ i ] );
			// found n
			if ( tmpI < 0 ) {
Y
yamahigashi 已提交
1644

Y
yamahigashi 已提交
1645
				if ( currentTopo > topologyN ) {
Y
yamahigashi 已提交
1646

Y
yamahigashi 已提交
1647
					topologyN = currentTopo;
Y
yamahigashi 已提交
1648

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

Y
yamahigashi 已提交
1651 1652 1653
				indices[ i ] = tmpI ^ - 1;
				topologyArr.push( currentTopo );
				currentTopo = 1;
Y
yamahigashi 已提交
1654

Y
yamahigashi 已提交
1655
			} else {
Y
yamahigashi 已提交
1656

Y
yamahigashi 已提交
1657 1658
				indices[ i ] = tmpI;
				currentTopo ++;
Y
yamahigashi 已提交
1659

Y
yamahigashi 已提交
1660
			}
Y
yamahigashi 已提交
1661

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

Y
yamahigashi 已提交
1664
		if ( topologyN === null ) {
Y
yamahigashi 已提交
1665

Y
yamahigashi 已提交
1666 1667 1668
			console.warn( "FBXLoader: topology N not found: " + this.node.attrName );
			console.warn( this.node );
			topologyN = 3;
Y
yamahigashi 已提交
1669

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

Y
yamahigashi 已提交
1672 1673 1674
		this.node.__cache_poly_topology_max = topologyN;
		this.node.__cache_poly_topology_arr = topologyArr;
		this.node.__cache_indices = indices;
Y
yamahigashi 已提交
1675

Y
yamahigashi 已提交
1676
		return this.node.__cache_indices;
Y
yamahigashi 已提交
1677

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

Y
yamahigashi 已提交
1680
	Geometry.prototype.getPolygonTopologyMax = function () {
Y
yamahigashi 已提交
1681

Y
yamahigashi 已提交
1682
		if ( this.node.__cache_indices && this.node.__cache_poly_topology_max ) {
Y
yamahigashi 已提交
1683

Y
yamahigashi 已提交
1684
			return this.node.__cache_poly_topology_max;
Y
yamahigashi 已提交
1685

Y
yamahigashi 已提交
1686
		}
Y
yamahigashi 已提交
1687

Y
yamahigashi 已提交
1688 1689
		this.getPolygonVertexIndices( this.node );
		return this.node.__cache_poly_topology_max;
Y
yamahigashi 已提交
1690

Y
yamahigashi 已提交
1691
	};
Y
yamahigashi 已提交
1692

Y
yamahigashi 已提交
1693
	Geometry.prototype.getPolygonTopologyArray = function () {
Y
yamahigashi 已提交
1694

Y
yamahigashi 已提交
1695
		if ( this.node.__cache_indices && this.node.__cache_poly_topology_max ) {
Y
yamahigashi 已提交
1696

Y
yamahigashi 已提交
1697
			return this.node.__cache_poly_topology_arr;
Y
yamahigashi 已提交
1698

Y
yamahigashi 已提交
1699
		}
Y
yamahigashi 已提交
1700

Y
yamahigashi 已提交
1701 1702
		this.getPolygonVertexIndices( this.node );
		return this.node.__cache_poly_topology_arr;
Y
yamahigashi 已提交
1703

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

Y
yamahigashi 已提交
1706 1707 1708 1709 1710 1711 1712
	// a - d
	// |   |
	// b - c
	//
	// [( a, b, c, d ) ...........
	// [( a, b, c ), (a, c, d )....
	Geometry.prototype.convertPolyIndicesToTri = function ( indices, strides ) {
Y
yamahigashi 已提交
1713

Y
yamahigashi 已提交
1714
		var res = [];
Y
yamahigashi 已提交
1715

Y
yamahigashi 已提交
1716 1717 1718 1719
		var i = 0;
		var tmp = [];
		var currentPolyNum = 0;
		var currentStride = 0;
Y
yamahigashi 已提交
1720

Y
yamahigashi 已提交
1721
		while ( i < indices.length ) {
Y
yamahigashi 已提交
1722

Y
yamahigashi 已提交
1723
			currentStride = strides[ currentPolyNum ];
Y
yamahigashi 已提交
1724

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

Y
yamahigashi 已提交
1728 1729 1730
				res.push( indices[ i ] );
				res.push( indices[ i + ( currentStride - 2 - j ) ] );
				res.push( indices[ i + ( currentStride - 1 - j ) ] );
Y
yamahigashi 已提交
1731

Y
yamahigashi 已提交
1732
			}
Y
yamahigashi 已提交
1733

Y
yamahigashi 已提交
1734 1735
			currentPolyNum ++;
			i += currentStride;
Y
yamahigashi 已提交
1736

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

Y
yamahigashi 已提交
1739
		return res;
Y
yamahigashi 已提交
1740

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

Y
yamahigashi 已提交
1743
	Geometry.prototype.addBones = function ( bones ) {
Y
yamahigashi 已提交
1744

Y
yamahigashi 已提交
1745
		this.bones = bones;
Y
yamahigashi 已提交
1746

Y
yamahigashi 已提交
1747
	};
Y
yamahigashi 已提交
1748 1749


Y
yamahigashi 已提交
1750
	function UV() {
Y
yamahigashi 已提交
1751

Y
yamahigashi 已提交
1752 1753 1754 1755 1756
		this.uv = null;
		this.map = null;
		this.ref = null;
		this.node = null;
		this.index = null;
Y
yamahigashi 已提交
1757

Y
yamahigashi 已提交
1758
	}
Y
yamahigashi 已提交
1759

Y
yamahigashi 已提交
1760
	UV.prototype.getUV = function ( node ) {
Y
yamahigashi 已提交
1761

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

Y
yamahigashi 已提交
1764
			return this.uv;
Y
yamahigashi 已提交
1765

Y
yamahigashi 已提交
1766
		} else {
Y
yamahigashi 已提交
1767

Y
yamahigashi 已提交
1768
			return this._parseText( node );
Y
yamahigashi 已提交
1769

Y
yamahigashi 已提交
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 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 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 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 2710 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
		}

	};

	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;

	};


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

	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
	parse_Data_ByPolygonVertex_Direct = function ( node, indices, strides, itemSize ) {

		// *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;

	};

	degToRad = function ( degrees ) {

		return degrees * Math.PI / 180;

	};

	radToDeg = function ( radians ) {

		return radians * 180 / Math.PI;

	};

	quatFromVec = function ( x, y, z ) {

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

		return quat;

	};


	// extend Array.prototype ?  ....uuuh
	toInt = function ( arr ) {

		return arr.map( function ( element ) {

			return parseInt( element );

		} );

	};

	toFloat = function ( arr ) {

		return arr.map( function ( element ) {

			return parseFloat( element );

		} );

	};

	toRad = function ( arr ) {

		return arr.map( function ( element ) {

			return degToRad( element );

		} );

	};

	toMat44 = function ( arr ) {

		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 已提交
2763 2764

} )();