-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisosurface.js
286 lines (251 loc) · 7.49 KB
/
isosurface.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
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
(function () {
//paralelization index
var n = 1,
//number of workers
nWorkers = Math.pow(8, n),
//ratio to divide the grid
den = n + 1,
//worker group
workerGroup,
//options
formData,
//3D context object
gl,
scene,
glData,
//rotation
rx = 0,
ry = 0,
rdx = 0.005,
rdy = 0.001,
//mouse position
mouseX = 0,
mouseY = 0,
//firefix
ff;
var $ = (function() {
var cache = {};
return function(d) {
if (d in cache) return cache[d];
return (cache[d] = document.getElementById(d));
};
})();
function getFormData() {
return {
rotate: $('auto-rotate').checked,
time: $('enable-time').checked,
isolevel: +$('isolevel').value,
fn: $('fn').value
};
}
//called when HMTL page is loaded
this.load = function() {
//Firefox?
ff = !document.body.innerText;
//initialize workers
workerGroup = new WorkerGroup('WorkerMarchingCube.js', nWorkers);
//initialize WebGL stuff
glData = initWebGL();
gl = glData.ctx;
scene = {
camera: new THREE.Camera(75, gl.viewportWidth / gl.viewportHeight, 0.001, 30),
lighting: {
enable: true,
ambient: [0.6, 0.6, 0.6],
directional: {
color: [0.9, 0.5, 0.0],
direction: [ Math.sin(Math.PI/4),
Math.cos(Math.PI/4),
Math.sin(Math.PI/4) ]
}
},
viewMatrix: new THREE.Matrix4,
elMatrix: new THREE.Matrix4
};
//update camera position
scene.camera.position.z = 5;
//set mouse listeners
gl.canvas.addEventListener('mousemove', function(e) {
var cameraPos = scene.camera.position;
mouseX = (e.pageX - gl.viewportWidth/2 - 200) / 400;
mouseY = (e.pageY - gl.viewportHeight/2) / 300;
cameraPos.x += (mouseX - cameraPos.x) * 0.5;
cameraPos.y += (-mouseY - cameraPos.y) * 0.5;
}, false);
gl.canvas.addEventListener(!ff? 'mousewheel' : 'DOMMouseScroll', function(e) {
e.preventDefault && e.preventDefault();
e.stopPropagation && e.stopPropagation();
var delta = e.wheelDelta? e.wheelDelta / 120 : -(e.detail || 0) / 3,
camera = scene.camera;
camera.position.z += ff? -delta/12 : -delta/2;
return false;
}, false);
//add predefined function menu listeners
var lis = document.querySelectorAll('ul.predefined-fn li');
for (var i = 0, l = lis.length; i < l; i++) {
var li = lis[i];
(function (elem) {
elem.addEventListener('click', function() {
var isolevel = $('isolevel'),
ta = $('fn'),
ans = elem.querySelectorAll('code')[0];
isolevel.value = elem.getAttribute('data-isolevel');
ta.value = ans.innerText || ans.textContent;
}, false);
})(li);
}
//get form data
formData = getFormData();
//
mapReduce();
};
function mapReduce() {
try {
var f = new Function('x,y,z,t', formData.fn);
$('fn').className = '';
} catch(e) {
$('fn').className = 'error';
}
var x = Grid.x,
xfrom = x.from,
xto = x.to,
xstep = x.step,
nx = ((xto - xfrom) / den),
y = Grid.y,
yfrom = y.from,
yto = y.to,
ystep = y.step,
ny = ((yto - yfrom) / den),
z = Grid.z,
zfrom = z.from,
zto = z.to,
zstep = z.step,
nz = ((zto - zfrom) / den);
workerGroup.map(function(nb) {
var idx = nb % den,
idy = ((nb / den) >> 0) % den,
idz = ((nb / den / den) >> 0) % den;
return {
grid: {
x: {
from: xfrom + idx * nx,
to: xfrom + idx * nx + nx,
step: xstep
},
y: {
from: yfrom + idy * ny,
to: yfrom + idy * ny + ny,
step: ystep
},
z: {
from: zfrom + idz * nz,
to: zfrom + idz * nz + nz,
step: zstep
}
},
isolevel: formData.isolevel,
fn: formData.fn,
time: formData.time
};
});
var indexAcum = 0, initialValue = {
vertices: [],
normals: [],
indices: []
};
workerGroup.reduce({
reduceFn: function (x, y) {
var l = y.vertices.length /3;
x.vertices = x.vertices.concat(y.vertices);
x.normals = x.normals.concat(y.normals);
while (l--) {
x.indices.push(indexAcum++);
}
return x;
},
initialValue: initialValue,
onComplete: render
});
}
//called once all workers information has been aggregated
function render(data) {
var vertices = data.vertices,
normals = data.normals,
indices = data.indices,
viewMatrix = scene.viewMatrix,
elMatrix = scene.elMatrix,
camera = scene.camera,
vertexBuffer = glData.vertexBuffer,
normalBuffer = glData.normalBuffer,
indexBuffer = glData.indexBuffer,
program = glData.program,
gl = glData.ctx,
lighting = scene.lighting,
fn = formData.fn,
isolevel = formData.isolevel;
//draw scene
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
//update camera position
camera.updateMatrix();
if (formData.rotate) {
rx += rdx;
ry += rdy;
//rx = Rot.x;
//ry = Rot.y;
}
// elMatrix.multiplySelf(THREE.Matrix4.translationMatrix(Rot.x,Rot.y,Rot.z));
//elMatrix.multiplySelf(THREE.Matrix4.rotationXMatrix(rx))
//elMatrix.multiplySelf(THREE.Matrix4.rotationYMatrix(ry))
elMatrix.multiply(THREE.Matrix4.rotationXMatrix(rx), THREE.Matrix4.rotationYMatrix(ry));
elMatrix.multiplySelf(THREE.Matrix4.translationMatrix(Trans.x,Trans.y,Trans.z));
viewMatrix.multiply(camera.matrix, elMatrix);
//send matrices
gl.uniformMatrix4fv(program.viewMatrix, false, viewMatrix.flatten());
gl.uniformMatrix4fv(program.projectionMatrix, false, camera.projectionMatrix.flatten());
//send normal matrix for lighting
var normalMatrix = THREE.Matrix4.makeInvert(viewMatrix);
normalMatrix.transpose();
gl.uniformMatrix4fv(program.normalMatrix, false, normalMatrix.flatten());
gl.uniform4f(program.color, 0.5, 0.5, 0.5, 1.0);
//send lighting data
gl.uniform1i(program.enableLighting, lighting.enable);
if(lighting.enable) {
//set ambient light color
if(lighting.ambient) {
var acolor = lighting.ambient;
gl.uniform3f(program.ambientColor, acolor[0], acolor[1], acolor[2]);
}
//set directional light
if(lighting.directional) {
var dir = lighting.directional,
color = dir.color,
pos = dir.direction,
vd = new THREE.Vector3(pos[0], pos[1], pos[2]).normalize();
gl.uniform3f(program.lightingDirection, vd.x, vd.y, vd.z);
gl.uniform3f(program.directionalColor, color[0], color[1], color[2]);
}
}
//send vertices
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
gl.vertexAttribPointer(program.position, 3, gl.FLOAT, false, 0, 0);
//send normals
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
gl.vertexAttribPointer(program.normal, 3, gl.FLOAT, false, 0, 0);
//send indices
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0);
//call the mapReduce to recalculate vertices and re-render the scene
formData = getFormData();
if (formData.time || fn != formData.fn || isolevel != formData.isolevel) {
setTimeout(mapReduce, 1000/30);
} else {
setTimeout(function() {
render(data);
} , 1000/30);
}
}
})();