-
Notifications
You must be signed in to change notification settings - Fork 0
/
meshViewer.html
349 lines (239 loc) · 8.75 KB
/
meshViewer.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
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
<html>
<head onload="init()">
<title>MeshViewer</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<canvas id="myCanvas" ondrag="dragging(event)"></canvas>
<input type="file" name="file" id="file" style="position:fixed;top:0px;left:0px;color:white">
<div style="position:fixed;bottom:0px;left:0px;padding-bottom:10px;">
<button onclick="zoomin()">Zoom In</button>
<button onclick="zoomout()">Zoom Out</button>
</div>
<div style="position:fixed;top:0px;right:0px;padding-top:10px;color:white;padding-right:10px">
<p>Open .obj file</p>
<p>Press Directional Keys to move</p>
<p>Press +/- to Zoom In/Zoom Out</p>
<p>Press x/y/z to Rotate across X/Y/Z axis</p>
</div>
<div style="position:fixed;bottom:0px;right:0px;padding-bottom:10px;color:white;padding-right:10px">
Currently Displaying in <span id="modeTypeText">Mesh</span> Mode<br>
<button onclick="enableMeshMode()" id="meshButt" disabled>Mesh Mode</button>
<button onclick="enableTextureMode()" id="textureButt">Texture Mode</button>
</div>
<script src="three.js"></script>
<script>
var myCanvas = document.getElementById('myCanvas');
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, (window.innerWidth/window.innerHeight), 0.15, 3000 );
camera.position.set( 0, 0, 0 );
camera.lookAt( 0, 0, 0 );
var renderer = new THREE.WebGLRenderer({canvas: myCanvas, antialias: true});
renderer.setSize( window.innerWidth, window.innerHeight );
//document.body.appendChild( renderer.domElement );
//scene.normalize();
var materialLine1 = new THREE.LineBasicMaterial( { color: 0xffffff } );
var lineGeometry1 = new THREE.Geometry();
lineGeometry1.vertices.push(new THREE.Vector3(0,0,0));
var line1 = new THREE.Line( lineGeometry1, materialLine1 );
scene.add( line1 );
/*
var geometry = new THREE.BoxGeometry( 7, 7, 7 );
var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );*/
var light = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(light);
var focusPoint;
var createTriangle = function(index1,index2,index3){
//console.log(count);
//count++;
var vector1=new THREE.Vector3( modelVertices[index1][0], modelVertices[index1][1], modelVertices[index1][2]);
var vector2=new THREE.Vector3( modelVertices[index2][0], modelVertices[index2][1], modelVertices[index2][2]);
var vector3=new THREE.Vector3( modelVertices[index3][0], modelVertices[index3][1], modelVertices[index3][2]);
var materialLine = new THREE.LineBasicMaterial( { color: 0xffffff } );
var lineGeometry = new THREE.Geometry();
lineGeometry.vertices.push(vector1);
lineGeometry.vertices.push(vector2);
lineGeometry.vertices.push(vector3);
lineGeometry.vertices.push(vector1);
var line = new THREE.Line( lineGeometry, materialLine );
line.position.z = -70;
scene.add( line );
focusPoint=line;
//render();
}
var createTriangleWithTexture = function(index1,index2,index3){
var vector1=new THREE.Vector3( modelVertices[index1][0], modelVertices[index1][1], modelVertices[index1][2]);
var vector2=new THREE.Vector3( modelVertices[index2][0], modelVertices[index2][1], modelVertices[index2][2]);
var vector3=new THREE.Vector3( modelVertices[index3][0], modelVertices[index3][1], modelVertices[index3][2]);
//CREATING GEOMETRY
var geometry = new THREE.Geometry();
geometry.vertices.push(vector1,vector2,vector3);
geometry.faces.push( new THREE.Face3( 0, 1, 2 ) );
var material = new THREE.MeshLambertMaterial({color: Math.random() *0xFFFFFF});
var mesh = new THREE.Mesh(geometry, material);
mesh.position.z = -70;
scene.add(mesh);
}
function render() {
renderer.render(scene, camera);
requestAnimationFrame(render);
}
var canvasCenter={
x:0,
y:0
}
function init(){
canvasCenter.x=myCanvas.width/2;
canvasCenter.y=myCanvas.height/2;
}
var triangleCords=[]
function createCircle(x,y,z){
var geometry = new THREE.CircleGeometry( 1, 32 );
var material = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
var circle = new THREE.Mesh( geometry, material );
scene.add( circle );
circle.position.x=x;
circle.position.y=y;
//circle.position.z=z;
}
var modelVertices=[];
var modelFaces=[];
function addVertice(vert){
modelVertices.push(vert);
//console.log(vert);
}
document.getElementById('file').onchange = function(){
var file = this.files[0];
var reader = new FileReader();
reader.onload = function(progressEvent){
// Entire file
//console.log(this.result);
var reg = /[+-]?\d+(\.\d+)?/g;
// By lines
var lines = this.result.split('\n');
for(var line = 0; line < lines.length; line++){
if (lines[line].beginsWith('v')) {
//console.log(lines[line]);
var vertex=lines[line].match(reg);
//createCircle(parseFloat(vertex[0]),parseFloat(vertex[1]),parseFloat(vertex[2]));
addVertice(vertex);
}
else if(lines[line].beginsWith('f')){
var vertex=lines[line].match(reg);
modelFaces.push(vertex);
//console.log(parseInt(vertex[0]));
//if(count<1000){
if(isTexture==false){
createTriangle(parseInt(vertex[0])-1,parseInt(vertex[1])-1,parseInt(vertex[2])-1);
}
else{
createTriangleWithTexture(parseInt(vertex[0])-1,parseInt(vertex[1])-1,parseInt(vertex[2])-1);
}
//}
}
}
render();
};
reader.readAsText(file);
};
String.prototype.beginsWith = function (string) {
return(this.indexOf(string) === 0);
};
async function zoomin(){
camera.position.z -= 2;
}
async function zoomout(){
camera.position.z += 2;
}
async function cameraMoveLeft(){
camera.position.x += 0.5;
}
async function cameraMoveRight(){
camera.position.x -= 0.5;
}
async function cameraMoveUp(){
camera.position.y -= 0.5;
}
async function cameraMoveDown(){
camera.position.y += 0.5;
}
document.onkeydown = checkKey;
function checkKey(e) {
console.log(e.keyCode)
var event = window.event ? window.event : e;
if (e.keyCode == '107') {
zoomin();
}
else if (e.keyCode == '109') {
zoomout();
}
else if (e.keyCode == '38') {
// up arrow
cameraMoveDown();
}
else if (e.keyCode == '40') {
cameraMoveUp();
}
else if (e.keyCode == '37') {
cameraMoveRight();
}
else if (e.keyCode == '39') {
cameraMoveLeft();
}
else if (e.keyCode == '88') {
camera.rotation.x+=0.01;
}
else if (e.keyCode == '89') {
camera.rotation.y+=0.01;
}
else if (e.keyCode == '90') {
camera.rotation.z+=0.01;
}
else if (e.keyCode == '16') {
camera.rotation.x+=1.57;
}
}
var isTexture=false;
function enableTextureMode(){
isTexture=true;
document.getElementById("meshButt").disabled=false;
document.getElementById("textureButt").disabled=true;
console.log(scene.children)
var ambChild;
while(scene.children.length > 0){
if(scene.children[0].type=="AmbientLight"){
ambChild=scene.children[0];
}
scene.remove(scene.children[0]);
}
scene.add(ambChild);
for(var i=0;i<modelFaces.length;i++){
createTriangleWithTexture(modelFaces[i][0]-1,modelFaces[i][1]-1,modelFaces[i][2]-1);
}
document.getElementById("modeTypeText").innerHTML="Texture";
}
function enableMeshMode(){
isTexture=false;
document.getElementById("meshButt").disabled=true;
document.getElementById("textureButt").disabled=false;
var ambChild;
while(scene.children.length > 0){
if(scene.children[0].type=="AmbientLight"){
ambChild=scene.children[0];
}
scene.remove(scene.children[0]);
}
scene.add(ambChild);
for(var i=0;i<modelFaces.length;i++){
createTriangle(modelFaces[i][0],modelFaces[i][1],modelFaces[i][2]);
}
document.getElementById("modeTypeText").innerHTML="Mesh";
}
</script>
</body>
</html>