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