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 116 117 118
		console.time( 'FBXLoader' );

		console.time( 'FBXLoader: TextParser' );
		var allNodes = new FBXParser().parse( text );
		console.timeEnd( 'FBXLoader: TextParser' );

Y
yamahigashi 已提交
119 120 121 122 123 124
		console.time( 'FBXLoader: ObjectParser' );
		scope.hierarchy = ( new Bones() ).parseHierarchy( allNodes );
		scope.weights	= ( new Weights() ).parse( allNodes, scope.hierarchy );
		scope.animations = ( new Animation() ).parse( allNodes, scope.hierarchy );
		scope.textures = ( new Textures() ).parse( allNodes, scope.hierarchy );
		console.timeEnd( 'FBXLoader: ObjectParser' );
Y
yamahigashi 已提交
125

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

Y
yamahigashi 已提交
130 131
		var container = new THREE.Object3D();
		for ( var i = 0; i < geometries.length; ++ i ) {
Y
yamahigashi 已提交
132

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

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

Y
yamahigashi 已提交
137
			}
Y
yamahigashi 已提交
138

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

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

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

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

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

Y
yamahigashi 已提交
152
		}
Y
yamahigashi 已提交
153

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

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

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

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

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

Y
yamahigashi 已提交
166
		}
Y
yamahigashi 已提交
167

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

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

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

Y
yamahigashi 已提交
176
			}
Y
yamahigashi 已提交
177

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

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

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

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

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

Y
yamahigashi 已提交
189
				}
Y
yamahigashi 已提交
190

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

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

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

Y
yamahigashi 已提交
197
		}
Y
yamahigashi 已提交
198

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

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

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

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

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

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

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

Y
yamahigashi 已提交
217
		}
Y
yamahigashi 已提交
218

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

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

Y
yamahigashi 已提交
223
		}
Y
yamahigashi 已提交
224

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

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

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

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

Y
yamahigashi 已提交
233
		}
Y
yamahigashi 已提交
234

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

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

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

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

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

Y
yamahigashi 已提交
251
		}
Y
yamahigashi 已提交
252

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

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

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

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

Y
yamahigashi 已提交
262
		}
Y
yamahigashi 已提交
263

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

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

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

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

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

Y
yamahigashi 已提交
280
		}
Y
yamahigashi 已提交
281

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

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

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

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

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

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

Y
yamahigashi 已提交
296
		}
Y
yamahigashi 已提交
297

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

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

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

Y
yamahigashi 已提交
304
			}
Y
yamahigashi 已提交
305

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

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

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

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

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
341
			}
Y
yamahigashi 已提交
342

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

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

Y
yamahigashi 已提交
347
			}
Y
yamahigashi 已提交
348

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

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

Y
yamahigashi 已提交
353
			}
Y
yamahigashi 已提交
354

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

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

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

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

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

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

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

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

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


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

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

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

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

Y
yamahigashi 已提交
390
			}
Y
yamahigashi 已提交
391

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

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

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

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

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

Y
yamahigashi 已提交
406
				}
Y
yamahigashi 已提交
407

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

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

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

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

Y
yamahigashi 已提交
421
				}
Y
yamahigashi 已提交
422

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

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

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

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

Y
yamahigashi 已提交
435
				}
Y
yamahigashi 已提交
436

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

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

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

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

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

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


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

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

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

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

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

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
		if ( mesh.geometry.animations === undefined ) {
Y
yamahigashi 已提交
473

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

Y
yamahigashi 已提交
476
		}
Y
yamahigashi 已提交
477

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

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

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

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

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

Y
yamahigashi 已提交
489
		}
Y
yamahigashi 已提交
490

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

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

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

Y
yamahigashi 已提交
499
			}
Y
yamahigashi 已提交
500

Y
yamahigashi 已提交
501
		}
Y
yamahigashi 已提交
502

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

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

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

Y
yamahigashi 已提交
510
			}
Y
yamahigashi 已提交
511

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

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

Y
yamahigashi 已提交
516
		}
Y
yamahigashi 已提交
517

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
573
		}
Y
yamahigashi 已提交
574

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

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

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

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

Y
yamahigashi 已提交
583
		}
Y
yamahigashi 已提交
584

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

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

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

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

Y
yamahigashi 已提交
596
			}
Y
yamahigashi 已提交
597

Y
yamahigashi 已提交
598
		}
Y
yamahigashi 已提交
599

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

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

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

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

Y
yamahigashi 已提交
610
		}
Y
yamahigashi 已提交
611

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

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

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

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

Y
yamahigashi 已提交
620
		}
Y
yamahigashi 已提交
621

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

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

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

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

Y
yamahigashi 已提交
630
		}
Y
yamahigashi 已提交
631

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

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

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

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

Y
yamahigashi 已提交
643
			}
Y
yamahigashi 已提交
644

Y
yamahigashi 已提交
645
		}
Y
yamahigashi 已提交
646

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

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

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

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

Y
yamahigashi 已提交
657
		}
Y
yamahigashi 已提交
658

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

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

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

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

Y
yamahigashi 已提交
668
		}
Y
yamahigashi 已提交
669

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

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
690
			}
Y
yamahigashi 已提交
691

Y
yamahigashi 已提交
692
		}
Y
yamahigashi 已提交
693

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
787
				}
Y
yamahigashi 已提交
788

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

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

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

Y
yamahigashi 已提交
800
				}
Y
yamahigashi 已提交
801

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

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

Y
yamahigashi 已提交
809
				}
Y
yamahigashi 已提交
810

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

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

Y
yamahigashi 已提交
824
				}
Y
yamahigashi 已提交
825

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

Y
yamahigashi 已提交
867
						}
Y
yamahigashi 已提交
868

Y
yamahigashi 已提交
869
					}
Y
yamahigashi 已提交
870

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

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

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

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

Y
yamahigashi 已提交
879
					}
Y
yamahigashi 已提交
880

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

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

Y
yamahigashi 已提交
885
				}
Y
yamahigashi 已提交
886

Y
yamahigashi 已提交
887
			}
Y
yamahigashi 已提交
888

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

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

Y
yamahigashi 已提交
897
			}
Y
yamahigashi 已提交
898

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
916
				}
Y
yamahigashi 已提交
917

Y
yamahigashi 已提交
918
			}
Y
yamahigashi 已提交
919

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

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

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
948
				}
Y
yamahigashi 已提交
949

Y
yamahigashi 已提交
950
			}
Y
yamahigashi 已提交
951

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

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

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

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

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

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

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

Y
yamahigashi 已提交
971
			}
Y
yamahigashi 已提交
972

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

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

Y
yamahigashi 已提交
980
			}
Y
yamahigashi 已提交
981

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

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

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

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

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

Y
yamahigashi 已提交
994
				}
Y
yamahigashi 已提交
995

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
1063
			}
Y
yamahigashi 已提交
1064

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

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

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
1091
		}
Y
yamahigashi 已提交
1092

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

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

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

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


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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
1156
				}
Y
yamahigashi 已提交
1157

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

Y
yamahigashi 已提交
1163
			}
Y
yamahigashi 已提交
1164

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


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

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

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

Y
yamahigashi 已提交
1180
			}
Y
yamahigashi 已提交
1181

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

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

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

Y
yamahigashi 已提交
1194
				}
Y
yamahigashi 已提交
1195

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

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

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

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

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

Y
yamahigashi 已提交
1209
			}
Y
yamahigashi 已提交
1210

Y
yamahigashi 已提交
1211
		}
Y
yamahigashi 已提交
1212

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

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

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

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

Y
yamahigashi 已提交
1232
		}
Y
yamahigashi 已提交
1233

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

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

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

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

Y
yamahigashi 已提交
1256 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
		   "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 已提交
1288

Y
yamahigashi 已提交
1289
	}
Y
yamahigashi 已提交
1290

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

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

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

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

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

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

Y
yamahigashi 已提交
1306
		}
Y
yamahigashi 已提交
1307

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

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

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

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

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

Y
yamahigashi 已提交
1322
			}
Y
yamahigashi 已提交
1323

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

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

Y
yamahigashi 已提交
1331
			}
Y
yamahigashi 已提交
1332

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

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

Y
yamahigashi 已提交
1337
			}
Y
yamahigashi 已提交
1338

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

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

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

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

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

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

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

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

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

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

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

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
	Bones.prototype.restoreBindPose = function ( node ) {
Y
yamahigashi 已提交
1376

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

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

Y
yamahigashi 已提交
1382
		}
Y
yamahigashi 已提交
1383

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

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

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

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

Y
yamahigashi 已提交
1396
		}
Y
yamahigashi 已提交
1397

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

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
1424
				}
Y
yamahigashi 已提交
1425

Y
yamahigashi 已提交
1426
			}
Y
yamahigashi 已提交
1427

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

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

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

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

Y
yamahigashi 已提交
1445
		}
Y
yamahigashi 已提交
1446

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

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

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

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

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

Y
yamahigashi 已提交
1457
			}
Y
yamahigashi 已提交
1458

Y
yamahigashi 已提交
1459
		}
Y
yamahigashi 已提交
1460

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

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

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

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

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

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

Y
yamahigashi 已提交
1474
			}
Y
yamahigashi 已提交
1475

Y
yamahigashi 已提交
1476
		}
Y
yamahigashi 已提交
1477

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

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

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

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

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
1503
		}
Y
yamahigashi 已提交
1504

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

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

Y
yamahigashi 已提交
1509
		}
Y
yamahigashi 已提交
1510

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

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

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

Y
yamahigashi 已提交
1519
			}
Y
yamahigashi 已提交
1520

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

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

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


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

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

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

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

Y
yamahigashi 已提交
1543
	}
Y
yamahigashi 已提交
1544

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

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

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

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

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

Y
yamahigashi 已提交
1558
		}
Y
yamahigashi 已提交
1559

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

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

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

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

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

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


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

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

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

Y
yamahigashi 已提交
1582
		}
Y
yamahigashi 已提交
1583

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

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

Y
yamahigashi 已提交
1590
		}
Y
yamahigashi 已提交
1591

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
1610
		}
Y
yamahigashi 已提交
1611

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

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

Y
yamahigashi 已提交
1618
		}
Y
yamahigashi 已提交
1619

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

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
1648
				}
Y
yamahigashi 已提交
1649

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

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

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

Y
yamahigashi 已提交
1659
			}
Y
yamahigashi 已提交
1660

Y
yamahigashi 已提交
1661
		}
Y
yamahigashi 已提交
1662

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

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

Y
yamahigashi 已提交
1669
		}
Y
yamahigashi 已提交
1670

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

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

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

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

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
1698
		}
Y
yamahigashi 已提交
1699

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

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

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

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

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

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

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

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

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

Y
yamahigashi 已提交
1731
			}
Y
yamahigashi 已提交
1732

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

Y
yamahigashi 已提交
1736
		}
Y
yamahigashi 已提交
1737

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

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

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

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

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


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

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

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

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

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

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

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

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

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

	};

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

} )();