-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
484 lines (428 loc) · 15.4 KB
/
index.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
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
const canvas = document.body.appendChild(document.createElement('canvas'));
const gl = canvas.getContext('webgl2');
const now = require('right-now');
const mat4 = require('gl-mat4');
const vec3 = require('gl-vec3');
const isosurface = require('./lib/fastIsosurface');
const shaders = require('./shaders/shaders.js');
const computeVertexNormals = require('./lib/computeVertexNormals').computeVertexNormals
var params = {
renderer: 'isosurface',
isocaps: true,
smoothing: false,
isoLevel: 0.65,
isoRange: 0.6,
raySteps: 256
};
var clipBox = {
min: [0.3, 0.3, 0.1],
max: [0.8, 0.8, 0.8]
};
var distance = 3;
var fov = 30;
var theta = 4;
var alpha = -0.5;
function ifWarn(name, str) {
if (str && /[^\s\0]/.test(str)) {
console.error(name+"\n"+str);
return str;
}
return false;
}
function createProgram(gl, vert, frag) {
var vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vs, vert);
gl.compileShader(vs);
var fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fs, frag);
gl.compileShader(fs);
var program = gl.createProgram();
gl.attachShader(program, vs);
gl.deleteShader(vs);
gl.attachShader(program, fs);
gl.deleteShader(fs);
gl.linkProgram(program);
var error = false;
error |= ifWarn('program', gl.getProgramInfoLog(program));
error |= ifWarn('vertex', gl.getShaderInfoLog(vs));
error |= ifWarn('fragment', gl.getShaderInfoLog(fs));
if (error) {
throw(new Error("Invalid shader"));
}
return program;
}
var rayProgram = createProgram(gl, shaders.rayMarch.vert, shaders.rayMarch.frag);
var isoProgram = createProgram(gl, shaders.isoSurface.vert, shaders.isoSurface.frag);
var isoCapProgram = createProgram(gl, shaders.isoCap.vert, shaders.isoCap.frag);
var down = false;
var downPos = [0, 0];
var pos = [0, 0];
var delta = [0, 0];
window.onmousedown = function(ev) {
if (ev.target.tagName === 'CANVAS') {
ev.preventDefault();
down = true;
downPos = [ev.clientX, ev.clientY];
}
};
window.onmousemove = function(ev) {
if (down) {
pos = [ev.clientX, ev.clientY];
delta = pos.map((v,i) => v - downPos[i]);
downPos = pos;
theta -= delta[0] * 0.01;
while (theta < 0) {
theta += Math.PI*2;
}
theta %= Math.PI*2;
alpha -= delta[1] * 0.01;
alpha = Math.max(-Math.PI/2, Math.min(Math.PI/2, alpha));
ev.preventDefault();
}
};
window.onmouseup = function(ev) {
if (down) {
ev.preventDefault();
down = false;
}
};
window.addEventListener('wheel', function(ev) {
ev.preventDefault();
var wheelDelta = ev.wheelDeltaY || ev.deltaY || ev.wheelDelta || ev.detail;
if (wheelDelta === undefined) {
wheelDelta = 0;
}
distance *= Math.pow(1.01, -wheelDelta);
distance = Math.max(0.5, Math.min(10, distance));
}, false);
var controls = document.createElement('form');
controls.className = 'controls';
controls.style.position = 'absolute';
controls.style.zIndex = 1;
controls.style.left = '10px';
controls.style.top = '10px';
controls.appendChild(createSlider('Min X', clipBox.min, 0, 0, 1));
controls.appendChild(createSlider('Min Y', clipBox.min, 1, 0, 1));
controls.appendChild(createSlider('Min Z', clipBox.min, 2, 0, 1));
controls.appendChild(createSlider('Max X', clipBox.max, 0, 0, 1));
controls.appendChild(createSlider('Max Y', clipBox.max, 1, 0, 1));
controls.appendChild(createSlider('Max Z', clipBox.max, 2, 0, 1));
controls.appendChild(createSlider('Iso level', params, 'isoLevel', 0, 1));
controls.appendChild(createSlider('Iso range', params, 'isoRange', 0, 1));
controls.appendChild(createRadioGroup('Renderer', params, 'renderer',
[
['Isosurface', 'isosurface'],
['Volume', 'volume']
]
));
controls.appendChild(createCheckbox('Smoothing', params, 'smoothing'));
controls.appendChild(createCheckbox('Isocaps', params, 'isocaps'));
controls.appendChild(createSlider('Raymarch steps', params, 'raySteps', 32, 384));
document.body.appendChild(controls);
function createSlider(name, targetObj, targetValue, minValue, maxValue) {
var reader = (x) => parseFloat(x.value);
return createInput(name, targetObj, targetValue, 'range', reader, {
min: minValue,
max: maxValue,
step: 0.001,
value: targetObj[targetValue]
});
}
function createCheckbox(name, targetObj, targetValue) {
var reader = (x) => x.checked;
return createInput(name, targetObj, targetValue, 'checkbox', reader, {
checked: targetObj[targetValue]
});
}
function createRadioGroup(name, targetObj, targetValue, options) {
var div = document.createElement('div');
var title = document.createElement('h4');
title.textContent = name;
div.appendChild(title);
options.forEach(function(opt) {
var [oname, ovalue] = opt;
var inputContainer = document.createElement('div');
var label = document.createElement('label');
label.textContent = ' ' + oname;
var input = document.createElement('input');
input.name = name;
input.type = 'radio';
input.value = ovalue;
input.checked = targetObj[targetValue] === ovalue;
input.oninput = input.onchange = function(ev) {
targetObj[targetValue] = this.form[name].value;
};
inputContainer.appendChild(input);
inputContainer.appendChild(label);
div.appendChild(inputContainer);
});
return div;
}
function createInput(name, targetObj, targetValue, type, reader, params) {
var inputContainer = document.createElement('div');
var label = document.createElement('label');
label.for = name;
label.textContent = name;
var input = document.createElement('input');
input.id = name;
input.type = type;
for (var i in params) {
input[i] = params[i];
}
input.oninput = input.onchange = function(ev) {
targetObj[targetValue] = reader(this);
};
inputContainer.appendChild(label);
inputContainer.appendChild(input);
return inputContainer;
}
var getData = function(fn, responseType, callback) {
if (!callback) {
callback = responseType;
responseType = 'text';
}
var xhr = new XMLHttpRequest;
xhr.responseType = responseType;
xhr.onload = function() {
callback(xhr.response);
};
xhr.open('GET', fn, true);
xhr.send();
};
var getDataMulti = function(fns, responseType, callback) {
if (!callback) {
callback = responseType;
responseType = 'text';
}
var todo = fns.length;
var results = [];
var handle = function(idx) {
return function(data) {
results[idx] = data;
todo--;
if (todo === 0) {
callback(results);
}
};
};
fns.forEach(function(fn, idx) { getData(fn, responseType, handle(idx)); });
};
var parseCSV = function(str) {
return str.replace(/^\s+|\s+$/g, '').split(/\r?\n/g).map(function(x) { return x.split(',').map(parseFloat) });
};
getData('data/MRbrain.txt', 'arraybuffer', function(mriBuffer) {
const dims = [256, 256, 109];
const [dataWidth, dataHeight, dataDepth] = dims;
const mri = new Uint16Array(mriBuffer);
for (var i=0; i<mri.length; i++) {
mri[i] = ((mri[i] << 8) & 0xff00) | (mri[i] >> 8);
}
var triangleMatrix = mat4.create()
var projectionMatrix = mat4.create()
// ISOSURFACE TODO
/*
[x] Isosurface using marching cubes
[x] Caps with alpha mask and clip box test
- looks bad, doesn't seal the volume
[x] Update isosurface on params changes
- it's very slow
[ ] Generate cap geometry
- Generate isosurface
- Raycast along clip plane and count intersections
- Generate triangles for rays with odd number of intersections
[x] Generate smooth vertex normals for the isosurface
- Find the faces that share the vertex
- Average the face normals
[ ] Experiment with marching cubes in the ray marcher
*/
var bounds = [
[0,0,0],
[dataWidth, dataHeight, dataDepth]
];
function updateIsosurface(gl, isoBuffer, isoNormalBuffer, dims, mri, clipBox) {
var isoLevel = ((params.isoLevel * 2000)+1300)|0;
var isoRange = (params.isoRange * 2000)|0;
var ibounds = [[0,0,0], dims];
var iso = isosurface.marchingCubes(dims, mri, isoLevel-isoRange, isoLevel+isoRange, ibounds);
if (params.smoothing) {
computeVertexNormals(iso.vertices, iso.normals, iso.normals);
}
gl.bindBuffer(gl.ARRAY_BUFFER, isoBuffer);
gl.bufferData(gl.ARRAY_BUFFER, iso.vertices, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, isoNormalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, iso.normals, gl.STATIC_DRAW);
return iso.vertices.length / 3;
}
function updateIsoCaps(gl, isoCapBuffer, isoCapNormalBuffer, dims, mri, clipBox) {
var isoLevel = ((params.isoLevel * 2000)+1300)|0;
var isoRange = (params.isoRange * 2000)|0;
var ibounds = [
clipBox.min.map((v,i) => Math.floor(v * dims[i])),
clipBox.max.map((v,i) => Math.ceil(v * dims[i])),
];
var cap = isosurface.marchingCubesCaps(dims, mri, isoLevel-isoRange, isoLevel+isoRange, ibounds);
gl.bindBuffer(gl.ARRAY_BUFFER, isoCapBuffer);
gl.bufferData(gl.ARRAY_BUFFER, cap.vertices, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, isoCapNormalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, cap.normals, gl.STATIC_DRAW);
return cap.vertices.length / 3;
}
var data = new Uint8Array(dataWidth*dataHeight*dataDepth*4);
var mriMin = 1/0;
var mriMax = -1/0;
for (var i=0; i<mri.length; i++) {
var v = mri[i];
if (v < mriMin) mriMin = v;
if (v > mriMax) mriMax = v;
}
for (var i=0; i<mri.length; i++) {
var v = Math.max(0, Math.min(1, (mri[i]-1300)/2000)) * 255;
data[i*4] = v;
data[i*4+1] = v;
data[i*4+2] = v;
data[i*4+3] = v > 0 ? 255 : 0;
}
var texture = gl.createTexture();
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_3D, texture);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_BASE_LEVEL, 0);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MAX_LEVEL, Math.log2(dataWidth));
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); //_MIPMAP_LINEAR);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texImage3D(
gl.TEXTURE_3D, // target
0, // level
gl.RGBA, // internalformat
dataWidth, // width
dataHeight, // height
dataDepth, // depth
0, // border
gl.RGBA, // format
gl.UNSIGNED_BYTE, // type
data // pixel
);
//gl.generateMipmap(gl.TEXTURE_3D);
var triangleBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, triangleBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
-1.0, +1.0, +0.0,
-1.0, -1.0, +0.0,
+1.0, -1.0, +0.0,
-1.0, +1.0, +0.0,
+1.0, -1.0, +0.0,
+1.0, +1.0, +0.0
]), gl.STATIC_DRAW);
var isoBuffer = gl.createBuffer();
var isoNormalBuffer = gl.createBuffer();
var isoCapBuffer = gl.createBuffer();
var isoCapNormalBuffer = gl.createBuffer();
var isoVerticeCount = 0;
var isoCapVerticeCount = 0;
var currentIsoLevel;
var currentIsoRange;
var currentSmoothing;
var currentBounds;
gl.getCachedUniformLocation = function(program, name) {
if (!program.uniformLocations) {
program.uniformLocations = {};
}
if (!program.uniformLocations[name]) {
program.uniformLocations[name] = this.getUniformLocation(program, name);
}
return program.uniformLocations[name];
};
function setUniforms(gl, program, width, height) {
gl.useProgram(program);
gl.uniform1i(gl.getCachedUniformLocation(program, 'uTexture'), 0);
gl.uniform1i(gl.getCachedUniformLocation(program, 'uIsocaps'), params.isocaps ? 1 : 0);
gl.uniform1f(gl.getCachedUniformLocation(program, 'uTime'), now() / 1000.0);
gl.uniform1f(gl.getCachedUniformLocation(program, 'uIsoLevel'), params.isoLevel);
gl.uniform1f(gl.getCachedUniformLocation(program, 'uIsoRange'), params.isoRange);
gl.uniform1f(gl.getCachedUniformLocation(program, 'uRaySteps'), params.raySteps);
gl.uniform2f(gl.getCachedUniformLocation(program, 'uResolution'), width, height);
gl.uniform3fv(gl.getCachedUniformLocation(program, 'uDimensions'), dims);
gl.uniform3fv(gl.getCachedUniformLocation(program, 'uClipBoxMin'), clipBox.min);
gl.uniform3fv(gl.getCachedUniformLocation(program, 'uClipBoxMax'), clipBox.max);
gl.uniform3fv(gl.getCachedUniformLocation(program, 'uLightPosition'), [1,1,1]);
gl.uniform4fv(gl.getCachedUniformLocation(program, 'uLightColor'), [1,1,1,1]);
gl.uniformMatrix4fv(gl.getCachedUniformLocation(program, 'uProjection'), false, projectionMatrix);
gl.uniformMatrix4fv(gl.getCachedUniformLocation(program, 'uModelView'), false, triangleMatrix);
}
function setBuffer(gl, location, buffer, a,b,c,d,e) {
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.enableVertexAttribArray(location);
gl.vertexAttribPointer(location, a, b, c, d, e);
}
function render() {
if (params.renderer === 'isosurface') {
var needIsosurfaceUpdate = (
params.smoothing !== currentSmoothing ||
params.isoLevel !== currentIsoLevel ||
params.isoRange !== currentIsoRange
);
var needIsoCapsUpdate = (
needIsosurfaceUpdate ||
JSON.stringify(clipBox) !== currentBounds
);
if (needIsosurfaceUpdate) {
isoVerticeCount = updateIsosurface(gl, isoBuffer, isoNormalBuffer, dims, mri, clipBox);
currentIsoLevel = params.isoLevel;
currentIsoRange = params.isoRange;
currentSmoothing = params.smoothing;
}
if (needIsoCapsUpdate) {
isoCapVerticeCount = updateIsoCaps(gl, isoCapBuffer, isoCapNormalBuffer, dims, mri, clipBox);
currentBounds = JSON.stringify(clipBox);
}
}
var width = gl.drawingBufferWidth;
var height = gl.drawingBufferHeight;
// Clear the screen and set the viewport before
// drawing anything
gl.clearColor(1,1,1,1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
gl.viewport(0, 0, width, height);
gl.enable(gl.DEPTH_TEST);
gl.depthMask(true);
// Calculate projection matrix
mat4.perspective(projectionMatrix, fov / 180 * Math.PI, width / height, 0.1, 100);
var pos = [
Math.cos(theta) * Math.cos(alpha) * distance,
Math.sin(alpha) * distance,
Math.sin(theta) * Math.cos(alpha) * distance
];
mat4.lookAt(triangleMatrix, pos, [0,0,0], [0,-1,0]);
mat4.translate(triangleMatrix, triangleMatrix, [-0.5, -0.5, -0.275]);
mat4.scale(triangleMatrix, triangleMatrix, [1, 1, 0.55]);
if (params.renderer === 'volume') {
setUniforms(gl, rayProgram, width, height);
setBuffer(gl, 0, triangleBuffer, 3, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
if (params.renderer === 'isosurface') {
setUniforms(gl, isoProgram, width, height);
setBuffer(gl, 0, isoBuffer, 3, gl.FLOAT, false, 0, 0);
setBuffer(gl, 1, isoNormalBuffer, 3, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, isoVerticeCount);
if (params.isocaps && isoCapVerticeCount > 0) {
setUniforms(gl, isoCapProgram, width, height);
setBuffer(gl, 0, isoCapBuffer, 3, gl.FLOAT, false, 0, 0);
setBuffer(gl, 1, isoCapNormalBuffer, 3, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, isoCapVerticeCount);
}
}
}
// Resize the canvas to fit the screen
window.addEventListener('resize'
, require('canvas-fit')(canvas)
, false
)
function tick() {
render();
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
})