| 1 | import { |
| 2 | BufferAttribute, |
| 3 | BufferGeometry, |
| 4 | Color, |
| 5 | FileLoader, |
| 6 | Float32BufferAttribute, |
| 7 | Loader, |
| 8 | Vector3 |
| 9 | } from 'three'; |
| 10 | |
| 11 | /** |
| 12 | * Description: A THREE loader for STL ASCII files, as created by Solidworks and other CAD programs. |
| 13 | * |
| 14 | * Supports both binary and ASCII encoded files, with automatic detection of type. |
| 15 | * |
| 16 | * The loader returns a non-indexed buffer geometry. |
| 17 | * |
| 18 | * Limitations: |
| 19 | * Binary decoding supports "Magics" color format (http://en.wikipedia.org/wiki/STL_(file_format)#Color_in_binary_STL). |
| 20 | * There is perhaps some question as to how valid it is to always assume little-endian-ness. |
| 21 | * ASCII decoding assumes file is UTF-8. |
| 22 | * |
| 23 | * Usage: |
| 24 | * const loader = new STLLoader(); |
| 25 | * loader.load( './models/stl/slotted_disk.stl', function ( geometry ) { |
| 26 | * scene.add( new THREE.Mesh( geometry ) ); |
| 27 | * }); |
| 28 | * |
| 29 | * For binary STLs geometry might contain colors for vertices. To use it: |
| 30 | * // use the same code to load STL as above |
| 31 | * if (geometry.hasColors) { |
| 32 | * material = new THREE.MeshPhongMaterial({ opacity: geometry.alpha, vertexColors: true }); |
| 33 | * } else { .... } |
| 34 | * const mesh = new THREE.Mesh( geometry, material ); |
| 35 | * |
| 36 | * For ASCII STLs containing multiple solids, each solid is assigned to a different group. |
| 37 | * Groups can be used to assign a different color by defining an array of materials with the same length of |
| 38 | * geometry.groups and passing it to the Mesh constructor: |
| 39 | * |
| 40 | * const mesh = new THREE.Mesh( geometry, material ); |
| 41 | * |
| 42 | * For example: |
| 43 | * |
| 44 | * const materials = []; |
| 45 | * const nGeometryGroups = geometry.groups.length; |
| 46 | * |
| 47 | * const colorMap = ...; // Some logic to index colors. |
| 48 | * |
| 49 | * for (let i = 0; i < nGeometryGroups; i++) { |
| 50 | * |
| 51 | * const material = new THREE.MeshPhongMaterial({ |
| 52 | * color: colorMap[i], |
| 53 | * wireframe: false |
| 54 | * }); |
| 55 | * |
| 56 | * } |
| 57 | * |
| 58 | * materials.push(material); |
| 59 | * const mesh = new THREE.Mesh(geometry, materials); |
| 60 | */ |
| 61 | |
| 62 | |
| 63 | class STLLoader extends Loader { |
| 64 | |
| 65 | constructor( manager ) { |
| 66 | |
| 67 | super( manager ); |
| 68 | |
| 69 | } |
| 70 | |
| 71 | load( url, onLoad, onProgress, onError ) { |
| 72 | |
| 73 | const scope = this; |
| 74 | |
| 75 | const loader = new FileLoader( this.manager ); |
| 76 | loader.setPath( this.path ); |
| 77 | loader.setResponseType( 'arraybuffer' ); |
| 78 | loader.setRequestHeader( this.requestHeader ); |
| 79 | loader.setWithCredentials( this.withCredentials ); |
| 80 | |
| 81 | loader.load( url, function ( text ) { |
| 82 | |
| 83 | try { |
| 84 | |
| 85 | onLoad( scope.parse( text ) ); |
| 86 | |
| 87 | } catch ( e ) { |
| 88 | |
| 89 | if ( onError ) { |
| 90 | |
| 91 | onError( e ); |
| 92 | |
| 93 | } else { |
| 94 | |
| 95 | console.error( e ); |
| 96 | |
| 97 | } |
| 98 | |
| 99 | scope.manager.itemError( url ); |
| 100 | |
| 101 | } |
| 102 | |
| 103 | }, onProgress, onError ); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | parse( data ) { |
| 108 | |
| 109 | function isBinary( data ) { |
| 110 | |
| 111 | const reader = new DataView( data ); |
| 112 | const face_size = ( 32 / 8 * 3 ) + ( ( 32 / 8 * 3 ) * 3 ) + ( 16 / 8 ); |
| 113 | const n_faces = reader.getUint32( 80, true ); |
| 114 | const expect = 80 + ( 32 / 8 ) + ( n_faces * face_size ); |
| 115 | |
| 116 | if ( expect === reader.byteLength ) { |
| 117 | |
| 118 | return true; |
| 119 | |
| 120 | } |
| 121 | |
| 122 | // An ASCII STL data must begin with 'solid ' as the first six bytes. |
| 123 | // However, ASCII STLs lacking the SPACE after the 'd' are known to be |
| 124 | // plentiful. So, check the first 5 bytes for 'solid'. |
| 125 | |
| 126 | // Several encodings, such as UTF-8, precede the text with up to 5 bytes: |
| 127 | // https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding |
| 128 | // Search for "solid" to start anywhere after those prefixes. |
| 129 | |
| 130 | // US-ASCII ordinal values for 's', 'o', 'l', 'i', 'd' |
| 131 | |
| 132 | const solid = [ 115, 111, 108, 105, 100 ]; |
| 133 | |
| 134 | for ( let off = 0; off < 5; off ++ ) { |
| 135 | |
| 136 | // If "solid" text is matched to the current offset, declare it to be an ASCII STL. |
| 137 | |
| 138 | if ( matchDataViewAt( solid, reader, off ) ) return false; |
| 139 | |
| 140 | } |
| 141 | |
| 142 | // Couldn't find "solid" text at the beginning; it is binary STL. |
| 143 | |
| 144 | return true; |
| 145 | |
| 146 | } |
| 147 | |
| 148 | function matchDataViewAt( query, reader, offset ) { |
| 149 | |
| 150 | // Check if each byte in query matches the corresponding byte from the current offset |
| 151 | |
| 152 | for ( let i = 0, il = query.length; i < il; i ++ ) { |
| 153 | |
| 154 | if ( query[ i ] !== reader.getUint8( offset + i ) ) return false; |
| 155 | |
| 156 | } |
| 157 | |
| 158 | return true; |
| 159 | |
| 160 | } |
| 161 | |
| 162 | function parseBinary( data ) { |
| 163 | |
| 164 | const reader = new DataView( data ); |
| 165 | const faces = reader.getUint32( 80, true ); |
| 166 | |
| 167 | let r, g, b, hasColors = false, colors; |
| 168 | let defaultR, defaultG, defaultB, alpha; |
| 169 | |
| 170 | // process STL header |
| 171 | // check for default color in header ("COLOR=rgba" sequence). |
| 172 | |
| 173 | for ( let index = 0; index < 80 - 10; index ++ ) { |
| 174 | |
| 175 | if ( ( reader.getUint32( index, false ) == 0x434F4C4F /*COLO*/ ) && |
| 176 | ( reader.getUint8( index + 4 ) == 0x52 /*'R'*/ ) && |
| 177 | ( reader.getUint8( index + 5 ) == 0x3D /*'='*/ ) ) { |
| 178 | |
| 179 | hasColors = true; |
| 180 | colors = new Float32Array( faces * 3 * 3 ); |
| 181 | |
| 182 | defaultR = reader.getUint8( index + 6 ) / 255; |
| 183 | defaultG = reader.getUint8( index + 7 ) / 255; |
| 184 | defaultB = reader.getUint8( index + 8 ) / 255; |
| 185 | alpha = reader.getUint8( index + 9 ) / 255; |
| 186 | |
| 187 | } |
| 188 | |
| 189 | } |
| 190 | |
| 191 | const dataOffset = 84; |
| 192 | const faceLength = 12 * 4 + 2; |
| 193 | |
| 194 | const geometry = new BufferGeometry(); |
| 195 | |
| 196 | const vertices = new Float32Array( faces * 3 * 3 ); |
| 197 | const normals = new Float32Array( faces * 3 * 3 ); |
| 198 | |
| 199 | const color = new Color(); |
| 200 | |
| 201 | for ( let face = 0; face < faces; face ++ ) { |
| 202 | |
| 203 | const start = dataOffset + face * faceLength; |
| 204 | const normalX = reader.getFloat32( start, true ); |
| 205 | const normalY = reader.getFloat32( start + 4, true ); |
| 206 | const normalZ = reader.getFloat32( start + 8, true ); |
| 207 | |
| 208 | if ( hasColors ) { |
| 209 | |
| 210 | const packedColor = reader.getUint16( start + 48, true ); |
| 211 | |
| 212 | if ( ( packedColor & 0x8000 ) === 0 ) { |
| 213 | |
| 214 | // facet has its own unique color |
| 215 | |
| 216 | r = ( packedColor & 0x1F ) / 31; |
| 217 | g = ( ( packedColor >> 5 ) & 0x1F ) / 31; |
| 218 | b = ( ( packedColor >> 10 ) & 0x1F ) / 31; |
| 219 | |
| 220 | } else { |
| 221 | |
| 222 | r = defaultR; |
| 223 | g = defaultG; |
| 224 | b = defaultB; |
| 225 | |
| 226 | } |
| 227 | |
| 228 | } |
| 229 | |
| 230 | for ( let i = 1; i <= 3; i ++ ) { |
| 231 | |
| 232 | const vertexstart = start + i * 12; |
| 233 | const componentIdx = ( face * 3 * 3 ) + ( ( i - 1 ) * 3 ); |
| 234 | |
| 235 | vertices[ componentIdx ] = reader.getFloat32( vertexstart, true ); |
| 236 | vertices[ componentIdx + 1 ] = reader.getFloat32( vertexstart + 4, true ); |
| 237 | vertices[ componentIdx + 2 ] = reader.getFloat32( vertexstart + 8, true ); |
| 238 | |
| 239 | normals[ componentIdx ] = normalX; |
| 240 | normals[ componentIdx + 1 ] = normalY; |
| 241 | normals[ componentIdx + 2 ] = normalZ; |
| 242 | |
| 243 | if ( hasColors ) { |
| 244 | |
| 245 | color.set( r, g, b ).convertSRGBToLinear(); |
| 246 | |
| 247 | colors[ componentIdx ] = color.r; |
| 248 | colors[ componentIdx + 1 ] = color.g; |
| 249 | colors[ componentIdx + 2 ] = color.b; |
| 250 | |
| 251 | } |
| 252 | |
| 253 | } |
| 254 | |
| 255 | } |
| 256 | |
| 257 | geometry.setAttribute( 'position', new BufferAttribute( vertices, 3 ) ); |
| 258 | geometry.setAttribute( 'normal', new BufferAttribute( normals, 3 ) ); |
| 259 | |
| 260 | if ( hasColors ) { |
| 261 | |
| 262 | geometry.setAttribute( 'color', new BufferAttribute( colors, 3 ) ); |
| 263 | geometry.hasColors = true; |
| 264 | geometry.alpha = alpha; |
| 265 | |
| 266 | } |
| 267 | |
| 268 | return geometry; |
| 269 | |
| 270 | } |
| 271 | |
| 272 | function parseASCII( data ) { |
| 273 | |
| 274 | const geometry = new BufferGeometry(); |
| 275 | const patternSolid = /solid([\s\S]*?)endsolid/g; |
| 276 | const patternFace = /facet([\s\S]*?)endfacet/g; |
| 277 | const patternName = /solid\s(.+)/; |
| 278 | let faceCounter = 0; |
| 279 | |
| 280 | const patternFloat = /[\s]+([+-]?(?:\d*)(?:\.\d*)?(?:[eE][+-]?\d+)?)/.source; |
| 281 | const patternVertex = new RegExp( 'vertex' + patternFloat + patternFloat + patternFloat, 'g' ); |
| 282 | const patternNormal = new RegExp( 'normal' + patternFloat + patternFloat + patternFloat, 'g' ); |
| 283 | |
| 284 | const vertices = []; |
| 285 | const normals = []; |
| 286 | const groupNames = []; |
| 287 | |
| 288 | const normal = new Vector3(); |
| 289 | |
| 290 | let result; |
| 291 | |
| 292 | let groupCount = 0; |
| 293 | let startVertex = 0; |
| 294 | let endVertex = 0; |
| 295 | |
| 296 | while ( ( result = patternSolid.exec( data ) ) !== null ) { |
| 297 | |
| 298 | startVertex = endVertex; |
| 299 | |
| 300 | const solid = result[ 0 ]; |
| 301 | |
| 302 | const name = ( result = patternName.exec( solid ) ) !== null ? result[ 1 ] : ''; |
| 303 | groupNames.push( name ); |
| 304 | |
| 305 | while ( ( result = patternFace.exec( solid ) ) !== null ) { |
| 306 | |
| 307 | let vertexCountPerFace = 0; |
| 308 | let normalCountPerFace = 0; |
| 309 | |
| 310 | const text = result[ 0 ]; |
| 311 | |
| 312 | while ( ( result = patternNormal.exec( text ) ) !== null ) { |
| 313 | |
| 314 | normal.x = parseFloat( result[ 1 ] ); |
| 315 | normal.y = parseFloat( result[ 2 ] ); |
| 316 | normal.z = parseFloat( result[ 3 ] ); |
| 317 | normalCountPerFace ++; |
| 318 | |
| 319 | } |
| 320 | |
| 321 | while ( ( result = patternVertex.exec( text ) ) !== null ) { |
| 322 | |
| 323 | vertices.push( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) ); |
| 324 | normals.push( normal.x, normal.y, normal.z ); |
| 325 | vertexCountPerFace ++; |
| 326 | endVertex ++; |
| 327 | |
| 328 | } |
| 329 | |
| 330 | // every face have to own ONE valid normal |
| 331 | |
| 332 | if ( normalCountPerFace !== 1 ) { |
| 333 | |
| 334 | console.error( 'THREE.STLLoader: Something isn\'t right with the normal of face number ' + faceCounter ); |
| 335 | |
| 336 | } |
| 337 | |
| 338 | // each face have to own THREE valid vertices |
| 339 | |
| 340 | if ( vertexCountPerFace !== 3 ) { |
| 341 | |
| 342 | console.error( 'THREE.STLLoader: Something isn\'t right with the vertices of face number ' + faceCounter ); |
| 343 | |
| 344 | } |
| 345 | |
| 346 | faceCounter ++; |
| 347 | |
| 348 | } |
| 349 | |
| 350 | const start = startVertex; |
| 351 | const count = endVertex - startVertex; |
| 352 | |
| 353 | geometry.userData.groupNames = groupNames; |
| 354 | |
| 355 | geometry.addGroup( start, count, groupCount ); |
| 356 | groupCount ++; |
| 357 | |
| 358 | } |
| 359 | |
| 360 | geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); |
| 361 | geometry.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); |
| 362 | |
| 363 | return geometry; |
| 364 | |
| 365 | } |
| 366 | |
| 367 | function ensureString( buffer ) { |
| 368 | |
| 369 | if ( typeof buffer !== 'string' ) { |
| 370 | |
| 371 | return new TextDecoder().decode( buffer ); |
| 372 | |
| 373 | } |
| 374 | |
| 375 | return buffer; |
| 376 | |
| 377 | } |
| 378 | |
| 379 | function ensureBinary( buffer ) { |
| 380 | |
| 381 | if ( typeof buffer === 'string' ) { |
| 382 | |
| 383 | const array_buffer = new Uint8Array( buffer.length ); |
| 384 | for ( let i = 0; i < buffer.length; i ++ ) { |
| 385 | |
| 386 | array_buffer[ i ] = buffer.charCodeAt( i ) & 0xff; // implicitly assumes little-endian |
| 387 | |
| 388 | } |
| 389 | |
| 390 | return array_buffer.buffer || array_buffer; |
| 391 | |
| 392 | } else { |
| 393 | |
| 394 | return buffer; |
| 395 | |
| 396 | } |
| 397 | |
| 398 | } |
| 399 | |
| 400 | // start |
| 401 | |
| 402 | const binData = ensureBinary( data ); |
| 403 | |
| 404 | return isBinary( binData ) ? parseBinary( binData ) : parseASCII( ensureString( data ) ); |
| 405 | |
| 406 | } |
| 407 | |
| 408 | } |
| 409 | |
| 410 | export { STLLoader }; |