three-stdlib 2.28.5 → 2.28.6
Sign up to get free protection for your applications and to get access to all the features.
- package/exporters/STLExporter.cjs +98 -119
- package/exporters/STLExporter.cjs.map +1 -1
- package/exporters/STLExporter.d.ts +18 -28
- package/exporters/STLExporter.js +99 -120
- package/exporters/STLExporter.js.map +1 -1
- package/package.json +1 -1
@@ -1,145 +1,124 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __defProp = Object.defineProperty;
|
3
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
4
|
-
var __publicField = (obj, key, value) => {
|
5
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
6
|
-
return value;
|
7
|
-
};
|
8
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
9
3
|
const THREE = require("three");
|
10
4
|
class STLExporter {
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
this.binary = false;
|
24
|
-
this.output = "";
|
25
|
-
this.offset = 80;
|
26
|
-
this.objects = [];
|
27
|
-
this.triangles = 0;
|
28
|
-
this.vA = new THREE.Vector3();
|
29
|
-
this.vB = new THREE.Vector3();
|
30
|
-
this.vC = new THREE.Vector3();
|
31
|
-
this.cb = new THREE.Vector3();
|
32
|
-
this.ab = new THREE.Vector3();
|
33
|
-
this.normal = new THREE.Vector3();
|
34
|
-
}
|
35
|
-
parse(scene, options) {
|
36
|
-
this.binary = options.binary !== void 0 ? options.binary : false;
|
37
|
-
scene.traverse((object) => {
|
38
|
-
if (object instanceof THREE.Mesh && object.isMesh) {
|
5
|
+
parse(scene, options = {}) {
|
6
|
+
options = Object.assign(
|
7
|
+
{
|
8
|
+
binary: false
|
9
|
+
},
|
10
|
+
options
|
11
|
+
);
|
12
|
+
const binary = options.binary;
|
13
|
+
const objects = [];
|
14
|
+
let triangles = 0;
|
15
|
+
scene.traverse(function(object) {
|
16
|
+
if (object.isMesh) {
|
39
17
|
const geometry = object.geometry;
|
40
|
-
if (!geometry.isBufferGeometry) {
|
41
|
-
throw new Error("THREE.STLExporter: Geometry is not of type THREE.BufferGeometry.");
|
42
|
-
}
|
43
18
|
const index = geometry.index;
|
44
19
|
const positionAttribute = geometry.getAttribute("position");
|
45
|
-
|
46
|
-
|
20
|
+
triangles += index !== null ? index.count / 3 : positionAttribute.count / 3;
|
21
|
+
objects.push({
|
47
22
|
object3d: object,
|
48
23
|
geometry
|
49
24
|
});
|
50
25
|
}
|
51
26
|
});
|
52
|
-
|
53
|
-
|
27
|
+
let output;
|
28
|
+
let offset = 80;
|
29
|
+
if (binary === true) {
|
30
|
+
const bufferLength = triangles * 2 + triangles * 3 * 4 * 4 + 80 + 4;
|
54
31
|
const arrayBuffer = new ArrayBuffer(bufferLength);
|
55
|
-
|
56
|
-
|
57
|
-
|
32
|
+
output = new DataView(arrayBuffer);
|
33
|
+
output.setUint32(offset, triangles, true);
|
34
|
+
offset += 4;
|
58
35
|
} else {
|
59
|
-
|
60
|
-
|
36
|
+
output = "";
|
37
|
+
output += "solid exported\n";
|
61
38
|
}
|
62
|
-
|
63
|
-
|
64
|
-
|
39
|
+
const vA = new THREE.Vector3();
|
40
|
+
const vB = new THREE.Vector3();
|
41
|
+
const vC = new THREE.Vector3();
|
42
|
+
const cb = new THREE.Vector3();
|
43
|
+
const ab = new THREE.Vector3();
|
44
|
+
const normal = new THREE.Vector3();
|
45
|
+
for (let i = 0, il = objects.length; i < il; i++) {
|
46
|
+
const object = objects[i].object3d;
|
47
|
+
const geometry = objects[i].geometry;
|
65
48
|
const index = geometry.index;
|
66
49
|
const positionAttribute = geometry.getAttribute("position");
|
67
|
-
if (
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
this.writeFace(a, b, c, positionAttribute, object);
|
81
|
-
}
|
50
|
+
if (index !== null) {
|
51
|
+
for (let j = 0; j < index.count; j += 3) {
|
52
|
+
const a = index.getX(j + 0);
|
53
|
+
const b = index.getX(j + 1);
|
54
|
+
const c = index.getX(j + 2);
|
55
|
+
writeFace(a, b, c, positionAttribute, object);
|
56
|
+
}
|
57
|
+
} else {
|
58
|
+
for (let j = 0; j < positionAttribute.count; j += 3) {
|
59
|
+
const a = j + 0;
|
60
|
+
const b = j + 1;
|
61
|
+
const c = j + 2;
|
62
|
+
writeFace(a, b, c, positionAttribute, object);
|
82
63
|
}
|
83
64
|
}
|
84
65
|
}
|
85
|
-
if (
|
86
|
-
|
66
|
+
if (binary === false) {
|
67
|
+
output += "endsolid exported\n";
|
87
68
|
}
|
88
|
-
return
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
69
|
+
return output;
|
70
|
+
function writeFace(a, b, c, positionAttribute, object) {
|
71
|
+
vA.fromBufferAttribute(positionAttribute, a);
|
72
|
+
vB.fromBufferAttribute(positionAttribute, b);
|
73
|
+
vC.fromBufferAttribute(positionAttribute, c);
|
74
|
+
if (object.isSkinnedMesh === true) {
|
75
|
+
object.applyBoneTransform(a, vA);
|
76
|
+
object.applyBoneTransform(b, vB);
|
77
|
+
object.applyBoneTransform(c, vC);
|
78
|
+
}
|
79
|
+
vA.applyMatrix4(object.matrixWorld);
|
80
|
+
vB.applyMatrix4(object.matrixWorld);
|
81
|
+
vC.applyMatrix4(object.matrixWorld);
|
82
|
+
writeNormal(vA, vB, vC);
|
83
|
+
writeVertex(vA);
|
84
|
+
writeVertex(vB);
|
85
|
+
writeVertex(vC);
|
86
|
+
if (binary === true) {
|
87
|
+
output.setUint16(offset, 0, true);
|
88
|
+
offset += 2;
|
89
|
+
} else {
|
90
|
+
output += " endloop\n";
|
91
|
+
output += " endfacet\n";
|
92
|
+
}
|
112
93
|
}
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
this.output += " outer loop\n";
|
94
|
+
function writeNormal(vA2, vB2, vC2) {
|
95
|
+
cb.subVectors(vC2, vB2);
|
96
|
+
ab.subVectors(vA2, vB2);
|
97
|
+
cb.cross(ab).normalize();
|
98
|
+
normal.copy(cb).normalize();
|
99
|
+
if (binary === true) {
|
100
|
+
output.setFloat32(offset, normal.x, true);
|
101
|
+
offset += 4;
|
102
|
+
output.setFloat32(offset, normal.y, true);
|
103
|
+
offset += 4;
|
104
|
+
output.setFloat32(offset, normal.z, true);
|
105
|
+
offset += 4;
|
106
|
+
} else {
|
107
|
+
output += " facet normal " + normal.x + " " + normal.y + " " + normal.z + "\n";
|
108
|
+
output += " outer loop\n";
|
109
|
+
}
|
130
110
|
}
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
`;
|
111
|
+
function writeVertex(vertex) {
|
112
|
+
if (binary === true) {
|
113
|
+
output.setFloat32(offset, vertex.x, true);
|
114
|
+
offset += 4;
|
115
|
+
output.setFloat32(offset, vertex.y, true);
|
116
|
+
offset += 4;
|
117
|
+
output.setFloat32(offset, vertex.z, true);
|
118
|
+
offset += 4;
|
119
|
+
} else {
|
120
|
+
output += " vertex " + vertex.x + " " + vertex.y + " " + vertex.z + "\n";
|
121
|
+
}
|
143
122
|
}
|
144
123
|
}
|
145
124
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"STLExporter.cjs","sources":["../../src/exporters/STLExporter.
|
1
|
+
{"version":3,"file":"STLExporter.cjs","sources":["../../src/exporters/STLExporter.js"],"sourcesContent":["import { Vector3 } from 'three'\n\n/**\n * Usage:\n * const exporter = new STLExporter();\n *\n * // second argument is a list of options\n * const data = exporter.parse( mesh, { binary: true } );\n *\n */\n\nclass STLExporter {\n parse(scene, options = {}) {\n options = Object.assign(\n {\n binary: false,\n },\n options,\n )\n\n const binary = options.binary\n\n //\n\n const objects = []\n let triangles = 0\n\n scene.traverse(function (object) {\n if (object.isMesh) {\n const geometry = object.geometry\n\n const index = geometry.index\n const positionAttribute = geometry.getAttribute('position')\n\n triangles += index !== null ? index.count / 3 : positionAttribute.count / 3\n\n objects.push({\n object3d: object,\n geometry: geometry,\n })\n }\n })\n\n let output\n let offset = 80 // skip header\n\n if (binary === true) {\n const bufferLength = triangles * 2 + triangles * 3 * 4 * 4 + 80 + 4\n const arrayBuffer = new ArrayBuffer(bufferLength)\n output = new DataView(arrayBuffer)\n output.setUint32(offset, triangles, true)\n offset += 4\n } else {\n output = ''\n output += 'solid exported\\n'\n }\n\n const vA = new Vector3()\n const vB = new Vector3()\n const vC = new Vector3()\n const cb = new Vector3()\n const ab = new Vector3()\n const normal = new Vector3()\n\n for (let i = 0, il = objects.length; i < il; i++) {\n const object = objects[i].object3d\n const geometry = objects[i].geometry\n\n const index = geometry.index\n const positionAttribute = geometry.getAttribute('position')\n\n if (index !== null) {\n // indexed geometry\n\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 writeFace(a, b, c, positionAttribute, object)\n }\n } else {\n // non-indexed geometry\n\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 writeFace(a, b, c, positionAttribute, object)\n }\n }\n }\n\n if (binary === false) {\n output += 'endsolid exported\\n'\n }\n\n return output\n\n function writeFace(a, b, c, positionAttribute, object) {\n vA.fromBufferAttribute(positionAttribute, a)\n vB.fromBufferAttribute(positionAttribute, b)\n vC.fromBufferAttribute(positionAttribute, c)\n\n if (object.isSkinnedMesh === true) {\n object.applyBoneTransform(a, vA)\n object.applyBoneTransform(b, vB)\n object.applyBoneTransform(c, vC)\n }\n\n vA.applyMatrix4(object.matrixWorld)\n vB.applyMatrix4(object.matrixWorld)\n vC.applyMatrix4(object.matrixWorld)\n\n writeNormal(vA, vB, vC)\n\n writeVertex(vA)\n writeVertex(vB)\n writeVertex(vC)\n\n if (binary === true) {\n output.setUint16(offset, 0, true)\n offset += 2\n } else {\n output += '\\t\\tendloop\\n'\n output += '\\tendfacet\\n'\n }\n }\n\n function writeNormal(vA, vB, vC) {\n cb.subVectors(vC, vB)\n ab.subVectors(vA, vB)\n cb.cross(ab).normalize()\n\n normal.copy(cb).normalize()\n\n if (binary === true) {\n output.setFloat32(offset, normal.x, true)\n offset += 4\n output.setFloat32(offset, normal.y, true)\n offset += 4\n output.setFloat32(offset, normal.z, true)\n offset += 4\n } else {\n output += '\\tfacet normal ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\\n'\n output += '\\t\\touter loop\\n'\n }\n }\n\n function writeVertex(vertex) {\n if (binary === true) {\n output.setFloat32(offset, vertex.x, true)\n offset += 4\n output.setFloat32(offset, vertex.y, true)\n offset += 4\n output.setFloat32(offset, vertex.z, true)\n offset += 4\n } else {\n output += '\\t\\t\\tvertex ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\\n'\n }\n }\n }\n}\n\nexport { STLExporter }\n"],"names":["Vector3","vA","vB","vC"],"mappings":";;;AAWA,MAAM,YAAY;AAAA,EAChB,MAAM,OAAO,UAAU,IAAI;AACzB,cAAU,OAAO;AAAA,MACf;AAAA,QACE,QAAQ;AAAA,MACT;AAAA,MACD;AAAA,IACD;AAED,UAAM,SAAS,QAAQ;AAIvB,UAAM,UAAU,CAAE;AAClB,QAAI,YAAY;AAEhB,UAAM,SAAS,SAAU,QAAQ;AA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AAG,IAAI;AACxC,kBAAU;AAAA,MAClB,OAAa;AACL,kBAAU,mBAAoB,OAAO,IAAI,MAAM,OAAO,IAAI,MAAM,OAAO,IAAI;AAC3E,kBAAU;AAAA,MACX;AAAA,IACF;AAED,aAAS,YAAY,QAAQ;AAC3B,UAAI,WAAW,MAAM;AACnB,eAAO,WAAW,QAAQ,OAAO,GAAG,IAAI;AACxC,kBAAU;AACV,eAAO,WAAW,QAAQ,OAAO,GAAG,IAAI;AACxC,kBAAU;AACV,eAAO,WAAW,QAAQ,OAAO,GAAG,IAAI;AACxC,kBAAU;AAAA,MAClB,OAAa;AACL,kBAAU,eAAkB,OAAO,IAAI,MAAM,OAAO,IAAI,MAAM,OAAO,IAAI;AAAA,MAC1E;AAAA,IACF;AAAA,EACF;AACH;;"}
|
@@ -1,31 +1,21 @@
|
|
1
|
-
import { Object3D } from 'three'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
import { Object3D } from 'three'
|
2
|
+
|
3
|
+
export interface STLExporterOptionsBinary {
|
4
|
+
binary: true
|
5
|
+
}
|
6
|
+
|
7
|
+
export interface STLExporterOptionsString {
|
8
|
+
binary?: false
|
9
|
+
}
|
10
|
+
|
10
11
|
export interface STLExporterOptions {
|
11
|
-
|
12
|
+
binary?: boolean
|
12
13
|
}
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
private vB;
|
21
|
-
private vC;
|
22
|
-
private cb;
|
23
|
-
private ab;
|
24
|
-
private normal;
|
25
|
-
constructor();
|
26
|
-
parse(scene: Object3D, options: STLExporterOptions): string | DataView;
|
27
|
-
private writeFace;
|
28
|
-
private writeNormal;
|
29
|
-
private writeVertex;
|
14
|
+
|
15
|
+
export class STLExporter {
|
16
|
+
constructor()
|
17
|
+
|
18
|
+
parse(scene: Object3D, options: STLExporterOptionsBinary): DataView
|
19
|
+
parse(scene: Object3D, options?: STLExporterOptionsString): string
|
20
|
+
parse(scene: Object3D, options?: STLExporterOptions): string | DataView
|
30
21
|
}
|
31
|
-
export { STLExporter };
|
package/exporters/STLExporter.js
CHANGED
@@ -1,143 +1,122 @@
|
|
1
|
-
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
3
|
-
var __publicField = (obj, key, value) => {
|
4
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
5
|
-
return value;
|
6
|
-
};
|
7
|
-
import { Vector3, Mesh, SkinnedMesh } from "three";
|
1
|
+
import { Vector3 } from "three";
|
8
2
|
class STLExporter {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
this.binary = false;
|
22
|
-
this.output = "";
|
23
|
-
this.offset = 80;
|
24
|
-
this.objects = [];
|
25
|
-
this.triangles = 0;
|
26
|
-
this.vA = new Vector3();
|
27
|
-
this.vB = new Vector3();
|
28
|
-
this.vC = new Vector3();
|
29
|
-
this.cb = new Vector3();
|
30
|
-
this.ab = new Vector3();
|
31
|
-
this.normal = new Vector3();
|
32
|
-
}
|
33
|
-
parse(scene, options) {
|
34
|
-
this.binary = options.binary !== void 0 ? options.binary : false;
|
35
|
-
scene.traverse((object) => {
|
36
|
-
if (object instanceof Mesh && object.isMesh) {
|
3
|
+
parse(scene, options = {}) {
|
4
|
+
options = Object.assign(
|
5
|
+
{
|
6
|
+
binary: false
|
7
|
+
},
|
8
|
+
options
|
9
|
+
);
|
10
|
+
const binary = options.binary;
|
11
|
+
const objects = [];
|
12
|
+
let triangles = 0;
|
13
|
+
scene.traverse(function(object) {
|
14
|
+
if (object.isMesh) {
|
37
15
|
const geometry = object.geometry;
|
38
|
-
if (!geometry.isBufferGeometry) {
|
39
|
-
throw new Error("THREE.STLExporter: Geometry is not of type THREE.BufferGeometry.");
|
40
|
-
}
|
41
16
|
const index = geometry.index;
|
42
17
|
const positionAttribute = geometry.getAttribute("position");
|
43
|
-
|
44
|
-
|
18
|
+
triangles += index !== null ? index.count / 3 : positionAttribute.count / 3;
|
19
|
+
objects.push({
|
45
20
|
object3d: object,
|
46
21
|
geometry
|
47
22
|
});
|
48
23
|
}
|
49
24
|
});
|
50
|
-
|
51
|
-
|
25
|
+
let output;
|
26
|
+
let offset = 80;
|
27
|
+
if (binary === true) {
|
28
|
+
const bufferLength = triangles * 2 + triangles * 3 * 4 * 4 + 80 + 4;
|
52
29
|
const arrayBuffer = new ArrayBuffer(bufferLength);
|
53
|
-
|
54
|
-
|
55
|
-
|
30
|
+
output = new DataView(arrayBuffer);
|
31
|
+
output.setUint32(offset, triangles, true);
|
32
|
+
offset += 4;
|
56
33
|
} else {
|
57
|
-
|
58
|
-
|
34
|
+
output = "";
|
35
|
+
output += "solid exported\n";
|
59
36
|
}
|
60
|
-
|
61
|
-
|
62
|
-
|
37
|
+
const vA = new Vector3();
|
38
|
+
const vB = new Vector3();
|
39
|
+
const vC = new Vector3();
|
40
|
+
const cb = new Vector3();
|
41
|
+
const ab = new Vector3();
|
42
|
+
const normal = new Vector3();
|
43
|
+
for (let i = 0, il = objects.length; i < il; i++) {
|
44
|
+
const object = objects[i].object3d;
|
45
|
+
const geometry = objects[i].geometry;
|
63
46
|
const index = geometry.index;
|
64
47
|
const positionAttribute = geometry.getAttribute("position");
|
65
|
-
if (
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
this.writeFace(a, b, c, positionAttribute, object);
|
79
|
-
}
|
48
|
+
if (index !== null) {
|
49
|
+
for (let j = 0; j < index.count; j += 3) {
|
50
|
+
const a = index.getX(j + 0);
|
51
|
+
const b = index.getX(j + 1);
|
52
|
+
const c = index.getX(j + 2);
|
53
|
+
writeFace(a, b, c, positionAttribute, object);
|
54
|
+
}
|
55
|
+
} else {
|
56
|
+
for (let j = 0; j < positionAttribute.count; j += 3) {
|
57
|
+
const a = j + 0;
|
58
|
+
const b = j + 1;
|
59
|
+
const c = j + 2;
|
60
|
+
writeFace(a, b, c, positionAttribute, object);
|
80
61
|
}
|
81
62
|
}
|
82
63
|
}
|
83
|
-
if (
|
84
|
-
|
64
|
+
if (binary === false) {
|
65
|
+
output += "endsolid exported\n";
|
85
66
|
}
|
86
|
-
return
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
67
|
+
return output;
|
68
|
+
function writeFace(a, b, c, positionAttribute, object) {
|
69
|
+
vA.fromBufferAttribute(positionAttribute, a);
|
70
|
+
vB.fromBufferAttribute(positionAttribute, b);
|
71
|
+
vC.fromBufferAttribute(positionAttribute, c);
|
72
|
+
if (object.isSkinnedMesh === true) {
|
73
|
+
object.applyBoneTransform(a, vA);
|
74
|
+
object.applyBoneTransform(b, vB);
|
75
|
+
object.applyBoneTransform(c, vC);
|
76
|
+
}
|
77
|
+
vA.applyMatrix4(object.matrixWorld);
|
78
|
+
vB.applyMatrix4(object.matrixWorld);
|
79
|
+
vC.applyMatrix4(object.matrixWorld);
|
80
|
+
writeNormal(vA, vB, vC);
|
81
|
+
writeVertex(vA);
|
82
|
+
writeVertex(vB);
|
83
|
+
writeVertex(vC);
|
84
|
+
if (binary === true) {
|
85
|
+
output.setUint16(offset, 0, true);
|
86
|
+
offset += 2;
|
87
|
+
} else {
|
88
|
+
output += " endloop\n";
|
89
|
+
output += " endfacet\n";
|
90
|
+
}
|
110
91
|
}
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
this.output += " outer loop\n";
|
92
|
+
function writeNormal(vA2, vB2, vC2) {
|
93
|
+
cb.subVectors(vC2, vB2);
|
94
|
+
ab.subVectors(vA2, vB2);
|
95
|
+
cb.cross(ab).normalize();
|
96
|
+
normal.copy(cb).normalize();
|
97
|
+
if (binary === true) {
|
98
|
+
output.setFloat32(offset, normal.x, true);
|
99
|
+
offset += 4;
|
100
|
+
output.setFloat32(offset, normal.y, true);
|
101
|
+
offset += 4;
|
102
|
+
output.setFloat32(offset, normal.z, true);
|
103
|
+
offset += 4;
|
104
|
+
} else {
|
105
|
+
output += " facet normal " + normal.x + " " + normal.y + " " + normal.z + "\n";
|
106
|
+
output += " outer loop\n";
|
107
|
+
}
|
128
108
|
}
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
`;
|
109
|
+
function writeVertex(vertex) {
|
110
|
+
if (binary === true) {
|
111
|
+
output.setFloat32(offset, vertex.x, true);
|
112
|
+
offset += 4;
|
113
|
+
output.setFloat32(offset, vertex.y, true);
|
114
|
+
offset += 4;
|
115
|
+
output.setFloat32(offset, vertex.z, true);
|
116
|
+
offset += 4;
|
117
|
+
} else {
|
118
|
+
output += " vertex " + vertex.x + " " + vertex.y + " " + vertex.z + "\n";
|
119
|
+
}
|
141
120
|
}
|
142
121
|
}
|
143
122
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"STLExporter.js","sources":["../../src/exporters/STLExporter.
|
1
|
+
{"version":3,"file":"STLExporter.js","sources":["../../src/exporters/STLExporter.js"],"sourcesContent":["import { Vector3 } from 'three'\n\n/**\n * Usage:\n * const exporter = new STLExporter();\n *\n * // second argument is a list of options\n * const data = exporter.parse( mesh, { binary: true } );\n *\n */\n\nclass STLExporter {\n parse(scene, options = {}) {\n options = Object.assign(\n {\n binary: false,\n },\n options,\n )\n\n const binary = options.binary\n\n //\n\n const objects = []\n let triangles = 0\n\n scene.traverse(function (object) {\n if (object.isMesh) {\n const geometry = object.geometry\n\n const index = geometry.index\n const positionAttribute = geometry.getAttribute('position')\n\n triangles += index !== null ? index.count / 3 : positionAttribute.count / 3\n\n objects.push({\n object3d: object,\n geometry: geometry,\n })\n }\n })\n\n let output\n let offset = 80 // skip header\n\n if (binary === true) {\n const bufferLength = triangles * 2 + triangles * 3 * 4 * 4 + 80 + 4\n const arrayBuffer = new ArrayBuffer(bufferLength)\n output = new DataView(arrayBuffer)\n output.setUint32(offset, triangles, true)\n offset += 4\n } else {\n output = ''\n output += 'solid exported\\n'\n }\n\n const vA = new Vector3()\n const vB = new Vector3()\n const vC = new Vector3()\n const cb = new Vector3()\n const ab = new Vector3()\n const normal = new Vector3()\n\n for (let i = 0, il = objects.length; i < il; i++) {\n const object = objects[i].object3d\n const geometry = objects[i].geometry\n\n const index = geometry.index\n const positionAttribute = geometry.getAttribute('position')\n\n if (index !== null) {\n // indexed geometry\n\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 writeFace(a, b, c, positionAttribute, object)\n }\n } else {\n // non-indexed geometry\n\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 writeFace(a, b, c, positionAttribute, object)\n }\n }\n }\n\n if (binary === false) {\n output += 'endsolid exported\\n'\n }\n\n return output\n\n function writeFace(a, b, c, positionAttribute, object) {\n vA.fromBufferAttribute(positionAttribute, a)\n vB.fromBufferAttribute(positionAttribute, b)\n vC.fromBufferAttribute(positionAttribute, c)\n\n if (object.isSkinnedMesh === true) {\n object.applyBoneTransform(a, vA)\n object.applyBoneTransform(b, vB)\n object.applyBoneTransform(c, vC)\n }\n\n vA.applyMatrix4(object.matrixWorld)\n vB.applyMatrix4(object.matrixWorld)\n vC.applyMatrix4(object.matrixWorld)\n\n writeNormal(vA, vB, vC)\n\n writeVertex(vA)\n writeVertex(vB)\n writeVertex(vC)\n\n if (binary === true) {\n output.setUint16(offset, 0, true)\n offset += 2\n } else {\n output += '\\t\\tendloop\\n'\n output += '\\tendfacet\\n'\n }\n }\n\n function writeNormal(vA, vB, vC) {\n cb.subVectors(vC, vB)\n ab.subVectors(vA, vB)\n cb.cross(ab).normalize()\n\n normal.copy(cb).normalize()\n\n if (binary === true) {\n output.setFloat32(offset, normal.x, true)\n offset += 4\n output.setFloat32(offset, normal.y, true)\n offset += 4\n output.setFloat32(offset, normal.z, true)\n offset += 4\n } else {\n output += '\\tfacet normal ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\\n'\n output += '\\t\\touter loop\\n'\n }\n }\n\n function writeVertex(vertex) {\n if (binary === true) {\n output.setFloat32(offset, vertex.x, true)\n offset += 4\n output.setFloat32(offset, vertex.y, true)\n offset += 4\n output.setFloat32(offset, vertex.z, true)\n offset += 4\n } else {\n output += '\\t\\t\\tvertex ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\\n'\n }\n }\n }\n}\n\nexport { STLExporter }\n"],"names":["vA","vB","vC"],"mappings":";AAWA,MAAM,YAAY;AAAA,EAChB,MAAM,OAAO,UAAU,IAAI;AACzB,cAAU,OAAO;AAAA,MACf;AAAA,QACE,QAAQ;AAAA,MACT;AAAA,MACD;AAAA,IACD;AAED,UAAM,SAAS,QAAQ;AAIvB,UAAM,UAAU,CAAE;AAClB,QAAI,YAAY;AAEhB,UAAM,SAAS,SAAU,QAAQ;AAC/B,UAAI,OAAO,QAAQ;AACjB,cAAM,WAAW,OAAO;AAExB,cAAM,QAAQ,SAAS;AACvB,cAAM,oBAAoB,SAAS,aAAa,UAAU;AAE1D,qBAAa,UAAU,OAAO,MAAM,QAAQ,IAAI,kBAAkB,QAAQ;AAE1E,gBAA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