STLExporter.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { Vector3 } from 'three';
  2. /**
  3. * Usage:
  4. * const exporter = new STLExporter();
  5. *
  6. * // second argument is a list of options
  7. * const data = exporter.parse( mesh, { binary: true } );
  8. *
  9. */
  10. class STLExporter {
  11. parse( scene, options = {} ) {
  12. options = Object.assign( {
  13. binary: false
  14. }, options );
  15. const binary = options.binary;
  16. //
  17. const objects = [];
  18. let triangles = 0;
  19. scene.traverse( function ( object ) {
  20. if ( object.isMesh ) {
  21. const geometry = object.geometry;
  22. const index = geometry.index;
  23. const positionAttribute = geometry.getAttribute( 'position' );
  24. triangles += ( index !== null ) ? ( index.count / 3 ) : ( positionAttribute.count / 3 );
  25. objects.push( {
  26. object3d: object,
  27. geometry: geometry
  28. } );
  29. }
  30. } );
  31. let output;
  32. let offset = 80; // skip header
  33. if ( binary === true ) {
  34. const bufferLength = triangles * 2 + triangles * 3 * 4 * 4 + 80 + 4;
  35. const arrayBuffer = new ArrayBuffer( bufferLength );
  36. output = new DataView( arrayBuffer );
  37. output.setUint32( offset, triangles, true ); offset += 4;
  38. } else {
  39. output = '';
  40. output += 'solid exported\n';
  41. }
  42. const vA = new Vector3();
  43. const vB = new Vector3();
  44. const vC = new Vector3();
  45. const cb = new Vector3();
  46. const ab = new Vector3();
  47. const normal = new Vector3();
  48. for ( let i = 0, il = objects.length; i < il; i ++ ) {
  49. const object = objects[ i ].object3d;
  50. const geometry = objects[ i ].geometry;
  51. const index = geometry.index;
  52. const positionAttribute = geometry.getAttribute( 'position' );
  53. if ( index !== null ) {
  54. // indexed geometry
  55. for ( let j = 0; j < index.count; j += 3 ) {
  56. const a = index.getX( j + 0 );
  57. const b = index.getX( j + 1 );
  58. const c = index.getX( j + 2 );
  59. writeFace( a, b, c, positionAttribute, object );
  60. }
  61. } else {
  62. // non-indexed geometry
  63. for ( let j = 0; j < positionAttribute.count; j += 3 ) {
  64. const a = j + 0;
  65. const b = j + 1;
  66. const c = j + 2;
  67. writeFace( a, b, c, positionAttribute, object );
  68. }
  69. }
  70. }
  71. if ( binary === false ) {
  72. output += 'endsolid exported\n';
  73. }
  74. return output;
  75. function writeFace( a, b, c, positionAttribute, object ) {
  76. vA.fromBufferAttribute( positionAttribute, a );
  77. vB.fromBufferAttribute( positionAttribute, b );
  78. vC.fromBufferAttribute( positionAttribute, c );
  79. if ( object.isSkinnedMesh === true ) {
  80. object.applyBoneTransform( a, vA );
  81. object.applyBoneTransform( b, vB );
  82. object.applyBoneTransform( c, vC );
  83. }
  84. vA.applyMatrix4( object.matrixWorld );
  85. vB.applyMatrix4( object.matrixWorld );
  86. vC.applyMatrix4( object.matrixWorld );
  87. writeNormal( vA, vB, vC );
  88. writeVertex( vA );
  89. writeVertex( vB );
  90. writeVertex( vC );
  91. if ( binary === true ) {
  92. output.setUint16( offset, 0, true ); offset += 2;
  93. } else {
  94. output += '\t\tendloop\n';
  95. output += '\tendfacet\n';
  96. }
  97. }
  98. function writeNormal( vA, vB, vC ) {
  99. cb.subVectors( vC, vB );
  100. ab.subVectors( vA, vB );
  101. cb.cross( ab ).normalize();
  102. normal.copy( cb ).normalize();
  103. if ( binary === true ) {
  104. output.setFloat32( offset, normal.x, true ); offset += 4;
  105. output.setFloat32( offset, normal.y, true ); offset += 4;
  106. output.setFloat32( offset, normal.z, true ); offset += 4;
  107. } else {
  108. output += '\tfacet normal ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
  109. output += '\t\touter loop\n';
  110. }
  111. }
  112. function writeVertex( vertex ) {
  113. if ( binary === true ) {
  114. output.setFloat32( offset, vertex.x, true ); offset += 4;
  115. output.setFloat32( offset, vertex.y, true ); offset += 4;
  116. output.setFloat32( offset, vertex.z, true ); offset += 4;
  117. } else {
  118. output += '\t\t\tvertex ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
  119. }
  120. }
  121. }
  122. }
  123. export { STLExporter };