|
@@ -1,11 +1,6 @@
|
|
|
import {
|
|
|
- BufferGeometry,
|
|
|
Color,
|
|
|
- Geometry,
|
|
|
- Line,
|
|
|
Matrix3,
|
|
|
- Mesh,
|
|
|
- Points,
|
|
|
Vector2,
|
|
|
Vector3
|
|
|
} from '../../../build/three.module.js';
|
|
@@ -41,129 +36,121 @@ OBJExporter.prototype = {
|
|
|
|
|
|
var normalMatrixWorld = new Matrix3();
|
|
|
|
|
|
- if ( geometry instanceof Geometry ) {
|
|
|
+ if ( geometry.isBufferGeometry !== true ) {
|
|
|
|
|
|
- geometry = new BufferGeometry().setFromObject( mesh );
|
|
|
+ throw new Error( 'THREE.OBJExporter: Geometry is not of type THREE.BufferGeometry.' );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( geometry instanceof BufferGeometry ) {
|
|
|
+ // shortcuts
|
|
|
+ var vertices = geometry.getAttribute( 'position' );
|
|
|
+ var normals = geometry.getAttribute( 'normal' );
|
|
|
+ var uvs = geometry.getAttribute( 'uv' );
|
|
|
+ var indices = geometry.getIndex();
|
|
|
|
|
|
- // shortcuts
|
|
|
- var vertices = geometry.getAttribute( 'position' );
|
|
|
- var normals = geometry.getAttribute( 'normal' );
|
|
|
- var uvs = geometry.getAttribute( 'uv' );
|
|
|
- var indices = geometry.getIndex();
|
|
|
+ // name of the mesh object
|
|
|
+ output += 'o ' + mesh.name + '\n';
|
|
|
|
|
|
- // name of the mesh object
|
|
|
- output += 'o ' + mesh.name + '\n';
|
|
|
+ // name of the mesh material
|
|
|
+ if ( mesh.material && mesh.material.name ) {
|
|
|
|
|
|
- // name of the mesh material
|
|
|
- if ( mesh.material && mesh.material.name ) {
|
|
|
+ output += 'usemtl ' + mesh.material.name + '\n';
|
|
|
|
|
|
- output += 'usemtl ' + mesh.material.name + '\n';
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- // vertices
|
|
|
+ }
|
|
|
|
|
|
- if ( vertices !== undefined ) {
|
|
|
+ // vertices
|
|
|
|
|
|
- for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
|
|
|
+ if ( vertices !== undefined ) {
|
|
|
|
|
|
- vertex.x = vertices.getX( i );
|
|
|
- vertex.y = vertices.getY( i );
|
|
|
- vertex.z = vertices.getZ( i );
|
|
|
+ for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
|
|
|
|
|
|
- // transform the vertex to world space
|
|
|
- vertex.applyMatrix4( mesh.matrixWorld );
|
|
|
+ vertex.x = vertices.getX( i );
|
|
|
+ vertex.y = vertices.getY( i );
|
|
|
+ vertex.z = vertices.getZ( i );
|
|
|
|
|
|
- // transform the vertex to export format
|
|
|
- output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
|
|
|
+ // transform the vertex to world space
|
|
|
+ vertex.applyMatrix4( mesh.matrixWorld );
|
|
|
|
|
|
- }
|
|
|
+ // transform the vertex to export format
|
|
|
+ output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
|
|
|
|
|
|
}
|
|
|
|
|
|
- // uvs
|
|
|
+ }
|
|
|
|
|
|
- if ( uvs !== undefined ) {
|
|
|
+ // uvs
|
|
|
|
|
|
- for ( i = 0, l = uvs.count; i < l; i ++, nbVertexUvs ++ ) {
|
|
|
+ if ( uvs !== undefined ) {
|
|
|
|
|
|
- uv.x = uvs.getX( i );
|
|
|
- uv.y = uvs.getY( i );
|
|
|
+ for ( i = 0, l = uvs.count; i < l; i ++, nbVertexUvs ++ ) {
|
|
|
|
|
|
- // transform the uv to export format
|
|
|
- output += 'vt ' + uv.x + ' ' + uv.y + '\n';
|
|
|
+ uv.x = uvs.getX( i );
|
|
|
+ uv.y = uvs.getY( i );
|
|
|
|
|
|
- }
|
|
|
+ // transform the uv to export format
|
|
|
+ output += 'vt ' + uv.x + ' ' + uv.y + '\n';
|
|
|
|
|
|
}
|
|
|
|
|
|
- // normals
|
|
|
+ }
|
|
|
|
|
|
- if ( normals !== undefined ) {
|
|
|
+ // normals
|
|
|
|
|
|
- normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
|
|
|
+ if ( normals !== undefined ) {
|
|
|
|
|
|
- for ( i = 0, l = normals.count; i < l; i ++, nbNormals ++ ) {
|
|
|
+ normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
|
|
|
|
|
|
- normal.x = normals.getX( i );
|
|
|
- normal.y = normals.getY( i );
|
|
|
- normal.z = normals.getZ( i );
|
|
|
+ for ( i = 0, l = normals.count; i < l; i ++, nbNormals ++ ) {
|
|
|
|
|
|
- // transform the normal to world space
|
|
|
- normal.applyMatrix3( normalMatrixWorld ).normalize();
|
|
|
+ normal.x = normals.getX( i );
|
|
|
+ normal.y = normals.getY( i );
|
|
|
+ normal.z = normals.getZ( i );
|
|
|
|
|
|
- // transform the normal to export format
|
|
|
- output += 'vn ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
|
|
|
+ // transform the normal to world space
|
|
|
+ normal.applyMatrix3( normalMatrixWorld ).normalize();
|
|
|
|
|
|
- }
|
|
|
+ // transform the normal to export format
|
|
|
+ output += 'vn ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
|
|
|
|
|
|
}
|
|
|
|
|
|
- // faces
|
|
|
-
|
|
|
- if ( indices !== null ) {
|
|
|
+ }
|
|
|
|
|
|
- for ( i = 0, l = indices.count; i < l; i += 3 ) {
|
|
|
+ // faces
|
|
|
|
|
|
- for ( m = 0; m < 3; m ++ ) {
|
|
|
+ if ( indices !== null ) {
|
|
|
|
|
|
- j = indices.getX( i + m ) + 1;
|
|
|
+ for ( i = 0, l = indices.count; i < l; i += 3 ) {
|
|
|
|
|
|
- face[ m ] = ( indexVertex + j ) + ( normals || uvs ? '/' + ( uvs ? ( indexVertexUvs + j ) : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
|
|
|
+ for ( m = 0; m < 3; m ++ ) {
|
|
|
|
|
|
- }
|
|
|
+ j = indices.getX( i + m ) + 1;
|
|
|
|
|
|
- // transform the face to export format
|
|
|
- output += 'f ' + face.join( ' ' ) + '\n';
|
|
|
+ face[ m ] = ( indexVertex + j ) + ( normals || uvs ? '/' + ( uvs ? ( indexVertexUvs + j ) : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
|
|
|
|
|
|
}
|
|
|
|
|
|
- } else {
|
|
|
+ // transform the face to export format
|
|
|
+ output += 'f ' + face.join( ' ' ) + '\n';
|
|
|
|
|
|
- for ( i = 0, l = vertices.count; i < l; i += 3 ) {
|
|
|
+ }
|
|
|
|
|
|
- for ( m = 0; m < 3; m ++ ) {
|
|
|
+ } else {
|
|
|
|
|
|
- j = i + m + 1;
|
|
|
+ for ( i = 0, l = vertices.count; i < l; i += 3 ) {
|
|
|
|
|
|
- face[ m ] = ( indexVertex + j ) + ( normals || uvs ? '/' + ( uvs ? ( indexVertexUvs + j ) : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
|
|
|
+ for ( m = 0; m < 3; m ++ ) {
|
|
|
|
|
|
- }
|
|
|
+ j = i + m + 1;
|
|
|
|
|
|
- // transform the face to export format
|
|
|
- output += 'f ' + face.join( ' ' ) + '\n';
|
|
|
+ face[ m ] = ( indexVertex + j ) + ( normals || uvs ? '/' + ( uvs ? ( indexVertexUvs + j ) : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- } else {
|
|
|
+ // transform the face to export format
|
|
|
+ output += 'f ' + face.join( ' ' ) + '\n';
|
|
|
|
|
|
- console.warn( 'THREE.OBJExporter.parseMesh(): geometry type unsupported', geometry );
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
@@ -181,65 +168,57 @@ OBJExporter.prototype = {
|
|
|
var geometry = line.geometry;
|
|
|
var type = line.type;
|
|
|
|
|
|
- if ( geometry instanceof Geometry ) {
|
|
|
+ if ( geometry.isBufferGeometry !== true ) {
|
|
|
|
|
|
- geometry = new BufferGeometry().setFromObject( line );
|
|
|
+ throw new Error( 'THREE.OBJExporter: Geometry is not of type THREE.BufferGeometry.' );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( geometry instanceof BufferGeometry ) {
|
|
|
-
|
|
|
- // shortcuts
|
|
|
- var vertices = geometry.getAttribute( 'position' );
|
|
|
+ // shortcuts
|
|
|
+ var vertices = geometry.getAttribute( 'position' );
|
|
|
|
|
|
- // name of the line object
|
|
|
- output += 'o ' + line.name + '\n';
|
|
|
+ // name of the line object
|
|
|
+ output += 'o ' + line.name + '\n';
|
|
|
|
|
|
- if ( vertices !== undefined ) {
|
|
|
+ if ( vertices !== undefined ) {
|
|
|
|
|
|
- for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
|
|
|
+ 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 );
|
|
|
+ vertex.x = vertices.getX( i );
|
|
|
+ vertex.y = vertices.getY( i );
|
|
|
+ vertex.z = vertices.getZ( i );
|
|
|
|
|
|
- // transform the vertex to world space
|
|
|
- vertex.applyMatrix4( line.matrixWorld );
|
|
|
+ // transform the vertex to world space
|
|
|
+ vertex.applyMatrix4( line.matrixWorld );
|
|
|
|
|
|
- // transform the vertex to export format
|
|
|
- output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
|
|
|
-
|
|
|
- }
|
|
|
+ // transform the vertex to export format
|
|
|
+ output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( type === 'Line' ) {
|
|
|
-
|
|
|
- output += 'l ';
|
|
|
+ }
|
|
|
|
|
|
- for ( j = 1, l = vertices.count; j <= l; j ++ ) {
|
|
|
+ if ( type === 'Line' ) {
|
|
|
|
|
|
- output += ( indexVertex + j ) + ' ';
|
|
|
+ output += 'l ';
|
|
|
|
|
|
- }
|
|
|
+ for ( j = 1, l = vertices.count; j <= l; j ++ ) {
|
|
|
|
|
|
- output += '\n';
|
|
|
+ output += ( indexVertex + j ) + ' ';
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( type === 'LineSegments' ) {
|
|
|
-
|
|
|
- for ( j = 1, k = j + 1, l = vertices.count; j < l; j += 2, k = j + 1 ) {
|
|
|
+ output += '\n';
|
|
|
|
|
|
- output += 'l ' + ( indexVertex + j ) + ' ' + ( indexVertex + k ) + '\n';
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( type === 'LineSegments' ) {
|
|
|
|
|
|
- }
|
|
|
+ for ( j = 1, k = j + 1, l = vertices.count; j < l; j += 2, k = j + 1 ) {
|
|
|
|
|
|
- } else {
|
|
|
+ output += 'l ' + ( indexVertex + j ) + ' ' + ( indexVertex + k ) + '\n';
|
|
|
|
|
|
- console.warn( 'THREE.OBJExporter.parseLine(): geometry type unsupported', geometry );
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
@@ -254,58 +233,50 @@ OBJExporter.prototype = {
|
|
|
|
|
|
var geometry = points.geometry;
|
|
|
|
|
|
- if ( geometry instanceof Geometry ) {
|
|
|
+ if ( geometry.isBufferGeometry !== true ) {
|
|
|
|
|
|
- geometry = new BufferGeometry().setFromObject( points );
|
|
|
+ throw new Error( 'THREE.OBJExporter: Geometry is not of type THREE.BufferGeometry.' );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( geometry instanceof BufferGeometry ) {
|
|
|
-
|
|
|
- var vertices = geometry.getAttribute( 'position' );
|
|
|
- var colors = geometry.getAttribute( 'color' );
|
|
|
-
|
|
|
- output += 'o ' + points.name + '\n';
|
|
|
-
|
|
|
- if ( vertices !== undefined ) {
|
|
|
+ var vertices = geometry.getAttribute( 'position' );
|
|
|
+ var colors = geometry.getAttribute( 'color' );
|
|
|
|
|
|
- for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
|
|
|
+ output += 'o ' + points.name + '\n';
|
|
|
|
|
|
- vertex.fromBufferAttribute( vertices, i );
|
|
|
- vertex.applyMatrix4( points.matrixWorld );
|
|
|
+ if ( vertices !== undefined ) {
|
|
|
|
|
|
- output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z;
|
|
|
+ for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
|
|
|
|
|
|
- if ( colors !== undefined ) {
|
|
|
+ vertex.fromBufferAttribute( vertices, i );
|
|
|
+ vertex.applyMatrix4( points.matrixWorld );
|
|
|
|
|
|
- color.fromBufferAttribute( colors, i );
|
|
|
+ output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z;
|
|
|
|
|
|
- output += ' ' + color.r + ' ' + color.g + ' ' + color.b;
|
|
|
+ if ( colors !== undefined ) {
|
|
|
|
|
|
- }
|
|
|
+ color.fromBufferAttribute( colors, i );
|
|
|
|
|
|
- output += '\n';
|
|
|
+ output += ' ' + color.r + ' ' + color.g + ' ' + color.b;
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- output += 'p ';
|
|
|
-
|
|
|
- for ( j = 1, l = vertices.count; j <= l; j ++ ) {
|
|
|
-
|
|
|
- output += ( indexVertex + j ) + ' ';
|
|
|
+ output += '\n';
|
|
|
|
|
|
}
|
|
|
|
|
|
- output += '\n';
|
|
|
+ }
|
|
|
|
|
|
- } else {
|
|
|
+ output += 'p ';
|
|
|
+
|
|
|
+ for ( j = 1, l = vertices.count; j <= l; j ++ ) {
|
|
|
|
|
|
- console.warn( 'THREE.OBJExporter.parsePoints(): geometry type unsupported', geometry );
|
|
|
+ output += ( indexVertex + j ) + ' ';
|
|
|
|
|
|
}
|
|
|
|
|
|
+ output += '\n';
|
|
|
+
|
|
|
// update index
|
|
|
indexVertex += nbVertex;
|
|
|
|
|
@@ -313,19 +284,19 @@ OBJExporter.prototype = {
|
|
|
|
|
|
object.traverse( function ( child ) {
|
|
|
|
|
|
- if ( child instanceof Mesh ) {
|
|
|
+ if ( child.isMesh === true ) {
|
|
|
|
|
|
parseMesh( child );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( child instanceof Line ) {
|
|
|
+ if ( child.isLine === true ) {
|
|
|
|
|
|
parseLine( child );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( child instanceof Points ) {
|
|
|
+ if ( child.isPoints === true ) {
|
|
|
|
|
|
parsePoints( child );
|
|
|
|