|
@@ -9,7 +9,9 @@
|
|
|
* - 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)
|
|
|
* - 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
|
|
|
* @author tentone
|
|
@@ -23,6 +25,7 @@ THREE.DRACOExporter.prototype = {
|
|
|
|
|
|
parse: function ( geometry, options ) {
|
|
|
|
|
|
+
|
|
|
if ( DracoEncoderModule === undefined ) {
|
|
|
|
|
|
throw new Error( 'THREE.DRACOExporter: required the draco_decoder to work.' );
|
|
@@ -36,15 +39,16 @@ THREE.DRACOExporter.prototype = {
|
|
|
decodeSpeed: 5,
|
|
|
encodeSpeed: 5,
|
|
|
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 encoder = new dracoEncoder.Encoder();
|
|
|
var builder = new dracoEncoder.MeshBuilder();
|
|
|
var mesh = new dracoEncoder.Mesh();
|
|
@@ -52,179 +56,57 @@ THREE.DRACOExporter.prototype = {
|
|
|
if ( geometry.isBufferGeometry === true ) {
|
|
|
|
|
|
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 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 );
|
|
|
-
|
|
|
// Faces
|
|
|
|
|
|
+ var indices = new Uint32Array( numIndices );
|
|
|
+
|
|
|
for ( var i = 0; i < numFaces; i ++ ) {
|
|
|
|
|
|
var index = i * 3;
|
|
@@ -238,6 +120,8 @@ THREE.DRACOExporter.prototype = {
|
|
|
|
|
|
// Vertex
|
|
|
|
|
|
+ var vertices = new Float32Array( geometry.vertices.length * 3 );
|
|
|
+
|
|
|
for ( var i = 0; i < geometry.vertices.length; i ++ ) {
|
|
|
|
|
|
var index = i * 3;
|
|
@@ -251,80 +135,66 @@ THREE.DRACOExporter.prototype = {
|
|
|
|
|
|
// 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 );
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|