STLLoader.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * @author aleeper / http://adamleeper.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author gero3 / https://github.com/gero3
  5. * @author Mugen87 / https://github.com/Mugen87
  6. *
  7. * Description: A THREE loader for STL ASCII files, as created by Solidworks and other CAD programs.
  8. *
  9. * Supports both binary and ASCII encoded files, with automatic detection of type.
  10. *
  11. * The loader returns a non-indexed buffer geometry.
  12. *
  13. * Limitations:
  14. * Binary decoding supports "Magics" color format (http://en.wikipedia.org/wiki/STL_(file_format)#Color_in_binary_STL).
  15. * There is perhaps some question as to how valid it is to always assume little-endian-ness.
  16. * ASCII decoding assumes file is UTF-8.
  17. *
  18. * Usage:
  19. * var loader = new THREE.STLLoader();
  20. * loader.load( './models/stl/slotted_disk.stl', function ( geometry ) {
  21. * scene.add( new THREE.Mesh( geometry ) );
  22. * });
  23. *
  24. * For binary STLs geometry might contain colors for vertices. To use it:
  25. * // use the same code to load STL as above
  26. * if (geometry.hasColors) {
  27. * material = new THREE.MeshPhongMaterial({ opacity: geometry.alpha, vertexColors: THREE.VertexColors });
  28. * } else { .... }
  29. * var mesh = new THREE.Mesh( geometry, material );
  30. */
  31. THREE.STLLoader = function ( manager ) {
  32. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  33. };
  34. THREE.STLLoader.prototype = {
  35. constructor: THREE.STLLoader,
  36. load: function ( url, onLoad, onProgress, onError ) {
  37. var scope = this;
  38. var loader = new THREE.FileLoader( scope.manager );
  39. loader.setResponseType( 'arraybuffer' );
  40. loader.load( url, function ( text ) {
  41. onLoad( scope.parse( text ) );
  42. }, onProgress, onError );
  43. },
  44. parse: function ( data ) {
  45. var isBinary = function () {
  46. var expect, face_size, n_faces, reader;
  47. reader = new DataView( binData );
  48. face_size = ( 32 / 8 * 3 ) + ( ( 32 / 8 * 3 ) * 3 ) + ( 16 / 8 );
  49. n_faces = reader.getUint32( 80, true );
  50. expect = 80 + ( 32 / 8 ) + ( n_faces * face_size );
  51. if ( expect === reader.byteLength ) {
  52. return true;
  53. }
  54. // Do we see 'solid' in the first few bytes of the data?
  55. // An ASCII STL data must begin with 'solid ' as the first six bytes.
  56. // However, ASCII STLs lacking the SPACE after the 'd' are known to be
  57. // plentiful.
  58. // This check can likely be restricted to the first 6 bytes of
  59. // the STL data. It's here applied to the first 50 bytes for
  60. // no particular good reason.
  61. var fileLength = reader.byteLength;
  62. if ( fileLength > 50 ) fileLength = 50;
  63. // US-ASCII ordinal values for 's', 'o', 'l', 'i', 'd'
  64. var solid = [ 115, 111, 108, 105, 100 ];
  65. var i = 0;
  66. for ( var index = 0; index < fileLength; index ++ ) {
  67. if ( reader.getUint8( index, false ) == solid[i] ) {
  68. i++;
  69. if ( i == 5 ) {
  70. // 'solid' seen near the start of the file
  71. return false;
  72. }
  73. } else {
  74. i = 0;
  75. }
  76. }
  77. // some binary files will have different size from expected,
  78. // checking characters higher than ASCII to confirm is binary
  79. var fileLength = reader.byteLength;
  80. for ( var index = 0; index < fileLength; index ++ ) {
  81. if ( reader.getUint8( index, false ) > 127 ) {
  82. return true;
  83. }
  84. }
  85. return false;
  86. };
  87. var binData = this.ensureBinary( data );
  88. return isBinary() ? this.parseBinary( binData ) : this.parseASCII( this.ensureString( data ) );
  89. },
  90. parseBinary: function ( data ) {
  91. var reader = new DataView( data );
  92. var faces = reader.getUint32( 80, true );
  93. var r, g, b, hasColors = false, colors;
  94. var defaultR, defaultG, defaultB, alpha;
  95. // process STL header
  96. // check for default color in header ("COLOR=rgba" sequence).
  97. for ( var index = 0; index < 80 - 10; index ++ ) {
  98. if ( ( reader.getUint32( index, false ) == 0x434F4C4F /*COLO*/ ) &&
  99. ( reader.getUint8( index + 4 ) == 0x52 /*'R'*/ ) &&
  100. ( reader.getUint8( index + 5 ) == 0x3D /*'='*/ ) ) {
  101. hasColors = true;
  102. colors = [];
  103. defaultR = reader.getUint8( index + 6 ) / 255;
  104. defaultG = reader.getUint8( index + 7 ) / 255;
  105. defaultB = reader.getUint8( index + 8 ) / 255;
  106. alpha = reader.getUint8( index + 9 ) / 255;
  107. }
  108. }
  109. var dataOffset = 84;
  110. var faceLength = 12 * 4 + 2;
  111. var geometry = new THREE.BufferGeometry();
  112. var vertices = [];
  113. var normals = [];
  114. for ( var face = 0; face < faces; face ++ ) {
  115. var start = dataOffset + face * faceLength;
  116. var normalX = reader.getFloat32( start, true );
  117. var normalY = reader.getFloat32( start + 4, true );
  118. var normalZ = reader.getFloat32( start + 8, true );
  119. if ( hasColors ) {
  120. var packedColor = reader.getUint16( start + 48, true );
  121. if ( ( packedColor & 0x8000 ) === 0 ) {
  122. // facet has its own unique color
  123. r = ( packedColor & 0x1F ) / 31;
  124. g = ( ( packedColor >> 5 ) & 0x1F ) / 31;
  125. b = ( ( packedColor >> 10 ) & 0x1F ) / 31;
  126. } else {
  127. r = defaultR;
  128. g = defaultG;
  129. b = defaultB;
  130. }
  131. }
  132. for ( var i = 1; i <= 3; i ++ ) {
  133. var vertexstart = start + i * 12;
  134. vertices.push( reader.getFloat32( vertexstart, true ) );
  135. vertices.push( reader.getFloat32( vertexstart + 4, true ) );
  136. vertices.push( reader.getFloat32( vertexstart + 8, true ) );
  137. normals.push( normalX, normalY, normalZ );
  138. if ( hasColors ) {
  139. colors.push( r, g, b );
  140. }
  141. }
  142. }
  143. geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  144. geometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
  145. if ( hasColors ) {
  146. geometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 3 ) );
  147. geometry.hasColors = true;
  148. geometry.alpha = alpha;
  149. }
  150. return geometry;
  151. },
  152. parseASCII: function ( data ) {
  153. var geometry, length, patternFace, patternNormal, patternVertex, result, text;
  154. geometry = new THREE.BufferGeometry();
  155. patternFace = /facet([\s\S]*?)endfacet/g;
  156. var vertices = [];
  157. var normals = [];
  158. var normal = new THREE.Vector3();
  159. while ( ( result = patternFace.exec( data ) ) !== null ) {
  160. text = result[ 0 ];
  161. patternNormal = /normal[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
  162. while ( ( result = patternNormal.exec( text ) ) !== null ) {
  163. normal.x = parseFloat( result[ 1 ] );
  164. normal.y = parseFloat( result[ 3 ] );
  165. normal.z = parseFloat( result[ 5 ] );
  166. }
  167. patternVertex = /vertex[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
  168. while ( ( result = patternVertex.exec( text ) ) !== null ) {
  169. vertices.push( parseFloat( result[ 1 ] ), parseFloat( result[ 3 ] ), parseFloat( result[ 5 ] ) );
  170. normals.push( normal.x, normal.y, normal.z );
  171. }
  172. }
  173. geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( vertices ), 3 ) );
  174. geometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
  175. return geometry;
  176. },
  177. ensureString: function ( buf ) {
  178. if ( typeof buf !== "string" ) {
  179. var array_buffer = new Uint8Array( buf );
  180. var strArray = [];
  181. for ( var i = 0; i < buf.byteLength; i ++ ) {
  182. strArray.push(String.fromCharCode( array_buffer[ i ] )); // implicitly assumes little-endian
  183. }
  184. return strArray.join('');
  185. } else {
  186. return buf;
  187. }
  188. },
  189. ensureBinary: function ( buf ) {
  190. if ( typeof buf === "string" ) {
  191. var array_buffer = new Uint8Array( buf.length );
  192. for ( var i = 0; i < buf.length; i ++ ) {
  193. array_buffer[ i ] = buf.charCodeAt( i ) & 0xff; // implicitly assumes little-endian
  194. }
  195. return array_buffer.buffer || array_buffer;
  196. } else {
  197. return buf;
  198. }
  199. }
  200. };