STLExporter.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @author kovacsv / http://kovacsv.hu/
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. THREE.STLExporter = function () {};
  6. THREE.STLExporter.prototype = {
  7. constructor: THREE.STLExporter,
  8. parse: ( function () {
  9. var vector = new THREE.Vector3();
  10. var normalMatrixWorld = new THREE.Matrix3();
  11. return function parse( scene ) {
  12. var output = '';
  13. output += 'solid exported\n';
  14. scene.traverse( function ( object ) {
  15. if ( object instanceof THREE.Mesh ) {
  16. var geometry = object.geometry;
  17. var matrixWorld = object.matrixWorld;
  18. if ( geometry instanceof THREE.Geometry ) {
  19. var vertices = geometry.vertices;
  20. var faces = geometry.faces;
  21. normalMatrixWorld.getNormalMatrix( matrixWorld );
  22. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  23. var face = faces[ i ];
  24. vector.copy( face.normal ).applyMatrix3( normalMatrixWorld ).normalize();
  25. output += '\tfacet normal ' + vector.x + ' ' + vector.y + ' ' + vector.z + '\n';
  26. output += '\t\touter loop\n';
  27. var indices = [ face.a, face.b, face.c ];
  28. for ( var j = 0; j < 3; j ++ ) {
  29. vector.copy( vertices[ indices[ j ] ] ).applyMatrix4( matrixWorld );
  30. output += '\t\t\tvertex ' + vector.x + ' ' + vector.y + ' ' + vector.z + '\n';
  31. }
  32. output += '\t\tendloop\n';
  33. output += '\tendfacet\n';
  34. }
  35. }
  36. }
  37. } );
  38. output += 'endsolid exported\n';
  39. return output;
  40. };
  41. }() )
  42. };