-
Notifications
You must be signed in to change notification settings - Fork 12
/
3DObjectViewer.htm
497 lines (425 loc) · 16.9 KB
/
3DObjectViewer.htm
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D Object Viewer</title>
<link rel="stylesheet" type="text/css" href="3DObjectViewer.css">
<meta name="theme-color" content="#3f729b"/>
<meta name="viewport" content="width=380, initial-scale=1, maximum-scale=1"/>
</head>
<body>
<div id="loading" class="loading_splash">
<div class="loading_splash_container">
<div class="lds-spinner"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>
</div>
</div>
<div class="content">
<div id="container"></div>
<div id="container2">
<span id="densityLabel"></span>: <span id="densityValue"></span> g/cc <input type="submit" class="buttonChanger" onclick="moreDensity(true);" value="+"/> <input type="submit" class="buttonChanger" onclick="moreDensity(false);" value="-"/><br />
<span id="weightLabel"></span>: <span id="weightValue"></span> g<br />
<span id="volumeLabel"></span>: <span id="volumeValue"></span> cm3<br />
<span id="sizeLabel"></span>: <span id="widthValue"></span> x <span id="heightValue"></span> x <span id="depthValue"></span> cm<br />
<hr class="separator"/>
<span id="costKilogramLabel"></span>: $<span id="costKilogramValue"></span> <input type="submit" class="buttonChanger" onclick="moreCost(true);" value="+"/> <input type="submit" class="buttonChanger" onclick="moreCost(false);" value="-"/><br />
<span id="costLabel"></span>: $<span id="costValue"></span><br />
<hr class="separator"/>
<span id="diameterLabel"></span>: <span id="diameterValue"></span> mm <input type="submit" class="buttonChanger" onclick="moreDiameter(true);" value="+"/> <input type="submit" class="buttonChanger" onclick="moreDiameter(false);" value="-"/><br />
<span id="speedLabel"></span>: <span id="speedValue"></span> mm/s <input type="submit" class="buttonChanger" onclick="moreSpeed(true);" value="+"/> <input type="submit" class="buttonChanger" onclick="moreSpeed(false);" value="-"/><br />
<span id="lengthLabel"></span>: <span id="lengthValue"></span> mm<br />
<span id="timeLabel"></span>: <span id="hoursValue"></span> <span id="hoursLabel"></span> <span id="minutesValue"></span> <span id="minutesLabel"></span><br />
</div>
<div class="container3">
<div class="leftitem">
<input type="file" id="modelOBJ" onChange="runViewer();" disabled>
</div>
</div>
</div>
<script src="3DObjectViewer.js"></script>
<script>
// THE BROWSER LANGUAGE VARIABLE
var userLanguage = window.navigator.userLanguage || window.navigator.language;
var STRING_ERROR = "";
if (userLanguage.substring(0,2)=="es")
{
STRING_ERROR = "ERROR: Por favor verifique que el modelo se encuentre en formato STL, OBJ o 3DS.";
}
else
{
STRING_ERROR = "ERROR: Please check that the model is a STL, OBJ or 3DS model.";
}
var container, camera, scene, renderer, controls, light, vol, mesh, height, heightFinal, width, widthFinal, depth, depthFinal;
var density = parseFloat("1.05");
var filament_cost = parseFloat("200");
var filament_diameter = parseFloat("1.75");
var printing_speed = parseFloat("150");
if (userLanguage.substring(0,2)=="es")
{
document.getElementById("densityLabel").innerHTML = "Densidad";
document.getElementById("weightLabel").innerHTML = "Peso";
document.getElementById("volumeLabel").innerHTML = "Volumen";
document.getElementById("sizeLabel").innerHTML = "Medidas";
document.getElementById("costKilogramLabel").innerHTML = "Costo de 1 kilogramo de filamento";
document.getElementById("costLabel").innerHTML = "Costo de impresión";
document.getElementById("diameterLabel").innerHTML = "Diámetro del filamento";
document.getElementById("speedLabel").innerHTML = "Velocidad de impresión";
document.getElementById("lengthLabel").innerHTML = "Longitud de filamento";
document.getElementById("timeLabel").innerHTML = "Tiempo de impresión";
document.getElementById("hoursLabel").innerHTML = "horas";
document.getElementById("minutesLabel").innerHTML = "minutos";
}
else
{
document.getElementById("densityLabel").innerHTML = "Density";
document.getElementById("weightLabel").innerHTML = "Weight";
document.getElementById("volumeLabel").innerHTML = "Volume";
document.getElementById("sizeLabel").innerHTML = "Size";
document.getElementById("costKilogramLabel").innerHTML = "Filament cost per kilogram";
document.getElementById("costLabel").innerHTML = "Printing cost";
document.getElementById("diameterLabel").innerHTML = "Filament diameter";
document.getElementById("speedLabel").innerHTML = "Printing speed";
document.getElementById("lengthLabel").innerHTML = "Filament length";
document.getElementById("timeLabel").innerHTML = "Build time";
document.getElementById("hoursLabel").innerHTML = "hours";
document.getElementById("minutesLabel").innerHTML = "minutes";
}
function init(file)
{
container = document.getElementById("container");
container.innerHTML= "";
camera = new THREE.PerspectiveCamera(37.8,window.innerWidth/window.innerHeight,1,100000);
camera.position.z=300;
camera.position.y=-500;
camera.position.x=-500;
camera.up=new THREE.Vector3(0,0,1);
scene = new THREE.Scene();
scene.background = new THREE.Color(0x303030);
var filename = file.name;
var extension = filename.split(".").pop().toLowerCase();
var reader = new FileReader();
document.getElementById("container2").style.display="none";
if (extension=="stl")
{
// SHOWING THE LOADING SPLASH
document.getElementById("loading").style.display = "block";
// GIVES TIME TO THE UI TO SHOW THE LOADING SPLASH
setTimeout(function()
{
reader.readAsArrayBuffer(file);
}, 500);
}
else if (extension=="3ds")
{
// SHOWING THE LOADING SPLASH
document.getElementById("loading").style.display = "block";
// GIVES TIME TO THE UI TO SHOW THE LOADING SPLASH
setTimeout(function()
{
reader.readAsArrayBuffer(file);
}, 500);
}
else if (extension=="obj")
{
// SHOWING THE LOADING SPLASH
document.getElementById("loading").style.display = "block";
// GIVES TIME TO THE UI TO SHOW THE LOADING SPLASH
setTimeout(function()
{
reader.readAsText(file);
}, 500);
}
else
{
// HIDDING THE LOADING SPLASH
document.getElementById("loading").style.display = "none";
document.getElementById("container2").style.display="none";
alert(STRING_ERROR);
}
reader.addEventListener("load", function (event)
{
try
{
var contents = event.target.result;
if (extension=="obj")
{
var object = new THREE.OBJLoader().parse(contents);
var sceneConverter = new THREE.Scene();
sceneConverter.add(object);
var exporter = new THREE.STLExporter();
contents = exporter.parse(sceneConverter);
}
else if (extension=="3ds")
{
var object = new THREE.TDSLoader().parse(contents);
var sceneConverter = new THREE.Scene();
sceneConverter.add(object);
var exporter = new THREE.STLExporter();
contents = exporter.parse(sceneConverter);
}
var geometry = new THREE.STLLoader().parse(contents);
geometry.computeFaceNormals();
geometry.computeVertexNormals();
geometry.center();
var material = new THREE.MeshPhongMaterial({color:0xffffff});
mesh = new THREE.Mesh(geometry, material);
// CALCULATING THE VOLUME
vol = 0;
mesh.traverse(function (child)
{
if (child instanceof THREE.Mesh)
{
var positions = child.geometry.getAttribute("position").array;
for(var i=0;i<positions.length; i+=9)
{
var t1 = {};
t1.x = positions[i+0];
t1.y = positions[i+1];
t1.z = positions[i+2];
var t2 = {};
t2.x = positions[i+3];
t2.y = positions[i+4];
t2.z = positions[i+5];
var t3 = {};
t3.x = positions[i+6];
t3.y = positions[i+7];
t3.z = positions[i+8];
vol += signedVolumeOfTriangle(t1,t2,t3);
}
}
});
var box = new THREE.Box3().setFromObject(mesh);
height = box.max.z - box.min.z;
width = box.max.x - box.min.x;
depth = box.max.y - box.min.y;
heightFinal = height / 10;heightFinal = heightFinal.toFixed(2);
widthFinal = width / 10;widthFinal = widthFinal.toFixed(2);
depthFinal = depth / 10;depthFinal = depthFinal.toFixed(2);
var volumeFinal = vol / 1000;volumeFinal = volumeFinal.toFixed(2);
var weightFinal = volumeFinal * density;weightFinal = weightFinal.toFixed(2);
var filament_length = parseFloat(( vol / ( filament_diameter / 2 ) ^ 2 / Math.PI ) * 2 / 10).toFixed(2);
filament_length = parseFloat(filament_length).toFixed(0);
var hours = Math.floor((filament_length / printing_speed) / 60);
hours = parseFloat(hours).toFixed(0);
var minutes = (filament_length / printing_speed) % 60;
minutes = parseFloat(minutes).toFixed(0);
if (minutes==0){minutes=1;}
var finalCost = weightFinal * filament_cost / 1000;
finalCost = parseFloat(finalCost).toFixed(2);
document.getElementById("container2").style.display="block";
document.getElementById("densityValue").innerHTML = density;
document.getElementById("weightValue").innerHTML = weightFinal;
document.getElementById("volumeValue").innerHTML = volumeFinal;
document.getElementById("widthValue").innerHTML = widthFinal;
document.getElementById("depthValue").innerHTML = depthFinal;
document.getElementById("heightValue").innerHTML = heightFinal;
document.getElementById("costKilogramValue").innerHTML = filament_cost;
document.getElementById("costValue").innerHTML = finalCost;
document.getElementById("diameterValue").innerHTML = filament_diameter;
document.getElementById("speedValue").innerHTML = printing_speed;
document.getElementById("lengthValue").innerHTML = filament_length;
document.getElementById("hoursValue").innerHTML = hours;
document.getElementById("minutesValue").innerHTML = minutes;
var distance;
if (height>width && height>depth)
{
distance = height * 2;
}
else if (width>height && width>depth)
{
distance = width * 2;
}
else if (depth>height && depth>width)
{
distance = depth * 2;
}
else
{
distance = depth * 4;
}
camera.position.set(0, -distance, 0);
var x = distance + 200;
var y = distance + 200;
var division_x=Math.floor(x/10);
var division_y=Math.floor(y/10);
var wirePlane=new THREE.Mesh(new THREE.PlaneGeometry(x,y,division_x,division_y),new THREE.MeshPhongMaterial({emissive:0x707070,color:0x000000,wireframe:true,wireframeLinewidth:1}));
wirePlane.receiveShadow=true;
wirePlane.position.z = box.min.z - 0.1;
scene.add(wirePlane);
// AN ALTERNATIVE FOR MOVING THE OBJECT USING THE MOUSE WITHIN THE RENDERER
// controls = new THREE.OrbitControls(camera, renderer.domElement);
controls = new THREE.OrbitControls(camera);
controls.update();
scene.add(mesh);
// HIDDING THE LOADING SPLASH
document.getElementById("loading").style.display = "none";
}
catch(err)
{
// HIDDING THE LOADING SPLASH
document.getElementById("loading").style.display = "none";
document.getElementById("container2").style.display="none";
alert(STRING_ERROR);
}
}, false);
light = new THREE.HemisphereLight(0xE8E8E8,0x000000,1);
light.position.set(0,0,0);
scene.add(light);
renderer = new THREE.WebGLRenderer({antialias:false});
renderer.setSize(window.innerWidth,window.innerHeight);
container.appendChild(renderer.domElement);
requestAnimationFrame(animate);
window.addEventListener("resize", onWindowResize, false );
}
function animate()
{
requestAnimationFrame(animate);
light.position.copy(camera.getWorldPosition());
renderer.render(scene,camera);
}
function onWindowResize()
{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function moreDensity(a)
{
var result;
if (a==true)
{
result = parseFloat(density) + parseFloat("0.05");
if (result<=10000)
{
density = result;
}
}
else
{
result = parseFloat(density) - parseFloat("0.05");
if (result>0)
{
density = result;
}
}
density = parseFloat(density).toFixed(2);
var heightFinal = height / 10;heightFinal = heightFinal.toFixed(2);
var widthFinal = width / 10;widthFinal = widthFinal.toFixed(2);
var depthFinal = depth / 10;depthFinal = depthFinal.toFixed(2);
var volumeFinal = vol / 1000;volumeFinal = volumeFinal.toFixed(2);
var weightFinal = volumeFinal * density;weightFinal = weightFinal.toFixed(2);
document.getElementById("densityValue").innerHTML = density;
document.getElementById("weightValue").innerHTML = weightFinal;
document.getElementById("volumeValue").innerHTML = volumeFinal;
document.getElementById("widthValue").innerHTML = widthFinal;
document.getElementById("depthValue").innerHTML = depthFinal;
document.getElementById("heightValue").innerHTML = heightFinal;
updateCost();
}
function moreCost(a)
{
var result;
if (a==true)
{
result = parseFloat(filament_cost) + parseFloat("5");
if (result<=10000)
{
filament_cost = result;
}
}
else
{
result = parseFloat(filament_cost) - parseFloat("5");
if (result>0)
{
filament_cost = result;
}
}
document.getElementById("costKilogramValue").innerHTML = filament_cost;
updateCost();
}
function updateCost()
{
var volumeFinal = vol / 1000;volumeFinal = volumeFinal.toFixed(2);
var weightFinal = volumeFinal * density;weightFinal = weightFinal.toFixed(2);
var finalCost = weightFinal * filament_cost / 1000;
finalCost = parseFloat(finalCost).toFixed(2);
document.getElementById("costValue").innerHTML = finalCost;
}
function moreDiameter(a)
{
var result;
if (a==true)
{
result = parseFloat(filament_diameter) + parseFloat("0.05");
if (result<=10000)
{
filament_diameter = result;
}
}
else
{
result = parseFloat(filament_diameter) - parseFloat("0.05");
if (result>0)
{
filament_diameter = result;
}
}
filament_diameter = parseFloat(filament_diameter).toFixed(2);
var filament_length = parseFloat(( vol / ( filament_diameter / 2 ) ^ 2 / Math.PI ) * 2 / 10).toFixed(2);
filament_length = parseFloat(filament_length).toFixed(0);
var hours = Math.floor((filament_length / printing_speed) / 60);
hours = parseFloat(hours).toFixed(0);
var minutes = (filament_length / printing_speed) % 60;
minutes = parseFloat(minutes).toFixed(0);
if (minutes==0){minutes=1;}
document.getElementById("diameterValue").innerHTML = filament_diameter;
document.getElementById("lengthValue").innerHTML = filament_length;
document.getElementById("hoursValue").innerHTML = hours;
document.getElementById("minutesValue").innerHTML = minutes;
}
function moreSpeed(a)
{
var result;
if (a==true)
{
result = parseFloat(printing_speed) + parseFloat("5");
if (result<=10000)
{
printing_speed = result;
}
}
else
{
result = parseFloat(printing_speed) - parseFloat("5");
if (result>0)
{
printing_speed = result;
}
}
printing_speed = parseFloat(printing_speed).toFixed(0);
var filament_length = parseFloat(( vol / ( filament_diameter / 2 ) ^ 2 / Math.PI ) * 2 / 10).toFixed(2);
var hours = Math.floor((filament_length / printing_speed) / 60);
hours = parseFloat(hours).toFixed(0);
var minutes = (filament_length / printing_speed) % 60;
minutes = parseFloat(minutes).toFixed(0);
document.getElementById("speedValue").innerHTML = printing_speed;
document.getElementById("hoursValue").innerHTML = hours;
document.getElementById("minutesValue").innerHTML = minutes;
}
function runViewer()
{
var fileInput = document.getElementById("modelOBJ");
if (fileInput.files[0]!=null)
{
init(fileInput.files[0]);
fileInput.value = null;
}
}
window.onload = function()
{
document.getElementById("modelOBJ").disabled = false;
document.getElementById("modelOBJ").value = null;
}
</script>
</body>
</html>