-
Notifications
You must be signed in to change notification settings - Fork 0
/
raymarching.html
574 lines (500 loc) · 15.9 KB
/
raymarching.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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<html>
<head>
<title>RayMarching example</title>
</head>
<script type="x-shader/x-vertex" id="passThruVS">
attribute vec2 coord;
varying vec2 pos;
void main(void) {
pos = coord;
gl_Position = vec4(coord, 0, 1.0);
gl_PointSize = 1.0;
}
</script>
<script type="x-shader/x-fragment" id="rayMarchingFS">
precision highp float;
varying vec2 pos;
const float THRESHOLD = 1e-2;
const int MAX_STEP = 100;
struct PointLight {
vec3 location;
vec3 color;
float attenuation;
};
struct Object {
// object origin
vec3 offs;
mat3 rs;
vec3 color;
// Light coefficients
float ambient, diffuse, specular, reflect;
};
struct Camera {
vec3 location;
mat3 rotation;
};
struct Ray {
vec3 origin, dir;
};
struct SurfaceProps {
// Light coefficients
float ambient, diffuse, specular, reflect;
vec3 point;
vec3 color;
vec3 norm;
};
// Scene defenition
const int numLights = 2;
const int numObjects = 4;
uniform Object objects[numObjects];
uniform PointLight lights[numLights];
uniform Camera camera;
float sqr(float x) { return x*x; }
float sqr(vec3 x) { return dot(x, x);}
float sphereSDF(vec3 p) {
return length(p) - 1.0;
}
float bumpySphereSDF(vec3 p) {
const float freq = 5.5;
const float amp = .4;
return length(p) - 1.0 - amp * sin(freq * p.x)*sin(freq * p.y)*sin(freq * p.z);
}
float planeSDF(vec3 p) {
return p.y + 1.0;
}
float torusSDF(vec3 p, vec2 t) {
vec2 q = vec2(length(p.xy) - t.x, p.z);
return length(q) - t.y;
}
// First argument is the distance, but 2nd is material
vec2 sdfUnion(vec2 a, vec2 b) { return a.x < b.x ? a : b; }
vec2 sdfUnion(vec2 a, vec2 b, vec2 c) { return sdfUnion(a, sdfUnion(b, c)); }
vec2 sdfUnion(vec2 a, vec2 b, vec2 c, vec2 d) { return sdfUnion(sdfUnion(a, b), sdfUnion(c, d)); }
vec2 sdf(vec3 p) {
return sdfUnion(vec2(bumpySphereSDF(objects[0].rs*(p-objects[0].offs)), 0.0),
vec2(torusSDF(objects[1].rs*(p-objects[1].offs), vec2(1.0, .4)), 1.0),
vec2(sphereSDF(objects[2].rs*(p-objects[2].offs)), 2.0),
vec2(planeSDF(p), 3.0));
}
vec3 computeNormal(vec3 p) {
// Normal can be computed by sampling SDF around the surface
vec3 rc = vec3(0.0);
vec3 onehot[3];
onehot[0] = vec3(THRESHOLD, 0.0, 0.0);
onehot[1] = vec3(0.0, THRESHOLD, 0.0);
onehot[2] = vec3(0.0, 0.0, THRESHOLD);
for(int i = 0; i < 3; ++i) {
vec3 e = onehot[i];
rc += e * sdf(p + e).x - e * sdf(p - e).x;
}
return normalize(rc);
}
void FillProps(in vec3 point, in float mat, out SurfaceProps props) {
props.point = point;
props.norm = computeNormal(point);
int matIdx = int(floor(mat));
for(int idx = 0 ; idx < numObjects; idx++) {
if (idx != matIdx) continue;
props.color = objects[idx].color;
props.ambient = objects[idx].ambient;
props.diffuse = objects[idx].diffuse;
props.specular = objects[idx].specular;
props.reflect = objects[idx].reflect;
}
// Implement checkerboard pattern for plane
if (matIdx == 3) {
if (sign((fract(.2*point.x)-.5)*(fract(.2*point.z)-.5)) > 0.0) {
props.color = vec3(1.0) - props.color;
}
}
}
vec2 computeDistance(in Ray ray) {
float distance = 2.0*THRESHOLD;
for(int step = 0; step < MAX_STEP; ++step) {
vec2 rc= sdf(ray.origin + distance * ray.dir);
if (abs(rc.x) < THRESHOLD) {
return vec2(distance, rc.y);
}
distance += rc.x;
}
return vec2(-1.0);
}
bool Scene_Intersect(in Ray ray) {
return computeDistance(ray).x >= 0.0;
}
float Scene_Intersect(in Ray ray, out SurfaceProps props) {
vec2 rc = computeDistance(ray);
if (rc.x >= 0.0) {
FillProps(ray.origin + rc.x * ray.dir, rc.y, props);
}
return rc.x;
}
void main(void) {
Ray ray = Ray(camera.location, normalize(camera.rotation * vec3(.6 * pos.x, .6 * pos.y, 1.0)));
vec3 color = vec3(0.0);
float weight = 1.0;
SurfaceProps props;
for(int depth = 0; depth < 5; ++depth) {
float distance = Scene_Intersect(ray, props);
if (distance < 0.0) {
color += weight * vec3(0.196, 0.6, 0.8);
break;
}
color += weight * props.ambient * props.color;
for(int idx = 0; idx < numLights; ++idx) {
vec3 lightDir = lights[idx].location - props.point;
Ray lightRay = Ray(props.point, normalize(lightDir));
// Check if light is occluded
if (Scene_Intersect(lightRay)) continue;
float attenuation = sqr(lights[idx].attenuation)/dot(lightDir, lightDir);
float angle = abs(dot(lightRay.dir, props.norm));
if (angle < THRESHOLD) continue;
// Add diffuse light component
if (props.diffuse >= THRESHOLD)
color += weight*props.diffuse*angle*attenuation*lights[idx].color*props.color;
// Add specular light component using Blinn–Phong reflection mode
if (props.specular >= THRESHOLD) {
vec3 halfway = normalize(lightRay.dir - ray.dir);
color += weight*props.specular*attenuation*pow(dot(halfway, props.norm), 30.0)*lights[idx].color;
}
}
if (weight * props.reflect < THRESHOLD) {
break;
}
float viewAngle = dot(ray.dir, props.norm);
weight *= props.reflect;
ray.dir -= props.norm * 2.0 * viewAngle;
ray.origin = props.point;
}
gl_FragColor = vec4(color, 1.0);
}
</script>
<script type="text/javascript">
function rotateX(angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
return [
1, 0, 0,
0, cos, -sin,
0, sin, cos,
];
}
function rotateY(angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
return [
cos, 0, sin,
0, 1, 0,
-sin, 0, cos,
];
}
function rotateZ(angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
return [
cos, -sin, 0,
sin, cos, 0,
0, 0, 1,
];
}
function identity(scale=1) {
return [
scale, 0, 0,
0, scale, 0,
0, 0, scale,
];
}
function mulMatVec(mat, vec) {
return [
vec[0]*mat[0] + vec[1]*mat[1] + vec[2]*mat[2],
vec[0]*mat[3] + vec[1]*mat[4] + vec[2]*mat[5],
vec[0]*mat[6] + vec[1]*mat[7] + vec[2]*mat[8],
];
}
function mulMatMat(mata, matb) {
const c1 = mulMatVec(mata, [matb[0], matb[3], matb[6]])
const c2 = mulMatVec(mata, [matb[1], matb[4], matb[7]])
const c3 = mulMatVec(mata, [matb[2], matb[5], matb[8]])
return [
c1[0], c2[0], c3[0],
c1[1], c2[1], c3[1],
c1[2], c2[2], c3[2],
];
}
class GLContext {
constructor(canvas) {
if (canvas == null)
throw 'Can not find canvas';
const ctx = canvas.getContext('webgl');
if (ctx == null)
throw 'Can not get WebGL context...';
ctx.viewport(0, 0, canvas.clientWidth, canvas.clientHeight);
ctx.clearColor(0, 0, 0, 1.0);
ctx.clear(ctx.COLOR_BUFFER_BIT);
this._ctx = ctx;
this.PRIMITIVES = {
POINTS: ctx.POINTS,
LINE_STRIP: ctx.LINE_STRIP,
LINES: ctx.LINES,
TRIANGLE_STRIP: ctx.TRIANGLE_STRIP,
TRIANGLES: ctx.TRIANGLES,
};
this.VERTEX_SHADER = ctx.VERTEX_SHADER;
this.FRAGMENT_SHADER = ctx.FRAGMENT_SHADER;
}
getGLContext() {
if (!this._ctx) throw 'Missing context';
return this._ctx;
}
compileShader(type, source) {
const glCtx = this.getGLContext();
var rc = glCtx.createShader(type);
glCtx.shaderSource(rc, source);
glCtx.compileShader(rc);
if (!glCtx.getShaderParameter(rc, glCtx.COMPILE_STATUS)) {
throw glCtx.getShaderInfoLog(rc) + ' in ' + source;
}
return rc;
}
getElementById(id) {
const element = document.getElementById(id);
if (element == null) {
throw 'Can not find shader by element id ' + id;
}
return element;
}
compileShaderById(id) {
const glCtx = this.getGLContext();
const element = this.getElementById(id);
const source = element.innerHTML;
if (element.type === 'x-shader/x-vertex') {
return this.compileShader(glCtx.VERTEX_SHADER, source);
}
if (element.type === 'x-shader/x-fragment') {
return this.compileShader(glCtx.FRAGMENT_SHADER, source);
}
throw 'Unknown shader type ' + element.type;
}
linkProgram(shaders) {
const glCtx = this.getGLContext();
const rc = glCtx.createProgram();
for(var cnt = 0; cnt < shaders.length; cnt++) {
if (!shaders[cnt]) {
throw 'Can not link program with null shaders';
}
glCtx.attachShader(rc, shaders[cnt]);
}
glCtx.linkProgram(rc);
if (!glCtx.getProgramParameter(rc, glCtx.LINK_STATUS)) {
throw glCtx.getProgramInfoLog(rc);
}
return rc;
}
setUniformf(name) {
const glCtx = this.getGLContext();
const prog = glCtx.getParameter(glCtx.CURRENT_PROGRAM);
const idx = glCtx.getUniformLocation(prog, name);
if (arguments.length == 2)
glCtx.uniform1f(idx, arguments[1]);
else if (arguments.length == 3)
glCtx.uniform2f(idx, arguments[1], arguments[2]);
else if (arguments.length == 4)
glCtx.uniform3f(idx, arguments[1], arguments[2], arguments[3]);
else if (arguments.length == 5)
glCtx.uniform4f(idx, arguments[1], arguments[2], arguments[3], arguments[4]);
else
throw "Unsupported number of uniform arguments";
}
setMatrix3fv(name, v) {
const glCtx = this.getGLContext();
const prog = glCtx.getParameter(glCtx.CURRENT_PROGRAM);
const idx = glCtx.getUniformLocation(prog, name);
if (v.length != 9)
throw "Unexpected number of 3x3 matrix arguments";
glCtx.uniformMatrix3fv(idx, false, v);
}
setUniformi(name) {
const glCtx = this.getGLContext();
const prog = glCtx.getParameter(glCtx.CURRENT_PROGRAM);
const idx = glCtx.getUniformLocation(prog, name);
if (arguments.length == 2)
glCtx.uniform1i(idx, arguments[1]);
else if (arguments.length == 3)
glCtx.uniform2i(idx, arguments[1], arguments[2]);
else if (arguments.length == 4)
glCtx.uniform3i(idx, arguments[1], arguments[2], arguments[3]);
else if (arguments.length == 5)
glCtx.uniform4i(idx, arguments[1], arguments[2], arguments[3], arguments[4]);
else
throw "Unsupported number of uniform arguments";
}
draw2DArray(type, name, arr) {
const glCtx = this.getGLContext();
const prog = glCtx.getParameter(glCtx.CURRENT_PROGRAM);
const idx = glCtx.getAttribLocation(prog, name);
var buf = glCtx.createBuffer();
glCtx.bindBuffer(glCtx.ARRAY_BUFFER, buf);
glCtx.bufferData(glCtx.ARRAY_BUFFER, new Float32Array(arr), glCtx.STATIC_DRAW);
glCtx.enableVertexAttribArray(idx);
glCtx.vertexAttribPointer(idx, 2, glCtx.FLOAT, false, 0, 0);
glCtx.drawArrays(type, 0, arr.length/2);
glCtx.deleteBuffer(buf);
}
}
class RayTracerScene {
constructor(canvas) {
const ctx = new GLContext(canvas);
const prog = ctx.linkProgram(['passThruVS', 'rayMarchingFS'].map(_ => ctx.compileShaderById(_)));
ctx.getGLContext().useProgram(prog);
this.ctx = ctx;
this.aspect = canvas.clientWidth/canvas.clientHeight;
this.defaultCameraDistance = this.aspect > 1.0 ? -1.4 : -5.0;
this.setCamera(0, 0, this.defaultCameraDistance);
// Bumpy sphere
this.setOffs(0, 2, 1, 3);
this.setRotateAndScale(0, 0.0, 0, 0, 1);
this.setColor(0, 0, 0, 1);
this.setMaterialProps(0, .1, .5, .6);
// Torus
this.setOffs(1, -3, 1, 4);
this.setRotateAndScale(1, 0, 0, .0, 1);
this.setColor(1, 1, 0, 0);
this.setMaterialProps(1, .1, .5, .6, 0);
// Sphere
this.setOffs(2, -.4, 1, 5);
this.setRotateAndScale(2, 0, 0, 0, 1.4);
this.setColor(2, 0, 1, 0);
this.setMaterialProps(2, .2, .5, .6, .3);
// Plane
this.setColor(3, 0.6, 0.8, 0.6);
this.setMaterialProps(3, .1, .5, .4, .4);
// Lights
ctx.setUniformf('lights[0].location', 8, 8, -5);
ctx.setUniformf('lights[0].color', 1, 1, 1);
ctx.setUniformf('lights[0].attenuation', 16);
ctx.setUniformf('lights[1].location', -8, 8, -5);
ctx.setUniformf('lights[1].color', 1, 1, 1);
ctx.setUniformf('lights[1].attenuation', 16);
}
animate(t) {
this.setRotateAndScale(0, 1e-3*t, 7e-4*t, 0, 1.0);
this.setRotateAndScale(1, 0, -1e-3*t, 0, 1.0);
this.render();
}
render(x0 = -1.0, y0 = -1.0, x1 = 1.0, y1 = 1.0) {
this.ctx.draw2DArray(this.ctx.PRIMITIVES.TRIANGLE_STRIP, 'coord', [x0, y0, x1, y0, x0, y1, x1, y1]);
}
setColor(i, r, g, b) {
this.ctx.setUniformf('objects['+i+'].color', r, g, b);
}
setOffs(i, x, y, z) {
this.ctx.setUniformf('objects['+i+'].offs', x, y, z);
}
setRotateAndScale(i, longitude, latitude, spin, scale) {
var rs = mulMatMat(rotateZ(spin), mulMatMat(mulMatMat(rotateY(-latitude), rotateX(longitude)), identity(1/scale)));
this.ctx.setMatrix3fv('objects['+i+'].rs', rs);
}
setMaterialProps(i, a=0, d=0, s=0, r=0) {
this.ctx.setUniformf('objects['+i+'].ambient',a);
this.ctx.setUniformf('objects['+i+'].diffuse',d);
this.ctx.setUniformf('objects['+i+'].specular',s);
this.ctx.setUniformf('objects['+i+'].reflect',r);
}
setCamera(x, y, z) {
this.ctx.setUniformf('camera.location', x, y, z);
this.ctx.setMatrix3fv('camera.rotation', [this.aspect, 0, 0, 0, 1, 0, 0, 0, 1]);
}
setCameraRotation(longitude, latitude, spin) {
var mat = mulMatMat(rotateY(-latitude), rotateX(longitude));
for(var i = 0; i < 3; i++) {
mat[3*i] *= this.aspect;
}
this.ctx.setMatrix3fv('camera.rotation', mat);
}
}
var orientationEventReceived = false;
function registerMotionEventListener() {
var compOrientation = {alpha: 0, beta: 0, gamma: 0};
window.addEventListener('devicemotion', function(event) {
orientationEventReceived = true;
compOrientation.alpha += event.rotationRate.alpha*event.interval;
compOrientation.beta += event.rotationRate.beta*event.interval;
compOrientation.gamma += event.rotationRate.gamma*event.interval;
const alpha = Math.PI* compOrientation.alpha / 180;
const beta = Math.PI* compOrientation.beta / 180;
const gamma = Math.PI* compOrientation.gamma / 180;
if (window.orientation == 0.0) {
scene.setCameraRotation(alpha, beta, gamma);
} else if (window.orientation == 90.0) {
scene.setCameraRotation(beta, alpha, alpha);
} else if (window.orientation == -90.0) {
scene.setCameraRotation(-beta, -alpha, alpha);
} else {
scene.setCameraRotation(0, 0, 0);
}
scene.render();
});
}
function requestPermissions() {
if (typeof DeviceMotionEvent === 'undefined') {
return;
}
if (typeof DeviceMotionEvent.requestPermission !== 'function') {
return;
}
DeviceMotionEvent.requestPermission(function(state) {
if (state == 'granted') {
registerMotionEventListener();
}
}).catch(console.error);
}
function displayClickToEnable() {
document.getElementById('click-to-enable').style.visibility='visible';
}
function configureTracer() {
const canvas = document.getElementById('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
try {
scene = new RayTracerScene(canvas);
scene.render();
var cameraDistance = scene.defaultCameraDistance;
var xn = 0.0;
var yn = 0.0;
canvas.addEventListener('mousemove', function(event) {
xn = scene.aspect*(event.x/canvas.clientWidth - .5);
yn = (event.y/canvas.clientHeight - .5);
scene.setCamera(xn, yn, cameraDistance);
scene.render();
});
canvas.addEventListener('wheel', function(event) {
cameraDistance += 1e-3 * event.deltaY;
scene.setCamera(xn, yn, cameraDistance);
scene.render();
});
registerMotionEventListener();
setTimeout(function() {
if (orientationEventReceived) {
return;
}
displayClickToEnable();
}, 1500);
setInterval(function() {
scene.animate(performance.now());
}, 100);
} catch (e) {
alert('Initialization failed: ' + e);
return;
}
}
</script>
</script>
<body onLoad="configureTracer();">
<DIV>
<H1 style="display:inline">RayMarching over SDF</H1>
<DIV style="display:inline;visibility:hidden" id="click-to-enable">
<INPUT TYPE="submit" onClick="requestPermissions();" value="Click here to enable orientation events" />
</DIV>
<canvas id="canvas" width="1024px" height="1024px"></canvas>
</body>
</html>