STLExporter.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { Vector3 } from 'three';
  2. /**
  3. * An exporter for STL.
  4. *
  5. * STL files describe only the surface geometry of a three-dimensional object without
  6. * any representation of color, texture or other common model attributes. The STL format
  7. * specifies both ASCII and binary representations, with binary being more compact.
  8. * STL files contain no scale information or indexes, and the units are arbitrary.
  9. *
  10. * ```js
  11. * const exporter = new STLExporter();
  12. * const data = exporter.parse( mesh, { binary: true } );
  13. * ```
  14. */
  15. class STLExporter {
  16. /**
  17. * Parses the given 3D object and generates the STL output.
  18. *
  19. * If the 3D object is composed of multiple children and geometry, they are merged into a single mesh in the file.
  20. *
  21. * @param {Object3D} scene - A scene, mesh or any other 3D object containing meshes to encode.
  22. * @param {STLExporter~Options} options - The export options.
  23. * @return {string|ArrayBuffer} The exported STL.
  24. */
  25. parse( scene, options = {} ) {
  26. options = Object.assign( {
  27. binary: false
  28. }, options );
  29. const binary = options.binary;
  30. //
  31. const objects = [];
  32. let triangles = 0;
  33. scene.traverse( function ( object ) {
  34. if ( object.isMesh ) {
  35. const geometry = object.geometry;
  36. const index = geometry.index;
  37. const positionAttribute = geometry.getAttribute( 'position' );
  38. triangles += ( index !== null ) ? ( index.count / 3 ) : ( positionAttribute.count / 3 );
  39. objects.push( {
  40. object3d: object,
  41. geometry: geometry
  42. } );
  43. }
  44. } );
  45. let output;
  46. let offset = 80; // skip header
  47. if ( binary === true ) {
  48. const bufferLength = triangles * 2 + triangles * 3 * 4 * 4 + 80 + 4;
  49. const arrayBuffer = new ArrayBuffer( bufferLength );
  50. output = new DataView( arrayBuffer );
  51. output.setUint32( offset, triangles, true ); offset += 4;
  52. } else {
  53. output = '';
  54. output += 'solid exported\n';
  55. }
  56. const vA = new Vector3();
  57. const vB = new Vector3();
  58. const vC = new Vector3();
  59. const cb = new Vector3();
  60. const ab = new Vector3();
  61. const normal = new Vector3();
  62. for ( let i = 0, il = objects.length; i < il; i ++ ) {
  63. const object = objects[ i ].object3d;
  64. const geometry = objects[ i ].geometry;
  65. const index = geometry.index;
  66. const positionAttribute = geometry.getAttribute( 'position' );
  67. if ( index !== null ) {
  68. // indexed geometry
  69. for ( let j = 0; j < index.count; j += 3 ) {
  70. const a = index.getX( j + 0 );
  71. const b = index.getX( j + 1 );
  72. const c = index.getX( j + 2 );
  73. writeFace( a, b, c, positionAttribute, object );
  74. }
  75. } else {
  76. // non-indexed geometry
  77. for ( let j = 0; j < positionAttribute.count; j += 3 ) {
  78. const a = j + 0;
  79. const b = j + 1;
  80. const c = j + 2;
  81. writeFace( a, b, c, positionAttribute, object );
  82. }
  83. }
  84. }
  85. if ( binary === false ) {
  86. output += 'endsolid exported\n';
  87. }
  88. return output;
  89. function writeFace( a, b, c, positionAttribute, object ) {
  90. vA.fromBufferAttribute( positionAttribute, a );
  91. vB.fromBufferAttribute( positionAttribute, b );
  92. vC.fromBufferAttribute( positionAttribute, c );
  93. if ( object.isSkinnedMesh === true ) {
  94. object.applyBoneTransform( a, vA );
  95. object.applyBoneTransform( b, vB );
  96. object.applyBoneTransform( c, vC );
  97. }
  98. vA.applyMatrix4( object.matrixWorld );
  99. vB.applyMatrix4( object.matrixWorld );
  100. vC.applyMatrix4( object.matrixWorld );
  101. writeNormal( vA, vB, vC );
  102. writeVertex( vA );
  103. writeVertex( vB );
  104. writeVertex( vC );
  105. if ( binary === true ) {
  106. output.setUint16( offset, 0, true ); offset += 2;
  107. } else {
  108. output += '\t\tendloop\n';
  109. output += '\tendfacet\n';
  110. }
  111. }
  112. function writeNormal( vA, vB, vC ) {
  113. cb.subVectors( vC, vB );
  114. ab.subVectors( vA, vB );
  115. cb.cross( ab ).normalize();
  116. normal.copy( cb ).normalize();
  117. if ( binary === true ) {
  118. output.setFloat32( offset, normal.x, true ); offset += 4;
  119. output.setFloat32( offset, normal.y, true ); offset += 4;
  120. output.setFloat32( offset, normal.z, true ); offset += 4;
  121. } else {
  122. output += '\tfacet normal ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
  123. output += '\t\touter loop\n';
  124. }
  125. }
  126. function writeVertex( vertex ) {
  127. if ( binary === true ) {
  128. output.setFloat32( offset, vertex.x, true ); offset += 4;
  129. output.setFloat32( offset, vertex.y, true ); offset += 4;
  130. output.setFloat32( offset, vertex.z, true ); offset += 4;
  131. } else {
  132. output += '\t\t\tvertex ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Export options of `STLExporter`.
  139. *
  140. * @typedef {Object} STLExporter~Options
  141. * @property {boolean} [binary=false] - Whether to export in binary format or ASCII.
  142. **/
  143. export { STLExporter };
粤ICP备19079148号