STLExporter.js 4.0 KB

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