1 line
9.2 KiB
Plaintext
1 line
9.2 KiB
Plaintext
|
|
{"version":3,"file":"STLExporter.cjs","sources":["../../src/exporters/STLExporter.ts"],"sourcesContent":["import {\n BufferAttribute,\n BufferGeometry,\n InterleavedBufferAttribute,\n Mesh,\n Object3D,\n SkinnedMesh,\n Vector3,\n} from 'three'\n\nexport interface STLExporterOptionsBinary {\n binary: true\n}\n\nexport interface STLExporterOptionsString {\n binary?: false\n}\n\nexport interface STLExporterOptions {\n binary?: boolean\n}\n\nconst isMesh = (object: unknown): object is Mesh => (object as any).isMesh\n\nexport class STLExporter {\n private binary = false\n\n private output: string | DataView = ''\n private offset: number = 80 // skip header\n\n private objects: { object3d: Object3D; geometry: BufferGeometry }[] = []\n private triangles: number = 0\n\n private vA = new Vector3()\n private vB = new Vector3()\n private vC = new Vector3()\n private cb = new Vector3()\n private ab = new Vector3()\n private normal = new Vector3()\n\n parse(scene: Object3D, options: STLExporterOptionsBinary): DataView\n parse(scene: Object3D, options?: STLExporterOptionsString): string\n parse(scene: Object3D, options?: STLExporterOptions): string | DataView {\n this.binary = options?.binary !== undefined ? options?.binary : false\n\n scene.traverse((object: Object3D) => {\n if (isMesh(object)) {\n const geometry = object.geometry\n\n if (!geometry.isBufferGeometry) {\n throw new Error('THREE.STLExporter: Geometry is not of type THREE.BufferGeometry.')\n }\n\n const index = geometry.index\n const positionAttribute = geometry.getAttribute('position') || null\n if (!positionAttribute) return\n\n this.triangles += index !== null ? index.count / 3 : positionAttribute.count / 3\n\n this.objects.push({\n object3d: object,\n geometry: geometry,\n })\n }\n })\n\n if (this.binary) {\n const bufferLength = this.triangles * 2 + this.triangles * 3 * 4 * 4 + 80 + 4\n const arrayBuffer = new ArrayBuffer(bufferLength)\n this.output = new DataView(arrayBuffer)\n this.output.setUint32(this.offset, this.triangles, true)\n this.offset += 4\n } else {\n this.output = ''\n this.output += 'solid exported\\n'\n }\n\n for (let i = 0, il = this.objects.length; i < il; i++) {\n const object = this.objects[i].object3d\n const geometry = this.objects[i].geometry\n\n const index = geometry.index\n const positionAttribute = geometry.getAttribute('position')\n\n if (index !== null) {\n // indexed geometry\n for (let j = 0; j < index.count; j += 3) {\n const a = index.getX(j + 0)\n const b = index.getX(j + 1)\n const c = index.getX(j + 2)\n\n this.writeFace(a, b, c, positionAttribute, object as SkinnedMesh)\n }\n } else {\n // non-indexed geometry\n for (let j = 0; j < positionAttribute.count; j += 3) {\n const a = j + 0\n const b = j + 1\n const c = j + 2\n\n this.writeFace(a, b, c, positionAttribute, object as SkinnedMesh)\n }\n }\n }\n\n if (!this.binary) {\n this.output += 'endsolid exported\\n'\n }\n\n return this.output\n }\n\n private writeFace(\n a: number,\n b: number,\n c: number,\n positionAttribute: BufferAttribute | InterleavedBufferAttribute,\n object: SkinnedMesh,\n ): void {\n this.vA.fromBufferAttribute(positionAttribute, a)\n this.vB.fromBufferAttribute(positionAttribute, b)\n this.vC.fromBufferAttribute(positionAttribute, c)\n\n if (object.isSkinnedMesh) {\n const mesh = object as Omit<SkinnedMesh, 'boneTransform' | 'applyBoneTransform'> &\n (\n | {\n boneTransform(index: number, vector: Vector3): Vector3\n }\n | {\n applyBoneTransform(index: number, vector: Vector3): Vector3\n }\n )\n\n // r151 https://github.com/mrdoob/three.js/pull/25586\n if ('applyBoneTransform' in mesh) {\n
|