浏览代码

TDSLoader: Force BufferGeometry output

Mugen87 7 年之前
父节点
当前提交
fd72e3e696
共有 1 个文件被更改,包括 21 次插入70 次删除
  1. 21 70
      examples/js/loaders/TDSLoader.js

+ 21 - 70
examples/js/loaders/TDSLoader.js

@@ -1,5 +1,5 @@
 /*
 /*
- * Autodesk 3DS threee.js file loader, based on lib3ds.
+ * Autodesk 3DS three.js file loader, based on lib3ds.
  *
  *
  * Loads geometry with uv and materials basic properties with texture support.
  * Loads geometry with uv and materials basic properties with texture support.
  *
  *
@@ -321,20 +321,9 @@ THREE.TDSLoader.prototype = {
 		var chunk = this.readChunk( data );
 		var chunk = this.readChunk( data );
 		var next = this.nextChunk( data, chunk );
 		var next = this.nextChunk( data, chunk );
 
 
-		var useBufferGeometry = false;
-		var geometry = null;
+		var geometry = new THREE.BufferGeometry();
 		var uvs = [];
 		var uvs = [];
 
 
-		if ( useBufferGeometry ) {
-
-			geometry = new THREE.BufferGeometry();
-
-		}	else {
-
-			geometry = new THREE.Geometry();
-
-		}
-
 		var material = new THREE.MeshPhongMaterial();
 		var material = new THREE.MeshPhongMaterial();
 		var mesh = new THREE.Mesh( geometry, material );
 		var mesh = new THREE.Mesh( geometry, material );
 		mesh.name = 'mesh';
 		mesh.name = 'mesh';
@@ -349,33 +338,23 @@ THREE.TDSLoader.prototype = {
 
 
 				//BufferGeometry
 				//BufferGeometry
 
 
-				if ( useBufferGeometry )	{
-
-					var vertices = [];
-					for ( var i = 0; i < points; i ++ )		{
-
-						vertices.push( this.readFloat( data ) );
-						vertices.push( this.readFloat( data ) );
-						vertices.push( this.readFloat( data ) );
-
-					}
-
-					geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
-
-				} else	{ //Geometry
+				var vertices = [];
 
 
-					for ( var i = 0; i < points; i ++ )		{
+				for ( var i = 0; i < points; i ++ )		{
 
 
-						geometry.vertices.push( new THREE.Vector3( this.readFloat( data ), this.readFloat( data ), this.readFloat( data ) ) );
-
-					}
+					vertices.push( this.readFloat( data ) );
+					vertices.push( this.readFloat( data ) );
+					vertices.push( this.readFloat( data ) );
 
 
 				}
 				}
 
 
+				geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
+
 			} else if ( next === FACE_ARRAY ) {
 			} else if ( next === FACE_ARRAY ) {
 
 
 				this.resetPosition( data );
 				this.resetPosition( data );
 				this.readFaceArray( data, mesh );
 				this.readFaceArray( data, mesh );
+				geometry.computeVertexNormals();
 
 
 			} else if ( next === TEX_VERTS ) {
 			} else if ( next === TEX_VERTS ) {
 
 
@@ -385,27 +364,17 @@ THREE.TDSLoader.prototype = {
 
 
 				//BufferGeometry
 				//BufferGeometry
 
 
-				if ( useBufferGeometry )	{
-
-					var uvs = [];
-					for ( var i = 0; i < texels; i ++ )		{
-
-						uvs.push( this.readFloat( data ) );
-						uvs.push( this.readFloat( data ) );
-
-					}
-					geometry.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( uvs ), 2 ) );
+				var uvs = [];
 
 
-				} else { //Geometry
+				for ( var i = 0; i < texels; i ++ )		{
 
 
-					uvs = [];
-					for ( var i = 0; i < texels; i ++ )		{
+					uvs.push( this.readFloat( data ) );
+					uvs.push( this.readFloat( data ) );
 
 
-						uvs.push( new THREE.Vector2( this.readFloat( data ), this.readFloat( data ) ) );
+				}
 
 
-					}
+				geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
 
 
-				}
 
 
 			} else if ( next === MESH_MATRIX ) {
 			} else if ( next === MESH_MATRIX ) {
 
 
@@ -464,28 +433,6 @@ THREE.TDSLoader.prototype = {
 
 
 		this.endChunk( chunk );
 		this.endChunk( chunk );
 
 
-		if ( ! useBufferGeometry ) {
-
-			//geometry.faceVertexUvs[0][faceIndex][vertexIndex]
-
-			if ( uvs.length > 0 ) {
-
-				var faceUV = [];
-
-				for ( var i = 0; i < geometry.faces.length; i ++ ) {
-
-					faceUV.push( [ uvs[ geometry.faces[ i ].a ], uvs[ geometry.faces[ i ].b ], uvs[ geometry.faces[ i ].c ] ] );
-
-				}
-
-				geometry.faceVertexUvs[ 0 ] = faceUV;
-
-			}
-
-			geometry.computeVertexNormals();
-
-		}
-
 		return mesh;
 		return mesh;
 
 
 	},
 	},
@@ -504,14 +451,18 @@ THREE.TDSLoader.prototype = {
 
 
 		this.debugMessage( '   Faces: ' + faces );
 		this.debugMessage( '   Faces: ' + faces );
 
 
+		var index = [];
+
 		for ( var i = 0; i < faces; ++ i ) {
 		for ( var i = 0; i < faces; ++ i ) {
 
 
-			mesh.geometry.faces.push( new THREE.Face3( this.readWord( data ), this.readWord( data ), this.readWord( data ) ) );
+			index.push( this.readWord( data ), this.readWord( data ), this.readWord( data ) );
 
 
 			var visibility = this.readWord( data );
 			var visibility = this.readWord( data );
 
 
 		}
 		}
 
 
+		mesh.geometry.setIndex( index );
+
 		//The rest of the FACE_ARRAY chunk is subchunks
 		//The rest of the FACE_ARRAY chunk is subchunks
 
 
 		while ( this.position < chunk.end ) {
 		while ( this.position < chunk.end ) {