AssimpLoader.js 50.6 KB
Newer Older
M
r105  
Mr.doob 已提交
1 2 3 4 5 6
import {
	Bone,
	BufferAttribute,
	BufferGeometry,
	Color,
	FileLoader,
M
r109  
Mr.doob 已提交
7
	Loader,
M
r105  
Mr.doob 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
	LoaderUtils,
	Matrix4,
	Mesh,
	MeshLambertMaterial,
	MeshPhongMaterial,
	Object3D,
	Quaternion,
	Skeleton,
	SkinnedMesh,
	TextureLoader,
	Vector3
} from "../../../build/three.module.js";

var AssimpLoader = function ( manager ) {

M
r109  
Mr.doob 已提交
23
	Loader.call( this, manager );
M
r105  
Mr.doob 已提交
24 25 26

};

M
r109  
Mr.doob 已提交
27
AssimpLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
M
r105  
Mr.doob 已提交
28 29 30 31 32 33 34

	constructor: AssimpLoader,

	load: function ( url, onLoad, onProgress, onError ) {

		var scope = this;

M
r109  
Mr.doob 已提交
35
		var path = ( scope.path === '' ) ? LoaderUtils.extractUrlBase( url ) : scope.path;
M
r105  
Mr.doob 已提交
36

M
r119  
Mr.doob 已提交
37
		var loader = new FileLoader( scope.manager );
M
r105  
Mr.doob 已提交
38 39
		loader.setPath( scope.path );
		loader.setResponseType( 'arraybuffer' );
M
r119  
Mr.doob 已提交
40
		loader.setRequestHeader( scope.requestHeader );
M
r105  
Mr.doob 已提交
41 42 43

		loader.load( url, function ( buffer ) {

M
r117  
Mr.doob 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
			try {

				onLoad( scope.parse( buffer, path ) );

			} catch ( e ) {

				if ( onError ) {

					onError( e );

				} else {

					console.error( e );

				}

				scope.manager.itemError( url );

			}
M
r105  
Mr.doob 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

		}, onProgress, onError );

	},

	parse: function ( buffer, path ) {

		var textureLoader = new TextureLoader( this.manager );
		textureLoader.setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );

		var Virtulous = {};

		Virtulous.KeyFrame = function ( time, matrix ) {

			this.time = time;
			this.matrix = matrix.clone();
			this.position = new Vector3();
			this.quaternion = new Quaternion();
			this.scale = new Vector3( 1, 1, 1 );
			this.matrix.decompose( this.position, this.quaternion, this.scale );
			this.clone = function () {

				var n = new Virtulous.KeyFrame( this.time, this.matrix );
				return n;

			};
M
r116  
Mr.doob 已提交
89

M
r105  
Mr.doob 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
			this.lerp = function ( nextKey, time ) {

				time -= this.time;
				var dist = ( nextKey.time - this.time );
				var l = time / dist;
				var l2 = 1 - l;
				var keypos = this.position;
				var keyrot = this.quaternion;
				//      var keyscl =  key.parentspaceScl || key.scl;
				var key2pos = nextKey.position;
				var key2rot = nextKey.quaternion;
				//  var key2scl =  key2.parentspaceScl || key2.scl;
				Virtulous.KeyFrame.tempAniPos.x = keypos.x * l2 + key2pos.x * l;
				Virtulous.KeyFrame.tempAniPos.y = keypos.y * l2 + key2pos.y * l;
				Virtulous.KeyFrame.tempAniPos.z = keypos.z * l2 + key2pos.z * l;
				//     tempAniScale.x = keyscl[0] * l2 + key2scl[0] * l;
				//     tempAniScale.y = keyscl[1] * l2 + key2scl[1] * l;
				//     tempAniScale.z = keyscl[2] * l2 + key2scl[2] * l;
				Virtulous.KeyFrame.tempAniQuat.set( keyrot.x, keyrot.y, keyrot.z, keyrot.w );
				Virtulous.KeyFrame.tempAniQuat.slerp( key2rot, l );
				return Virtulous.KeyFrame.tempAniMatrix.compose( Virtulous.KeyFrame.tempAniPos, Virtulous.KeyFrame.tempAniQuat, Virtulous.KeyFrame.tempAniScale );

			};

		};

		Virtulous.KeyFrame.tempAniPos = new Vector3();
		Virtulous.KeyFrame.tempAniQuat = new Quaternion();
		Virtulous.KeyFrame.tempAniScale = new Vector3( 1, 1, 1 );
		Virtulous.KeyFrame.tempAniMatrix = new Matrix4();
		Virtulous.KeyFrameTrack = function () {

			this.keys = [];
			this.target = null;
			this.time = 0;
			this.length = 0;
			this._accelTable = {};
			this.fps = 20;
			this.addKey = function ( key ) {

				this.keys.push( key );

			};
M
r116  
Mr.doob 已提交
133

M
r105  
Mr.doob 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
			this.init = function () {

				this.sortKeys();

				if ( this.keys.length > 0 )
					this.length = this.keys[ this.keys.length - 1 ].time;
				else
					this.length = 0;

				if ( ! this.fps ) return;

				for ( var j = 0; j < this.length * this.fps; j ++ ) {

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

						if ( this.keys[ i ].time == j ) {

							this._accelTable[ j ] = i;
							break;

						} else if ( this.keys[ i ].time < j / this.fps && this.keys[ i + 1 ] && this.keys[ i + 1 ].time >= j / this.fps ) {

							this._accelTable[ j ] = i;
							break;

						}

					}

				}

			};

			this.parseFromThree = function ( data ) {

				var fps = data.fps;
				this.target = data.node;
				var track = data.hierarchy[ 0 ].keys;
				for ( var i = 0; i < track.length; i ++ ) {

					this.addKey( new Virtulous.KeyFrame( i / fps || track[ i ].time, track[ i ].targets[ 0 ].data ) );

				}
M
r116  
Mr.doob 已提交
177

M
r105  
Mr.doob 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
				this.init();

			};

			this.parseFromCollada = function ( data ) {

				var track = data.keys;
				var fps = this.fps;

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

					this.addKey( new Virtulous.KeyFrame( i / fps || track[ i ].time, track[ i ].matrix ) );

				}

				this.init();

			};

			this.sortKeys = function () {

				this.keys.sort( this.keySortFunc );

			};

			this.keySortFunc = function ( a, b ) {

				return a.time - b.time;

			};

			this.clone = function () {

				var t = new Virtulous.KeyFrameTrack();
				t.target = this.target;
				t.time = this.time;
				t.length = this.length;

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

					t.addKey( this.keys[ i ].clone() );

				}

				t.init();
				return t;

			};

			this.reTarget = function ( root, compareitor ) {

				if ( ! compareitor ) compareitor = Virtulous.TrackTargetNodeNameCompare;
				this.target = compareitor( root, this.target );

			};

			this.keySearchAccel = function ( time ) {

				time *= this.fps;
				time = Math.floor( time );
				return this._accelTable[ time ] || 0;

			};

			this.setTime = function ( time ) {

				time = Math.abs( time );
				if ( this.length )
					time = time % this.length + .05;
				var key0 = null;
				var key1 = null;

				for ( var i = this.keySearchAccel( time ); i < this.keys.length; i ++ ) {

					if ( this.keys[ i ].time == time ) {

						key0 = this.keys[ i ];
						key1 = this.keys[ i ];
						break;

					} else if ( this.keys[ i ].time < time && this.keys[ i + 1 ] && this.keys[ i + 1 ].time > time ) {

						key0 = this.keys[ i ];
						key1 = this.keys[ i + 1 ];
						break;

					} else if ( this.keys[ i ].time < time && i == this.keys.length - 1 ) {

						key0 = this.keys[ i ];
						key1 = this.keys[ 0 ].clone();
						key1.time += this.length + .05;
						break;

					}

				}

				if ( key0 && key1 && key0 !== key1 ) {

					this.target.matrixAutoUpdate = false;
					this.target.matrix.copy( key0.lerp( key1, time ) );
					this.target.matrixWorldNeedsUpdate = true;
					return;

				}

				if ( key0 && key1 && key0 == key1 ) {

					this.target.matrixAutoUpdate = false;
					this.target.matrix.copy( key0.matrix );
					this.target.matrixWorldNeedsUpdate = true;
					return;

				}

			};

		};

		Virtulous.TrackTargetNodeNameCompare = function ( root, target ) {

			function find( node, name ) {

				if ( node.name == name )
					return node;

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

					var r = find( node.children[ i ], name );
					if ( r ) return r;

				}

				return null;

			}

			return find( root, target.name );

		};

		Virtulous.Animation = function () {

			this.tracks = [];
			this.length = 0;

			this.addTrack = function ( track ) {

				this.tracks.push( track );
				this.length = Math.max( track.length, this.length );

			};

			this.setTime = function ( time ) {

				this.time = time;

				for ( var i = 0; i < this.tracks.length; i ++ )
					this.tracks[ i ].setTime( time );

			};

			this.clone = function ( target, compareitor ) {

				if ( ! compareitor ) compareitor = Virtulous.TrackTargetNodeNameCompare;
				var n = new Virtulous.Animation();
				n.target = target;
				for ( var i = 0; i < this.tracks.length; i ++ ) {

					var track = this.tracks[ i ].clone();
					track.reTarget( target, compareitor );
					n.addTrack( track );

				}

				return n;

			};

		};

		var ASSBIN_CHUNK_AICAMERA = 0x1234;
		var ASSBIN_CHUNK_AILIGHT = 0x1235;
		var ASSBIN_CHUNK_AITEXTURE = 0x1236;
		var ASSBIN_CHUNK_AIMESH = 0x1237;
		var ASSBIN_CHUNK_AINODEANIM = 0x1238;
		var ASSBIN_CHUNK_AISCENE = 0x1239;
		var ASSBIN_CHUNK_AIBONE = 0x123a;
		var ASSBIN_CHUNK_AIANIMATION = 0x123b;
		var ASSBIN_CHUNK_AINODE = 0x123c;
		var ASSBIN_CHUNK_AIMATERIAL = 0x123d;
		var ASSBIN_CHUNK_AIMATERIALPROPERTY = 0x123e;
		var ASSBIN_MESH_HAS_POSITIONS = 0x1;
		var ASSBIN_MESH_HAS_NORMALS = 0x2;
		var ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS = 0x4;
		var ASSBIN_MESH_HAS_TEXCOORD_BASE = 0x100;
		var ASSBIN_MESH_HAS_COLOR_BASE = 0x10000;
		var AI_MAX_NUMBER_OF_COLOR_SETS = 1;
		var AI_MAX_NUMBER_OF_TEXTURECOORDS = 4;
		//var aiLightSource_UNDEFINED = 0x0;
		//! A directional light source has a well-defined direction
		//! but is infinitely far away. That's quite a good
		//! approximation for sun light.
		var aiLightSource_DIRECTIONAL = 0x1;
		//! A point light source has a well-defined position
		//! in space but no direction - it emits light in all
		//! directions. A normal bulb is a point light.
		//var aiLightSource_POINT = 0x2;
		//! A spot light source emits light in a specific
		//! angle. It has a position and a direction it is pointing to.
		//! A good example for a spot light is a light spot in
		//! sport arenas.
		var aiLightSource_SPOT = 0x3;
		//! The generic light level of the world, including the bounces
		//! of all other lightsources.
		//! Typically, there's at most one ambient light in a scene.
		//! This light type doesn't have a valid position, direction, or
		//! other properties, just a color.
		//var aiLightSource_AMBIENT = 0x4;
		/** Flat shading. Shading is done on per-face base,
		 *  diffuse only. Also known as 'faceted shading'.
		 */
		//var aiShadingMode_Flat = 0x1;
		/** Simple Gouraud shading.
		 */
		//var aiShadingMode_Gouraud = 0x2;
		/** Phong-Shading -
		 */
		//var aiShadingMode_Phong = 0x3;
		/** Phong-Blinn-Shading
		 */
		//var aiShadingMode_Blinn = 0x4;
		/** Toon-Shading per pixel
		 *
		 *  Also known as 'comic' shader.
		 */
		//var aiShadingMode_Toon = 0x5;
		/** OrenNayar-Shading per pixel
		 *
		 *  Extension to standard Lambertian shading, taking the
		 *  roughness of the material into account
		 */
		//var aiShadingMode_OrenNayar = 0x6;
		/** Minnaert-Shading per pixel
		 *
		 *  Extension to standard Lambertian shading, taking the
		 *  "darkness" of the material into account
		 */
		//var aiShadingMode_Minnaert = 0x7;
		/** CookTorrance-Shading per pixel
		 *
		 *  Special shader for metallic surfaces.
		 */
		//var aiShadingMode_CookTorrance = 0x8;
		/** No shading at all. Constant light influence of 1.0.
		 */
		//var aiShadingMode_NoShading = 0x9;
		/** Fresnel shading
		 */
		//var aiShadingMode_Fresnel = 0xa;
		//var aiTextureType_NONE = 0x0;
		/** The texture is combined with the result of the diffuse
		 *  lighting equation.
		 */
		var aiTextureType_DIFFUSE = 0x1;
		/** The texture is combined with the result of the specular
		 *  lighting equation.
		 */
		//var aiTextureType_SPECULAR = 0x2;
		/** The texture is combined with the result of the ambient
		 *  lighting equation.
		 */
		//var aiTextureType_AMBIENT = 0x3;
		/** The texture is added to the result of the lighting
		 *  calculation. It isn't influenced by incoming light.
		 */
		//var aiTextureType_EMISSIVE = 0x4;
		/** The texture is a height map.
		 *
		 *  By convention, higher gray-scale values stand for
		 *  higher elevations from the base height.
		 */
		//var aiTextureType_HEIGHT = 0x5;
		/** The texture is a (tangent space) normal-map.
		 *
		 *  Again, there are several conventions for tangent-space
		 *  normal maps. Assimp does (intentionally) not
		 *  distinguish here.
		 */
		var aiTextureType_NORMALS = 0x6;
		/** The texture defines the glossiness of the material.
		 *
		 *  The glossiness is in fact the exponent of the specular
		 *  (phong) lighting equation. Usually there is a conversion
		 *  function defined to map the linear color values in the
		 *  texture to a suitable exponent. Have fun.
		 */
		//var aiTextureType_SHININESS = 0x7;
		/** The texture defines per-pixel opacity.
		 *
		 *  Usually 'white' means opaque and 'black' means
		 *  'transparency'. Or quite the opposite. Have fun.
		 */
		var aiTextureType_OPACITY = 0x8;
		/** Displacement texture
		 *
		 *  The exact purpose and format is application-dependent.
		 *  Higher color values stand for higher vertex displacements.
		 */
		//var aiTextureType_DISPLACEMENT = 0x9;
		/** Lightmap texture (aka Ambient Occlusion)
		 *
		 *  Both 'Lightmaps' and dedicated 'ambient occlusion maps' are
		 *  covered by this material property. The texture contains a
		 *  scaling value for the final color value of a pixel. Its
		 *  intensity is not affected by incoming light.
		 */
		var aiTextureType_LIGHTMAP = 0xA;
		/** Reflection texture
		 *
		 * Contains the color of a perfect mirror reflection.
		 * Rarely used, almost never for real-time applications.
		 */
		//var aiTextureType_REFLECTION = 0xB;
		/** Unknown texture
		 *
		 *  A texture reference that does not match any of the definitions
		 *  above is considered to be 'unknown'. It is still imported,
		 *  but is excluded from any further postprocessing.
		 */
		//var aiTextureType_UNKNOWN = 0xC;
		var BONESPERVERT = 4;

		function ASSBIN_MESH_HAS_TEXCOORD( n ) {

			return ASSBIN_MESH_HAS_TEXCOORD_BASE << n;

		}

		function ASSBIN_MESH_HAS_COLOR( n ) {

			return ASSBIN_MESH_HAS_COLOR_BASE << n;

		}

		function markBones( scene ) {

			for ( var i in scene.mMeshes ) {

				var mesh = scene.mMeshes[ i ];
				for ( var k in mesh.mBones ) {

					var boneNode = scene.findNode( mesh.mBones[ k ].mName );
					if ( boneNode )
						boneNode.isBone = true;

				}

			}

		}
M
r116  
Mr.doob 已提交
539

M
r105  
Mr.doob 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
		function cloneTreeToBones( root, scene ) {

			var rootBone = new Bone();
			rootBone.matrix.copy( root.matrix );
			rootBone.matrixWorld.copy( root.matrixWorld );
			rootBone.position.copy( root.position );
			rootBone.quaternion.copy( root.quaternion );
			rootBone.scale.copy( root.scale );
			scene.nodeCount ++;
			rootBone.name = "bone_" + root.name + scene.nodeCount.toString();

			if ( ! scene.nodeToBoneMap[ root.name ] )
				scene.nodeToBoneMap[ root.name ] = [];
			scene.nodeToBoneMap[ root.name ].push( rootBone );
			for ( var i in root.children ) {

				var child = cloneTreeToBones( root.children[ i ], scene );
M
r106  
Mr.doob 已提交
557
				rootBone.add( child );
M
r105  
Mr.doob 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

			}

			return rootBone;

		}

		function sortWeights( indexes, weights ) {

			var pairs = [];

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

				pairs.push( {
					i: indexes[ i ],
					w: weights[ i ]
				} );

			}

			pairs.sort( function ( a, b ) {

				return b.w - a.w;

			 } );

			while ( pairs.length < 4 ) {

				pairs.push( {
					i: 0,
					w: 0
				} );

			}

			if ( pairs.length > 4 )
				pairs.length = 4;
			var sum = 0;

			for ( var i = 0; i < 4; i ++ ) {

				sum += pairs[ i ].w * pairs[ i ].w;

			}

			sum = Math.sqrt( sum );

			for ( var i = 0; i < 4; i ++ ) {

				pairs[ i ].w = pairs[ i ].w / sum;
				indexes[ i ] = pairs[ i ].i;
				weights[ i ] = pairs[ i ].w;

			}

		}

		function findMatchingBone( root, name ) {

			if ( root.name.indexOf( "bone_" + name ) == 0 )
				return root;

			for ( var i in root.children ) {

				var ret = findMatchingBone( root.children[ i ], name );

				if ( ret )
					return ret;

			}

			return undefined;

		}

		function aiMesh() {

			this.mPrimitiveTypes = 0;
			this.mNumVertices = 0;
			this.mNumFaces = 0;
			this.mNumBones = 0;
			this.mMaterialIndex = 0;
			this.mVertices = [];
			this.mNormals = [];
			this.mTangents = [];
			this.mBitangents = [];
			this.mColors = [
				[]
			];
			this.mTextureCoords = [
				[]
			];
			this.mFaces = [];
			this.mBones = [];
			this.hookupSkeletons = function ( scene ) {

				if ( this.mBones.length == 0 ) return;

				var allBones = [];
				var offsetMatrix = [];
				var skeletonRoot = scene.findNode( this.mBones[ 0 ].mName );

				while ( skeletonRoot.mParent && skeletonRoot.mParent.isBone ) {

					skeletonRoot = skeletonRoot.mParent;

				}

				var threeSkeletonRoot = skeletonRoot.toTHREE( scene );
				var threeSkeletonRootBone = cloneTreeToBones( threeSkeletonRoot, scene );
				this.threeNode.add( threeSkeletonRootBone );

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

					var bone = findMatchingBone( threeSkeletonRootBone, this.mBones[ i ].mName );

					if ( bone ) {

						var tbone = bone;
						allBones.push( tbone );
						//tbone.matrixAutoUpdate = false;
						offsetMatrix.push( this.mBones[ i ].mOffsetMatrix.toTHREE() );

					} else {

						var skeletonRoot = scene.findNode( this.mBones[ i ].mName );
						if ( ! skeletonRoot ) return;
						var threeSkeletonRoot = skeletonRoot.toTHREE( scene );
						var threeSkeletonRootBone = cloneTreeToBones( threeSkeletonRoot, scene );
						this.threeNode.add( threeSkeletonRootBone );
						var bone = findMatchingBone( threeSkeletonRootBone, this.mBones[ i ].mName );
						var tbone = bone;
						allBones.push( tbone );
						//tbone.matrixAutoUpdate = false;
						offsetMatrix.push( this.mBones[ i ].mOffsetMatrix.toTHREE() );

					}

				}
M
r116  
Mr.doob 已提交
697

M
r105  
Mr.doob 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
				var skeleton = new Skeleton( allBones, offsetMatrix );

				this.threeNode.bind( skeleton, new Matrix4() );
				this.threeNode.material.skinning = true;

			};

			this.toTHREE = function ( scene ) {

				if ( this.threeNode ) return this.threeNode;
				var geometry = new BufferGeometry();
				var mat;
				if ( scene.mMaterials[ this.mMaterialIndex ] )
					mat = scene.mMaterials[ this.mMaterialIndex ].toTHREE( scene );
				else
					mat = new MeshLambertMaterial();
				geometry.setIndex( new BufferAttribute( new Uint32Array( this.mIndexArray ), 1 ) );
M
r110  
Mr.doob 已提交
715
				geometry.setAttribute( 'position', new BufferAttribute( this.mVertexBuffer, 3 ) );
M
r105  
Mr.doob 已提交
716
				if ( this.mNormalBuffer && this.mNormalBuffer.length > 0 )
M
r110  
Mr.doob 已提交
717
					geometry.setAttribute( 'normal', new BufferAttribute( this.mNormalBuffer, 3 ) );
M
r105  
Mr.doob 已提交
718
				if ( this.mColorBuffer && this.mColorBuffer.length > 0 )
M
r110  
Mr.doob 已提交
719
					geometry.setAttribute( 'color', new BufferAttribute( this.mColorBuffer, 4 ) );
M
r105  
Mr.doob 已提交
720
				if ( this.mTexCoordsBuffers[ 0 ] && this.mTexCoordsBuffers[ 0 ].length > 0 )
M
r110  
Mr.doob 已提交
721
					geometry.setAttribute( 'uv', new BufferAttribute( new Float32Array( this.mTexCoordsBuffers[ 0 ] ), 2 ) );
M
r105  
Mr.doob 已提交
722
				if ( this.mTexCoordsBuffers[ 1 ] && this.mTexCoordsBuffers[ 1 ].length > 0 )
M
r110  
Mr.doob 已提交
723
					geometry.setAttribute( 'uv1', new BufferAttribute( new Float32Array( this.mTexCoordsBuffers[ 1 ] ), 2 ) );
M
r105  
Mr.doob 已提交
724
				if ( this.mTangentBuffer && this.mTangentBuffer.length > 0 )
M
r110  
Mr.doob 已提交
725
					geometry.setAttribute( 'tangents', new BufferAttribute( this.mTangentBuffer, 3 ) );
M
r105  
Mr.doob 已提交
726
				if ( this.mBitangentBuffer && this.mBitangentBuffer.length > 0 )
M
r110  
Mr.doob 已提交
727
					geometry.setAttribute( 'bitangents', new BufferAttribute( this.mBitangentBuffer, 3 ) );
M
r105  
Mr.doob 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
				if ( this.mBones.length > 0 ) {

					var weights = [];
					var bones = [];

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

						for ( var j = 0; j < this.mBones[ i ].mWeights.length; j ++ ) {

							var weight = this.mBones[ i ].mWeights[ j ];
							if ( weight ) {

								if ( ! weights[ weight.mVertexId ] ) weights[ weight.mVertexId ] = [];
								if ( ! bones[ weight.mVertexId ] ) bones[ weight.mVertexId ] = [];
								weights[ weight.mVertexId ].push( weight.mWeight );
								bones[ weight.mVertexId ].push( parseInt( i ) );

							}

						}

					}

					for ( var i in bones ) {

						sortWeights( bones[ i ], weights[ i ] );

					}

					var _weights = [];
					var _bones = [];

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

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

							if ( weights[ i ] && bones[ i ] ) {

								_weights.push( weights[ i ][ j ] );
								_bones.push( bones[ i ][ j ] );

							} else {

								_weights.push( 0 );
								_bones.push( 0 );

							}

						}

					}

M
r110  
Mr.doob 已提交
780 781
					geometry.setAttribute( 'skinWeight', new BufferAttribute( new Float32Array( _weights ), BONESPERVERT ) );
					geometry.setAttribute( 'skinIndex', new BufferAttribute( new Float32Array( _bones ), BONESPERVERT ) );
M
r105  
Mr.doob 已提交
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

				}

				var mesh;

				if ( this.mBones.length == 0 )
					mesh = new Mesh( geometry, mat );

				if ( this.mBones.length > 0 ) {

					mesh = new SkinnedMesh( geometry, mat );
					mesh.normalizeSkinWeights();

				}

				this.threeNode = mesh;
				//mesh.matrixAutoUpdate = false;
				return mesh;

			};

		}

		function aiFace() {

			this.mNumIndices = 0;
			this.mIndices = [];

		}

		function aiVector3D() {

			this.x = 0;
			this.y = 0;
			this.z = 0;

			this.toTHREE = function () {

				return new Vector3( this.x, this.y, this.z );

			};

		}

		function aiColor3D() {

			this.r = 0;
			this.g = 0;
			this.b = 0;
			this.a = 0;
			this.toTHREE = function () {

				return new Color( this.r, this.g, this.b );

			};

		}

		function aiQuaternion() {

			this.x = 0;
			this.y = 0;
			this.z = 0;
			this.w = 0;
			this.toTHREE = function () {

				return new Quaternion( this.x, this.y, this.z, this.w );

			};

		}

		function aiVertexWeight() {

			this.mVertexId = 0;
			this.mWeight = 0;

		}

		function aiString() {

			this.data = [];
			this.toString = function () {

				var str = '';
				this.data.forEach( function ( i ) {

					str += ( String.fromCharCode( i ) );

				} );
				return str.replace( /[^\x20-\x7E]+/g, '' );

			};

		}

		function aiVectorKey() {

			this.mTime = 0;
			this.mValue = null;

		}

		function aiQuatKey() {

			this.mTime = 0;
			this.mValue = null;

		}

		function aiNode() {

			this.mName = '';
			this.mTransformation = [];
			this.mNumChildren = 0;
			this.mNumMeshes = 0;
			this.mMeshes = [];
			this.mChildren = [];
			this.toTHREE = function ( scene ) {

				if ( this.threeNode ) return this.threeNode;
				var o = new Object3D();
				o.name = this.mName;
				o.matrix = this.mTransformation.toTHREE();

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

					o.add( this.mChildren[ i ].toTHREE( scene ) );

				}

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

					o.add( scene.mMeshes[ this.mMeshes[ i ] ].toTHREE( scene ) );

				}

				this.threeNode = o;
				//o.matrixAutoUpdate = false;
				o.matrix.decompose( o.position, o.quaternion, o.scale );
				return o;

			};

		}

		function aiBone() {

			this.mName = '';
			this.mNumWeights = 0;
			this.mOffsetMatrix = 0;

		}

		function aiMaterialProperty() {

			this.mKey = "";
			this.mSemantic = 0;
			this.mIndex = 0;
			this.mData = [];
			this.mDataLength = 0;
			this.mType = 0;
			this.dataAsColor = function () {

				var array = ( new Uint8Array( this.mData ) ).buffer;
				var reader = new DataView( array );
				var r = reader.getFloat32( 0, true );
				var g = reader.getFloat32( 4, true );
				var b = reader.getFloat32( 8, true );
				//var a = reader.getFloat32(12, true);
				return new Color( r, g, b );

			};

			this.dataAsFloat = function () {

				var array = ( new Uint8Array( this.mData ) ).buffer;
				var reader = new DataView( array );
				var r = reader.getFloat32( 0, true );
				return r;

			};

			this.dataAsBool = function () {

				var array = ( new Uint8Array( this.mData ) ).buffer;
				var reader = new DataView( array );
				var r = reader.getFloat32( 0, true );
				return !! r;

			};

			this.dataAsString = function () {

				var s = new aiString();
				s.data = this.mData;
				return s.toString();

			};

			this.dataAsMap = function () {

				var s = new aiString();
				s.data = this.mData;
				var path = s.toString();
				path = path.replace( /\\/g, '/' );

				if ( path.indexOf( '/' ) != - 1 ) {

					path = path.substr( path.lastIndexOf( '/' ) + 1 );

				}

				return textureLoader.load( path );

			};

		}
M
r116  
Mr.doob 已提交
1000

M
r105  
Mr.doob 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 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 1077 1078 1079 1080 1081 1082 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 1144 1145 1146 1147 1148 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 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
		var namePropMapping = {

			"?mat.name": "name",
			"$mat.shadingm": "shading",
			"$mat.twosided": "twoSided",
			"$mat.wireframe": "wireframe",
			"$clr.ambient": "ambient",
			"$clr.diffuse": "color",
			"$clr.specular": "specular",
			"$clr.emissive": "emissive",
			"$clr.transparent": "transparent",
			"$clr.reflective": "reflect",
			"$mat.shininess": "shininess",
			"$mat.reflectivity": "reflectivity",
			"$mat.refracti": "refraction",
			"$tex.file": "map"

		};

		var nameTypeMapping = {

			"?mat.name": "string",
			"$mat.shadingm": "bool",
			"$mat.twosided": "bool",
			"$mat.wireframe": "bool",
			"$clr.ambient": "color",
			"$clr.diffuse": "color",
			"$clr.specular": "color",
			"$clr.emissive": "color",
			"$clr.transparent": "color",
			"$clr.reflective": "color",
			"$mat.shininess": "float",
			"$mat.reflectivity": "float",
			"$mat.refracti": "float",
			"$tex.file": "map"

		};

		function aiMaterial() {

			this.mNumAllocated = 0;
			this.mNumProperties = 0;
			this.mProperties = [];
			this.toTHREE = function () {

				var mat = new MeshPhongMaterial();

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

					if ( nameTypeMapping[ this.mProperties[ i ].mKey ] == 'float' )
						mat[ namePropMapping[ this.mProperties[ i ].mKey ] ] = this.mProperties[ i ].dataAsFloat();
					if ( nameTypeMapping[ this.mProperties[ i ].mKey ] == 'color' )
						mat[ namePropMapping[ this.mProperties[ i ].mKey ] ] = this.mProperties[ i ].dataAsColor();
					if ( nameTypeMapping[ this.mProperties[ i ].mKey ] == 'bool' )
						mat[ namePropMapping[ this.mProperties[ i ].mKey ] ] = this.mProperties[ i ].dataAsBool();
					if ( nameTypeMapping[ this.mProperties[ i ].mKey ] == 'string' )
						mat[ namePropMapping[ this.mProperties[ i ].mKey ] ] = this.mProperties[ i ].dataAsString();
					if ( nameTypeMapping[ this.mProperties[ i ].mKey ] == 'map' ) {

						var prop = this.mProperties[ i ];
						if ( prop.mSemantic == aiTextureType_DIFFUSE )
							mat.map = this.mProperties[ i ].dataAsMap();
						if ( prop.mSemantic == aiTextureType_NORMALS )
							mat.normalMap = this.mProperties[ i ].dataAsMap();
						if ( prop.mSemantic == aiTextureType_LIGHTMAP )
							mat.lightMap = this.mProperties[ i ].dataAsMap();
						if ( prop.mSemantic == aiTextureType_OPACITY )
							mat.alphaMap = this.mProperties[ i ].dataAsMap();

					}

				}

				mat.ambient.r = .53;
				mat.ambient.g = .53;
				mat.ambient.b = .53;
				mat.color.r = 1;
				mat.color.g = 1;
				mat.color.b = 1;
				return mat;

			};

		}


		function veclerp( v1, v2, l ) {

			var v = new Vector3();
			var lm1 = 1 - l;
			v.x = v1.x * l + v2.x * lm1;
			v.y = v1.y * l + v2.y * lm1;
			v.z = v1.z * l + v2.z * lm1;
			return v;

		}

		function quatlerp( q1, q2, l ) {

			return q1.clone().slerp( q2, 1 - l );

		}

		function sampleTrack( keys, time, lne, lerp ) {

			if ( keys.length == 1 ) return keys[ 0 ].mValue.toTHREE();

			var dist = Infinity;
			var key = null;
			var nextKey = null;

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

				var timeDist = Math.abs( keys[ i ].mTime - time );

				if ( timeDist < dist && keys[ i ].mTime <= time ) {

					dist = timeDist;
					key = keys[ i ];
					nextKey = keys[ i + 1 ];

				}

			}

			if ( ! key ) {

				return null;

			} else if ( nextKey ) {

				var dT = nextKey.mTime - key.mTime;
				var T = key.mTime - time;
				var l = T / dT;

				return lerp( key.mValue.toTHREE(), nextKey.mValue.toTHREE(), l );

			} else {

				nextKey = keys[ 0 ].clone();
				nextKey.mTime += lne;

				var dT = nextKey.mTime - key.mTime;
				var T = key.mTime - time;
				var l = T / dT;

				return lerp( key.mValue.toTHREE(), nextKey.mValue.toTHREE(), l );

			}

		}

		function aiNodeAnim() {

			this.mNodeName = "";
			this.mNumPositionKeys = 0;
			this.mNumRotationKeys = 0;
			this.mNumScalingKeys = 0;
			this.mPositionKeys = [];
			this.mRotationKeys = [];
			this.mScalingKeys = [];
			this.mPreState = "";
			this.mPostState = "";
			this.init = function ( tps ) {

				if ( ! tps ) tps = 1;

				function t( t ) {

					t.mTime /= tps;

				}

				this.mPositionKeys.forEach( t );
				this.mRotationKeys.forEach( t );
				this.mScalingKeys.forEach( t );

			};

			this.sortKeys = function () {

				function comp( a, b ) {

					return a.mTime - b.mTime;

				}

				this.mPositionKeys.sort( comp );
				this.mRotationKeys.sort( comp );
				this.mScalingKeys.sort( comp );

			};

			this.getLength = function () {

				return Math.max(
					Math.max.apply( null, this.mPositionKeys.map( function ( a ) {

						return a.mTime;

					} ) ),
					Math.max.apply( null, this.mRotationKeys.map( function ( a ) {

						return a.mTime;

					} ) ),
					Math.max.apply( null, this.mScalingKeys.map( function ( a ) {

						return a.mTime;

				 } ) )
				);

			};

			this.toTHREE = function ( o ) {

				this.sortKeys();
				var length = this.getLength();
				var track = new Virtulous.KeyFrameTrack();

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

					var matrix = new Matrix4();
					var time = i;
					var pos = sampleTrack( this.mPositionKeys, time, length, veclerp );
					var scale = sampleTrack( this.mScalingKeys, time, length, veclerp );
					var rotation = sampleTrack( this.mRotationKeys, time, length, quatlerp );
					matrix.compose( pos, rotation, scale );

					var key = new Virtulous.KeyFrame( time, matrix );
					track.addKey( key );

				}

				track.target = o.findNode( this.mNodeName ).toTHREE();

				var tracks = [ track ];

				if ( o.nodeToBoneMap[ this.mNodeName ] ) {

					for ( var i = 0; i < o.nodeToBoneMap[ this.mNodeName ].length; i ++ ) {

						var t2 = track.clone();
						t2.target = o.nodeToBoneMap[ this.mNodeName ][ i ];
						tracks.push( t2 );

					}

				}

				return tracks;

			};

		}

		function aiAnimation() {

			this.mName = "";
			this.mDuration = 0;
			this.mTicksPerSecond = 0;
			this.mNumChannels = 0;
			this.mChannels = [];
			this.toTHREE = function ( root ) {

				var animationHandle = new Virtulous.Animation();

				for ( var i in this.mChannels ) {

					this.mChannels[ i ].init( this.mTicksPerSecond );

					var tracks = this.mChannels[ i ].toTHREE( root );

					for ( var j in tracks ) {

						tracks[ j ].init();
						animationHandle.addTrack( tracks[ j ] );

					}

				}

				animationHandle.length = Math.max.apply( null, animationHandle.tracks.map( function ( e ) {

					return e.length;

				} ) );
				return animationHandle;

			};

		}

		function aiTexture() {

			this.mWidth = 0;
			this.mHeight = 0;
			this.texAchFormatHint = [];
			this.pcData = [];

		}

		function aiLight() {

			this.mName = '';
			this.mType = 0;
			this.mAttenuationConstant = 0;
			this.mAttenuationLinear = 0;
			this.mAttenuationQuadratic = 0;
			this.mAngleInnerCone = 0;
			this.mAngleOuterCone = 0;
			this.mColorDiffuse = null;
			this.mColorSpecular = null;
			this.mColorAmbient = null;

		}

		function aiCamera() {

			this.mName = '';
			this.mPosition = null;
			this.mLookAt = null;
			this.mUp = null;
			this.mHorizontalFOV = 0;
			this.mClipPlaneNear = 0;
			this.mClipPlaneFar = 0;
			this.mAspect = 0;

		}

		function aiScene() {

M
r106  
Mr.doob 已提交
1334 1335 1336 1337
			this.versionMajor = 0;
			this.versionMinor = 0;
			this.versionRevision = 0;
			this.compileFlags = 0;
M
r105  
Mr.doob 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
			this.mFlags = 0;
			this.mNumMeshes = 0;
			this.mNumMaterials = 0;
			this.mNumAnimations = 0;
			this.mNumTextures = 0;
			this.mNumLights = 0;
			this.mNumCameras = 0;
			this.mRootNode = null;
			this.mMeshes = [];
			this.mMaterials = [];
			this.mAnimations = [];
			this.mLights = [];
			this.mCameras = [];
			this.nodeToBoneMap = {};
			this.findNode = function ( name, root ) {

				if ( ! root ) {

					root = this.mRootNode;

				}

				if ( root.mName == name ) {

					return root;

				}

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

					var ret = this.findNode( name, root.mChildren[ i ] );
					if ( ret ) return ret;

				}

				return null;

			};

			this.toTHREE = function () {

				this.nodeCount = 0;

				markBones( this );

				var o = this.mRootNode.toTHREE( this );

				for ( var i in this.mMeshes )
					this.mMeshes[ i ].hookupSkeletons( this );

				if ( this.mAnimations.length > 0 ) {

					var a = this.mAnimations[ 0 ].toTHREE( this );

				}

				return { object: o, animation: a };

			};

		}

		function aiMatrix4() {

			this.elements = [
				[],
				[],
				[],
				[]
			];
			this.toTHREE = function () {

				var m = new Matrix4();

				for ( var i = 0; i < 4; ++ i ) {

					for ( var i2 = 0; i2 < 4; ++ i2 ) {

						m.elements[ i * 4 + i2 ] = this.elements[ i2 ][ i ];

					}

				}

				return m;

			};

		}

		var littleEndian = true;

		function readFloat( dataview ) {

			var val = dataview.getFloat32( dataview.readOffset, littleEndian );
			dataview.readOffset += 4;
			return val;

		}

		function Read_double( dataview ) {

			var val = dataview.getFloat64( dataview.readOffset, littleEndian );
			dataview.readOffset += 8;
			return val;

		}

		function Read_uint8_t( dataview ) {

			var val = dataview.getUint8( dataview.readOffset );
			dataview.readOffset += 1;
			return val;

		}

		function Read_uint16_t( dataview ) {

			var val = dataview.getUint16( dataview.readOffset, littleEndian );
			dataview.readOffset += 2;
			return val;

		}

		function Read_unsigned_int( dataview ) {

			var val = dataview.getUint32( dataview.readOffset, littleEndian );
			dataview.readOffset += 4;
			return val;

		}

		function Read_uint32_t( dataview ) {

			var val = dataview.getUint32( dataview.readOffset, littleEndian );
			dataview.readOffset += 4;
			return val;

		}

		function Read_aiVector3D( stream ) {

			var v = new aiVector3D();
			v.x = readFloat( stream );
			v.y = readFloat( stream );
			v.z = readFloat( stream );
			return v;

		}

		function Read_aiColor3D( stream ) {

			var c = new aiColor3D();
			c.r = readFloat( stream );
			c.g = readFloat( stream );
			c.b = readFloat( stream );
			return c;

		}

		function Read_aiQuaternion( stream ) {

			var v = new aiQuaternion();
			v.w = readFloat( stream );
			v.x = readFloat( stream );
			v.y = readFloat( stream );
			v.z = readFloat( stream );
			return v;

		}

		function Read_aiString( stream ) {

			var s = new aiString();
			var stringlengthbytes = Read_unsigned_int( stream );
			stream.ReadBytes( s.data, 1, stringlengthbytes );
			return s.toString();

		}

		function Read_aiVertexWeight( stream ) {

			var w = new aiVertexWeight();
			w.mVertexId = Read_unsigned_int( stream );
			w.mWeight = readFloat( stream );
			return w;

		}

		function Read_aiMatrix4x4( stream ) {

			var m = new aiMatrix4();

			for ( var i = 0; i < 4; ++ i ) {

				for ( var i2 = 0; i2 < 4; ++ i2 ) {

					m.elements[ i ][ i2 ] = readFloat( stream );

				}

			}

			return m;

		}

		function Read_aiVectorKey( stream ) {

			var v = new aiVectorKey();
			v.mTime = Read_double( stream );
			v.mValue = Read_aiVector3D( stream );
			return v;

		}

		function Read_aiQuatKey( stream ) {

			var v = new aiQuatKey();
			v.mTime = Read_double( stream );
			v.mValue = Read_aiQuaternion( stream );
			return v;

		}

		function ReadArray_aiVertexWeight( stream, data, size ) {

			for ( var i = 0; i < size; i ++ ) data[ i ] = Read_aiVertexWeight( stream );

		}

		function ReadArray_aiVectorKey( stream, data, size ) {

			for ( var i = 0; i < size; i ++ ) data[ i ] = Read_aiVectorKey( stream );

		}

		function ReadArray_aiQuatKey( stream, data, size ) {

			for ( var i = 0; i < size; i ++ ) data[ i ] = Read_aiQuatKey( stream );

		}

		function ReadBounds( stream, T /*p*/, n ) {

			// not sure what to do here, the data isn't really useful.
M
r120  
Mr.doob 已提交
1584
			return stream.Seek( sizeof( T ) * n, aiOrigin_CUR ); // eslint-disable-line no-undef
M
r105  
Mr.doob 已提交
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792

		}

		function ai_assert( bool ) {

			if ( ! bool )
				throw ( "asset failed" );

		}

		function ReadBinaryNode( stream, parent, depth ) {

			var chunkID = Read_uint32_t( stream );
			ai_assert( chunkID == ASSBIN_CHUNK_AINODE );
			/*uint32_t size =*/
			Read_uint32_t( stream );
			var node = new aiNode();
			node.mParent = parent;
			node.mDepth = depth;
			node.mName = Read_aiString( stream );
			node.mTransformation = Read_aiMatrix4x4( stream );
			node.mNumChildren = Read_unsigned_int( stream );
			node.mNumMeshes = Read_unsigned_int( stream );

			if ( node.mNumMeshes ) {

				node.mMeshes = [];

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

					node.mMeshes[ i ] = Read_unsigned_int( stream );

				}

			}

			if ( node.mNumChildren ) {

				node.mChildren = [];

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

					var node2 = ReadBinaryNode( stream, node, depth ++ );
					node.mChildren[ i ] = node2;

				}

			}

			return node;

		}

		// -----------------------------------------------------------------------------------

		function ReadBinaryBone( stream, b ) {

			var chunkID = Read_uint32_t( stream );
			ai_assert( chunkID == ASSBIN_CHUNK_AIBONE );
			/*uint32_t size =*/
			Read_uint32_t( stream );
			b.mName = Read_aiString( stream );
			b.mNumWeights = Read_unsigned_int( stream );
			b.mOffsetMatrix = Read_aiMatrix4x4( stream );
			// for the moment we write dumb min/max values for the bones, too.
			// maybe I'll add a better, hash-like solution later
			if ( shortened ) {

				ReadBounds( stream, b.mWeights, b.mNumWeights );

			} else {

				// else write as usual

				b.mWeights = [];
				ReadArray_aiVertexWeight( stream, b.mWeights, b.mNumWeights );

			}

			return b;

		}

		function ReadBinaryMesh( stream, mesh ) {

			var chunkID = Read_uint32_t( stream );
			ai_assert( chunkID == ASSBIN_CHUNK_AIMESH );
			/*uint32_t size =*/
			Read_uint32_t( stream );
			mesh.mPrimitiveTypes = Read_unsigned_int( stream );
			mesh.mNumVertices = Read_unsigned_int( stream );
			mesh.mNumFaces = Read_unsigned_int( stream );
			mesh.mNumBones = Read_unsigned_int( stream );
			mesh.mMaterialIndex = Read_unsigned_int( stream );
			mesh.mNumUVComponents = [];
			// first of all, write bits for all existent vertex components
			var c = Read_unsigned_int( stream );

			if ( c & ASSBIN_MESH_HAS_POSITIONS ) {

				if ( shortened ) {

					ReadBounds( stream, mesh.mVertices, mesh.mNumVertices );

				} else {

					// else write as usual

					mesh.mVertices = [];
					mesh.mVertexBuffer = stream.subArray32( stream.readOffset, stream.readOffset + mesh.mNumVertices * 3 * 4 );
					stream.Seek( mesh.mNumVertices * 3 * 4, aiOrigin_CUR );

				}

			}

			if ( c & ASSBIN_MESH_HAS_NORMALS ) {

				if ( shortened ) {

					ReadBounds( stream, mesh.mNormals, mesh.mNumVertices );

				} else {

					// else write as usual

					mesh.mNormals = [];
					mesh.mNormalBuffer = stream.subArray32( stream.readOffset, stream.readOffset + mesh.mNumVertices * 3 * 4 );
					stream.Seek( mesh.mNumVertices * 3 * 4, aiOrigin_CUR );

				}

			}

			if ( c & ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS ) {

				if ( shortened ) {

					ReadBounds( stream, mesh.mTangents, mesh.mNumVertices );
					ReadBounds( stream, mesh.mBitangents, mesh.mNumVertices );

				} else {

					// else write as usual

					mesh.mTangents = [];
					mesh.mTangentBuffer = stream.subArray32( stream.readOffset, stream.readOffset + mesh.mNumVertices * 3 * 4 );
					stream.Seek( mesh.mNumVertices * 3 * 4, aiOrigin_CUR );
					mesh.mBitangents = [];
					mesh.mBitangentBuffer = stream.subArray32( stream.readOffset, stream.readOffset + mesh.mNumVertices * 3 * 4 );
					stream.Seek( mesh.mNumVertices * 3 * 4, aiOrigin_CUR );

				}

			}

			for ( var n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS; ++ n ) {

				if ( ! ( c & ASSBIN_MESH_HAS_COLOR( n ) ) ) break;

				if ( shortened ) {

					ReadBounds( stream, mesh.mColors[ n ], mesh.mNumVertices );

				} else {

					// else write as usual

					mesh.mColors[ n ] = [];
					mesh.mColorBuffer = stream.subArray32( stream.readOffset, stream.readOffset + mesh.mNumVertices * 4 * 4 );
					stream.Seek( mesh.mNumVertices * 4 * 4, aiOrigin_CUR );

				}

			}

			mesh.mTexCoordsBuffers = [];

			for ( var n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++ n ) {

				if ( ! ( c & ASSBIN_MESH_HAS_TEXCOORD( n ) ) ) break;

				// write number of UV components
				mesh.mNumUVComponents[ n ] = Read_unsigned_int( stream );

				if ( shortened ) {

					ReadBounds( stream, mesh.mTextureCoords[ n ], mesh.mNumVertices );

				} else {

					// else write as usual

					mesh.mTextureCoords[ n ] = [];
					//note that assbin always writes 3d texcoords
					mesh.mTexCoordsBuffers[ n ] = [];

					for ( var uv = 0; uv < mesh.mNumVertices; uv ++ ) {

						mesh.mTexCoordsBuffers[ n ].push( readFloat( stream ) );
						mesh.mTexCoordsBuffers[ n ].push( readFloat( stream ) );
						readFloat( stream );

					}

				}

			}
M
r116  
Mr.doob 已提交
1793

M
r105  
Mr.doob 已提交
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
			// write faces. There are no floating-point calculations involved
			// in these, so we can write a simple hash over the face data
			// to the dump file. We generate a single 32 Bit hash for 512 faces
			// using Assimp's standard hashing function.
			if ( shortened ) {

				Read_unsigned_int( stream );

			} else {

				// else write as usual

				// if there are less than 2^16 vertices, we can simply use 16 bit integers ...
				mesh.mFaces = [];
				mesh.mIndexArray = [];

				for ( var i = 0; i < mesh.mNumFaces; ++ i ) {

					var f = mesh.mFaces[ i ] = new aiFace();
					// BOOST_STATIC_ASSERT(AI_MAX_FACE_INDICES <= 0xffff);
					f.mNumIndices = Read_uint16_t( stream );
					f.mIndices = [];

					for ( var a = 0; a < f.mNumIndices; ++ a ) {

						if ( mesh.mNumVertices < ( 1 << 16 ) ) {

							f.mIndices[ a ] = Read_uint16_t( stream );

						} else {

							f.mIndices[ a ] = Read_unsigned_int( stream );

						}



					}

					if ( f.mNumIndices === 3 ) {

						mesh.mIndexArray.push( f.mIndices[ 0 ] );
						mesh.mIndexArray.push( f.mIndices[ 1 ] );
						mesh.mIndexArray.push( f.mIndices[ 2 ] );

					} else if ( f.mNumIndices === 4 ) {

						mesh.mIndexArray.push( f.mIndices[ 0 ] );
						mesh.mIndexArray.push( f.mIndices[ 1 ] );
						mesh.mIndexArray.push( f.mIndices[ 2 ] );
						mesh.mIndexArray.push( f.mIndices[ 2 ] );
						mesh.mIndexArray.push( f.mIndices[ 3 ] );
						mesh.mIndexArray.push( f.mIndices[ 0 ] );

					} else {

						throw ( new Error( "Sorry, can't currently triangulate polys. Use the triangulate preprocessor in Assimp." ) );

					}



				}

			}
M
r116  
Mr.doob 已提交
1859

M
r105  
Mr.doob 已提交
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
			// write bones
			if ( mesh.mNumBones ) {

				mesh.mBones = [];

				for ( var a = 0; a < mesh.mNumBones; ++ a ) {

					mesh.mBones[ a ] = new aiBone();
					ReadBinaryBone( stream, mesh.mBones[ a ] );

				}

			}

		}

		function ReadBinaryMaterialProperty( stream, prop ) {

			var chunkID = Read_uint32_t( stream );
			ai_assert( chunkID == ASSBIN_CHUNK_AIMATERIALPROPERTY );
			/*uint32_t size =*/
			Read_uint32_t( stream );
			prop.mKey = Read_aiString( stream );
			prop.mSemantic = Read_unsigned_int( stream );
			prop.mIndex = Read_unsigned_int( stream );
			prop.mDataLength = Read_unsigned_int( stream );
			prop.mType = Read_unsigned_int( stream );
			prop.mData = [];
			stream.ReadBytes( prop.mData, 1, prop.mDataLength );

		}

		// -----------------------------------------------------------------------------------

		function ReadBinaryMaterial( stream, mat ) {

			var chunkID = Read_uint32_t( stream );
			ai_assert( chunkID == ASSBIN_CHUNK_AIMATERIAL );
			/*uint32_t size =*/
			Read_uint32_t( stream );
			mat.mNumAllocated = mat.mNumProperties = Read_unsigned_int( stream );

			if ( mat.mNumProperties ) {

				if ( mat.mProperties ) {

					delete mat.mProperties;

				}

				mat.mProperties = [];

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

					mat.mProperties[ i ] = new aiMaterialProperty();
					ReadBinaryMaterialProperty( stream, mat.mProperties[ i ] );

				}

			}

		}
M
r116  
Mr.doob 已提交
1922

M
r105  
Mr.doob 已提交
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
		function ReadBinaryNodeAnim( stream, nd ) {

			var chunkID = Read_uint32_t( stream );
			ai_assert( chunkID == ASSBIN_CHUNK_AINODEANIM );
			/*uint32_t size =*/
			Read_uint32_t( stream );
			nd.mNodeName = Read_aiString( stream );
			nd.mNumPositionKeys = Read_unsigned_int( stream );
			nd.mNumRotationKeys = Read_unsigned_int( stream );
			nd.mNumScalingKeys = Read_unsigned_int( stream );
			nd.mPreState = Read_unsigned_int( stream );
			nd.mPostState = Read_unsigned_int( stream );

			if ( nd.mNumPositionKeys ) {

				if ( shortened ) {

					ReadBounds( stream, nd.mPositionKeys, nd.mNumPositionKeys );

				} else {

					// else write as usual

					nd.mPositionKeys = [];
					ReadArray_aiVectorKey( stream, nd.mPositionKeys, nd.mNumPositionKeys );

				}

			}

			if ( nd.mNumRotationKeys ) {

				if ( shortened ) {

					ReadBounds( stream, nd.mRotationKeys, nd.mNumRotationKeys );

				} else {

		 			// else write as usual

					nd.mRotationKeys = [];
					ReadArray_aiQuatKey( stream, nd.mRotationKeys, nd.mNumRotationKeys );

				}

			}

			if ( nd.mNumScalingKeys ) {

				if ( shortened ) {

					ReadBounds( stream, nd.mScalingKeys, nd.mNumScalingKeys );

				} else {

	 				// else write as usual

					nd.mScalingKeys = [];
					ReadArray_aiVectorKey( stream, nd.mScalingKeys, nd.mNumScalingKeys );

				}

			}

		}
M
r116  
Mr.doob 已提交
1988

M
r105  
Mr.doob 已提交
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
		function ReadBinaryAnim( stream, anim ) {

			var chunkID = Read_uint32_t( stream );
			ai_assert( chunkID == ASSBIN_CHUNK_AIANIMATION );
			/*uint32_t size =*/
			Read_uint32_t( stream );
			anim.mName = Read_aiString( stream );
			anim.mDuration = Read_double( stream );
			anim.mTicksPerSecond = Read_double( stream );
			anim.mNumChannels = Read_unsigned_int( stream );

			if ( anim.mNumChannels ) {

				anim.mChannels = [];

				for ( var a = 0; a < anim.mNumChannels; ++ a ) {

					anim.mChannels[ a ] = new aiNodeAnim();
					ReadBinaryNodeAnim( stream, anim.mChannels[ a ] );

				}

			}

		}

		function ReadBinaryTexture( stream, tex ) {

			var chunkID = Read_uint32_t( stream );
			ai_assert( chunkID == ASSBIN_CHUNK_AITEXTURE );
			/*uint32_t size =*/
			Read_uint32_t( stream );
			tex.mWidth = Read_unsigned_int( stream );
			tex.mHeight = Read_unsigned_int( stream );
			stream.ReadBytes( tex.achFormatHint, 1, 4 );

			if ( ! shortened ) {

				if ( ! tex.mHeight ) {

					tex.pcData = [];
					stream.ReadBytes( tex.pcData, 1, tex.mWidth );

				} else {

					tex.pcData = [];
					stream.ReadBytes( tex.pcData, 1, tex.mWidth * tex.mHeight * 4 );

				}

			}

		}
M
r116  
Mr.doob 已提交
2042

M
r105  
Mr.doob 已提交
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
		function ReadBinaryLight( stream, l ) {

			var chunkID = Read_uint32_t( stream );
			ai_assert( chunkID == ASSBIN_CHUNK_AILIGHT );
			/*uint32_t size =*/
			Read_uint32_t( stream );
			l.mName = Read_aiString( stream );
			l.mType = Read_unsigned_int( stream );

			if ( l.mType != aiLightSource_DIRECTIONAL ) {

				l.mAttenuationConstant = readFloat( stream );
				l.mAttenuationLinear = readFloat( stream );
				l.mAttenuationQuadratic = readFloat( stream );

			}

			l.mColorDiffuse = Read_aiColor3D( stream );
			l.mColorSpecular = Read_aiColor3D( stream );
			l.mColorAmbient = Read_aiColor3D( stream );

			if ( l.mType == aiLightSource_SPOT ) {

				l.mAngleInnerCone = readFloat( stream );
				l.mAngleOuterCone = readFloat( stream );

			}

		}
M
r116  
Mr.doob 已提交
2072

M
r105  
Mr.doob 已提交
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
		function ReadBinaryCamera( stream, cam ) {

			var chunkID = Read_uint32_t( stream );
			ai_assert( chunkID == ASSBIN_CHUNK_AICAMERA );
			/*uint32_t size =*/
			Read_uint32_t( stream );
			cam.mName = Read_aiString( stream );
			cam.mPosition = Read_aiVector3D( stream );
			cam.mLookAt = Read_aiVector3D( stream );
			cam.mUp = Read_aiVector3D( stream );
			cam.mHorizontalFOV = readFloat( stream );
			cam.mClipPlaneNear = readFloat( stream );
			cam.mClipPlaneFar = readFloat( stream );
			cam.mAspect = readFloat( stream );

		}

		function ReadBinaryScene( stream, scene ) {

			var chunkID = Read_uint32_t( stream );
			ai_assert( chunkID == ASSBIN_CHUNK_AISCENE );
			/*uint32_t size =*/
			Read_uint32_t( stream );
			scene.mFlags = Read_unsigned_int( stream );
			scene.mNumMeshes = Read_unsigned_int( stream );
			scene.mNumMaterials = Read_unsigned_int( stream );
			scene.mNumAnimations = Read_unsigned_int( stream );
			scene.mNumTextures = Read_unsigned_int( stream );
			scene.mNumLights = Read_unsigned_int( stream );
			scene.mNumCameras = Read_unsigned_int( stream );
			// Read node graph
			scene.mRootNode = new aiNode();
			scene.mRootNode = ReadBinaryNode( stream, null, 0 );
			// Read all meshes
			if ( scene.mNumMeshes ) {

				scene.mMeshes = [];

				for ( var i = 0; i < scene.mNumMeshes; ++ i ) {

					scene.mMeshes[ i ] = new aiMesh();
					ReadBinaryMesh( stream, scene.mMeshes[ i ] );

				}

			}
M
r116  
Mr.doob 已提交
2119

M
r105  
Mr.doob 已提交
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
			// Read materials
			if ( scene.mNumMaterials ) {

				scene.mMaterials = [];

				for ( var i = 0; i < scene.mNumMaterials; ++ i ) {

					scene.mMaterials[ i ] = new aiMaterial();
					ReadBinaryMaterial( stream, scene.mMaterials[ i ] );

				}

			}
M
r116  
Mr.doob 已提交
2133

M
r105  
Mr.doob 已提交
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
			// Read all animations
			if ( scene.mNumAnimations ) {

				scene.mAnimations = [];

				for ( var i = 0; i < scene.mNumAnimations; ++ i ) {

					scene.mAnimations[ i ] = new aiAnimation();
					ReadBinaryAnim( stream, scene.mAnimations[ i ] );

				}

			}
M
r116  
Mr.doob 已提交
2147

M
r105  
Mr.doob 已提交
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
			// Read all textures
			if ( scene.mNumTextures ) {

				scene.mTextures = [];

				for ( var i = 0; i < scene.mNumTextures; ++ i ) {

					scene.mTextures[ i ] = new aiTexture();
					ReadBinaryTexture( stream, scene.mTextures[ i ] );

				}

			}
M
r116  
Mr.doob 已提交
2161

M
r105  
Mr.doob 已提交
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
			// Read lights
			if ( scene.mNumLights ) {

				scene.mLights = [];

				for ( var i = 0; i < scene.mNumLights; ++ i ) {

					scene.mLights[ i ] = new aiLight();
					ReadBinaryLight( stream, scene.mLights[ i ] );

				}

			}
M
r116  
Mr.doob 已提交
2175

M
r105  
Mr.doob 已提交
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
			// Read cameras
			if ( scene.mNumCameras ) {

				scene.mCameras = [];

				for ( var i = 0; i < scene.mNumCameras; ++ i ) {

					scene.mCameras[ i ] = new aiCamera();
					ReadBinaryCamera( stream, scene.mCameras[ i ] );

				}

			}

		}
M
r116  
Mr.doob 已提交
2191

M
r105  
Mr.doob 已提交
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
		var aiOrigin_CUR = 0;
		var aiOrigin_BEG = 1;

		function extendStream( stream ) {

			stream.readOffset = 0;
			stream.Seek = function ( off, ori ) {

				if ( ori == aiOrigin_CUR ) {

					stream.readOffset += off;

				}
M
r116  
Mr.doob 已提交
2205

M
r105  
Mr.doob 已提交
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
				if ( ori == aiOrigin_BEG ) {

					stream.readOffset = off;

				}

			};

			stream.ReadBytes = function ( buff, size, n ) {

				var bytes = size * n;
				for ( var i = 0; i < bytes; i ++ )
					buff[ i ] = Read_uint8_t( this );

			};

			stream.subArray32 = function ( start, end ) {

				var buff = this.buffer;
				var newbuff = buff.slice( start, end );
				return new Float32Array( newbuff );

			};

			stream.subArrayUint16 = function ( start, end ) {

				var buff = this.buffer;
				var newbuff = buff.slice( start, end );
				return new Uint16Array( newbuff );

			};

			stream.subArrayUint8 = function ( start, end ) {

				var buff = this.buffer;
				var newbuff = buff.slice( start, end );
				return new Uint8Array( newbuff );

			};

			stream.subArrayUint32 = function ( start, end ) {

				var buff = this.buffer;
				var newbuff = buff.slice( start, end );
				return new Uint32Array( newbuff );

			};

		}

		var shortened, compressed;

		function InternReadFile( pFiledata ) {

			var pScene = new aiScene();
			var stream = new DataView( pFiledata );
			extendStream( stream );
			stream.Seek( 44, aiOrigin_CUR ); // signature
			/*unsigned int versionMajor =*/
M
r106  
Mr.doob 已提交
2265
			pScene.versionMajor = Read_unsigned_int( stream );
M
r105  
Mr.doob 已提交
2266
			/*unsigned int versionMinor =*/
M
r106  
Mr.doob 已提交
2267
			pScene.versionMinor = Read_unsigned_int( stream );
M
r105  
Mr.doob 已提交
2268
			/*unsigned int versionRevision =*/
M
r106  
Mr.doob 已提交
2269
			pScene.versionRevision = Read_unsigned_int( stream );
M
r105  
Mr.doob 已提交
2270
			/*unsigned int compileFlags =*/
M
r106  
Mr.doob 已提交
2271
			pScene.compileFlags = Read_unsigned_int( stream );
M
r105  
Mr.doob 已提交
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
			shortened = Read_uint16_t( stream ) > 0;
			compressed = Read_uint16_t( stream ) > 0;
			if ( shortened )
				throw "Shortened binaries are not supported!";
			stream.Seek( 256, aiOrigin_CUR ); // original filename
			stream.Seek( 128, aiOrigin_CUR ); // options
			stream.Seek( 64, aiOrigin_CUR ); // padding
			if ( compressed ) {

				var uncompressedSize = Read_uint32_t( stream );
				var compressedSize = stream.FileSize() - stream.Tell();
				var compressedData = [];
				stream.Read( compressedData, 1, compressedSize );
				var uncompressedData = [];
M
r120  
Mr.doob 已提交
2286
				uncompress( uncompressedData, uncompressedSize, compressedData, compressedSize ); // eslint-disable-line no-undef
M
r105  
Mr.doob 已提交
2287 2288 2289 2290 2291 2292 2293 2294 2295
				var buff = new ArrayBuffer( uncompressedData );
				ReadBinaryScene( buff, pScene );

			} else {

				ReadBinaryScene( stream, pScene );

			}

M
r106  
Mr.doob 已提交
2296 2297
			return pScene.toTHREE();

M
r105  
Mr.doob 已提交
2298 2299 2300 2301 2302 2303
		}

		return InternReadFile( buffer );

	}

M
r109  
Mr.doob 已提交
2304
} );
M
r105  
Mr.doob 已提交
2305 2306

export { AssimpLoader };