Browse Source

Support for buffer geometry

Tentone 6 years ago
parent
commit
32769cc816
2 changed files with 67 additions and 196 deletions
  1. 63 193
      examples/js/exporters/DracoExporter.js
  2. 4 3
      examples/misc_exporter_draco.html

+ 63 - 193
examples/js/exporters/DracoExporter.js

@@ -9,7 +9,9 @@
  *  - decodeSpeed, indicates how to tune the encoder regarding decode speed (0 gives better speed but worst quality)
  *  - decodeSpeed, indicates how to tune the encoder regarding decode speed (0 gives better speed but worst quality)
  *  - encodeSpeed, indicates how to tune the encoder parameters (0 gives better speed but worst quality)
  *  - encodeSpeed, indicates how to tune the encoder parameters (0 gives better speed but worst quality)
  *  - encoderMethod
  *  - encoderMethod
- *  - quantization, indicates the presision of each type of data stored in the draco file
+ *  - quantization, indicates the presision of each type of data stored in the draco file in the order (POSITION, NORMAL, COLOR, TEX_COORD, GENERIC)
+ *  - exportUvs
+ *  - exportNormals
  *
  *
  * @class DRACOExporter
  * @class DRACOExporter
  * @author tentone
  * @author tentone
@@ -23,6 +25,7 @@ THREE.DRACOExporter.prototype = {
 
 
 	parse: function ( geometry, options ) {
 	parse: function ( geometry, options ) {
 
 
+
 		if ( DracoEncoderModule === undefined ) {
 		if ( DracoEncoderModule === undefined ) {
 
 
 			throw new Error( 'THREE.DRACOExporter: required the draco_decoder to work.' );
 			throw new Error( 'THREE.DRACOExporter: required the draco_decoder to work.' );
@@ -36,15 +39,16 @@ THREE.DRACOExporter.prototype = {
 				decodeSpeed: 5,
 				decodeSpeed: 5,
 				encodeSpeed: 5,
 				encodeSpeed: 5,
 				encoderMethod: THREE.DRACOExporter.MESH_EDGEBREAKER_ENCODING,
 				encoderMethod: THREE.DRACOExporter.MESH_EDGEBREAKER_ENCODING,
-				quantization: [ 16, 8, 8, 8, 16 ]
+				quantization: [ 16, 8, 8, 8, 8 ],
+				exportUvs: true,
+				exportNormals: true,
+				exportColor: false
 
 
 			};
 			};
 
 
 		}
 		}
 
 
-
 		var dracoEncoder = DracoEncoderModule();
 		var dracoEncoder = DracoEncoderModule();
-
 		var encoder = new dracoEncoder.Encoder();
 		var encoder = new dracoEncoder.Encoder();
 		var builder = new dracoEncoder.MeshBuilder();
 		var builder = new dracoEncoder.MeshBuilder();
 		var mesh = new dracoEncoder.Mesh();
 		var mesh = new dracoEncoder.Mesh();
@@ -52,179 +56,57 @@ THREE.DRACOExporter.prototype = {
 		if ( geometry.isBufferGeometry === true ) {
 		if ( geometry.isBufferGeometry === true ) {
 
 
 			var vertices = geometry.getAttribute( 'position' );
 			var vertices = geometry.getAttribute( 'position' );
-			var faces = geometry.getIndex();
-			var normals = geometry.getAttribute( 'normal' );
-			var uvs = geometry.getAttribute( 'uv' );
-
-			console.log(vertices, faces, normals, uvs);
-
-			return new THREE.Geometry();
-
-			var numFaces = faces.length;
-			var numPoints = vertices.length;
-			var numIndices = numFaces * 3;
-
-			var indices = new Uint32Array( numIndices );
-			var vertices = new Float32Array( geometry.vertices.length * 3 );
-			var normals = new Float32Array( geometry.vertices.length * 3 );
-
-			// Faces
-
-			for ( var i = 0; i < numFaces; i ++ ) {
-
-				var index = i * 3;
-				indices[ index ] = geometry.faces[ i ].a;
-				indices[ index + 1 ] = geometry.faces[ i ].b;
-				indices[ index + 2 ] = geometry.faces[ i ].c;
-
-			}
-
-			builder.AddFacesToMesh( mesh, numFaces, indices );
-
-			// Vertex
-
-			for ( var i = 0; i < geometry.vertices.length; i ++ ) {
-
-				var index = i * 3;
-				vertices[ index ] = geometry.vertices[ i ].x;
-				vertices[ index + 1 ] = geometry.vertices[ i ].y;
-				vertices[ index + 2 ] = geometry.vertices[ i ].z;
-
-			}
-
-			builder.AddFloatAttributeToMesh( mesh, dracoEncoder.POSITION, numPoints, 3, vertices );
-
-			// Normals
-
-			for ( var face of geometry.faces ) {
-
-				normals[ face[ 'a' ] * 3 ] = face.vertexNormals[ 0 ].x;
-				normals[ ( face[ 'a' ] * 3 ) + 1 ] = face.vertexNormals[ 0 ].y;
-				normals[ ( face[ 'a' ] * 3 ) + 2 ] = face.vertexNormals[ 0 ].z;
-
-				normals[ face[ 'b' ] * 3 ] = face.vertexNormals[ 1 ].x;
-				normals[ ( face[ 'b' ] * 3 ) + 1 ] = face.vertexNormals[ 1 ].y;
-				normals[ ( face[ 'b' ] * 3 ) + 2 ] = face.vertexNormals[ 1 ].z;
-
-				normals[ face[ 'c' ] * 3 ] = face.vertexNormals[ 2 ].x;
-				normals[ ( face[ 'c' ] * 3 ) + 1 ] = face.vertexNormals[ 2 ].y;
-				normals[ ( face[ 'c' ] * 3 ) + 2 ] = face.vertexNormals[ 2 ].z;
-
-			}
-
-			builder.AddFloatAttributeToMesh( mesh, dracoEncoder.NORMAL, numPoints, 3, normals );
-
-			// vertices
-
-			if ( vertices !== undefined ) {
-
-				for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
-
-					vertex.x = vertices.getX( i );
-					vertex.y = vertices.getY( i );
-					vertex.z = vertices.getZ( i );
-
-					// transfrom the vertex to world space
-					vertex.applyMatrix4( mesh.matrixWorld );
-
-					// transform the vertex to export format
-					output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
-
-				}
-
-			}
+			builder.AddFloatAttributeToMesh( mesh, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
 
 
-			// uvs
+			var faces = geometry.getIndex();
+			builder.AddFacesToMesh( mesh, faces.count, faces.array );
 
 
-			if ( uvs !== undefined ) {
+			if ( options.exportNormals === true ) {
 
 
-				for ( i = 0, l = uvs.count; i < l; i ++, nbVertexUvs ++ ) {
+				var normals = geometry.getAttribute( 'normal' );
 
 
-					uv.x = uvs.getX( i );
-					uv.y = uvs.getY( i );
+				if ( normals !== undefined ) {
 
 
-					// transform the uv to export format
-					output += 'vt ' + uv.x + ' ' + uv.y + '\n';
+					builder.AddFloatAttributeToMesh( mesh, dracoEncoder.NORMAL, normals.count, normals.itemSize, normals.array );
 
 
 				}
 				}
 
 
 			}
 			}
 
 
-			// normals
-
-			if ( normals !== undefined ) {
-
-				normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
-
-				for ( i = 0, l = normals.count; i < l; i ++, nbNormals ++ ) {
+			if ( options.exportUvs === true ) {
 
 
-					normal.x = normals.getX( i );
-					normal.y = normals.getY( i );
-					normal.z = normals.getZ( i );
+				var uvs = geometry.getAttribute( 'uv' );
 
 
-					// transfrom the normal to world space
-					normal.applyMatrix3( normalMatrixWorld );
+				if ( uvs !== undefined ) {
 
 
-					// transform the normal to export format
-					output += 'vn ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
+					builder.AddFloatAttributeToMesh( mesh, dracoEncoder.TEX_COORD, uvs.count, uvs.itemSize, uvs.array );
 
 
 				}
 				}
 
 
 			}
 			}
 
 
-			// faces
-
-			if ( indices !== null ) {
-
-				for ( i = 0, l = indices.count; i < l; i += 3 ) {
-
-					for ( m = 0; m < 3; m ++ ) {
-
-						j = indices.getX( i + m ) + 1;
-
-						face[ m ] = ( indexVertex + j ) + ( normals || uvs ? '/' + ( uvs ? ( indexVertexUvs + j ) : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
-
-					}
-
-					// transform the face to export format
-					output += 'f ' + face.join( ' ' ) + "\n";
-
-				}
-
-			} else {
+			if ( options.exportColor === true ) {
 
 
-				for ( i = 0, l = vertices.count; i < l; i += 3 ) {
+				var colors = geometry.getAttribute( 'color' );
 
 
-					for ( m = 0; m < 3; m ++ ) {
+				if ( colors !== undefined ) {
 
 
-						j = i + m + 1;
-
-						face[ m ] = ( indexVertex + j ) + ( normals || uvs ? '/' + ( uvs ? ( indexVertexUvs + j ) : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
-
-					}
-
-					// transform the face to export format
-					output += 'f ' + face.join( ' ' ) + "\n";
+					builder.AddFloatAttributeToMesh( mesh, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array );
 
 
 				}
 				}
 
 
 			}
 			}
 
 
-			/*
-			var convert = new THREE.Geometry();
-			convert.fromBufferGeometry( geometry );
-			geometry = convert;
+		} else if ( geometry.isGeometry === true ) {
 
 
 			var numFaces = geometry.faces.length;
 			var numFaces = geometry.faces.length;
 			var numPoints = geometry.vertices.length;
 			var numPoints = geometry.vertices.length;
 			var numIndices = numFaces * 3;
 			var numIndices = numFaces * 3;
 
 
-			var indices = new Uint32Array( numIndices );
-			var vertices = new Float32Array( geometry.vertices.length * 3 );
-			var normals = new Float32Array( geometry.vertices.length * 3 );
-
 			// Faces
 			// Faces
 
 
+			var indices = new Uint32Array( numIndices );
+
 			for ( var i = 0; i < numFaces; i ++ ) {
 			for ( var i = 0; i < numFaces; i ++ ) {
 
 
 				var index = i * 3;
 				var index = i * 3;
@@ -238,6 +120,8 @@ THREE.DRACOExporter.prototype = {
 
 
 			// Vertex
 			// Vertex
 
 
+			var vertices = new Float32Array( geometry.vertices.length * 3 );
+
 			for ( var i = 0; i < geometry.vertices.length; i ++ ) {
 			for ( var i = 0; i < geometry.vertices.length; i ++ ) {
 
 
 				var index = i * 3;
 				var index = i * 3;
@@ -251,80 +135,66 @@ THREE.DRACOExporter.prototype = {
 
 
 			// Normals
 			// Normals
 
 
-			for ( var face of geometry.faces ) {
+			if ( options.exportNormals === true ) {
 
 
-				normals[ face[ 'a' ] * 3 ] = face.vertexNormals[ 0 ].x;
-				normals[ ( face[ 'a' ] * 3 ) + 1 ] = face.vertexNormals[ 0 ].y;
-				normals[ ( face[ 'a' ] * 3 ) + 2 ] = face.vertexNormals[ 0 ].z;
+				var normals = new Float32Array( geometry.vertices.length * 3 );
 
 
-				normals[ face[ 'b' ] * 3 ] = face.vertexNormals[ 1 ].x;
-				normals[ ( face[ 'b' ] * 3 ) + 1 ] = face.vertexNormals[ 1 ].y;
-				normals[ ( face[ 'b' ] * 3 ) + 2 ] = face.vertexNormals[ 1 ].z;
+				for ( var face of geometry.faces ) {
 
 
-				normals[ face[ 'c' ] * 3 ] = face.vertexNormals[ 2 ].x;
-				normals[ ( face[ 'c' ] * 3 ) + 1 ] = face.vertexNormals[ 2 ].y;
-				normals[ ( face[ 'c' ] * 3 ) + 2 ] = face.vertexNormals[ 2 ].z;
+					normals[ face.a * 3 ] = face.vertexNormals[ 0 ].x;
+					normals[ ( face.a * 3 ) + 1 ] = face.vertexNormals[ 0 ].y;
+					normals[ ( face.a * 3 ) + 2 ] = face.vertexNormals[ 0 ].z;
 
 
-			}
-
-			builder.AddFloatAttributeToMesh( mesh, dracoEncoder.NORMAL, numPoints, 3, normals );
-			*/
+					normals[ face.b * 3 ] = face.vertexNormals[ 1 ].x;
+					normals[ ( face.b * 3 ) + 1 ] = face.vertexNormals[ 1 ].y;
+					normals[ ( face.b * 3 ) + 2 ] = face.vertexNormals[ 1 ].z;
 
 
-		} else if ( geometry.isGeometry === true ) {
+					normals[ face.c * 3 ] = face.vertexNormals[ 2 ].x;
+					normals[ ( face.c * 3 ) + 1 ] = face.vertexNormals[ 2 ].y;
+					normals[ ( face.c * 3 ) + 2 ] = face.vertexNormals[ 2 ].z;
 
 
-			var numFaces = geometry.faces.length;
-			var numPoints = geometry.vertices.length;
-			var numIndices = numFaces * 3;
+				}
 
 
-			var indices = new Uint32Array( numIndices );
-			var vertices = new Float32Array( geometry.vertices.length * 3 );
-			var normals = new Float32Array( geometry.vertices.length * 3 );
+				builder.AddFloatAttributeToMesh( mesh, dracoEncoder.NORMAL, numPoints, 3, normals );
 
 
-			// Faces
+			}
 
 
-			for ( var i = 0; i < numFaces; i ++ ) {
+			// Uvs
 
 
-				var index = i * 3;
-				indices[ index ] = geometry.faces[ i ].a;
-				indices[ index + 1 ] = geometry.faces[ i ].b;
-				indices[ index + 2 ] = geometry.faces[ i ].c;
+			if ( options.exportUvs === true ) {
 
 
-			}
+				var uvs = new Uint32Array( geometry.vertices.length * 2 );
 
 
-			builder.AddFacesToMesh( mesh, numFaces, indices );
+				for ( var i = 0; i < geometry.faceVertexUvs.length; i ++ ) {
 
 
-			// Vertex
+					var index = i * 2;
+					uvs[ index ] = geometry.faceVertexUvs[ i ].x;
+					uvs[ index + 1 ] = geometry.faceVertexUvs[ i ].y;
 
 
-			for ( var i = 0; i < geometry.vertices.length; i ++ ) {
+				}
 
 
-				var index = i * 3;
-				vertices[ index ] = geometry.vertices[ i ].x;
-				vertices[ index + 1 ] = geometry.vertices[ i ].y;
-				vertices[ index + 2 ] = geometry.vertices[ i ].z;
+				builder.AddFloatAttributeToMesh( mesh, dracoEncoder.TEX_COORD, numPoints, 2, uvs );
 
 
 			}
 			}
 
 
-			builder.AddFloatAttributeToMesh( mesh, dracoEncoder.POSITION, numPoints, 3, vertices );
+			// Color
 
 
-			// Normals
+			if ( options.exportColor === true ) {
 
 
-			for ( var face of geometry.faces ) {
+				var colors = new Uint32Array( geometry.vertices.length * 3 );
 
 
-				normals[ face[ 'a' ] * 3 ] = face.vertexNormals[ 0 ].x;
-				normals[ ( face[ 'a' ] * 3 ) + 1 ] = face.vertexNormals[ 0 ].y;
-				normals[ ( face[ 'a' ] * 3 ) + 2 ] = face.vertexNormals[ 0 ].z;
+				for ( var i = 0; i < geometry.colors.length; i ++ ) {
 
 
-				normals[ face[ 'b' ] * 3 ] = face.vertexNormals[ 1 ].x;
-				normals[ ( face[ 'b' ] * 3 ) + 1 ] = face.vertexNormals[ 1 ].y;
-				normals[ ( face[ 'b' ] * 3 ) + 2 ] = face.vertexNormals[ 1 ].z;
+					var index = i * 3;
+					colors[ index ] = geometry.colors[ i ].x;
+					colors[ index + 1 ] = geometry.colors[ i ].y;
+					colors[ index + 2 ] = geometry.colors[ i ].z;
 
 
-				normals[ face[ 'c' ] * 3 ] = face.vertexNormals[ 2 ].x;
-				normals[ ( face[ 'c' ] * 3 ) + 1 ] = face.vertexNormals[ 2 ].y;
-				normals[ ( face[ 'c' ] * 3 ) + 2 ] = face.vertexNormals[ 2 ].z;
+				}
 
 
-			}
+				builder.AddFloatAttributeToMesh( mesh, dracoEncoder.COLOR, numPoints, 3, colors );
 
 
-			builder.AddFloatAttributeToMesh( mesh, dracoEncoder.NORMAL, numPoints, 3, normals );
+			}
 
 
 		}
 		}
 
 

+ 4 - 3
examples/misc_exporter_draco.html

@@ -36,7 +36,7 @@
 		<script src="js/libs/draco/draco_encoder.js"></script>
 		<script src="js/libs/draco/draco_encoder.js"></script>
 
 
 		<script src="js/controls/OrbitControls.js"></script>
 		<script src="js/controls/OrbitControls.js"></script>
-		<script src="js/exporters/DracoExporter.js"></script>
+		<script src="js/exporters/DRACOExporter.js"></script>
 
 
 		<script src="js/WebGL.js"></script>
 		<script src="js/WebGL.js"></script>
 
 
@@ -62,7 +62,7 @@
 				scene.background = new THREE.Color( 0xa0a0a0 );
 				scene.background = new THREE.Color( 0xa0a0a0 );
 				scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
 				scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
 
 
-				exporter = new THREE.DracoExporter();
+				exporter = new THREE.DRACOExporter();
 
 
 				//
 				//
 
 
@@ -93,7 +93,8 @@
 
 
 				// export mesh
 				// export mesh
 
 
-				var geometry = new THREE.TorusKnotGeometry( 50, 15, 200, 30 );
+				var geometry = new THREE.TorusKnotBufferGeometry( 50, 15, 200, 30 );
+				//var geometry = new THREE.TorusKnotGeometry( 50, 15, 200, 30 );
 				var material = new THREE.MeshPhongMaterial( { color: 0x00ff00 } );
 				var material = new THREE.MeshPhongMaterial( { color: 0x00ff00 } );
 
 
 				mesh = new THREE.Mesh( geometry, material );
 				mesh = new THREE.Mesh( geometry, material );