ColladaLoader.js 75.7 KB
Newer Older
T
credits  
timk 已提交
1 2 3 4
/**
 * @author Tim Knip / http://www.floorplanner.com/ / tim at floorplanner.com
 */

M
Mr.doob 已提交
5
THREE.ColladaLoader = function () {
6

T
timk 已提交
7 8 9
	var COLLADA = null;
	var scene = null;
	var daeScene;
10 11 12

	var readyCallbackFunc = null;

T
timk 已提交
13 14 15 16 17 18 19
 	var sources = {};
	var images = {};
	var animations = {};
	var controllers = {};
	var geometries = {};
	var materials = {};
	var effects = {};
20
	var cameras = {};
21

22
	var animData;
T
timk 已提交
23
	var visualScenes;
T
timk 已提交
24
	var baseUrl;
T
timk 已提交
25 26
	var morphs;
	var skins;
27 28

	var flip_uv = true;
T
timk 已提交
29
	var preferredShading = THREE.SmoothShading;
M
Mr.doob 已提交
30

31
	var options = {
32 33 34 35
		// Force Geometry to always be centered at the local origin of the
		// containing Mesh.
		centerGeometry: false,

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
		// Axis conversion is done for geometries, animations, and controllers.
		// If we ever pull cameras or lights out of the COLLADA file, they'll
		// need extra work.
		convertUpAxis: false,

		subdivideFaces: true,

		upAxis: 'Y'
	};

	// TODO: support unit conversion as well
	var colladaUnit = 1.0;
	var colladaUp = 'Y';
	var upConversion = null;

	var TO_RADIANS = Math.PI / 180;

53 54 55
	function load ( url, readyCallback, progressCallback ) {

		var length = 0;
56 57 58

		if ( document.implementation && document.implementation.createDocument ) {

T
timk 已提交
59 60
			var req = new XMLHttpRequest();

61
			if ( req.overrideMimeType ) req.overrideMimeType( "text/xml" );
T
timk 已提交
62 63

			req.onreadystatechange = function() {
64 65 66 67

				if( req.readyState == 4 ) {

					if( req.status == 0 || req.status == 200 ) {
M
Mr.doob 已提交
68

69 70 71 72 73 74 75 76 77 78 79

						if ( req.responseXML ) {

							readyCallbackFunc = readyCallback;
							parse( req.responseXML, undefined, url );

						} else {

							console.error( "ColladaLoader: Empty or non-existing file (" + url + ")" );

						}
80

T
timk 已提交
81
					}
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96
				} else if ( req.readyState == 3 ) {

					if ( progressCallback ) {

						if ( length == 0 ) {

							length = req.getResponseHeader( "Content-Length" );

						}

						progressCallback( { total: length, loaded: req.responseText.length } );

					}

T
timk 已提交
97
				}
98

T
timk 已提交
99
			}
100 101 102 103

			req.open( "GET", url, true );
			req.send( null );

T
timk 已提交
104
		} else {
105

106
			alert( "Don't know how to parse XML!" );
107

T
timk 已提交
108
		}
109 110 111

	};

112
	function parse( doc, callBack, url ) {
113

T
timk 已提交
114 115
		COLLADA = doc;
		callBack = callBack || readyCallbackFunc;
116 117 118

		if ( url !== undefined ) {

119
			var parts = url.split( '/' );
T
timk 已提交
120
			parts.pop();
121
			baseUrl = ( parts.length < 1 ? '.' : parts.join( '/' ) ) + '/';
122

T
timk 已提交
123
		}
124

125 126
		parseAsset();
		setUpConversion();
127 128 129 130
		images = parseLib( "//dae:library_images/dae:image", _Image, "image" );
		materials = parseLib( "//dae:library_materials/dae:material", Material, "material") ;
		effects = parseLib( "//dae:library_effects/dae:effect", Effect, "effect" );
		geometries = parseLib( "//dae:library_geometries/dae:geometry", Geometry, "geometry" );
131
		cameras = parseLib( ".//dae:library_cameras/dae:camera", Camera, "camera" );
132 133 134 135
		controllers = parseLib( "//dae:library_controllers/dae:controller", Controller, "controller" );
		animations = parseLib( "//dae:library_animations/dae:animation", Animation, "animation" );
		visualScenes = parseLib( ".//dae:library_visual_scenes/dae:visual_scene", VisualScene, "visual_scene" );

T
timk 已提交
136 137
		morphs = [];
		skins = [];
138

T
timk 已提交
139 140
		daeScene = parseScene();
		scene = new THREE.Object3D();
141

142
		for ( var i = 0; i < daeScene.nodes.length; i ++ ) {
143

144
			scene.add( createSceneGraph( daeScene.nodes[ i ] ) );
145

T
timk 已提交
146
		}
T
timk 已提交
147

T
timk 已提交
148 149 150
		createAnimations();

		var result = {
151 152

			scene: scene,
T
timk 已提交
153 154
			morphs: morphs,
			skins: skins,
155
			animations: animData,
T
timk 已提交
156 157 158
			dae: {
				images: images,
				materials: materials,
159
				cameras: cameras,
T
timk 已提交
160 161 162 163 164 165 166
				effects: effects,
				geometries: geometries,
				controllers: controllers,
				animations: animations,
				visualScenes: visualScenes,
				scene: daeScene
			}
167

T
timk 已提交
168
		};
169 170 171 172 173

		if ( callBack ) {

			callBack( result );

T
timk 已提交
174
		}
175

T
timk 已提交
176
		return result;
177 178 179 180 181

	};

	function setPreferredShading ( shading ) {

T
timk 已提交
182
		preferredShading = shading;
183 184 185

	};

186 187
	function parseAsset () {

188
		var elements = COLLADA.evaluate( '//dae:asset', COLLADA, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
189 190 191

		var element = elements.iterateNext();

192
		if ( element && element.childNodes ) {
193

194
			for ( var i = 0; i < element.childNodes.length; i ++ ) {
195

196
				var child = element.childNodes[ i ];
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

				switch ( child.nodeName ) {

					case 'unit':

						var meter = child.getAttribute( 'meter' );

						if ( meter ) {

							colladaUnit = parseFloat( meter );

						}

						break;

					case 'up_axis':

						colladaUp = child.textContent.charAt(0);
						break;

				}

			}

		}

	};

225 226
	function parseLib ( q, classSpec, prefix ) {

227
		var elements = COLLADA.evaluate(q, COLLADA, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;
228

T
timk 已提交
229 230 231
		var lib = {};
		var element = elements.iterateNext();
		var i = 0;
232 233 234 235

		while ( element ) {

			var daeElement = ( new classSpec() ).parse( element );
236
			if ( !daeElement.id || daeElement.id.length == 0 ) daeElement.id = prefix + ( i ++ );
237
			lib[ daeElement.id ] = daeElement;
238

T
timk 已提交
239
			element = elements.iterateNext();
240

T
timk 已提交
241
		}
242

T
timk 已提交
243
		return lib;
244 245 246

	};

247
	function parseScene() {
248

249
		var sceneElement = COLLADA.evaluate( './/dae:scene/dae:instance_visual_scene', COLLADA, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ).iterateNext();
250 251 252 253

		if ( sceneElement ) {

			var url = sceneElement.getAttribute( 'url' ).replace( /^#/, '' );
254
			return visualScenes[ url.length > 0 ? url : 'visual_scene0' ];
255

T
timk 已提交
256
		} else {
257

T
timk 已提交
258
			return null;
259

T
timk 已提交
260
		}
261 262 263

	};

264
	function createAnimations() {
265

266 267 268 269 270 271 272
		animData = [];

		// fill in the keys
		recurseHierarchy( scene );

	};

273
	function recurseHierarchy( node ) {
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

		var n = daeScene.getChildById( node.name, true ),
			newData = null;

		if ( n && n.keys ) {

			newData = {
				fps: 60,
				hierarchy: [ {
					node: n,
					keys: n.keys,
					sids: n.sids
				} ],
				node: node,
				name: 'animation_' + node.name,
				length: 0
			};
291

292
			animData.push(newData);
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307
			for ( var i = 0, il = n.keys.length; i < il; i++ ) {

				newData.length = Math.max( newData.length, n.keys[i].time );

			}

		} else  {

			newData = {
				hierarchy: [ {
					keys: [],
					sids: []
				} ]
			}
308 309 310

		}

311 312 313 314 315 316 317 318 319 320 321 322 323 324
		for ( var i = 0, il = node.children.length; i < il; i++ ) {

			var d = recurseHierarchy( node.children[i] );

			for ( var j = 0, jl = d.hierarchy.length; j < jl; j ++ ) {

				newData.hierarchy.push( {
					keys: [],
					sids: []
				} );

			}

		}
325

326
		return newData;
327 328 329 330 331

	};

	function calcAnimationBounds () {

T
timk 已提交
332 333
		var start = 1000000;
		var end = -start;
T
timk 已提交
334
		var frames = 0;
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

		for ( var id in animations ) {

			var animation = animations[ id ];

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

				var sampler = animation.sampler[ i ];
				sampler.create();

				start = Math.min( start, sampler.startTime );
				end = Math.max( end, sampler.endTime );
				frames = Math.max( frames, sampler.input.length );

			}

		}

		return { start:start, end:end, frames:frames };

	};

	function createMorph ( geometry, ctrl ) {

		var morphCtrl = ctrl instanceof InstanceController ? controllers[ ctrl.url ] : ctrl;

		if ( !morphCtrl || !morphCtrl.morph ) {

T
timk 已提交
363 364
			console.log("could not find morph controller!");
			return;
365

T
timk 已提交
366
		}
367

T
timk 已提交
368 369
		var morph = morphCtrl.morph;

370 371 372 373 374 375 376
		for ( var i = 0; i < morph.targets.length; i ++ ) {

			var target_id = morph.targets[ i ];
			var daeGeometry = geometries[ target_id ];

			if ( !daeGeometry.mesh ||
				 !daeGeometry.mesh.primitives ||
377
				 !daeGeometry.mesh.primitives.length ) {
378
				 continue;
T
timk 已提交
379
			}
380 381 382 383 384

			var target = daeGeometry.mesh.primitives[ 0 ].geometry;

			if ( target.vertices.length === geometry.vertices.length ) {

T
timk 已提交
385
				geometry.morphTargets.push( { name: "target_1", vertices: target.vertices } );
386

T
timk 已提交
387
			}
388

T
timk 已提交
389
		}
390

T
timk 已提交
391
		geometry.morphTargets.push( { name: "target_Z", vertices: geometry.vertices } );
392 393 394 395 396 397 398 399 400

	};

	function createSkin ( geometry, ctrl, applyBindShape ) {

		var skinCtrl = controllers[ ctrl.url ];

		if ( !skinCtrl || !skinCtrl.skin ) {

401
			console.log( "could not find skin controller!" );
T
timk 已提交
402
			return;
403

T
timk 已提交
404
		}
405 406 407

		if ( !ctrl.skeleton || !ctrl.skeleton.length ) {

408
			console.log( "could not find the skeleton for the skin!" );
T
timk 已提交
409
			return;
410

T
timk 已提交
411
		}
412

T
timk 已提交
413
		var skin = skinCtrl.skin;
414
		var skeleton = daeScene.getChildById( ctrl.skeleton[ 0 ] );
T
timk 已提交
415
		var hierarchy = [];
416

T
timk 已提交
417
		applyBindShape = applyBindShape !== undefined ? applyBindShape : true;
418

T
timk 已提交
419 420 421
		var bones = [];
		geometry.skinWeights = [];
		geometry.skinIndices = [];
422

423 424
		//createBones( geometry.bones, skin, hierarchy, skeleton, null, -1 );
		//createWeights( skin, geometry.bones, geometry.skinIndices, geometry.skinWeights );
425

T
timk 已提交
426 427 428 429 430 431 432 433 434
		/*
		geometry.animation = {
			name: 'take_001',
			fps: 30,
			length: 2,
			JIT: true,
			hierarchy: hierarchy
		};
		*/
435 436 437 438 439

		if ( applyBindShape ) {

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

440
				skin.bindShapeMatrix.multiplyVector3( geometry.vertices[ i ] );
441

T
timk 已提交
442
			}
443

T
timk 已提交
444
		}
445 446 447 448 449

	};

	function setupSkeleton ( node, bones, frame, parent ) {

T
timk 已提交
450
		node.world = node.world || new THREE.Matrix4();
451 452 453 454 455 456 457 458 459
		node.world.copy( node.matrix );

		if ( node.channels && node.channels.length ) {

			var channel = node.channels[ 0 ];
			var m = channel.sampler.output[ frame ];

			if ( m instanceof THREE.Matrix4 ) {

460
				node.world.copy( m );
461

T
timk 已提交
462
			}
463

T
timk 已提交
464
		}
465 466 467 468 469

		if ( parent ) {

			node.world.multiply( parent, node.world );

T
timk 已提交
470 471
		}

472 473 474 475 476 477
		bones.push( node );

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

			setupSkeleton( node.nodes[ i ], bones, frame, node.world );

T
timk 已提交
478
		}
479 480 481 482 483

	};

	function setupSkinningMatrices ( bones, skin ) {

T
timk 已提交
484
		// FIXME: this is dumb...
485 486 487 488

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

			var bone = bones[ i ];
T
timk 已提交
489
			var found = -1;
490

M
Mr.doob 已提交
491 492
			if ( bone.type != 'JOINT' ) continue;

493 494 495 496
			for ( var j = 0; j < skin.joints.length; j ++ ) {

				if ( bone.sid == skin.joints[ j ] ) {

T
timk 已提交
497 498
					found = j;
					break;
499

T
timk 已提交
500
				}
501

T
timk 已提交
502
			}
503

504
			if ( found >= 0 ) {
505 506 507

				var inv = skin.invBindMatrices[ found ];

T
timk 已提交
508 509
				bone.invBindMatrix = inv;
				bone.skinningMatrix = new THREE.Matrix4();
T
hmm  
timk 已提交
510
				bone.skinningMatrix.multiply(bone.world, inv); // (IBMi * JMi)
511

T
timk 已提交
512
				bone.weights = [];
513 514 515 516 517 518 519 520 521 522 523

				for ( var j = 0; j < skin.weights.length; j ++ ) {

					for (var k = 0; k < skin.weights[ j ].length; k ++) {

						var w = skin.weights[ j ][ k ];

						if ( w.joint == found ) {

							bone.weights.push( w );

T
timk 已提交
524
						}
525

T
timk 已提交
526
					}
527

T
timk 已提交
528
				}
529

T
timk 已提交
530
			} else {
531

M
Mr.doob 已提交
532
				throw 'ColladaLoader: Could not find joint \'' + bone.sid + '\'.';
533

T
timk 已提交
534
			}
535

T
timk 已提交
536
		}
537 538 539 540 541 542 543

	};

	function applySkin ( geometry, instanceCtrl, frame ) {

		var skinController = controllers[ instanceCtrl.url ];

T
timk 已提交
544
		frame = frame !== undefined ? frame : 40;
545 546 547

		if ( !skinController || !skinController.skin ) {

M
Mr.doob 已提交
548
			console.log( 'ColladaLoader: Could not find skin controller.' );
T
timk 已提交
549
			return;
550

T
timk 已提交
551 552
		}

553 554
		if ( !instanceCtrl.skeleton || !instanceCtrl.skeleton.length ) {

M
Mr.doob 已提交
555
			console.log( 'ColladaLoader: Could not find the skeleton for the skin. ' );
T
timk 已提交
556
			return;
557

T
timk 已提交
558
		}
559

T
timk 已提交
560
		var animationBounds = calcAnimationBounds();
561 562 563
		var skeleton = daeScene.getChildById( instanceCtrl.skeleton[0], true ) ||
					   daeScene.getChildBySid( instanceCtrl.skeleton[0], true );

T
timk 已提交
564 565 566 567
		var i, j, w, vidx, weight;
		var v = new THREE.Vector3(), o, s;

		// move vertices to bind shape
568 569 570

		for ( i = 0; i < geometry.vertices.length; i ++ ) {

571
			skinController.skin.bindShapeMatrix.multiplyVector3( geometry.vertices[i] );
572

T
timk 已提交
573
		}
574

T
timk 已提交
575
		// process animation, or simply pose the rig if no animation
576 577 578

		for ( frame = 0; frame < animationBounds.frames; frame ++ ) {

T
timk 已提交
579 580
			var bones = [];
			var skinned = [];
581

T
timk 已提交
582
			// zero skinned vertices
583 584 585

			for ( i = 0; i < geometry.vertices.length; i++ ) {

M
Mr.doob 已提交
586
				skinned.push( new THREE.Vector3() );
587

T
timk 已提交
588
			}
589 590

			// process the frame and setup the rig with a fresh
T
timk 已提交
591
			// transform, possibly from the bone's animation channel(s)
592 593 594 595

			setupSkeleton( skeleton, bones, frame );
			setupSkinningMatrices( bones, skinController.skin );

T
timk 已提交
596
			// skin 'm
597 598 599

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

M
Mr.doob 已提交
600 601
				if ( bones[ i ].type != 'JOINT' ) continue;

602 603 604
				for ( j = 0; j < bones[ i ].weights.length; j ++ ) {

					w = bones[ i ].weights[ j ];
T
timk 已提交
605 606
					vidx = w.index;
					weight = w.weight;
607

T
timk 已提交
608 609
					o = geometry.vertices[vidx];
					s = skinned[vidx];
610

611 612 613
					v.x = o.x;
					v.y = o.y;
					v.z = o.z;
614

T
timk 已提交
615
					bones[i].skinningMatrix.multiplyVector3(v);
616

617 618 619
					s.x += (v.x * weight);
					s.y += (v.y * weight);
					s.z += (v.z * weight);
620

T
timk 已提交
621
				}
622

T
timk 已提交
623
			}
624

T
timk 已提交
625
			geometry.morphTargets.push( { name: "target_" + frame, vertices: skinned } );
626

T
timk 已提交
627
		}
628 629 630 631 632

	};

	function createSceneGraph ( node, parent ) {

T
timk 已提交
633 634
		var obj = new THREE.Object3D();
		var skinned = false;
T
timk 已提交
635 636
		var skinController;
		var morphController;
637
		var i, j;
638

T
timk 已提交
639
		// FIXME: controllers
640

641
		for ( i = 0; i < node.controllers.length; i ++ ) {
642

643
			var controller = controllers[ node.controllers[ i ].url ];
644 645 646

			switch ( controller.type ) {

T
timk 已提交
647
				case 'skin':
648

649
					if ( geometries[ controller.skin.source ] ) {
650

T
timk 已提交
651
						var inst_geom = new InstanceGeometry();
652

T
timk 已提交
653
						inst_geom.url = controller.skin.source;
654 655 656
						inst_geom.instance_material = node.controllers[ i ].instance_material;

						node.geometries.push( inst_geom );
T
timk 已提交
657
						skinned = true;
658
						skinController = node.controllers[ i ];
659

660
					} else if ( controllers[ controller.skin.source ] ) {
661

T
timk 已提交
662 663
						// urgh: controller can be chained
						// handle the most basic case...
664 665

						var second = controllers[ controller.skin.source ];
T
timk 已提交
666 667
						morphController = second;
					//	skinController = node.controllers[i];
668 669 670

						if ( second.morph && geometries[ second.morph.source ] ) {

T
timk 已提交
671
							var inst_geom = new InstanceGeometry();
672

T
timk 已提交
673
							inst_geom.url = second.morph.source;
674 675 676 677
							inst_geom.instance_material = node.controllers[ i ].instance_material;

							node.geometries.push( inst_geom );

T
timk 已提交
678
						}
679

T
timk 已提交
680
					}
681

T
timk 已提交
682
					break;
683

T
timk 已提交
684
				case 'morph':
685

686
					if ( geometries[ controller.morph.source ] ) {
687

T
timk 已提交
688
						var inst_geom = new InstanceGeometry();
689

T
timk 已提交
690
						inst_geom.url = controller.morph.source;
691 692 693 694
						inst_geom.instance_material = node.controllers[ i ].instance_material;

						node.geometries.push( inst_geom );
						morphController = node.controllers[ i ];
695

T
timk 已提交
696
					}
697

M
Mr.doob 已提交
698
					console.log( 'ColladaLoader: Morph-controller partially supported.' );
699

T
timk 已提交
700 701
				default:
					break;
702

T
timk 已提交
703
			}
704

T
timk 已提交
705
		}
706

T
timk 已提交
707
		// FIXME: multi-material mesh?
T
timk 已提交
708
		// geometries
709 710 711

		for ( i = 0; i < node.geometries.length; i ++ ) {

T
timk 已提交
712 713 714
			var instance_geometry = node.geometries[i];
			var instance_materials = instance_geometry.instance_material;
			var geometry = geometries[instance_geometry.url];
T
timk 已提交
715
			var used_materials = {};
716
			var used_materials_array = [];
T
timk 已提交
717 718
			var num_materials = 0;
			var first_material;
719 720 721

			if ( geometry ) {

722
				if ( !geometry.mesh || !geometry.mesh.primitives )
T
timk 已提交
723
					continue;
724

725
				if ( obj.name.length == 0 ) {
726

T
timk 已提交
727
					obj.name = geometry.id;
728

T
timk 已提交
729
				}
730

T
timk 已提交
731
				// collect used fx for this geometry-instance
732 733 734 735 736

				if ( instance_materials ) {

					for ( j = 0; j < instance_materials.length; j ++ ) {

737 738
						var instance_material = instance_materials[ j ];
						var mat = materials[ instance_material.target ];
739
						var effect_id = mat.instance_effect.url;
740
						var shader = effects[ effect_id ].shader;
741

T
timk 已提交
742
						shader.material.opacity = !shader.material.opacity ? 1 : shader.material.opacity;
743 744
						used_materials[ instance_material.symbol ] = num_materials;
						used_materials_array.push( shader.material )
T
timk 已提交
745
						first_material = shader.material;
746
						first_material.name = mat.name == null || mat.name === '' ? mat.id : mat.name;
747 748
						num_materials ++;

T
timk 已提交
749
					}
750

T
timk 已提交
751
				}
752

T
timk 已提交
753
				var mesh;
754
				var material = first_material || new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.FlatShading } );
T
timk 已提交
755
				var geom = geometry.mesh.geometry3js;
756 757 758

				if ( num_materials > 1 ) {

T
timk 已提交
759
					material = new THREE.MeshFaceMaterial();
760
					geom.materials = used_materials_array;
761

762 763 764
					for ( j = 0; j < geom.faces.length; j ++ ) {

						var face = geom.faces[ j ];
765
						face.materialIndex = used_materials[ face.daeMaterial ]
766

T
timk 已提交
767
					}
768

T
timk 已提交
769
				}
770 771 772 773 774

				if ( skinController !== undefined) {

					applySkin( geom, skinController );

T
timk 已提交
775
					material.morphTargets = true;
776

T
timk 已提交
777 778
					mesh = new THREE.SkinnedMesh( geom, material );
					mesh.skeleton = skinController.skeleton;
779
					mesh.skinController = controllers[ skinController.url ];
T
timk 已提交
780
					mesh.skinInstanceController = skinController;
T
timk 已提交
781
					mesh.name = 'skin_' + skins.length;
782 783 784 785 786 787 788

					skins.push( mesh );

				} else if ( morphController !== undefined ) {

					createMorph( geom, morphController );

T
timk 已提交
789
					material.morphTargets = true;
790

T
timk 已提交
791 792
					mesh = new THREE.Mesh( geom, material );
					mesh.name = 'morph_' + morphs.length;
793 794 795

					morphs.push( mesh );

T
timk 已提交
796
				} else {
797

T
timk 已提交
798
					mesh = new THREE.Mesh( geom, material );
799
					// mesh.geom.name = geometry.id;
800

T
timk 已提交
801
				}
802

803
				node.geometries.length > 1 ? obj.add( mesh ) : obj = mesh;
804

T
timk 已提交
805
			}
806

T
timk 已提交
807
		}
808

809 810 811 812 813 814 815 816 817
		for ( i = 0; i < node.cameras.length; i ++ ) {

			var instance_camera = node.cameras[i];
			var cparams = cameras[instance_camera.url];

			obj = new THREE.PerspectiveCamera(cparams.fov, cparams.aspect_ratio, cparams.znear, cparams.zfar);

		}

818
		obj.name = node.id || "";
819 820 821 822 823 824
		obj.matrix = node.matrix;
		var props = node.matrix.decompose();
		obj.position = props[ 0 ];
		obj.quaternion = props[ 1 ];
		obj.useQuaternion = true;
		obj.scale = props[ 2 ];
825

826 827 828 829 830 831 832 833
		if ( options.centerGeometry && obj.geometry ) {

			var delta = THREE.GeometryUtils.center( obj.geometry );
			obj.quaternion.multiplyVector3( delta.multiplySelf( obj.scale ) );
			obj.position.subSelf( delta );

		}

834 835
		for ( i = 0; i < node.nodes.length; i ++ ) {

836
			obj.add( createSceneGraph( node.nodes[i], node ) );
837

T
timk 已提交
838
		}
839

T
timk 已提交
840
		return obj;
841 842 843 844 845 846 847 848 849

	};

	function getJointId( skin, id ) {

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

			if ( skin.joints[ i ] == id ) {

T
timk 已提交
850
				return i;
851

T
timk 已提交
852
			}
853

T
timk 已提交
854
		}
855 856 857 858 859

	};

	function getLibraryNode( id ) {

860
		return COLLADA.evaluate( './/dae:library_nodes//dae:node[@id=\'' + id + '\']', COLLADA, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ).iterateNext();
861 862 863 864 865

	};

	function getChannelsForNode (node ) {

T
timk 已提交
866 867 868
		var channels = [];
		var startTime = 1000000;
		var endTime = -1000000;
869 870 871

		for ( var id in animations ) {

T
timk 已提交
872
			var animation = animations[id];
873 874 875

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

T
timk 已提交
876 877 878
				var channel = animation.channel[i];
				var sampler = animation.sampler[i];
				var id = channel.target.split('/')[0];
879 880 881

				if ( id == node.id ) {

T
timk 已提交
882 883 884 885 886
					sampler.create();
					channel.sampler = sampler;
					startTime = Math.min(startTime, sampler.startTime);
					endTime = Math.max(endTime, sampler.endTime);
					channels.push(channel);
887

T
timk 已提交
888
				}
889

T
timk 已提交
890
			}
891

T
timk 已提交
892
		}
893 894 895

		if ( channels.length ) {

T
timk 已提交
896
			node.startTime = startTime;
897 898
			node.endTime = endTime;

T
timk 已提交
899
		}
900

T
timk 已提交
901
		return channels;
902 903 904 905 906

	};

	function calcFrameDuration( node ) {

T
timk 已提交
907
		var minT = 10000000;
908

909
		for ( var i = 0; i < node.channels.length; i ++ ) {
910

T
timk 已提交
911
			var sampler = node.channels[i].sampler;
912

913
			for ( var j = 0; j < sampler.input.length - 1; j ++ ) {
914

915 916 917
				var t0 = sampler.input[ j ];
				var t1 = sampler.input[ j + 1 ];
				minT = Math.min( minT, t1 - t0 );
918

T
timk 已提交
919 920
			}
		}
921

T
timk 已提交
922
		return minT;
923 924 925 926 927

	};

	function calcMatrixAt( node, t ) {

T
timk 已提交
928
		var animated = {};
929

T
timk 已提交
930
		var i, j;
931 932 933 934

		for ( i = 0; i < node.channels.length; i ++ ) {

			var channel = node.channels[ i ];
T
timk 已提交
935
			animated[ channel.sid ] = channel;
936

T
timk 已提交
937
		}
938

T
timk 已提交
939
		var matrix = new THREE.Matrix4();
940 941 942

		for ( i = 0; i < node.transforms.length; i ++ ) {

943 944
			var transform = node.transforms[ i ];
			var channel = animated[ transform.sid ];
945 946 947

			if ( channel !== undefined ) {

T
timk 已提交
948 949
				var sampler = channel.sampler;
				var value;
950

951
				for ( j = 0; j < sampler.input.length - 1; j ++ ) {
952

953
					if ( sampler.input[ j + 1 ] > t ) {
954

955
						value = sampler.output[ j ];
T
timk 已提交
956 957
						//console.log(value.flatten)
						break;
958

T
timk 已提交
959
					}
960

T
timk 已提交
961
				}
962 963 964 965 966 967 968

				if ( value !== undefined ) {

					if ( value instanceof THREE.Matrix4 ) {

						matrix = matrix.multiply( matrix, value );

T
timk 已提交
969
					} else {
970

T
timk 已提交
971
						// FIXME: handle other types
972 973 974

						matrix = matrix.multiply( matrix, transform.matrix );

T
timk 已提交
975
					}
976

T
timk 已提交
977
				} else {
978 979 980

					matrix = matrix.multiply( matrix, transform.matrix );

T
timk 已提交
981
				}
982

T
timk 已提交
983
			} else {
984 985 986

				matrix = matrix.multiply( matrix, transform.matrix );

T
timk 已提交
987
			}
988

T
timk 已提交
989
		}
990

T
timk 已提交
991
		return matrix;
992 993 994 995 996 997 998

	};

	function bakeAnimations ( node ) {

		if ( node.channels && node.channels.length ) {

999 1000
			var keys = [],
				sids = [];
1001

1002
			for ( var i = 0, il = node.channels.length; i < il; i++ ) {
1003

1004 1005 1006 1007
				var channel = node.channels[i],
					fullSid = channel.fullSid,
					sampler = channel.sampler,
					input = sampler.input,
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
					transform = node.getTransformBySid( channel.sid ),
					member;

				if ( channel.arrIndices ) {

					member = [];

					for ( var j = 0, jl = channel.arrIndices.length; j < jl; j++ ) {

						member[ j ] = getConvertedIndex( channel.arrIndices[ j ] );

					}

				} else {

					member = getConvertedMember( channel.member );

				}
1026

1027
				if ( transform ) {
1028

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
					if ( sids.indexOf( fullSid ) === -1 ) {

						sids.push( fullSid );

					}

					for ( var j = 0, jl = input.length; j < jl; j++ ) {

						var time = input[j],
							data = sampler.getData( transform.type, j ),
							key = findKey( keys, time );

						if ( !key ) {

							key = new Key( time );
							var timeNdx = findTimeNdx( keys, time );
							keys.splice( timeNdx == -1 ? keys.length : timeNdx, 0, key );

						}

						key.addTarget( fullSid, transform, member, data );

					}

				} else {

					console.log( 'Could not find transform "' + channel.sid + '" in node ' + node.id );

				}

			}

			// post process
			for ( var i = 0; i < sids.length; i++ ) {

				var sid = sids[ i ];

				for ( var j = 0; j < keys.length; j++ ) {

					var key = keys[ j ];

					if ( !key.hasTarget( sid ) ) {

						interpolateKeys( keys, key, j, sid );

					}

				}
1077

T
timk 已提交
1078
			}
1079

T
timk 已提交
1080
			node.keys = keys;
1081
			node.sids = sids;
1082

T
timk 已提交
1083
		}
1084 1085 1086

	};

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	function findKey ( keys, time) {

		var retVal = null;

		for ( var i = 0, il = keys.length; i < il && retVal == null; i++ ) {

			var key = keys[i];

			if ( key.time === time ) {

				retVal = key;

			} else if ( key.time > time ) {

				break;

			}

		}

		return retVal;

	};

	function findTimeNdx ( keys, time) {

		var ndx = -1;

		for ( var i = 0, il = keys.length; i < il && ndx == -1; i++ ) {

			var key = keys[i];

			if ( key.time >= time ) {

				ndx = i;

			}

		}

		return ndx;

	};

	function interpolateKeys ( keys, key, ndx, fullSid ) {

		var prevKey = getPrevKeyWith( keys, fullSid, ndx ? ndx-1 : 0 ),
			nextKey = getNextKeyWith( keys, fullSid, ndx+1 );

		if ( prevKey && nextKey ) {

			var scale = (key.time - prevKey.time) / (nextKey.time - prevKey.time),
				prevTarget = prevKey.getTarget( fullSid ),
				nextData = nextKey.getTarget( fullSid ).data,
				prevData = prevTarget.data,
				data;

1144 1145 1146 1147 1148
			if ( prevTarget.type === 'matrix' ) {

				data = prevData;

			} else if ( prevData.length ) {
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211

				data = [];

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

					data[ i ] = prevData[ i ] + ( nextData[ i ] - prevData[ i ] ) * scale;

				}

			} else {

				data = prevData + ( nextData - prevData ) * scale;

			}

			key.addTarget( fullSid, prevTarget.transform, prevTarget.member, data );

		}

	};

	// Get next key with given sid

	function getNextKeyWith( keys, fullSid, ndx ) {

		for ( ; ndx < keys.length; ndx++ ) {

			var key = keys[ ndx ];

			if ( key.hasTarget( fullSid ) ) {

				return key;

			}

		}

		return null;

	};

	// Get previous key with given sid

	function getPrevKeyWith( keys, fullSid, ndx ) {

		ndx = ndx >= 0 ? ndx : ndx + keys.length;

		for ( ; ndx >= 0; ndx-- ) {

			var key = keys[ ndx ];

			if ( key.hasTarget( fullSid ) ) {

				return key;

			}

		}

		return null;

	};

T
timk 已提交
1212
	function _Image() {
1213

T
timk 已提交
1214 1215
		this.id = "";
		this.init_from = "";
1216 1217 1218

	};

T
timk 已提交
1219
	_Image.prototype.parse = function(element) {
1220

T
timk 已提交
1221
		this.id = element.getAttribute('id');
1222 1223 1224 1225 1226 1227 1228

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

			var child = element.childNodes[ i ];

			if ( child.nodeName == 'init_from' ) {

T
timk 已提交
1229
				this.init_from = child.textContent;
1230

T
timk 已提交
1231
			}
1232

T
timk 已提交
1233
		}
1234

T
timk 已提交
1235
		return this;
1236 1237 1238

	};

T
timk 已提交
1239
	function Controller() {
1240

T
timk 已提交
1241 1242 1243 1244 1245
		this.id = "";
		this.name = "";
		this.type = "";
		this.skin = null;
		this.morph = null;
1246 1247 1248 1249 1250

	};

	Controller.prototype.parse = function( element ) {

T
timk 已提交
1251 1252 1253
		this.id = element.getAttribute('id');
		this.name = element.getAttribute('name');
		this.type = "none";
1254 1255 1256 1257 1258 1259 1260

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

			var child = element.childNodes[ i ];

			switch ( child.nodeName ) {

T
timk 已提交
1261
				case 'skin':
1262

T
timk 已提交
1263 1264 1265
					this.skin = (new Skin()).parse(child);
					this.type = child.nodeName;
					break;
1266

T
timk 已提交
1267
				case 'morph':
1268

T
timk 已提交
1269 1270 1271
					this.morph = (new Morph()).parse(child);
					this.type = child.nodeName;
					break;
1272

T
timk 已提交
1273 1274
				default:
					break;
1275

T
timk 已提交
1276 1277
			}
		}
1278

T
timk 已提交
1279
		return this;
1280 1281 1282

	};

T
timk 已提交
1283
	function Morph() {
1284

M
Mr.doob 已提交
1285 1286 1287 1288
		this.method = null;
		this.source = null;
		this.targets = null;
		this.weights = null;
1289 1290 1291 1292 1293

	};

	Morph.prototype.parse = function( element ) {

T
timk 已提交
1294 1295 1296
		var sources = {};
		var inputs = [];
		var i;
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307

		this.method = element.getAttribute( 'method' );
		this.source = element.getAttribute( 'source' ).replace( /^#/, '' );

		for ( i = 0; i < element.childNodes.length; i ++ ) {

			var child = element.childNodes[ i ];
			if ( child.nodeType != 1 ) continue;

			switch ( child.nodeName ) {

T
timk 已提交
1308
				case 'source':
1309 1310 1311

					var source = ( new Source() ).parse( child );
					sources[ source.id ] = source;
T
timk 已提交
1312
					break;
1313

T
timk 已提交
1314
				case 'targets':
1315 1316

					inputs = this.parseInputs( child );
T
timk 已提交
1317
					break;
1318

T
timk 已提交
1319
				default:
1320 1321

					console.log( child.nodeName );
T
timk 已提交
1322
					break;
1323

T
timk 已提交
1324
			}
1325

T
timk 已提交
1326
		}
1327 1328 1329 1330 1331 1332 1333 1334

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

			var input = inputs[ i ];
			var source = sources[ input.source ];

			switch ( input.semantic ) {

T
timk 已提交
1335
				case 'MORPH_TARGET':
1336

T
timk 已提交
1337 1338
					this.targets = source.read();
					break;
1339

T
timk 已提交
1340
				case 'MORPH_WEIGHT':
1341

T
timk 已提交
1342 1343
					this.weights = source.read();
					break;
1344

T
timk 已提交
1345 1346
				default:
					break;
1347

T
timk 已提交
1348 1349
			}
		}
1350

T
timk 已提交
1351
		return this;
1352 1353 1354

	};

T
timk 已提交
1355
	Morph.prototype.parseInputs = function(element) {
1356

T
timk 已提交
1357
		var inputs = [];
1358 1359 1360

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

T
timk 已提交
1361
			var child = element.childNodes[i];
1362 1363 1364 1365
			if ( child.nodeType != 1) continue;

			switch ( child.nodeName ) {

T
timk 已提交
1366
				case 'input':
1367 1368

					inputs.push( (new Input()).parse(child) );
T
timk 已提交
1369
					break;
1370

T
timk 已提交
1371 1372 1373 1374
				default:
					break;
			}
		}
1375

T
timk 已提交
1376
		return inputs;
1377 1378 1379

	};

T
timk 已提交
1380
	function Skin() {
1381

T
timk 已提交
1382
		this.source = "";
T
timk 已提交
1383 1384 1385 1386
		this.bindShapeMatrix = null;
		this.invBindMatrices = [];
		this.joints = [];
		this.weights = [];
1387 1388 1389 1390 1391

	};

	Skin.prototype.parse = function( element ) {

T
timk 已提交
1392 1393
		var sources = {};
		var joints, weights;
1394 1395

		this.source = element.getAttribute( 'source' ).replace( /^#/, '' );
T
timk 已提交
1396 1397 1398
		this.invBindMatrices = [];
		this.joints = [];
		this.weights = [];
1399 1400 1401

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

T
timk 已提交
1402
			var child = element.childNodes[i];
1403 1404 1405 1406
			if ( child.nodeType != 1 ) continue;

			switch ( child.nodeName ) {

T
timk 已提交
1407
				case 'bind_shape_matrix':
1408

T
timk 已提交
1409
					var f = _floats(child.textContent);
1410
					this.bindShapeMatrix = getConvertedMat4( f );
T
timk 已提交
1411
					break;
1412

T
timk 已提交
1413
				case 'source':
1414

T
timk 已提交
1415
					var src = new Source().parse(child);
1416
					sources[ src.id ] = src;
T
timk 已提交
1417
					break;
1418

T
timk 已提交
1419
				case 'joints':
1420

T
timk 已提交
1421 1422
					joints = child;
					break;
1423

T
timk 已提交
1424
				case 'vertex_weights':
1425

T
timk 已提交
1426 1427
					weights = child;
					break;
1428

T
timk 已提交
1429
				default:
1430 1431

					console.log( child.nodeName );
T
timk 已提交
1432
					break;
1433

T
timk 已提交
1434 1435
			}
		}
1436 1437 1438 1439

		this.parseJoints( joints, sources );
		this.parseWeights( weights, sources );

T
timk 已提交
1440
		return this;
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452

	};

	Skin.prototype.parseJoints = function ( element, sources ) {

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

			var child = element.childNodes[ i ];
			if ( child.nodeType != 1 ) continue;

			switch ( child.nodeName ) {

T
timk 已提交
1453
				case 'input':
1454 1455 1456 1457 1458 1459

					var input = ( new Input() ).parse( child );
					var source = sources[ input.source ];

					if ( input.semantic == 'JOINT' ) {

T
timk 已提交
1460
						this.joints = source.read();
1461 1462 1463

					} else if ( input.semantic == 'INV_BIND_MATRIX' ) {

T
timk 已提交
1464
						this.invBindMatrices = source.read();
1465

T
timk 已提交
1466
					}
1467

T
timk 已提交
1468
					break;
1469

T
timk 已提交
1470 1471 1472
				default:
					break;
			}
1473

T
timk 已提交
1474
		}
1475 1476 1477 1478 1479

	};

	Skin.prototype.parseWeights = function ( element, sources ) {

T
timk 已提交
1480
		var v, vcount, inputs = [];
1481 1482 1483 1484 1485 1486 1487 1488

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

			var child = element.childNodes[ i ];
			if ( child.nodeType != 1 ) continue;

			switch ( child.nodeName ) {

T
timk 已提交
1489
				case 'input':
1490

1491
					inputs.push( ( new Input() ).parse( child ) );
T
timk 已提交
1492
					break;
1493

T
timk 已提交
1494
				case 'v':
1495

1496
					v = _ints( child.textContent );
T
timk 已提交
1497
					break;
1498

T
timk 已提交
1499
				case 'vcount':
1500

1501
					vcount = _ints( child.textContent );
T
timk 已提交
1502
					break;
1503

T
timk 已提交
1504 1505
				default:
					break;
1506

T
timk 已提交
1507
			}
1508

T
timk 已提交
1509
		}
1510

T
timk 已提交
1511
		var index = 0;
1512 1513 1514

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

T
timk 已提交
1515 1516
			var numBones = vcount[i];
			var vertex_weights = [];
1517 1518 1519

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

T
timk 已提交
1520
				var influence = {};
1521 1522 1523 1524 1525 1526 1527 1528

				for ( var k = 0; k < inputs.length; k ++ ) {

					var input = inputs[ k ];
					var value = v[ index + input.offset ];

					switch ( input.semantic ) {

T
timk 已提交
1529
						case 'JOINT':
1530

T
timk 已提交
1531
							influence.joint = value;//this.joints[value];
T
timk 已提交
1532
							break;
1533

T
timk 已提交
1534
						case 'WEIGHT':
1535 1536

							influence.weight = sources[ input.source ].data[ value ];
T
timk 已提交
1537
							break;
1538

T
timk 已提交
1539 1540
						default:
							break;
1541

T
timk 已提交
1542
					}
1543

T
timk 已提交
1544
				}
1545 1546

				vertex_weights.push( influence );
T
timk 已提交
1547 1548
				index += inputs.length;
			}
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563

			for ( var j = 0; j < vertex_weights.length; j ++ ) {

				vertex_weights[ j ].index = i;

			}

			this.weights.push( vertex_weights );

		}

	};

	function VisualScene () {

T
timk 已提交
1564 1565 1566 1567
		this.id = "";
		this.name = "";
		this.nodes = [];
		this.scene = new THREE.Object3D();
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578

	};

	VisualScene.prototype.getChildById = function( id, recursive ) {

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

			var node = this.nodes[ i ].getChildById( id, recursive );

			if ( node ) {

T
timk 已提交
1579
				return node;
1580

T
timk 已提交
1581
			}
1582

T
timk 已提交
1583
		}
1584

T
timk 已提交
1585
		return null;
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596

	};

	VisualScene.prototype.getChildBySid = function( sid, recursive ) {

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

			var node = this.nodes[ i ].getChildBySid( sid, recursive );

			if ( node ) {

T
timk 已提交
1597
				return node;
1598

T
timk 已提交
1599
			}
1600

T
timk 已提交
1601
		}
1602

T
timk 已提交
1603
		return null;
1604 1605 1606 1607 1608 1609 1610

	};

	VisualScene.prototype.parse = function( element ) {

		this.id = element.getAttribute( 'id' );
		this.name = element.getAttribute( 'name' );
T
timk 已提交
1611
		this.nodes = [];
1612 1613 1614 1615 1616 1617 1618 1619

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

			var child = element.childNodes[ i ];
			if ( child.nodeType != 1 ) continue;

			switch ( child.nodeName ) {

T
timk 已提交
1620
				case 'node':
1621 1622

					this.nodes.push( ( new Node() ).parse( child ) );
T
timk 已提交
1623
					break;
1624

T
timk 已提交
1625 1626
				default:
					break;
1627

T
timk 已提交
1628
			}
1629

T
timk 已提交
1630
		}
1631

T
timk 已提交
1632
		return this;
1633 1634 1635

	};

T
timk 已提交
1636
	function Node() {
1637

T
timk 已提交
1638 1639 1640 1641 1642 1643 1644
		this.id = "";
		this.name = "";
		this.sid = "";
		this.nodes = [];
		this.controllers = [];
		this.transforms = [];
		this.geometries = [];
T
timk 已提交
1645
		this.channels = [];
T
timk 已提交
1646
		this.matrix = new THREE.Matrix4();
1647 1648 1649 1650 1651 1652 1653

	};

	Node.prototype.getChannelForTransform = function( transformSid ) {

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

T
timk 已提交
1654 1655 1656 1657 1658 1659 1660 1661
			var channel = this.channels[i];
			var parts = channel.target.split('/');
			var id = parts.shift();
			var sid = parts.shift();
			var dotSyntax = (sid.indexOf(".") >= 0);
			var arrSyntax = (sid.indexOf("(") >= 0);
			var arrIndices;
			var member;
1662 1663 1664

			if ( dotSyntax ) {

T
timk 已提交
1665 1666 1667
				parts = sid.split(".");
				sid = parts.shift();
				member = parts.shift();
1668 1669 1670

			} else if ( arrSyntax ) {

T
timk 已提交
1671 1672
				arrIndices = sid.split("(");
				sid = arrIndices.shift();
1673 1674 1675 1676 1677

				for ( var j = 0; j < arrIndices.length; j ++ ) {

					arrIndices[ j ] = parseInt( arrIndices[ j ].replace( /\)/, '' ) );

T
timk 已提交
1678
				}
1679

T
timk 已提交
1680
			}
1681

1682
			if ( sid == transformSid ) {
1683

1684
				channel.info = { sid: sid, dotSyntax: dotSyntax, arrSyntax: arrSyntax, arrIndices: arrIndices };
T
timk 已提交
1685
				return channel;
1686

T
timk 已提交
1687
			}
1688

T
timk 已提交
1689
		}
1690

T
timk 已提交
1691
		return null;
1692 1693 1694 1695 1696 1697 1698

	};

	Node.prototype.getChildById = function ( id, recursive ) {

		if ( this.id == id ) {

T
timk 已提交
1699
			return this;
1700

T
timk 已提交
1701
		}
1702 1703 1704 1705 1706 1707 1708 1709 1710

		if ( recursive ) {

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

				var n = this.nodes[ i ].getChildById( id, recursive );

				if ( n ) {

T
timk 已提交
1711
					return n;
1712

T
timk 已提交
1713
				}
1714

T
timk 已提交
1715
			}
1716

T
timk 已提交
1717
		}
1718

T
timk 已提交
1719
		return null;
1720 1721 1722 1723 1724 1725 1726

	};

	Node.prototype.getChildBySid = function ( sid, recursive ) {

		if ( this.sid == sid ) {

T
timk 已提交
1727
			return this;
1728

T
timk 已提交
1729
		}
1730 1731 1732 1733 1734 1735 1736 1737 1738

		if ( recursive ) {

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

				var n = this.nodes[ i ].getChildBySid( sid, recursive );

				if ( n ) {

T
timk 已提交
1739
					return n;
1740

T
timk 已提交
1741
				}
1742

T
timk 已提交
1743 1744
			}
		}
1745

T
timk 已提交
1746
		return null;
1747 1748 1749 1750 1751 1752 1753 1754 1755

	};

	Node.prototype.getTransformBySid = function ( sid ) {

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

			if ( this.transforms[ i ].sid == sid ) return this.transforms[ i ];

T
timk 已提交
1756
		}
1757

T
timk 已提交
1758
		return null;
1759 1760 1761 1762 1763

	};

	Node.prototype.parse = function( element ) {

T
timk 已提交
1764
		var url;
1765

T
timk 已提交
1766 1767 1768 1769
		this.id = element.getAttribute('id');
		this.sid = element.getAttribute('sid');
		this.name = element.getAttribute('name');
		this.type = element.getAttribute('type');
1770

T
timk 已提交
1771
		this.type = this.type == 'JOINT' ? this.type : 'NODE';
1772

T
timk 已提交
1773 1774 1775
		this.nodes = [];
		this.transforms = [];
		this.geometries = [];
1776
		this.cameras = [];
T
timk 已提交
1777 1778 1779
		this.controllers = [];
		this.matrix = new THREE.Matrix4();

1780 1781 1782
		for ( var i = 0; i < element.childNodes.length; i ++ ) {

			var child = element.childNodes[ i ];
1783
			if ( child.nodeType != 1 ) continue;
1784 1785 1786

			switch ( child.nodeName ) {

T
timk 已提交
1787
				case 'node':
1788 1789

					this.nodes.push( ( new Node() ).parse( child ) );
T
timk 已提交
1790
					break;
1791

T
timk 已提交
1792
				case 'instance_camera':
1793

1794
					this.cameras.push( ( new InstanceCamera() ).parse( child ) );
T
timk 已提交
1795
					break;
1796

T
timk 已提交
1797
				case 'instance_controller':
1798 1799

					this.controllers.push( ( new InstanceController() ).parse( child ) );
T
timk 已提交
1800
					break;
1801

T
timk 已提交
1802
				case 'instance_geometry':
1803 1804

					this.geometries.push( ( new InstanceGeometry() ).parse( child ) );
T
timk 已提交
1805
					break;
1806

T
timk 已提交
1807
				case 'instance_light':
1808

T
timk 已提交
1809
					break;
1810

T
timk 已提交
1811
				case 'instance_node':
1812 1813 1814 1815 1816 1817 1818 1819

					url = child.getAttribute( 'url' ).replace( /^#/, '' );
					var iNode = getLibraryNode( url );

					if ( iNode ) {

						this.nodes.push( ( new Node() ).parse( iNode )) ;

T
timk 已提交
1820
					}
1821

T
timk 已提交
1822
					break;
1823

T
timk 已提交
1824 1825 1826 1827 1828 1829
				case 'rotate':
				case 'translate':
				case 'scale':
				case 'matrix':
				case 'lookat':
				case 'skew':
1830 1831

					this.transforms.push( ( new Transform() ).parse( child ) );
T
timk 已提交
1832
					break;
1833

T
timk 已提交
1834 1835
				case 'extra':
					break;
1836

T
timk 已提交
1837
				default:
1838 1839

					console.log( child.nodeName );
T
timk 已提交
1840
					break;
1841

T
timk 已提交
1842
			}
1843

T
timk 已提交
1844
		}
1845 1846 1847 1848

		this.channels = getChannelsForNode( this );
		bakeAnimations( this );

T
timk 已提交
1849
		this.updateMatrix();
1850

T
timk 已提交
1851
		return this;
1852 1853 1854 1855 1856

	};

	Node.prototype.updateMatrix = function () {

T
timk 已提交
1857
		this.matrix.identity();
1858 1859 1860

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

1861
			this.transforms[ i ].apply( this.matrix );
1862

T
timk 已提交
1863
		}
1864 1865 1866 1867 1868

	};

	function Transform () {

T
timk 已提交
1869 1870 1871
		this.sid = "";
		this.type = "";
		this.data = [];
1872
		this.obj = null;
1873 1874 1875 1876 1877 1878

	};

	Transform.prototype.parse = function ( element ) {

		this.sid = element.getAttribute( 'sid' );
T
timk 已提交
1879
		this.type = element.nodeName;
1880
		this.data = _floats( element.textContent );
1881
		this.convert();
1882

T
timk 已提交
1883
		return this;
1884 1885 1886

	};

1887
	Transform.prototype.convert = function () {
1888

1889
		switch ( this.type ) {
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
			case 'matrix':

				this.obj = getConvertedMat4( this.data );
				break;

			case 'rotate':

				this.angle = this.data[3] * TO_RADIANS;

			case 'translate':

				fixCoords( this.data, -1 );
				this.obj = new THREE.Vector3( this.data[ 0 ], this.data[ 1 ], this.data[ 2 ] );
				break;

			case 'scale':

				fixCoords( this.data, 1 );
				this.obj = new THREE.Vector3( this.data[ 0 ], this.data[ 1 ], this.data[ 2 ] );
				break;

			default:
				console.log( 'Can not convert Transform of type ' + this.type );
				break;

		}

	};

	Transform.prototype.apply = function ( matrix ) {
1921 1922 1923

		switch ( this.type ) {

T
timk 已提交
1924
			case 'matrix':
1925

1926
				matrix.multiplySelf( this.obj );
T
timk 已提交
1927
				break;
1928

T
timk 已提交
1929
			case 'translate':
1930

1931
				matrix.translate( this.obj );
T
timk 已提交
1932
				break;
1933

T
timk 已提交
1934
			case 'rotate':
1935

1936
				matrix.rotateByAxis( this.obj, this.angle );
T
timk 已提交
1937
				break;
1938

T
timk 已提交
1939
			case 'scale':
1940

1941
				matrix.scale( this.obj );
T
timk 已提交
1942
				break;
1943

1944 1945 1946 1947 1948 1949
		}

	};

	Transform.prototype.update = function ( data, member ) {

1950 1951
		var members = [ 'X', 'Y', 'Z', 'ANGLE' ];

1952 1953 1954 1955
		switch ( this.type ) {

			case 'matrix':

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
				if ( ! member ) {

					this.obj.copy( data );

				} else if ( member.length === 1 ) {

					switch ( member[ 0 ] ) {

						case 0:

							this.obj.n11 = data[ 0 ];
							this.obj.n21 = data[ 1 ];
							this.obj.n31 = data[ 2 ];
							this.obj.n41 = data[ 3 ];

							break;

						case 1:

							this.obj.n12 = data[ 0 ];
							this.obj.n22 = data[ 1 ];
							this.obj.n32 = data[ 2 ];
							this.obj.n42 = data[ 3 ];

							break;

						case 2:

							this.obj.n13 = data[ 0 ];
							this.obj.n23 = data[ 1 ];
							this.obj.n33 = data[ 2 ];
							this.obj.n43 = data[ 3 ];

							break;

						case 3:

							this.obj.n14 = data[ 0 ];
							this.obj.n24 = data[ 1 ];
							this.obj.n34 = data[ 2 ];
							this.obj.n44 = data[ 3 ];

							break;

					}

				} else if ( member.length === 2 ) {

					var propName = 'n' + ( member[ 0 ] + 1 ) + ( member[ 1 ] + 1 );
					this.obj[ propName ] = data;
					
				} else {

					console.log('Incorrect addressing of matrix in transform.');

				}

T
timk 已提交
2013
				break;
2014

2015 2016 2017
			case 'translate':
			case 'scale':

2018 2019 2020 2021 2022 2023
				if ( Object.prototype.toString.call( member ) === '[object Array]' ) {

					member = members[ member[ 0 ] ];

				}

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
				switch ( member ) {

					case 'X':

						this.obj.x = data;
						break;

					case 'Y':

						this.obj.y = data;
						break;

					case 'Z':

						this.obj.z = data;
						break;

					default:

						this.obj.x = data[ 0 ];
						this.obj.y = data[ 1 ];
						this.obj.z = data[ 2 ];
						break;

				}

				break;

			case 'rotate':

2054 2055 2056 2057 2058 2059
				if ( Object.prototype.toString.call( member ) === '[object Array]' ) {

					member = members[ member[ 0 ] ];

				}

2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
				switch ( member ) {

					case 'X':

						this.obj.x = data;
						break;

					case 'Y':

						this.obj.y = data;
						break;
2071

2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
					case 'Z':

						this.obj.z = data;
						break;

					case 'ANGLE':

						this.angle = data * TO_RADIANS;
						break;

					default:

						this.obj.x = data[ 0 ];
						this.obj.y = data[ 1 ];
						this.obj.z = data[ 2 ];
						this.angle = data[ 3 ] * TO_RADIANS;
						break;

				}
				break;

		}
2094 2095

	};
T
timk 已提交
2096 2097

	function InstanceController() {
2098

T
timk 已提交
2099 2100 2101
		this.url = "";
		this.skeleton = [];
		this.instance_material = [];
2102 2103 2104 2105 2106

	};

	InstanceController.prototype.parse = function ( element ) {

T
timk 已提交
2107 2108 2109
		this.url = element.getAttribute('url').replace(/^#/, '');
		this.skeleton = [];
		this.instance_material = [];
2110 2111 2112

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

T
timk 已提交
2113 2114
			var child = element.childNodes[i];
			if (child.nodeType != 1) continue;
2115 2116 2117

			switch ( child.nodeName ) {

T
timk 已提交
2118
				case 'skeleton':
2119 2120

					this.skeleton.push( child.textContent.replace(/^#/, '') );
T
timk 已提交
2121
					break;
2122

T
timk 已提交
2123
				case 'bind_material':
2124

2125
					var instances = COLLADA.evaluate( './/dae:instance_material', child, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
2126 2127 2128

					if ( instances ) {

T
timk 已提交
2129
						var instance = instances.iterateNext();
2130 2131 2132

						while ( instance ) {

T
timk 已提交
2133
							this.instance_material.push((new InstanceMaterial()).parse(instance));
2134 2135
							instance = instances.iterateNext();

T
timk 已提交
2136
						}
2137

T
timk 已提交
2138
					}
2139

T
timk 已提交
2140
					break;
2141

T
timk 已提交
2142 2143
				case 'extra':
					break;
2144

T
timk 已提交
2145 2146
				default:
					break;
2147

T
timk 已提交
2148 2149
			}
		}
2150

T
timk 已提交
2151
		return this;
2152 2153 2154 2155 2156

	};

	function InstanceMaterial () {

T
timk 已提交
2157 2158
		this.symbol = "";
		this.target = "";
2159 2160 2161 2162 2163

	};

	InstanceMaterial.prototype.parse = function ( element ) {

T
timk 已提交
2164 2165 2166
		this.symbol = element.getAttribute('symbol');
		this.target = element.getAttribute('target').replace(/^#/, '');
		return this;
2167 2168 2169

	};

T
timk 已提交
2170
	function InstanceGeometry() {
2171

T
timk 已提交
2172 2173
		this.url = "";
		this.instance_material = [];
2174 2175 2176

	};

2177
	InstanceGeometry.prototype.parse = function ( element ) {
2178

T
timk 已提交
2179 2180
		this.url = element.getAttribute('url').replace(/^#/, '');
		this.instance_material = [];
2181 2182 2183

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

T
timk 已提交
2184
			var child = element.childNodes[i];
2185
			if ( child.nodeType != 1 ) continue;
2186 2187 2188

			if ( child.nodeName == 'bind_material' ) {

2189
				var instances = COLLADA.evaluate( './/dae:instance_material', child, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
2190

2191
				if ( instances ) {
2192

T
timk 已提交
2193
					var instance = instances.iterateNext();
2194 2195 2196 2197 2198 2199

					while ( instance ) {

						this.instance_material.push( (new InstanceMaterial()).parse(instance) );
						instance = instances.iterateNext();

T
timk 已提交
2200
					}
2201

T
timk 已提交
2202
				}
2203

T
timk 已提交
2204
				break;
2205

T
timk 已提交
2206
			}
2207

T
timk 已提交
2208
		}
2209

T
timk 已提交
2210
		return this;
2211 2212 2213

	};

T
timk 已提交
2214
	function Geometry() {
2215

T
timk 已提交
2216 2217
		this.id = "";
		this.mesh = null;
2218 2219 2220 2221 2222

	};

	Geometry.prototype.parse = function ( element ) {

T
timk 已提交
2223
		this.id = element.getAttribute('id');
2224 2225 2226

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

T
timk 已提交
2227
			var child = element.childNodes[i];
2228 2229 2230

			switch ( child.nodeName ) {

T
timk 已提交
2231
				case 'mesh':
2232

T
timk 已提交
2233 2234
					this.mesh = (new Mesh(this)).parse(child);
					break;
2235

2236
				case 'extra':
2237

2238 2239 2240
					// console.log( child );
					break;

T
timk 已提交
2241 2242 2243 2244
				default:
					break;
			}
		}
2245

T
timk 已提交
2246
		return this;
2247 2248 2249 2250 2251

	};

	function Mesh( geometry ) {

T
timk 已提交
2252 2253 2254
		this.geometry = geometry.id;
		this.primitives = [];
		this.vertices = null;
M
Mr.doob 已提交
2255
		this.geometry3js = null;
2256 2257 2258 2259 2260

	};

	Mesh.prototype.parse = function( element ) {

T
timk 已提交
2261
		this.primitives = [];
2262

T
timk 已提交
2263
		var i, j;
2264 2265 2266

		for ( i = 0; i < element.childNodes.length; i ++ ) {

2267
			var child = element.childNodes[ i ];
2268

2269
			switch ( child.nodeName ) {
2270

T
timk 已提交
2271
				case 'source':
2272

2273
					_source( child );
T
timk 已提交
2274
					break;
2275

T
timk 已提交
2276
				case 'vertices':
2277

2278
					this.vertices = ( new Vertices() ).parse( child );
T
timk 已提交
2279
					break;
2280

T
timk 已提交
2281
				case 'triangles':
2282

2283
					this.primitives.push( ( new Triangles().parse( child ) ) );
T
timk 已提交
2284
					break;
2285

T
timk 已提交
2286
				case 'polygons':
2287

2288 2289
					this.primitives.push( ( new Polygons().parse( child ) ) );
					break;
2290

T
timk 已提交
2291
				case 'polylist':
2292

2293
					this.primitives.push( ( new Polylist().parse( child ) ) );
T
timk 已提交
2294
					break;
2295

T
timk 已提交
2296 2297
				default:
					break;
2298

T
timk 已提交
2299
			}
2300

T
timk 已提交
2301
		}
2302

T
timk 已提交
2303
		this.geometry3js = new THREE.Geometry();
2304 2305 2306

		var vertexData = sources[ this.vertices.input['POSITION'].source ].data;

2307
		for ( i = 0; i < vertexData.length; i += 3 ) {
2308

M
Mr.doob 已提交
2309
			this.geometry3js.vertices.push( getConvertedVec3( vertexData, i ).clone() );
2310

T
timk 已提交
2311
		}
T
timk 已提交
2312

2313 2314
		for ( i = 0; i < this.primitives.length; i ++ ) {

2315
			var primitive = this.primitives[ i ];
2316
			primitive.setVertices( this.vertices );
2317
			this.handlePrimitive( primitive, this.geometry3js );
2318

T
timk 已提交
2319
		}
2320

T
timk 已提交
2321 2322
		this.geometry3js.computeCentroids();
		this.geometry3js.computeFaceNormals();
2323
		
2324
		if ( this.geometry3js.calcNormals ) {
2325 2326
			
			this.geometry3js.computeVertexNormals();
2327
			delete this.geometry3js.calcNormals;
2328 2329 2330
			
		}
		
T
timk 已提交
2331
		this.geometry3js.computeBoundingBox();
2332

T
timk 已提交
2333
		return this;
2334 2335 2336

	};

2337
	Mesh.prototype.handlePrimitive = function( primitive, geom ) {
2338

2339
		var j, k, pList = primitive.p, inputs = primitive.inputs;
T
timk 已提交
2340
		var input, index, idx32;
2341
		var source, numParams;
2342
		var vcIndex = 0, vcount = 3, maxOffset = 0;
T
timk 已提交
2343
		var texture_sets = [];
2344 2345 2346

		for ( j = 0; j < inputs.length; j ++ ) {

2347
			input = inputs[ j ];
2348 2349
			var offset = input.offset + 1;
			maxOffset = (maxOffset < offset)? offset : maxOffset;
2350

2351
			switch ( input.semantic ) {
2352

2353 2354 2355
				case 'TEXCOORD':
					texture_sets.push( input.set );
					break;
2356

T
timk 已提交
2357
			}
2358

T
timk 已提交
2359
		}
2360

2361
		for ( var pCount = 0; pCount < pList.length; ++pCount ) {
2362

2363
			var p = pList[ pCount ], i = 0;
2364

2365
			while ( i < p.length ) {
2366

2367 2368
				var vs = [];
				var ns = [];
2369
				var ts = null;
2370
				var cs = [];
2371

2372
				if ( primitive.vcount ) {
2373

2374
					vcount = primitive.vcount.length ? primitive.vcount[ vcIndex ++ ] : primitive.vcount;
2375

2376
				} else {
2377

2378
					vcount = p.length / maxOffset;
2379

2380
				}
2381 2382


2383
				for ( j = 0; j < vcount; j ++ ) {
2384

2385
					for ( k = 0; k < inputs.length; k ++ ) {
2386

2387 2388
						input = inputs[ k ];
						source = sources[ input.source ];
2389

2390 2391 2392
						index = p[ i + ( j * maxOffset ) + input.offset ];
						numParams = source.accessor.params.length;
						idx32 = index * numParams;
2393

2394
						switch ( input.semantic ) {
2395

2396
							case 'VERTEX':
2397

2398
								vs.push( index );
2399

2400
								break;
2401

2402
							case 'NORMAL':
2403

2404
								ns.push( getConvertedVec3( source.data, idx32 ) );
2405

2406
								break;
2407

2408
							case 'TEXCOORD':
2409

2410
								ts = ts || { };
2411 2412 2413
								if ( ts[ input.set ] === undefined ) ts[ input.set ] = [];
								// invert the V
								ts[ input.set ].push( new THREE.UV( source.data[ idx32 ], 1.0 - source.data[ idx32 + 1 ] ) );
2414

2415
								break;
2416

2417
							case 'COLOR':
2418

2419 2420 2421 2422 2423
								cs.push( new THREE.Color().setRGB( source.data[ idx32 ], source.data[ idx32 + 1 ], source.data[ idx32 + 2 ] ) );

								break;

							default:
2424
							
2425 2426 2427
								break;

						}
2428

T
timk 已提交
2429
					}
2430

T
timk 已提交
2431
				}
2432

2433
				if ( ns.length == 0 ) {
2434 2435

					// check the vertices inputs
2436
					input = this.vertices.input.NORMAL;
2437

2438
					if ( input ) {
2439

2440 2441
						source = sources[ input.source ];
						numParams = source.accessor.params.length;
2442

2443
						for ( var ndx = 0, len = vs.length; ndx < len; ndx++ ) {
2444

2445
							ns.push( getConvertedVec3( source.data, vs[ ndx ] * numParams ) );
2446

2447
						}
2448

2449 2450
					} else {

2451
						geom.calcNormals = true;
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

					}

				}

				if ( !ts ) {

					ts = { };
					// check the vertices inputs
					input = this.vertices.input.TEXCOORD;

					if ( input ) {

						texture_sets.push( input.set );
						source = sources[ input.source ];
						numParams = source.accessor.params.length;

						for ( var ndx = 0, len = vs.length; ndx < len; ndx++ ) {

							idx32 = vs[ ndx ] * numParams;
							if ( ts[ input.set ] === undefined ) ts[ input.set ] = [ ];
							// invert the V
							ts[ input.set ].push( new THREE.UV( source.data[ idx32 ], 1.0 - source.data[ idx32 + 1 ] ) );

						}

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
				if ( cs.length == 0 ) {

					// check the vertices inputs
					input = this.vertices.input.COLOR;

					if ( input ) {

						source = sources[ input.source ];
						numParams = source.accessor.params.length;

						for ( var ndx = 0, len = vs.length; ndx < len; ndx++ ) {

							idx32 = vs[ ndx ] * numParams;
							cs.push( new THREE.Color().setRGB( source.data[ idx32 ], source.data[ idx32 + 1 ], source.data[ idx32 + 2 ] ) );

						}

					}

				}

				var face = null, faces = [], uv, uvArr;

2505
				if ( vcount === 3 ) {
2506

2507
					faces.push( new THREE.Face3( vs[0], vs[1], vs[2], ns, cs.length ? cs : new THREE.Color() ) );
2508

2509 2510
				} else if ( vcount === 4 ) {
					faces.push( new THREE.Face4( vs[0], vs[1], vs[2], vs[3], ns, cs.length ? cs : new THREE.Color() ) );
2511

2512
				} else if ( vcount > 4 && options.subdivideFaces ) {
2513

2514 2515
					var clr = cs.length ? cs : new THREE.Color(),
						vec1, vec2, vec3, v1, v2, norm;
2516

2517 2518
					// subdivide into multiple Face3s
					for ( k = 1; k < vcount-1; ) {
2519

2520 2521
						// FIXME: normals don't seem to be quite right
						faces.push( new THREE.Face3( vs[0], vs[k], vs[k+1], [ ns[0], ns[k++], ns[k] ],  clr ) );
2522

2523
					}
2524 2525

				}
2526

2527
				if ( faces.length ) {
2528

2529
					for (var ndx = 0, len = faces.length; ndx < len; ndx++) {
2530

2531 2532 2533
						face = faces[ndx];
						face.daeMaterial = primitive.material;
						geom.faces.push( face );
2534

2535
						for ( k = 0; k < texture_sets.length; k++ ) {
2536

2537
							uv = ts[ texture_sets[k] ];
2538

2539
							if ( vcount > 4 ) {
2540

2541 2542
								// Grab the right UVs for the vertices in this face
								uvArr = [ uv[0], uv[ndx+1], uv[ndx+2] ];
2543

2544
							} else if ( vcount === 4 ) {
2545

2546
								uvArr = [ uv[0], uv[1], uv[2], uv[3] ];
2547

2548
							} else {
2549

2550
								uvArr = [ uv[0], uv[1], uv[2] ];
2551

2552
							}
2553

2554
							if ( !geom.faceVertexUvs[k] ) {
2555

2556
								geom.faceVertexUvs[k] = [];
2557

2558
							}
2559

2560
							geom.faceVertexUvs[k].push( uvArr );
2561

2562
						}
2563 2564 2565

					}

2566
				} else {
2567

2568
					console.log( 'dropped face with vcount ' + vcount + ' for geometry with id: ' + geom.id );
2569

2570
				}
2571

2572
				i += maxOffset * vcount;
2573

2574
			}
T
timk 已提交
2575
		}
2576 2577 2578

	};

2579
	function Polygons () {
2580

T
timk 已提交
2581 2582 2583
		this.material = "";
		this.count = 0;
		this.inputs = [];
M
Mr.doob 已提交
2584
		this.vcount = null;
T
timk 已提交
2585 2586
		this.p = [];
		this.geometry = new THREE.Geometry();
2587 2588 2589

	};

2590
	Polygons.prototype.setVertices = function ( vertices ) {
2591 2592 2593 2594 2595 2596 2597

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

			if ( this.inputs[ i ].source == vertices.id ) {

				this.inputs[ i ].source = vertices.input[ 'POSITION' ].source;

T
timk 已提交
2598
			}
2599

T
timk 已提交
2600
		}
2601 2602 2603

	};

2604
	Polygons.prototype.parse = function ( element ) {
2605

2606 2607
		this.material = element.getAttribute( 'material' );
		this.count = _attr_as_int( element, 'count', 0 );
2608 2609 2610 2611 2612 2613 2614

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

			var child = element.childNodes[ i ];

			switch ( child.nodeName ) {

T
timk 已提交
2615
				case 'input':
2616

2617
					this.inputs.push( ( new Input() ).parse( element.childNodes[ i ] ) );
T
timk 已提交
2618
					break;
2619

T
timk 已提交
2620
				case 'vcount':
2621

2622
					this.vcount = _ints( child.textContent );
T
timk 已提交
2623
					break;
2624

T
timk 已提交
2625
				case 'p':
2626

2627 2628 2629 2630 2631 2632
					this.p.push( _ints( child.textContent ) );
					break;

				case 'ph':

					console.warn( 'polygon holes not yet supported!' );
T
timk 已提交
2633
					break;
2634

T
timk 已提交
2635 2636
				default:
					break;
2637

T
timk 已提交
2638
			}
2639

T
timk 已提交
2640
		}
2641

T
timk 已提交
2642
		return this;
2643 2644 2645

	};

2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
	function Polylist () {

		Polygons.call( this );

		this.vcount = [];

	};

	Polylist.prototype = new Polygons();
	Polylist.prototype.constructor = Polylist;

	function Triangles () {

		Polygons.call( this );

		this.vcount = 3;

2663 2664
	};

2665 2666
	Triangles.prototype = new Polygons();
	Triangles.prototype.constructor = Triangles;
2667

T
timk 已提交
2668
	function Accessor() {
2669

T
timk 已提交
2670 2671 2672 2673
		this.source = "";
		this.count = 0;
		this.stride = 0;
		this.params = [];
2674 2675 2676

	};

2677
	Accessor.prototype.parse = function ( element ) {
2678

T
timk 已提交
2679
		this.params = [];
2680 2681 2682
		this.source = element.getAttribute( 'source' );
		this.count = _attr_as_int( element, 'count', 0 );
		this.stride = _attr_as_int( element, 'stride', 0 );
2683 2684 2685

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

2686
			var child = element.childNodes[ i ];
2687 2688 2689

			if ( child.nodeName == 'param' ) {

T
timk 已提交
2690
				var param = {};
2691 2692 2693
				param[ 'name' ] = child.getAttribute( 'name' );
				param[ 'type' ] = child.getAttribute( 'type' );
				this.params.push( param );
2694

T
timk 已提交
2695
			}
2696

T
timk 已提交
2697
		}
2698

T
timk 已提交
2699
		return this;
2700 2701 2702

	};

T
timk 已提交
2703
	function Vertices() {
2704

T
timk 已提交
2705
		this.input = {};
2706 2707 2708 2709 2710

	};

	Vertices.prototype.parse = function ( element ) {

T
timk 已提交
2711
		this.id = element.getAttribute('id');
2712 2713 2714 2715 2716

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

			if ( element.childNodes[i].nodeName == 'input' ) {

2717 2718
				var input = ( new Input() ).parse( element.childNodes[ i ] );
				this.input[ input.semantic ] = input;
2719

T
timk 已提交
2720
			}
2721

T
timk 已提交
2722
		}
2723

T
timk 已提交
2724
		return this;
2725 2726 2727 2728 2729

	};

	function Input () {

T
timk 已提交
2730 2731 2732 2733
		this.semantic = "";
		this.offset = 0;
		this.source = "";
		this.set = 0;
2734 2735 2736 2737 2738

	};

	Input.prototype.parse = function ( element ) {

T
timk 已提交
2739 2740 2741 2742
		this.semantic = element.getAttribute('semantic');
		this.source = element.getAttribute('source').replace(/^#/, '');
		this.set = _attr_as_int(element, 'set', -1);
		this.offset = _attr_as_int(element, 'offset', 0);
2743 2744 2745

		if ( this.semantic == 'TEXCOORD' && this.set < 0 ) {

T
timk 已提交
2746
			this.set = 0;
2747

T
timk 已提交
2748
		}
2749

T
timk 已提交
2750
		return this;
2751 2752 2753 2754 2755

	};

	function Source ( id ) {

T
timk 已提交
2756 2757
		this.id = id;
		this.type = null;
2758 2759 2760 2761 2762 2763 2764 2765 2766

	};

	Source.prototype.parse = function ( element ) {

		this.id = element.getAttribute( 'id' );

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

T
timk 已提交
2767
			var child = element.childNodes[i];
2768 2769 2770

			switch ( child.nodeName ) {

T
timk 已提交
2771
				case 'bool_array':
2772

2773
					this.data = _bools( child.textContent );
T
timk 已提交
2774 2775
					this.type = child.nodeName;
					break;
2776

T
timk 已提交
2777
				case 'float_array':
2778

2779
					this.data = _floats( child.textContent );
T
timk 已提交
2780 2781
					this.type = child.nodeName;
					break;
2782

T
timk 已提交
2783
				case 'int_array':
2784

2785
					this.data = _ints( child.textContent );
T
timk 已提交
2786 2787
					this.type = child.nodeName;
					break;
2788

T
timk 已提交
2789 2790
				case 'IDREF_array':
				case 'Name_array':
2791

2792
					this.data = _strings( child.textContent );
T
timk 已提交
2793 2794
					this.type = child.nodeName;
					break;
2795

T
timk 已提交
2796
				case 'technique_common':
2797 2798 2799

					for ( var j = 0; j < child.childNodes.length; j ++ ) {

2800
						if ( child.childNodes[ j ].nodeName == 'accessor' ) {
2801

2802
							this.accessor = ( new Accessor() ).parse( child.childNodes[ j ] );
T
timk 已提交
2803
							break;
2804

T
timk 已提交
2805 2806 2807
						}
					}
					break;
2808

T
timk 已提交
2809
				default:
M
Mr.doob 已提交
2810
					// console.log(child.nodeName);
T
timk 已提交
2811
					break;
2812

T
timk 已提交
2813
			}
2814

T
timk 已提交
2815
		}
2816

T
timk 已提交
2817
		return this;
2818 2819 2820 2821 2822

	};

	Source.prototype.read = function () {

T
timk 已提交
2823
		var result = [];
2824

T
timk 已提交
2825
		//for (var i = 0; i < this.accessor.params.length; i++) {
2826

2827
			var param = this.accessor.params[ 0 ];
2828

T
timk 已提交
2829
			//console.log(param.name + " " + param.type);
2830

2831
			switch ( param.type ) {
2832

T
timk 已提交
2833
				case 'IDREF':
2834
				case 'Name': case 'name':
T
timk 已提交
2835
				case 'float':
2836

T
timk 已提交
2837
					return this.data;
2838

T
timk 已提交
2839
				case 'float4x4':
2840 2841 2842

					for ( var j = 0; j < this.data.length; j += 16 ) {

2843
						var s = this.data.slice( j, j + 16 );
2844
						var m = getConvertedMat4( s );
2845
						result.push( m );
T
timk 已提交
2846
					}
2847

T
timk 已提交
2848
					break;
2849

T
timk 已提交
2850
				default:
2851

M
Mr.doob 已提交
2852
					console.log( 'ColladaLoader: Source: Read dont know how to read ' + param.type + '.' );
T
timk 已提交
2853
					break;
2854

T
timk 已提交
2855
			}
2856

T
timk 已提交
2857
		//}
2858

T
timk 已提交
2859
		return result;
2860 2861 2862 2863 2864

	};

	function Material () {

T
timk 已提交
2865 2866 2867
		this.id = "";
		this.name = "";
		this.instance_effect = null;
2868 2869 2870 2871 2872

	};

	Material.prototype.parse = function ( element ) {

2873 2874
		this.id = element.getAttribute( 'id' );
		this.name = element.getAttribute( 'name' );
2875 2876 2877

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

2878 2879 2880
			if ( element.childNodes[ i ].nodeName == 'instance_effect' ) {

				this.instance_effect = ( new InstanceEffect() ).parse( element.childNodes[ i ] );
T
timk 已提交
2881
				break;
2882

T
timk 已提交
2883
			}
2884

T
timk 已提交
2885
		}
2886

T
timk 已提交
2887
		return this;
2888 2889 2890 2891 2892

	};

	function ColorOrTexture () {

2893 2894
		this.color = new THREE.Color( 0 );
		this.color.setRGB( Math.random(), Math.random(), Math.random() );
T
timk 已提交
2895
		this.color.a = 1.0;
2896

T
timk 已提交
2897 2898
		this.texture = null;
		this.texcoord = null;
2899
		this.texOpts = null;
2900 2901 2902 2903 2904

	};

	ColorOrTexture.prototype.isColor = function () {

2905
		return ( this.texture == null );
2906 2907 2908 2909 2910

	};

	ColorOrTexture.prototype.isTexture = function () {

2911
		return ( this.texture != null );
2912 2913 2914 2915 2916 2917 2918

	};

	ColorOrTexture.prototype.parse = function ( element ) {

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

2919 2920
			var child = element.childNodes[ i ];
			if ( child.nodeType != 1 ) continue;
2921 2922 2923

			switch ( child.nodeName ) {

T
timk 已提交
2924
				case 'color':
2925

2926
					var rgba = _floats( child.textContent );
T
timk 已提交
2927
					this.color = new THREE.Color(0);
2928
					this.color.setRGB( rgba[0], rgba[1], rgba[2] );
T
timk 已提交
2929 2930
					this.color.a = rgba[3];
					break;
2931

T
timk 已提交
2932
				case 'texture':
2933

T
timk 已提交
2934 2935
					this.texture = child.getAttribute('texture');
					this.texcoord = child.getAttribute('texcoord');
2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946
					// Defaults from:
					// https://collada.org/mediawiki/index.php/Maya_texture_placement_MAYA_extension
					this.texOpts = {
						offsetU: 0,
						offsetV: 0,
						repeatU: 1,
						repeatV: 1,
						wrapU: 1,
						wrapV: 1,
					};
					this.parseTexture( child );
T
timk 已提交
2947
					break;
2948

T
timk 已提交
2949 2950
				default:
					break;
2951

T
timk 已提交
2952
			}
2953

T
timk 已提交
2954
		}
2955

T
timk 已提交
2956
		return this;
2957 2958 2959

	};

2960 2961
	ColorOrTexture.prototype.parseTexture = function ( element ) {

2962
		if ( ! element.childNodes ) return this;
2963

2964 2965
		// This should be supported by Maya, 3dsMax, and MotionBuilder

2966
		if ( element.childNodes[1] && element.childNodes[1].nodeName === 'extra' ) {
2967

2968
			element = element.childNodes[1];
2969

2970
			if ( element.childNodes[1] && element.childNodes[1].nodeName === 'technique' ) {
2971

2972
				element = element.childNodes[1];
2973 2974 2975 2976 2977

			}

		}

2978
		for ( var i = 0; i < element.childNodes.length; i ++ ) {
2979

2980
			var child = element.childNodes[ i ];
2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009

			switch ( child.nodeName ) {

				case 'offsetU':
				case 'offsetV':
				case 'repeatU':
				case 'repeatV':

					this.texOpts[ child.nodeName ] = parseFloat( child.textContent );
					break;

				case 'wrapU':
				case 'wrapV':

					this.texOpts[ child.nodeName ] = parseInt( child.textContent );
					break;

				default:
					this.texOpts[ child.nodeName ] = child.textContent;
					break;

			}

		}

		return this;

	};

3010 3011
	function Shader ( type, effect ) {

T
timk 已提交
3012
		this.type = type;
T
timk 已提交
3013
		this.effect = effect;
M
Mr.doob 已提交
3014
		this.material = null;
3015 3016 3017 3018 3019 3020 3021

	};

	Shader.prototype.parse = function ( element ) {

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

3022 3023
			var child = element.childNodes[ i ];
			if ( child.nodeType != 1 ) continue;
3024

3025
			switch ( child.nodeName ) {
3026

T
timk 已提交
3027 3028 3029 3030 3031
				case 'ambient':
				case 'emission':
				case 'diffuse':
				case 'specular':
				case 'transparent':
3032

3033
					this[ child.nodeName ] = ( new ColorOrTexture() ).parse( child );
T
timk 已提交
3034
					break;
3035

T
timk 已提交
3036 3037 3038
				case 'shininess':
				case 'reflectivity':
				case 'transparency':
3039

3040
					var f = evaluateXPath( child, './/dae:float' );
3041 3042 3043 3044

					if ( f.length > 0 )
						this[ child.nodeName ] = parseFloat( f[ 0 ].textContent );

T
timk 已提交
3045
					break;
3046

T
timk 已提交
3047 3048
				default:
					break;
3049

T
timk 已提交
3050
			}
3051

T
timk 已提交
3052
		}
3053

T
timk 已提交
3054
		this.create();
T
timk 已提交
3055
		return this;
3056 3057 3058

	};

T
timk 已提交
3059
	Shader.prototype.create = function() {
3060

T
timk 已提交
3061
		var props = {};
3062 3063 3064 3065 3066 3067
		var transparent = ( this['transparency'] !== undefined && this['transparency'] < 1.0 );

		for ( var prop in this ) {

			switch ( prop ) {

T
timk 已提交
3068 3069 3070 3071
				case 'ambient':
				case 'emission':
				case 'diffuse':
				case 'specular':
3072

T
timk 已提交
3073
					var cot = this[prop];
3074 3075 3076 3077 3078 3079 3080

					if ( cot instanceof ColorOrTexture ) {

						if ( cot.isTexture() ) {

							if ( this.effect.sampler && this.effect.surface ) {

3081
								if ( this.effect.sampler.source == this.effect.surface.sid ) {
3082

T
timk 已提交
3083
									var image = images[this.effect.surface.init_from];
3084 3085 3086

									if ( image ) {

3087
										var texture = THREE.ImageUtils.loadTexture(baseUrl + image.init_from);
3088 3089
										texture.wrapS = cot.texOpts.wrapU ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
										texture.wrapT = cot.texOpts.wrapV ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
3090 3091 3092 3093 3094
										texture.offset.x = cot.texOpts.offsetU;
										texture.offset.y = cot.texOpts.offsetV;
										texture.repeat.x = cot.texOpts.repeatU;
										texture.repeat.y = cot.texOpts.repeatV;
										props['map'] = texture;
3095

3096 3097 3098
										// Texture with baked lighting?
										if ( prop == 'emission' ) props[ 'emissive' ] = 0xffffff;

T
timk 已提交
3099
									}
3100

T
timk 已提交
3101
								}
3102

T
timk 已提交
3103
							}
3104

3105
						} else if ( prop == 'diffuse' || !transparent ) {
3106

3107 3108 3109 3110 3111 3112 3113 3114 3115
							if ( prop == 'emission' ) {

								props[ 'emissive' ] = cot.color.getHex();

							} else {

								props[ prop ] = cot.color.getHex();

							}
3116

T
timk 已提交
3117
						}
3118

T
timk 已提交
3119
					}
3120

T
timk 已提交
3121
					break;
3122

T
timk 已提交
3123 3124
				case 'shininess':
				case 'reflectivity':
3125

3126
					props[ prop ] = this[ prop ];
T
timk 已提交
3127
					break;
3128

T
timk 已提交
3129
				case 'transparency':
3130 3131 3132

					if ( transparent ) {

3133 3134
						props[ 'transparent' ] = true;
						props[ 'opacity' ] = this[ prop ];
T
timk 已提交
3135
						transparent = true;
3136

T
timk 已提交
3137
					}
3138

T
timk 已提交
3139
					break;
3140

T
timk 已提交
3141 3142
				default:
					break;
3143

T
timk 已提交
3144
			}
3145

T
timk 已提交
3146 3147
		}

3148 3149 3150 3151
		props[ 'shading' ] = preferredShading;

		switch ( this.type ) {

T
timk 已提交
3152
			case 'constant':
3153 3154 3155

				props.color = props.emission;
				this.material = new THREE.MeshBasicMaterial( props );
T
timk 已提交
3156
				break;
3157

T
timk 已提交
3158 3159
			case 'phong':
			case 'blinn':
3160

3161 3162 3163
				props.color = props.diffuse;
				this.material = new THREE.MeshPhongMaterial( props );
				break;
3164

3165 3166
			case 'lambert':
			default:
3167

3168 3169
				props.color = props.diffuse;
				this.material = new THREE.MeshLambertMaterial( props );
T
timk 已提交
3170
				break;
3171

T
timk 已提交
3172
		}
3173

T
timk 已提交
3174
		return this.material;
3175 3176 3177 3178 3179

	};

	function Surface ( effect ) {

T
timk 已提交
3180
		this.effect = effect;
M
Mr.doob 已提交
3181 3182
		this.init_from = null;
		this.format = null;
3183 3184 3185 3186 3187 3188 3189

	};

	Surface.prototype.parse = function ( element ) {

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

3190 3191
			var child = element.childNodes[ i ];
			if ( child.nodeType != 1 ) continue;
3192 3193 3194

			switch ( child.nodeName ) {

T
timk 已提交
3195
				case 'init_from':
3196

T
timk 已提交
3197 3198
					this.init_from = child.textContent;
					break;
3199

T
timk 已提交
3200
				case 'format':
3201

T
timk 已提交
3202 3203
					this.format = child.textContent;
					break;
3204

T
timk 已提交
3205
				default:
3206

3207
					console.log( "unhandled Surface prop: " + child.nodeName );
T
timk 已提交
3208
					break;
3209

T
timk 已提交
3210
			}
3211

T
timk 已提交
3212
		}
3213

T
timk 已提交
3214
		return this;
3215 3216 3217 3218 3219

	};

	function Sampler2D ( effect ) {

T
timk 已提交
3220
		this.effect = effect;
M
Mr.doob 已提交
3221 3222 3223 3224 3225 3226
		this.source = null;
		this.wrap_s = null;
		this.wrap_t = null;
		this.minfilter = null;
		this.magfilter = null;
		this.mipfilter = null;
3227 3228 3229 3230 3231 3232 3233 3234 3235 3236

	};

	Sampler2D.prototype.parse = function ( element ) {

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

			var child = element.childNodes[ i ];
			if ( child.nodeType != 1 ) continue;

3237
			switch ( child.nodeName ) {
3238

T
timk 已提交
3239
				case 'source':
3240

T
timk 已提交
3241 3242
					this.source = child.textContent;
					break;
3243

T
timk 已提交
3244
				case 'minfilter':
3245

T
timk 已提交
3246 3247
					this.minfilter = child.textContent;
					break;
3248

T
timk 已提交
3249
				case 'magfilter':
3250

T
timk 已提交
3251 3252
					this.magfilter = child.textContent;
					break;
3253

T
timk 已提交
3254
				case 'mipfilter':
3255

T
timk 已提交
3256 3257
					this.mipfilter = child.textContent;
					break;
3258

T
timk 已提交
3259
				case 'wrap_s':
3260

T
timk 已提交
3261 3262
					this.wrap_s = child.textContent;
					break;
3263

T
timk 已提交
3264
				case 'wrap_t':
3265

T
timk 已提交
3266 3267
					this.wrap_t = child.textContent;
					break;
3268

T
timk 已提交
3269
				default:
3270

3271
					console.log( "unhandled Sampler2D prop: " + child.nodeName );
T
timk 已提交
3272
					break;
3273

T
timk 已提交
3274
			}
3275

T
timk 已提交
3276
		}
3277

T
timk 已提交
3278
		return this;
3279 3280 3281 3282 3283

	};

	function Effect () {

T
timk 已提交
3284 3285 3286
		this.id = "";
		this.name = "";
		this.shader = null;
M
Mr.doob 已提交
3287 3288
		this.surface = null;
		this.sampler = null;
3289 3290 3291 3292 3293 3294 3295

	};

	Effect.prototype.create = function () {

		if ( this.shader == null ) {

T
timk 已提交
3296
			return null;
3297

T
timk 已提交
3298
		}
3299 3300 3301 3302 3303 3304 3305

	};

	Effect.prototype.parse = function ( element ) {

		this.id = element.getAttribute( 'id' );
		this.name = element.getAttribute( 'name' );
T
timk 已提交
3306
		this.shader = null;
3307 3308 3309 3310 3311 3312 3313 3314

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

			var child = element.childNodes[ i ];
			if ( child.nodeType != 1 ) continue;

			switch ( child.nodeName ) {

T
timk 已提交
3315
				case 'profile_COMMON':
3316 3317

					this.parseTechnique( this.parseProfileCOMMON( child ) );
T
timk 已提交
3318
					break;
3319

T
timk 已提交
3320 3321
				default:
					break;
3322

T
timk 已提交
3323
			}
3324

T
timk 已提交
3325
		}
3326

T
timk 已提交
3327
		return this;
3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341

	};

	Effect.prototype.parseNewparam = function ( element ) {

		var sid = element.getAttribute( 'sid' );

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

			var child = element.childNodes[ i ];
			if ( child.nodeType != 1 ) continue;

			switch ( child.nodeName ) {

T
timk 已提交
3342
				case 'surface':
3343 3344

					this.surface = ( new Surface( this ) ).parse( child );
T
timk 已提交
3345 3346
					this.surface.sid = sid;
					break;
3347

T
timk 已提交
3348
				case 'sampler2D':
3349 3350

					this.sampler = ( new Sampler2D( this ) ).parse( child );
T
timk 已提交
3351 3352
					this.sampler.sid = sid;
					break;
3353

T
timk 已提交
3354
				case 'extra':
3355

T
timk 已提交
3356
					break;
3357

T
timk 已提交
3358
				default:
3359 3360

					console.log( child.nodeName );
T
timk 已提交
3361
					break;
3362

T
timk 已提交
3363
			}
3364

T
timk 已提交
3365
		}
3366 3367 3368 3369 3370

	};

	Effect.prototype.parseProfileCOMMON = function ( element ) {

T
timk 已提交
3371
		var technique;
3372 3373 3374 3375 3376 3377 3378 3379 3380

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

			var child = element.childNodes[ i ];

			if ( child.nodeType != 1 ) continue;

			switch ( child.nodeName ) {

T
timk 已提交
3381
				case 'profile_COMMON':
3382 3383

					this.parseProfileCOMMON( child );
T
timk 已提交
3384
					break;
3385

T
timk 已提交
3386
				case 'technique':
3387

T
timk 已提交
3388
					technique = child;
T
timk 已提交
3389
					break;
3390

T
timk 已提交
3391
				case 'newparam':
3392 3393

					this.parseNewparam( child );
T
timk 已提交
3394
					break;
3395

3396 3397 3398 3399 3400 3401
				case 'image':

					var _image = ( new _Image() ).parse( child );
					images[ _image.id ] = _image;
					break;

T
timk 已提交
3402 3403
				case 'extra':
					break;
3404

T
timk 已提交
3405
				default:
3406 3407

					console.log( child.nodeName );
T
timk 已提交
3408
					break;
3409

T
timk 已提交
3410
			}
3411

T
timk 已提交
3412
		}
3413

T
timk 已提交
3414
		return technique;
3415 3416 3417 3418 3419 3420 3421

	};

	Effect.prototype.parseTechnique= function ( element ) {

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

T
timk 已提交
3422
			var child = element.childNodes[i];
3423
			if ( child.nodeType != 1 ) continue;
3424 3425 3426

			switch ( child.nodeName ) {

3427
				case 'constant':
T
timk 已提交
3428 3429 3430
				case 'lambert':
				case 'blinn':
				case 'phong':
3431 3432

					this.shader = ( new Shader( child.nodeName, this ) ).parse( child );
T
timk 已提交
3433
					break;
3434

T
timk 已提交
3435 3436
				default:
					break;
3437

T
timk 已提交
3438
			}
3439

T
timk 已提交
3440
		}
3441 3442 3443 3444 3445

	};

	function InstanceEffect () {

T
timk 已提交
3446
		this.url = "";
3447 3448 3449 3450 3451 3452

	};

	InstanceEffect.prototype.parse = function ( element ) {

		this.url = element.getAttribute( 'url' ).replace( /^#/, '' );
T
timk 已提交
3453
		return this;
3454 3455

	};
T
timk 已提交
3456 3457

	function Animation() {
3458

T
timk 已提交
3459 3460 3461 3462 3463
		this.id = "";
		this.name = "";
		this.source = {};
		this.sampler = [];
		this.channel = [];
3464 3465 3466 3467 3468 3469 3470

	};

	Animation.prototype.parse = function ( element ) {

		this.id = element.getAttribute( 'id' );
		this.name = element.getAttribute( 'name' );
T
timk 已提交
3471
		this.source = {};
3472 3473 3474 3475 3476 3477 3478 3479 3480

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

			var child = element.childNodes[ i ];

			if ( child.nodeType != 1 ) continue;

			switch ( child.nodeName ) {

3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499
				case 'animation':

					var anim = ( new Animation() ).parse( child );

					for ( var src in anim.source ) {

						this.source[ src ] = anim.source[ src ];

					}

					for ( var j = 0; j < anim.channel.length; j ++ ) {

						this.channel.push( anim.channel[ j ] );
						this.sampler.push( anim.sampler[ j ] );

					}

					break;

T
timk 已提交
3500
				case 'source':
3501 3502 3503

					var src = ( new Source() ).parse( child );
					this.source[ src.id ] = src;
T
timk 已提交
3504
					break;
3505

T
timk 已提交
3506
				case 'sampler':
3507 3508

					this.sampler.push( ( new Sampler( this ) ).parse( child ) );
T
timk 已提交
3509
					break;
3510

T
timk 已提交
3511
				case 'channel':
3512 3513

					this.channel.push( ( new Channel( this ) ).parse( child ) );
T
timk 已提交
3514
					break;
3515

T
timk 已提交
3516 3517
				default:
					break;
3518

T
timk 已提交
3519
			}
3520

T
timk 已提交
3521
		}
3522

T
timk 已提交
3523
		return this;
T
timk 已提交
3524

3525 3526 3527 3528
	};

	function Channel( animation ) {

T
timk 已提交
3529 3530 3531
		this.animation = animation;
		this.source = "";
		this.target = "";
3532
		this.fullSid = null;
M
Mr.doob 已提交
3533 3534 3535 3536 3537
		this.sid = null;
		this.dotSyntax = null;
		this.arrSyntax = null;
		this.arrIndices = null;
		this.member = null;
3538 3539 3540 3541 3542 3543 3544 3545 3546 3547

	};

	Channel.prototype.parse = function ( element ) {

		this.source = element.getAttribute( 'source' ).replace( /^#/, '' );
		this.target = element.getAttribute( 'target' );

		var parts = this.target.split( '/' );

T
timk 已提交
3548 3549
		var id = parts.shift();
		var sid = parts.shift();
3550 3551 3552 3553 3554 3555

		var dotSyntax = ( sid.indexOf(".") >= 0 );
		var arrSyntax = ( sid.indexOf("(") >= 0 );

		if ( dotSyntax ) {

T
timk 已提交
3556
			parts = sid.split(".");
3557 3558
			this.sid = parts.shift();
			this.member = parts.shift();
3559 3560 3561

		} else if ( arrSyntax ) {

3562 3563
			var arrIndices = sid.split("(");
			this.sid = arrIndices.shift();
3564 3565 3566 3567 3568

			for (var j = 0; j < arrIndices.length; j ++ ) {

				arrIndices[j] = parseInt( arrIndices[j].replace(/\)/, '') );

T
timk 已提交
3569
			}
3570

3571 3572 3573 3574 3575 3576
			this.arrIndices = arrIndices;

		} else {

			this.sid = sid;

T
timk 已提交
3577
		}
3578

3579
		this.fullSid = sid;
T
timk 已提交
3580 3581
		this.dotSyntax = dotSyntax;
		this.arrSyntax = arrSyntax;
3582

T
timk 已提交
3583
		return this;
3584 3585 3586 3587 3588

	};

	function Sampler ( animation ) {

T
timk 已提交
3589 3590 3591
		this.id = "";
		this.animation = animation;
		this.inputs = [];
M
Mr.doob 已提交
3592 3593
		this.input = null;
		this.output = null;
3594
		this.strideOut = null;
M
Mr.doob 已提交
3595 3596 3597
		this.interpolation = null;
		this.startTime = null;
		this.endTime = null;
T
timk 已提交
3598
		this.duration = 0;
3599 3600 3601 3602 3603 3604

	};

	Sampler.prototype.parse = function ( element ) {

		this.id = element.getAttribute( 'id' );
T
timk 已提交
3605
		this.inputs = [];
3606 3607 3608 3609 3610 3611 3612 3613

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

			var child = element.childNodes[ i ];
			if ( child.nodeType != 1 ) continue;

			switch ( child.nodeName ) {

T
timk 已提交
3614
				case 'input':
3615 3616

					this.inputs.push( (new Input()).parse( child ) );
T
timk 已提交
3617
					break;
3618

T
timk 已提交
3619 3620
				default:
					break;
3621

T
timk 已提交
3622
			}
3623

T
timk 已提交
3624
		}
3625

T
timk 已提交
3626
		return this;
3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638

	};

	Sampler.prototype.create = function () {

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

			var input = this.inputs[ i ];
			var source = this.animation.source[ input.source ];

			switch ( input.semantic ) {

T
timk 已提交
3639
				case 'INPUT':
3640

T
timk 已提交
3641 3642
					this.input = source.read();
					break;
3643

T
timk 已提交
3644
				case 'OUTPUT':
3645

T
timk 已提交
3646
					this.output = source.read();
3647
					this.strideOut = source.accessor.stride;
T
timk 已提交
3648
					break;
3649

T
timk 已提交
3650
				case 'INTERPOLATION':
3651

T
timk 已提交
3652 3653
					this.interpolation = source.read();
					break;
3654

T
timk 已提交
3655
				case 'IN_TANGENT':
3656

T
timk 已提交
3657
					break;
3658

T
timk 已提交
3659
				case 'OUT_TANGENT':
3660

T
timk 已提交
3661
					break;
3662

T
timk 已提交
3663
				default:
3664

T
timk 已提交
3665 3666
					console.log(input.semantic);
					break;
3667

T
timk 已提交
3668
			}
3669

T
timk 已提交
3670
		}
3671

T
timk 已提交
3672 3673 3674
		this.startTime = 0;
		this.endTime = 0;
		this.duration = 0;
3675 3676 3677

		if ( this.input.length ) {

T
timk 已提交
3678 3679
			this.startTime = 100000000;
			this.endTime = -100000000;
3680 3681 3682 3683 3684 3685

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

				this.startTime = Math.min( this.startTime, this.input[ i ] );
				this.endTime = Math.max( this.endTime, this.input[ i ] );

T
timk 已提交
3686
			}
3687

T
timk 已提交
3688
			this.duration = this.endTime - this.startTime;
3689

T
timk 已提交
3690
		}
3691 3692 3693

	};

3694 3695 3696 3697
	Sampler.prototype.getData = function ( type, ndx ) {

		var data;

3698 3699 3700 3701 3702
		if ( type === 'matrix' && this.strideOut === 16 ) {

			data = this.output[ ndx ];

		} else if ( this.strideOut > 1 ) {
3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729

			data = [];
			ndx *= this.strideOut;

			for ( var i = 0; i < this.strideOut; ++i ) {

				data[ i ] = this.output[ ndx + i ];

			}

			if ( this.strideOut === 3 ) {

				switch ( type ) {

					case 'rotate':
					case 'translate':

						fixCoords( data, -1 );
						break;

					case 'scale':

						fixCoords( data, 1 );
						break;

				}

3730 3731 3732 3733
			} else if ( this.strideOut === 4 && type === 'matrix' ) {

				fixCoords( data, -1 );

3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820
			}

		} else {

			data = this.output[ ndx ];

		}

		return data;

	};

	function Key ( time ) {

		this.targets = [];
		this.time = time;

	};

	Key.prototype.addTarget = function ( fullSid, transform, member, data ) {

		this.targets.push( {
			sid: fullSid,
			member: member,
			transform: transform,
			data: data
		} );

	};

	Key.prototype.apply = function ( opt_sid ) {

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

			var target = this.targets[ i ];

			if ( !opt_sid || target.sid === opt_sid ) {

				target.transform.update( target.data, target.member );

			}

		}

	};

	Key.prototype.getTarget = function ( fullSid ) {

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

			if ( this.targets[ i ].sid === fullSid ) {

				return this.targets[ i ];

			}

		}

		return null;

	};

	Key.prototype.hasTarget = function ( fullSid ) {

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

			if ( this.targets[ i ].sid === fullSid ) {

				return true;

			}

		}

		return false;

	};

	// TODO: Currently only doing linear interpolation. Should support full COLLADA spec.
	Key.prototype.interpolate = function ( nextKey, time ) {

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

			var target = this.targets[ i ],
				nextTarget = nextKey.getTarget( target.sid ),
				data;

3821
			if ( target.transform.type !== 'matrix' && nextTarget ) {
3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861

				var scale = ( time - this.time ) / ( nextKey.time - this.time ),
					nextData = nextTarget.data,
					prevData = target.data;

				// check scale error

				if ( scale < 0 || scale > 1 ) {

					console.log( "Key.interpolate: Warning! Scale out of bounds:" + scale );
					scale = scale < 0 ? 0 : 1;

				}

				if ( prevData.length ) {

					data = [];

					for ( var j = 0; j < prevData.length; ++j ) {

						data[ j ] = prevData[ j ] + ( nextData[ j ] - prevData[ j ] ) * scale;

					}

				} else {

					data = prevData + ( nextData - prevData ) * scale;

				}

			} else {

				data = target.data;

			}

			target.transform.update( data, target.member );

		}

M
Mr.doob 已提交
3862 3863 3864
	};

	function Camera() {
3865 3866 3867

		this.id = "";
		this.name = "";
3868
		this.technique = "";
3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909

	};

	Camera.prototype.parse = function ( element ) {

		this.id = element.getAttribute( 'id' );
		this.name = element.getAttribute( 'name' );

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

			var child = element.childNodes[ i ];
			if ( child.nodeType != 1 ) continue;

			switch ( child.nodeName ) {

				case 'optics':

					this.parseOptics( child );
					break;

				default:
					break;

			}

		}

		return this;

	};

	Camera.prototype.parseOptics = function ( element ) {

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

			if ( element.childNodes[ i ].nodeName == 'technique_common' ) {

				var technique = element.childNodes[ i ];

				for ( var j = 0; j < technique.childNodes.length; j ++ ) {

3910 3911 3912
					this.technique = technique.childNodes[ j ].nodeName;

					if ( this.technique == 'perspective' ) {
3913 3914 3915 3916 3917 3918

						var perspective = technique.childNodes[ j ];

						for ( var k = 0; k < perspective.childNodes.length; k ++ ) {

							var param = perspective.childNodes[ k ];
M
Mr.doob 已提交
3919

3920 3921
							switch ( param.nodeName ) {

3922 3923 3924
								case 'yfov':
									this.yfov = param.textContent;
									break;
3925
								case 'xfov':
3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956
									this.xfov = param.textContent;
									break;
								case 'znear':
									this.znear = param.textContent;
									break;
								case 'zfar':
									this.zfar = param.textContent;
									break;
								case 'aspect_ratio':
									this.aspect_ratio = param.textContent;
									break;

							}

						}

					} else if ( this.technique == 'orthographic' ) {

						var orthographic = technique.childNodes[ j ];

						for ( var k = 0; k < orthographic.childNodes.length; k ++ ) {

							var param = orthographic.childNodes[ k ];

							switch ( param.nodeName ) {

								case 'xmag':
									this.xmag = param.textContent;
									break;
								case 'ymag':
									this.ymag = param.textContent;
3957 3958
									break;
								case 'znear':
3959
									this.znear = param.textContent;
3960 3961
									break;
								case 'zfar':
3962
									this.zfar = param.textContent;
3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982
									break;
								case 'aspect_ratio':
									this.aspect_ratio = param.textContent;
									break;

							}

						}

					}

				}
				
			}

		}
		
		return this;

	};
M
Mr.doob 已提交
3983

3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995
	function InstanceCamera() {

		this.url = "";

	};

	InstanceCamera.prototype.parse = function ( element ) {

		this.url = element.getAttribute('url').replace(/^#/, '');

		return this;

3996 3997
	};

3998
	function _source( element ) {
3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012

		var id = element.getAttribute( 'id' );

		if ( sources[ id ] != undefined ) {

			return sources[ id ];

		}

		sources[ id ] = ( new Source(id )).parse( element );
		return sources[ id ];

	};

4013
	function _nsResolver( nsPrefix ) {
4014 4015 4016

		if ( nsPrefix == "dae" ) {

T
timk 已提交
4017
			return "http://www.collada.org/2005/11/COLLADASchema";
4018

T
timk 已提交
4019
		}
4020

T
timk 已提交
4021
		return null;
4022 4023 4024

	};

4025
	function _bools( str ) {
4026 4027

		var raw = _strings( str );
T
timk 已提交
4028
		var data = [];
4029

4030
		for ( var i = 0, l = raw.length; i < l; i ++ ) {
4031 4032

			data.push( (raw[i] == 'true' || raw[i] == '1') ? true : false );
4033

T
timk 已提交
4034
		}
4035

T
timk 已提交
4036
		return data;
4037 4038 4039

	};

4040
	function _floats( str ) {
4041

T
timk 已提交
4042 4043
		var raw = _strings(str);
		var data = [];
4044

4045
		for ( var i = 0, l = raw.length; i < l; i ++ ) {
4046 4047 4048

			data.push( parseFloat( raw[ i ] ) );

T
timk 已提交
4049
		}
4050

T
timk 已提交
4051
		return data;
4052 4053

	};
T
timk 已提交
4054

4055
	function _ints( str ) {
4056 4057

		var raw = _strings( str );
T
timk 已提交
4058
		var data = [];
4059

4060
		for ( var i = 0, l = raw.length; i < l; i ++ ) {
4061 4062 4063

			data.push( parseInt( raw[ i ], 10 ) );

T
timk 已提交
4064
		}
4065

T
timk 已提交
4066
		return data;
4067 4068 4069

	};

4070
	function _strings( str ) {
4071

4072
		return ( str.length > 0 ) ? _trimString( str ).split( /\s+/ ) : [];
4073 4074

	};
T
timk 已提交
4075

4076
	function _trimString( str ) {
4077 4078 4079 4080

		return str.replace( /^\s+/, "" ).replace( /\s+$/, "" );

	};
T
timk 已提交
4081

4082
	function _attr_as_float( element, name, defaultValue ) {
4083 4084 4085 4086 4087

		if ( element.hasAttribute( name ) ) {

			return parseFloat( element.getAttribute( name ) );

T
timk 已提交
4088
		} else {
4089

T
timk 已提交
4090
			return defaultValue;
4091

T
timk 已提交
4092
		}
4093 4094

	};
T
timk 已提交
4095

4096
	function _attr_as_int( element, name, defaultValue ) {
4097 4098 4099 4100 4101

		if ( element.hasAttribute( name ) ) {

			return parseInt( element.getAttribute( name ), 10) ;

T
timk 已提交
4102
		} else {
4103

T
timk 已提交
4104
			return defaultValue;
4105

T
timk 已提交
4106
		}
4107 4108

	};
T
timk 已提交
4109

4110
	function _attr_as_string( element, name, defaultValue ) {
4111 4112 4113 4114 4115

		if ( element.hasAttribute( name ) ) {

			return element.getAttribute( name );

T
timk 已提交
4116
		} else {
4117

T
timk 已提交
4118
			return defaultValue;
4119

T
timk 已提交
4120
		}
4121 4122 4123

	};

4124
	function _format_float( f, num ) {
4125 4126 4127

		if ( f === undefined ) {

T
timk 已提交
4128
			var s = '0.';
4129 4130 4131

			while ( s.length < num + 2 ) {

T
timk 已提交
4132
				s += '0';
4133

T
timk 已提交
4134
			}
4135

T
timk 已提交
4136
			return s;
4137

T
timk 已提交
4138
		}
4139

T
timk 已提交
4140
		num = num || 2;
4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154

		var parts = f.toString().split( '.' );
		parts[ 1 ] = parts.length > 1 ? parts[ 1 ].substr( 0, num ) : "0";

		while( parts[ 1 ].length < num ) {

			parts[ 1 ] += '0';

		}

		return parts.join( '.' );

	};

4155
	function evaluateXPath( node, query ) {
4156

4157
		var instances = COLLADA.evaluate( query, node, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
4158

T
timk 已提交
4159 4160
		var inst = instances.iterateNext();
		var result = [];
4161 4162 4163 4164

		while ( inst ) {

			result.push( inst );
T
timk 已提交
4165
			inst = instances.iterateNext();
4166 4167 4168

		}

T
timk 已提交
4169
		return result;
4170 4171 4172

	};

4173 4174
	// Up axis conversion

4175
	function setUpConversion() {
4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205

		if ( !options.convertUpAxis || colladaUp === options.upAxis ) {

			upConversion = null;

		} else {

			switch ( colladaUp ) {

				case 'X':

					upConversion = options.upAxis === 'Y' ? 'XtoY' : 'XtoZ';
					break;

				case 'Y':

					upConversion = options.upAxis === 'X' ? 'YtoX' : 'YtoZ';
					break;

				case 'Z':

					upConversion = options.upAxis === 'X' ? 'ZtoX' : 'ZtoY';
					break;

			}

		}

	};

4206
	function fixCoords( data, sign ) {
4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263

		if ( !options.convertUpAxis || colladaUp === options.upAxis ) {

			return;

		}

		switch ( upConversion ) {

			case 'XtoY':

				var tmp = data[ 0 ];
				data[ 0 ] = sign * data[ 1 ];
				data[ 1 ] = tmp;
				break;

			case 'XtoZ':

				var tmp = data[ 2 ];
				data[ 2 ] = data[ 1 ];
				data[ 1 ] = data[ 0 ];
				data[ 0 ] = tmp;
				break;

			case 'YtoX':

				var tmp = data[ 0 ];
				data[ 0 ] = data[ 1 ];
				data[ 1 ] = sign * tmp;
				break;

			case 'YtoZ':

				var tmp = data[ 1 ];
				data[ 1 ] = sign * data[ 2 ];
				data[ 2 ] = tmp;
				break;

			case 'ZtoX':

				var tmp = data[ 0 ];
				data[ 0 ] = data[ 1 ];
				data[ 1 ] = data[ 2 ];
				data[ 2 ] = tmp;
				break;

			case 'ZtoY':

				var tmp = data[ 1 ];
				data[ 1 ] = data[ 2 ];
				data[ 2 ] = sign * tmp;
				break;

		}

	};

4264
	function getConvertedVec3( data, offset ) {
4265 4266 4267 4268 4269 4270 4271

		var arr = [ data[ offset ], data[ offset + 1 ], data[ offset + 2 ] ];
		fixCoords( arr, -1 );
		return new THREE.Vector3( arr[ 0 ], arr[ 1 ], arr[ 2 ] );

	};

4272
	function getConvertedMat4( data ) {
4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328

		if ( options.convertUpAxis ) {

			// First fix rotation and scale

			// Columns first
			var arr = [ data[ 0 ], data[ 4 ], data[ 8 ] ];
			fixCoords( arr, -1 );
			data[ 0 ] = arr[ 0 ];
			data[ 4 ] = arr[ 1 ];
			data[ 8 ] = arr[ 2 ];
			arr = [ data[ 1 ], data[ 5 ], data[ 9 ] ];
			fixCoords( arr, -1 );
			data[ 1 ] = arr[ 0 ];
			data[ 5 ] = arr[ 1 ];
			data[ 9 ] = arr[ 2 ];
			arr = [ data[ 2 ], data[ 6 ], data[ 10 ] ];
			fixCoords( arr, -1 );
			data[ 2 ] = arr[ 0 ];
			data[ 6 ] = arr[ 1 ];
			data[ 10 ] = arr[ 2 ];
			// Rows second
			arr = [ data[ 0 ], data[ 1 ], data[ 2 ] ];
			fixCoords( arr, -1 );
			data[ 0 ] = arr[ 0 ];
			data[ 1 ] = arr[ 1 ];
			data[ 2 ] = arr[ 2 ];
			arr = [ data[ 4 ], data[ 5 ], data[ 6 ] ];
			fixCoords( arr, -1 );
			data[ 4 ] = arr[ 0 ];
			data[ 5 ] = arr[ 1 ];
			data[ 6 ] = arr[ 2 ];
			arr = [ data[ 8 ], data[ 9 ], data[ 10 ] ];
			fixCoords( arr, -1 );
			data[ 8 ] = arr[ 0 ];
			data[ 9 ] = arr[ 1 ];
			data[ 10 ] = arr[ 2 ];

			// Now fix translation
			arr = [ data[ 3 ], data[ 7 ], data[ 11 ] ];
			fixCoords( arr, -1 );
			data[ 3 ] = arr[ 0 ];
			data[ 7 ] = arr[ 1 ];
			data[ 11 ] = arr[ 2 ];

		}

		return new THREE.Matrix4(
			data[0], data[1], data[2], data[3],
			data[4], data[5], data[6], data[7],
			data[8], data[9], data[10], data[11],
			data[12], data[13], data[14], data[15]
			);

	};

4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344
	function getConvertedIndex( index ) {

		if ( index > -1 && index < 3 ) {

			var members = ['X', 'Y', 'Z'],
				indices = { X: 0, Y: 1, Z: 2 };

			index = getConvertedMember( members[ index ] );
			index = indices[ index ];

		}

		return index;
		
	};

4345
	function getConvertedMember( member ) {
4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420

		if ( options.convertUpAxis ) {

			switch ( member ) {

				case 'X':

					switch ( upConversion ) {

						case 'XtoY':
						case 'XtoZ':
						case 'YtoX':

							member = 'Y';
							break;

						case 'ZtoX':

							member = 'Z';
							break;

					}

					break;

				case 'Y':

					switch ( upConversion ) {

						case 'XtoY':
						case 'YtoX':
						case 'ZtoX':

							member = 'X';
							break;

						case 'XtoZ':
						case 'YtoZ':
						case 'ZtoY':

							member = 'Z';
							break;

					}

					break;

				case 'Z':

					switch ( upConversion ) {

						case 'XtoZ':

							member = 'X';
							break;

						case 'YtoZ':
						case 'ZtoX':
						case 'ZtoY':

							member = 'Y';
							break;

					}

					break;

			}

		}

		return member;

	};

T
timk 已提交
4421
	return {
4422

T
timk 已提交
4423
		load: load,
4424
		parse: parse,
T
timk 已提交
4425
		setPreferredShading: setPreferredShading,
T
timk 已提交
4426
		applySkin: applySkin,
4427 4428
		geometries : geometries,
		options: options
4429

T
timk 已提交
4430
	};
4431

M
Mr.doob 已提交
4432
};