Skip to content

Commit b5c4815

Browse files
committed
Add blobs example for SDFs
1 parent 2ba9a62 commit b5c4815

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

sdf/frag.glsl

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ precision mediump float;
44

55
uniform vec3 resolution;
66
uniform float pixelRatio;
7+
uniform float time;
78
uniform vec3 eye;
89
uniform vec3 center;
910
uniform int example;
@@ -25,17 +26,27 @@ float sdCylinder(vec3 p, vec3 c) {
2526
}
2627

2728
float sdTorus(vec3 p, vec2 t) {
28-
vec2 q = vec2(length(p.xz)-t.x,p.y);
29-
return length(q)-t.y;
29+
vec2 q = vec2(length(p.xz)-t.x,p.y);
30+
return length(q)-t.y;
3031
}
3132

33+
float sdVerticalCapsule(vec3 p, float h, float r) {
34+
p.y -= clamp(p.y, 0.0, h);
35+
return length(p) - r;
36+
}
37+
38+
3239
// End primitive SDFs --
3340

3441
float opSmoothUnion( float d1, float d2, float k ) {
3542
float h = clamp( 0.5 + 0.5*(d2-d1)/k, 0.0, 1.0 );
3643
return mix( d2, d1, h ) - k*h*(1.0-h);
3744
}
3845

46+
mat2 rotate(float theta) {
47+
return mat2(cos(theta), -sin(theta), sin(theta), cos(theta));
48+
}
49+
3950
float map(vec3 pos) {
4051
if (example == 1) {
4152
float torus = sdTorus(pos, vec2(1.0, 0.15));
@@ -47,6 +58,17 @@ float map(vec3 pos) {
4758
float cyl2 = sdCylinder(pos.yzx, vec3(0.0, 0.0, 0.5));
4859
float cyl3 = sdCylinder(pos.zxy, vec3(0.0, 0.0, 0.5));
4960
return max(-min(cyl1, min(cyl2, cyl3)), base);
61+
} else if (example == 3) {
62+
const float tau = 6.28318;
63+
float phase = time / 10.0;
64+
float torus1 = sdTorus(vec3(rotate(tau * phase) * pos.xy, pos.z), vec2(0.8, 0.2));
65+
float torus2 = sdTorus(vec3(pos.x, rotate(tau * (phase + 0.6)) * pos.yz), vec2(0.8, 0.2));
66+
67+
vec3 vpos = vec3(rotate(2.0 * tau * phase) * pos.xy, pos.z);
68+
vpos.y += 0.8 + 0.1 * sin(tau * phase * 1.2);
69+
float rod = sdVerticalCapsule(vpos, 1.6, 0.2);
70+
71+
return opSmoothUnion(opSmoothUnion(torus1, torus2, 0.4), rod, 0.6);
5072
}
5173
}
5274

@@ -115,6 +137,10 @@ vec3 calcColor(vec3 pos, vec3 normal) {
115137
} else if (example == 2) {
116138
mag += 6.0 * phong(pos, normal, vec3(2.5, 3.0, 1.0));
117139
mag += 10.0 * phong(pos, normal, vec3(0.0, 8.0, 0.0));
140+
} else if (example == 3) {
141+
mag += 5.0 * phong(pos, normal, vec3(0.0, 4.0, 0.0));
142+
mag += 4.0 * phong(pos, normal, vec3(2.5, 3.0, 1.0));
143+
mag += 1.5 * phong(pos, normal, vec3(-5.0, -1.0, -5.0));
118144
}
119145
return vec3(sqrt(mag + 0.001)); // inverse gamma correction
120146
}

sdf/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function initPane() {
4646

4747
pane.addInput(params, "scale", { min: 0, max: 50 });
4848
pane.addInput(params, "grid", { min: 5, max: 40, step: 1 });
49-
pane.addInput(params, "example", { options: { torus: 1, csg: 2 } });
49+
pane.addInput(params, "example", { options: { torus: 1, csg: 2, blobs: 3 } });
5050
pane.addInput(params, "mode", {
5151
options: { shading: 3, curvature: 2, normal: 1 },
5252
});
@@ -88,6 +88,7 @@ const common = regl({
8888
Math.min(drawingBufferWidth, drawingBufferHeight),
8989
],
9090
pixelRatio: regl.context("pixelRatio"),
91+
time: regl.context("time"),
9192
},
9293
});
9394

0 commit comments

Comments
 (0)