|
@@ -1,7 +1,6 @@
|
|
/**
|
|
/**
|
|
* @author kovacsv / http://kovacsv.hu/
|
|
* @author kovacsv / http://kovacsv.hu/
|
|
* @author mrdoob / http://mrdoob.com/
|
|
* @author mrdoob / http://mrdoob.com/
|
|
- * @author mudcube / http://mudcu.be/
|
|
|
|
*/
|
|
*/
|
|
|
|
|
|
THREE.STLExporter = function () {};
|
|
THREE.STLExporter = function () {};
|
|
@@ -15,92 +14,57 @@ THREE.STLExporter.prototype = {
|
|
var vector = new THREE.Vector3();
|
|
var vector = new THREE.Vector3();
|
|
var normalMatrixWorld = new THREE.Matrix3();
|
|
var normalMatrixWorld = new THREE.Matrix3();
|
|
|
|
|
|
- return function ( scene, encodeBinary ) {
|
|
|
|
|
|
+ return function ( scene ) {
|
|
|
|
|
|
- if ( encodeBinary ) {
|
|
|
|
- var triangles = 0;
|
|
|
|
- scene.traverse( function ( object ) {
|
|
|
|
- if ( !(object instanceof THREE.Mesh) ) return;
|
|
|
|
- triangles += object.geometry.faces.length;
|
|
|
|
- });
|
|
|
|
|
|
+ var output = '';
|
|
|
|
|
|
- var offset = 80; // skip header
|
|
|
|
- var bufferLength = triangles * 2 + triangles * 3 * 4 * 4 + 80 + 4;
|
|
|
|
- var arrayBuffer = new ArrayBuffer(bufferLength);
|
|
|
|
- var output = new DataView(arrayBuffer);
|
|
|
|
- output.setUint32(offset, triangles, true); offset += 4;
|
|
|
|
- } else {
|
|
|
|
- var output = 'solid exported\n';
|
|
|
|
- }
|
|
|
|
|
|
+ output += 'solid exported\n';
|
|
|
|
|
|
scene.traverse( function ( object ) {
|
|
scene.traverse( function ( object ) {
|
|
|
|
|
|
- if ( !(object instanceof THREE.Mesh) ) return;
|
|
|
|
- if ( !(object.geometry instanceof THREE.Geometry )) return;
|
|
|
|
|
|
+ if ( object instanceof THREE.Mesh ) {
|
|
|
|
|
|
- var geometry = object.geometry;
|
|
|
|
- var matrixWorld = object.matrixWorld;
|
|
|
|
|
|
+ var geometry = object.geometry;
|
|
|
|
+ var matrixWorld = object.matrixWorld;
|
|
|
|
|
|
- var vertices = geometry.vertices;
|
|
|
|
- var faces = geometry.faces;
|
|
|
|
|
|
+ if ( geometry instanceof THREE.Geometry ) {
|
|
|
|
|
|
- normalMatrixWorld.getNormalMatrix( matrixWorld );
|
|
|
|
|
|
+ var vertices = geometry.vertices;
|
|
|
|
+ var faces = geometry.faces;
|
|
|
|
|
|
- for ( var i = 0, l = faces.length; i < l; i ++ ) {
|
|
|
|
|
|
+ normalMatrixWorld.getNormalMatrix( matrixWorld );
|
|
|
|
|
|
- var face = faces[ i ];
|
|
|
|
|
|
+ for ( var i = 0, l = faces.length; i < l; i ++ ) {
|
|
|
|
|
|
- vector.copy( face.normal ).applyMatrix3( normalMatrixWorld ).normalize();
|
|
|
|
|
|
+ var face = faces[ i ];
|
|
|
|
|
|
- if ( encodeBinary ) {
|
|
|
|
|
|
+ vector.copy( face.normal ).applyMatrix3( normalMatrixWorld ).normalize();
|
|
|
|
|
|
- output.setFloat32(offset, vector.x, true); offset += 4; // normal
|
|
|
|
- output.setFloat32(offset, vector.y, true); offset += 4;
|
|
|
|
- output.setFloat32(offset, vector.z, true); offset += 4;
|
|
|
|
|
|
+ output += '\tfacet normal ' + vector.x + ' ' + vector.y + ' ' + vector.z + '\n';
|
|
|
|
+ output += '\t\touter loop\n';
|
|
|
|
|
|
- } else {
|
|
|
|
|
|
+ var indices = [ face.a, face.b, face.c ];
|
|
|
|
|
|
- output += '\tfacet normal ' + vector.x + ' ' + vector.y + ' ' + vector.z + '\n';
|
|
|
|
- output += '\t\touter loop\n';
|
|
|
|
|
|
+ for ( var j = 0; j < 3; j ++ ) {
|
|
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- var indices = [ face.a, face.b, face.c ];
|
|
|
|
-
|
|
|
|
- for ( var j = 0; j < 3; j ++ ) {
|
|
|
|
|
|
+ vector.copy( vertices[ indices[ j ] ] ).applyMatrix4( matrixWorld );
|
|
|
|
|
|
- vector.copy( vertices[ indices[ j ] ] ).applyMatrix4( matrixWorld );
|
|
|
|
|
|
+ output += '\t\t\tvertex ' + vector.x + ' ' + vector.y + ' ' + vector.z + '\n';
|
|
|
|
|
|
- if ( encodeBinary ) {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- output.setFloat32(offset, vector.x, true); offset += 4; // vertices
|
|
|
|
- output.setFloat32(offset, vector.y, true); offset += 4;
|
|
|
|
- output.setFloat32(offset, vector.z, true); offset += 4;
|
|
|
|
-
|
|
|
|
- } else {
|
|
|
|
-
|
|
|
|
- output += '\t\t\tvertex ' + vector.x + ' ' + vector.y + ' ' + vector.z + '\n';
|
|
|
|
|
|
+ output += '\t\tendloop\n';
|
|
|
|
+ output += '\tendfacet\n';
|
|
|
|
|
|
}
|
|
}
|
|
- }
|
|
|
|
-
|
|
|
|
- if ( encodeBinary ) {
|
|
|
|
-
|
|
|
|
- output.setUint16(offset, 0, true); offset += 2; // attribute byte count
|
|
|
|
-
|
|
|
|
- } else {
|
|
|
|
-
|
|
|
|
- output += '\t\tendloop\n';
|
|
|
|
- output += '\tendfacet\n';
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
} );
|
|
} );
|
|
-
|
|
|
|
- if ( !encodeBinary ) {
|
|
|
|
- output += 'endsolid exported\n';
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ output += 'endsolid exported\n';
|
|
|
|
|
|
return output;
|
|
return output;
|
|
|
|
|