forked from jmoenig/Snap
-
Notifications
You must be signed in to change notification settings - Fork 13
/
STLExporter.js
166 lines (98 loc) · 3.62 KB
/
STLExporter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* @author kovacsv / http://kovacsv.hu/
* @author mrdoob / http://mrdoob.com/
* @author mudcube / http://mudcu.be/
* @author Mugen87 / https://github.com/Mugen87
*
* Usage:
* var exporter = new THREE.STLExporter();
*
* // second argument is a list of options
* var data = exporter.parse( mesh, { binary: true } );
*
*/
THREE.STLExporter = function () {};
THREE.STLExporter.prototype = {
constructor: THREE.STLExporter,
parse: ( function () {
var vector = new THREE.Vector3();
var normalMatrixWorld = new THREE.Matrix3();
return function parse( scene, options ) {
if ( options === undefined ) options = {};
var binary = options.binary !== undefined ? options.binary : false;
//
var objects = [];
var triangles = 0;
scene.traverse( function ( object ) {
if ( object.isMesh ) {
var geometry = object.geometry;
if ( geometry.isBufferGeometry ) {
geometry = new THREE.Geometry().fromBufferGeometry( geometry );
}
if ( geometry.isGeometry ) {
triangles += geometry.faces.length;
objects.push( {
geometry: geometry,
matrixWorld: object.matrixWorld
} );
}
}
} );
if ( binary ) {
var offset = 80; // skip header
var bufferLength = triangles * 2 + triangles * 3 * 4 * 4 + 80 + 4;
var arrayBuffer = new ArrayBuffer( bufferLength );
var output = new DataView( arrayBuffer );
output.setUint32( offset, triangles, true ); offset += 4;
for ( var i = 0, il = objects.length; i < il; i ++ ) {
var object = objects[ i ];
var vertices = object.geometry.vertices;
var faces = object.geometry.faces;
var matrixWorld = object.matrixWorld;
normalMatrixWorld.getNormalMatrix( matrixWorld );
for ( var j = 0, jl = faces.length; j < jl; j ++ ) {
var face = faces[ j ];
vector.copy( face.normal ).applyMatrix3( normalMatrixWorld ).normalize();
output.setFloat32( offset, vector.x, true ); offset += 4; // normal
output.setFloat32( offset, vector.y, true ); offset += 4;
output.setFloat32( offset, vector.z, true ); offset += 4;
var indices = [ face.a, face.b, face.c ];
for ( var k = 0; k < 3; k ++ ) {
vector.copy( vertices[ indices[ k ] ] ).applyMatrix4( matrixWorld );
output.setFloat32( offset, vector.x, true ); offset += 4; // vertices
output.setFloat32( offset, vector.y, true ); offset += 4;
output.setFloat32( offset, vector.z, true ); offset += 4;
}
output.setUint16( offset, 0, true ); offset += 2; // attribute byte count
}
}
return output;
} else {
var output = '';
output += 'solid exported\n';
for ( var i = 0, il = objects.length; i < il; i ++ ) {
var object = objects[ i ];
var vertices = object.geometry.vertices;
var faces = object.geometry.faces;
var matrixWorld = object.matrixWorld;
normalMatrixWorld.getNormalMatrix( matrixWorld );
for ( var j = 0, jl = faces.length; j < jl; j ++ ) {
var face = faces[ j ];
vector.copy( face.normal ).applyMatrix3( normalMatrixWorld ).normalize();
output += '\tfacet normal ' + vector.x + ' ' + vector.y + ' ' + vector.z + '\n';
output += '\t\touter loop\n';
var indices = [ face.a, face.b, face.c ];
for ( var k = 0; k < 3; k ++ ) {
vector.copy( vertices[ indices[ k ] ] ).applyMatrix4( matrixWorld );
output += '\t\t\tvertex ' + vector.x + ' ' + vector.y + ' ' + vector.z + '\n';
}
output += '\t\tendloop\n';
output += '\tendfacet\n';
}
}
output += 'endsolid exported\n';
return output;
}
};
}() )
};