summit/frontend/node_modules/three-stdlib/loaders/STLLoader.cjs.map

1 line
15 KiB
Plaintext
Raw Normal View History

2025-12-08 16:31:30 +00:00
{"version":3,"file":"STLLoader.cjs","sources":["../../src/loaders/STLLoader.js"],"sourcesContent":["import {\n BufferAttribute,\n BufferGeometry,\n FileLoader,\n Float32BufferAttribute,\n Loader,\n LoaderUtils,\n Vector3,\n} from 'three'\nimport { decodeText } from '../_polyfill/LoaderUtils'\n\n/**\n * Description: A THREE loader for STL ASCII files, as created by Solidworks and other CAD programs.\n *\n * Supports both binary and ASCII encoded files, with automatic detection of type.\n *\n * The loader returns a non-indexed buffer geometry.\n *\n * Limitations:\n * Binary decoding supports \"Magics\" color format (http://en.wikipedia.org/wiki/STL_(file_format)#Color_in_binary_STL).\n * There is perhaps some question as to how valid it is to always assume little-endian-ness.\n * ASCII decoding assumes file is UTF-8.\n *\n * Usage:\n * const loader = new STLLoader();\n * loader.load( './models/stl/slotted_disk.stl', function ( geometry ) {\n * scene.add( new THREE.Mesh( geometry ) );\n * });\n *\n * For binary STLs geometry might contain colors for vertices. To use it:\n * // use the same code to load STL as above\n * if (geometry.hasColors) {\n * material = new THREE.MeshPhongMaterial({ opacity: geometry.alpha, vertexColors: true });\n * } else { .... }\n * const mesh = new THREE.Mesh( geometry, material );\n *\n * For ASCII STLs containing multiple solids, each solid is assigned to a different group.\n * Groups can be used to assign a different color by defining an array of materials with the same length of\n * geometry.groups and passing it to the Mesh constructor:\n *\n * const mesh = new THREE.Mesh( geometry, material );\n *\n * For example:\n *\n * const materials = [];\n * const nGeometryGroups = geometry.groups.length;\n *\n * const colorMap = ...; // Some logic to index colors.\n *\n * for (let i = 0; i < nGeometryGroups; i++) {\n *\n *\t\tconst material = new THREE.MeshPhongMaterial({\n *\t\t\tcolor: colorMap[i],\n *\t\t\twireframe: false\n *\t\t});\n *\n * }\n *\n * materials.push(material);\n * const mesh = new THREE.Mesh(geometry, materials);\n */\n\nclass STLLoader extends Loader {\n constructor(manager) {\n super(manager)\n }\n\n load(url, onLoad, onProgress, onError) {\n const scope = this\n\n const loader = new FileLoader(this.manager)\n loader.setPath(this.path)\n loader.setResponseType('arraybuffer')\n loader.setRequestHeader(this.requestHeader)\n loader.setWithCredentials(this.withCredentials)\n\n loader.load(\n url,\n function (text) {\n try {\n onLoad(scope.parse(text))\n } catch (e) {\n if (onError) {\n onError(e)\n } else {\n console.error(e)\n }\n\n scope.manager.itemError(url)\n }\n },\n onProgress,\n onError,\n )\n }\n\n parse(data) {\n function isBinary(data) {\n const reader = new DataView(data)\n const face_size = (32 / 8) * 3 + (32 / 8) * 3 * 3 + 16 / 8\n const n_faces = reader.getUint32(80, true)\n const expect = 80 + 32 / 8 + n_faces * face_size\n\n if (expect === reader.byteLength) {\n return true\n }\n\n // An ASCII STL data must begin with 'solid ' as the first six bytes.\n // However, ASCII STLs lacking the SPACE after the 'd' are known to be\n // plentiful. So, check the first 5 bytes for 'solid'.\n\n // Several encodings, such as UTF-8, precede the text with up to 5 bytes:\n // https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding\n // Search for \"solid\" to start anywhere after those prefixes.\n\n // US-ASCII ordinal values for 's', 'o', 'l', 'i', 'd'\n\n const solid = [115, 111, 108, 105, 100]\n\n for (let off = 0; off < 5; off++) {\n // If \"solid\" text is matched to the current offset, declare it to be an ASCII STL.\n\n if (matchDataViewAt(solid, reader, off)) return false\n }\n\n // Couldn't find \"solid\" text at the beginning; it is binary STL.\n\n return true\n }\n