STLExporter.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * @author kovacsv / http://kovacsv.hu/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author mudcube / http://mudcu.be/
  5. * @author Mugen87 / https://github.com/Mugen87
  6. *
  7. * Usage:
  8. * var exporter = new STLExporter();
  9. *
  10. * // second argument is a list of options
  11. * var data = exporter.parse( mesh, { binary: true } );
  12. *
  13. */
  14. import {
  15. Geometry,
  16. Matrix3,
  17. Vector3
  18. } from "../../../build/three.module.js";
  19. var STLExporter = function () {};
  20. STLExporter.prototype = {
  21. constructor: STLExporter,
  22. parse: ( function () {
  23. var vector = new Vector3();
  24. var normalMatrixWorld = new Matrix3();
  25. return function parse( scene, options ) {
  26. if ( options === undefined ) options = {};
  27. var binary = options.binary !== undefined ? options.binary : false;
  28. //
  29. var objects = [];
  30. var triangles = 0;
  31. scene.traverse( function ( object ) {
  32. if ( object.isMesh ) {
  33. var geometry = object.geometry;
  34. if ( geometry.isBufferGeometry ) {
  35. geometry = new Geometry().fromBufferGeometry( geometry );
  36. }
  37. if ( geometry.isGeometry ) {
  38. triangles += geometry.faces.length;
  39. objects.push( {
  40. geometry: geometry,
  41. matrixWorld: object.matrixWorld
  42. } );
  43. }
  44. }
  45. } );
  46. if ( binary ) {
  47. var offset = 80; // skip header
  48. var bufferLength = triangles * 2 + triangles * 3 * 4 * 4 + 80 + 4;
  49. var arrayBuffer = new ArrayBuffer( bufferLength );
  50. var output = new DataView( arrayBuffer );
  51. output.setUint32( offset, triangles, true ); offset += 4;
  52. for ( var i = 0, il = objects.length; i < il; i ++ ) {
  53. var object = objects[ i ];
  54. var vertices = object.geometry.vertices;
  55. var faces = object.geometry.faces;
  56. var matrixWorld = object.matrixWorld;
  57. normalMatrixWorld.getNormalMatrix( matrixWorld );
  58. for ( var j = 0, jl = faces.length; j < jl; j ++ ) {
  59. var face = faces[ j ];
  60. vector.copy( face.normal ).applyMatrix3( normalMatrixWorld ).normalize();
  61. output.setFloat32( offset, vector.x, true ); offset += 4; // normal
  62. output.setFloat32( offset, vector.y, true ); offset += 4;
  63. output.setFloat32( offset, vector.z, true ); offset += 4;
  64. var indices = [ face.a, face.b, face.c ];
  65. for ( var k = 0; k < 3; k ++ ) {
  66. vector.copy( vertices[ indices[ k ] ] ).applyMatrix4( matrixWorld );
  67. output.setFloat32( offset, vector.x, true ); offset += 4; // vertices
  68. output.setFloat32( offset, vector.y, true ); offset += 4;
  69. output.setFloat32( offset, vector.z, true ); offset += 4;
  70. }
  71. output.setUint16( offset, 0, true ); offset += 2; // attribute byte count
  72. }
  73. }
  74. return output;
  75. } else {
  76. var output = '';
  77. output += 'solid exported\n';
  78. for ( var i = 0, il = objects.length; i < il; i ++ ) {
  79. var object = objects[ i ];
  80. var vertices = object.geometry.vertices;
  81. var faces = object.geometry.faces;
  82. var matrixWorld = object.matrixWorld;
  83. normalMatrixWorld.getNormalMatrix( matrixWorld );
  84. for ( var j = 0, jl = faces.length; j < jl; j ++ ) {
  85. var face = faces[ j ];
  86. vector.copy( face.normal ).applyMatrix3( normalMatrixWorld ).normalize();
  87. output += '\tfacet normal ' + vector.x + ' ' + vector.y + ' ' + vector.z + '\n';
  88. output += '\t\touter loop\n';
  89. var indices = [ face.a, face.b, face.c ];
  90. for ( var k = 0; k < 3; k ++ ) {
  91. vector.copy( vertices[ indices[ k ] ] ).applyMatrix4( matrixWorld );
  92. output += '\t\t\tvertex ' + vector.x + ' ' + vector.y + ' ' + vector.z + '\n';
  93. }
  94. output += '\t\tendloop\n';
  95. output += '\tendfacet\n';
  96. }
  97. }
  98. output += 'endsolid exported\n';
  99. return output;
  100. }
  101. };
  102. }() )
  103. };
  104. export { STLExporter };