-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.html
294 lines (260 loc) · 12 KB
/
index.html
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!doctype html>
<html lang="en">
<head>
<title>stl viewer</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<script src="three.js"></script>
<script src="stats.js"></script>
<script src="detector.js"></script>
<script>
var camera, scene, renderer,
geometry, material, mesh, light1, stats;
function trim (str) {
str = str.replace(/^\s+/, '');
for (var i = str.length - 1; i >= 0; i--) {
if (/\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}
return str;
}
// Notes:
// - STL file format: http://en.wikipedia.org/wiki/STL_(file_format)
// - 80 byte unused header
// - All binary STLs are assumed to be little endian, as per wiki doc
var parseStlBinary = function(stl) {
var geo = new THREE.Geometry();
var dv = new DataView(stl, 80); // 80 == unused header
var isLittleEndian = true;
var triangles = dv.getUint32(0, isLittleEndian);
// console.log('arraybuffer length: ' + stl.byteLength);
// console.log('number of triangles: ' + triangles);
var offset = 4;
for (var i = 0; i < triangles; i++) {
// Get the normal for this triangle
var normal = new THREE.Vector3(
dv.getFloat32(offset, isLittleEndian),
dv.getFloat32(offset+4, isLittleEndian),
dv.getFloat32(offset+8, isLittleEndian)
);
offset += 12;
// Get all 3 vertices for this triangle
for (var j = 0; j < 3; j++) {
geo.vertices.push(
new THREE.Vector3(
dv.getFloat32(offset, isLittleEndian),
dv.getFloat32(offset+4, isLittleEndian),
dv.getFloat32(offset+8, isLittleEndian)
)
);
offset += 12
}
// there's also a Uint16 "attribute byte count" that we
// don't need, it should always be zero.
offset += 2;
// Create a new face for from the vertices and the normal
geo.faces.push(new THREE.Face3(i*3, i*3+1, i*3+2, normal));
}
// The binary STL I'm testing with seems to have all
// zeroes for the normals, unlike its ASCII counterpart.
// We can use three.js to compute the normals for us, though,
// once we've assembled our geometry. This is a relatively
// expensive operation, but only needs to be done once.
geo.computeFaceNormals();
mesh = new THREE.Mesh(
geo,
// new THREE.MeshNormalMaterial({
// overdraw:true
// }
new THREE.MeshLambertMaterial({
overdraw:true,
color: 0xaa0000,
shading: THREE.FlatShading
}
));
scene.add(mesh);
stl = null;
};
var parseStl = function(stl) {
var state = '';
var lines = stl.split('\n');
var geo = new THREE.Geometry();
var name, parts, line, normal, done, vertices = [];
var vCount = 0;
stl = null;
for (var len = lines.length, i = 0; i < len; i++) {
if (done) {
break;
}
line = trim(lines[i]);
parts = line.split(' ');
switch (state) {
case '':
if (parts[0] !== 'solid') {
console.error(line);
console.error('Invalid state "' + parts[0] + '", should be "solid"');
return;
} else {
name = parts[1];
state = 'solid';
}
break;
case 'solid':
if (parts[0] !== 'facet' || parts[1] !== 'normal') {
console.error(line);
console.error('Invalid state "' + parts[0] + '", should be "facet normal"');
return;
} else {
normal = [
parseFloat(parts[2]),
parseFloat(parts[3]),
parseFloat(parts[4])
];
state = 'facet normal';
}
break;
case 'facet normal':
if (parts[0] !== 'outer' || parts[1] !== 'loop') {
console.error(line);
console.error('Invalid state "' + parts[0] + '", should be "outer loop"');
return;
} else {
state = 'vertex';
}
break;
case 'vertex':
if (parts[0] === 'vertex') {
geo.vertices.push(new THREE.Vector3(
parseFloat(parts[1]),
parseFloat(parts[2]),
parseFloat(parts[3])
));
} else if (parts[0] === 'endloop') {
geo.faces.push( new THREE.Face3( vCount*3, vCount*3+1, vCount*3+2, new THREE.Vector3(normal[0], normal[1], normal[2]) ) );
vCount++;
state = 'endloop';
} else {
console.error(line);
console.error('Invalid state "' + parts[0] + '", should be "vertex" or "endloop"');
return;
}
break;
case 'endloop':
if (parts[0] !== 'endfacet') {
console.error(line);
console.error('Invalid state "' + parts[0] + '", should be "endfacet"');
return;
} else {
state = 'endfacet';
}
break;
case 'endfacet':
if (parts[0] === 'endsolid') {
//mesh = new THREE.Mesh( geo, new THREE.MeshNormalMaterial({overdraw:true}));
mesh = new THREE.Mesh(
geo,
new THREE.MeshLambertMaterial({
overdraw:true,
color: 0xaa0000,
shading: THREE.FlatShading
}
));
scene.add(mesh);
done = true;
} else if (parts[0] === 'facet' && parts[1] === 'normal') {
normal = [
parseFloat(parts[2]),
parseFloat(parts[3]),
parseFloat(parts[4])
];
if (vCount % 1000 === 0) {
console.log(normal);
}
state = 'facet normal';
} else {
console.error(line);
console.error('Invalid state "' + parts[0] + '", should be "endsolid" or "facet normal"');
return;
}
break;
default:
console.error('Invalid state "' + state + '"');
break;
}
}
};
init();
animate();
function init() {
//Detector.addGetWebGLMessage();
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 70;
camera.position.y = 0;
scene.add( camera );
var directionalLight = new THREE.DirectionalLight( 0xffffff );
directionalLight.position.x = 0;
directionalLight.position.y = 0;
directionalLight.position.z = 1;
directionalLight.position.normalize();
scene.add( directionalLight );
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if ( xhr.readyState == 4 ) {
if ( xhr.status == 200 || xhr.status == 0 ) {
var rep = xhr.response; // || xhr.mozResponseArrayBuffer;
console.log(rep);
parseStlBinary(rep);
//parseStl(xhr.responseText);
mesh.rotation.x = 5;
mesh.rotation.z = .25;
console.log('done parsing');
}
}
}
xhr.onerror = function(e) {
console.log(e);
}
xhr.open( "GET", 'Octocat-v1.stl', true );
xhr.responseType = "arraybuffer";
//xhr.setRequestHeader("Accept","text/plain");
//xhr.setRequestHeader("Content-Type","text/plain");
//xhr.setRequestHeader('charset', 'x-user-defined');
xhr.send( null );
renderer = new THREE.WebGLRenderer(); //new THREE.CanvasRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
document.body.appendChild(stats.domElement);
}
function animate() {
// note: three.js includes requestAnimationFrame shim
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
//mesh.rotation.x += 0.01;
if (mesh) {
mesh.rotation.z += 0.02;
}
//light1.position.z -= 1;
renderer.render( scene, camera );
}
</script>
</body>
</html>