Raymarcher
glint · Examples · Tutorials · Gallery
Signed distance fields with soft shadows. Drag to orbit, tweak the parameters.
Audio-reactive: it moves with music from a microphone, line-in or a file.

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.
The code
One pass, in GLSL.
fragment shader
#version 300 es
precision highp float;
uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 spectrum;
uniform float blend;
uniform vec3 tint;
in vec2 v_texcoord;
out vec4 fragColor;
float sdBox(vec3 p, vec3 b) { vec3 q = abs(p) - b; return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0); }
float smin(float a, float b, float k) { float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0); return mix(b, a, h) - k * h * (1.0 - h); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }
float map(vec3 p) {
float ground = p.y + 1.0;
vec3 q = p;
q.xz *= rot(time * 0.4);
q.xy *= rot(time * 0.3);
float box = sdBox(q, vec3(0.55)) - 0.08;
float ball = length(p - vec3(sin(time) * 0.9, 0.2 + 0.3 * sin(time * 1.7), cos(time) * 0.9)) - 0.45 - spectrum.x * 0.5;
return min(ground, smin(box, ball, blend));
}
vec3 normalAt(vec3 p) {
vec2 e = vec2(0.001, 0.0);
return normalize(vec3(map(p + e.xyy) - map(p - e.xyy), map(p + e.yxy) - map(p - e.yxy), map(p + e.yyx) - map(p - e.yyx)));
}
float shadow(vec3 ro, vec3 rd) {
float res = 1.0, t = 0.02;
for (int i = 0; i < 48; i++) {
float h = map(ro + rd * t);
res = min(res, 10.0 * h / t);
t += clamp(h, 0.02, 0.2);
if (res < 0.001 || t > 8.0) break;
}
return clamp(res, 0.0, 1.0);
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
float yaw = (mouse.x - 0.5) * 6.28, pitch = (mouse.y - 0.3) * 1.5;
vec3 ro = vec3(0.0, 0.5, 4.0);
ro.yz *= rot(-pitch);
ro.xz *= rot(yaw);
vec3 fw = normalize(-ro), rt = normalize(cross(fw, vec3(0, 1, 0))), up = cross(rt, fw);
vec3 rd = normalize(uv.x * rt + uv.y * up + 1.6 * fw);
float t = 0.0;
for (int i = 0; i < 128; i++) {
float d = map(ro + rd * t);
if (d < 0.001 || t > 20.0) break;
t += d;
}
vec3 sky = mix(vec3(0.05, 0.06, 0.1), vec3(0.3, 0.35, 0.5), uv.y + 0.5);
vec3 col = sky;
if (t < 20.0) {
vec3 p = ro + rd * t, n = normalAt(p);
vec3 l = normalize(vec3(0.6, 0.8, 0.4));
float dif = max(dot(n, l), 0.0) * shadow(p + n * 0.01, l);
float amb = 0.5 + 0.5 * n.y;
vec3 albedo = p.y < -0.99 ? vec3(0.4) * (0.6 + 0.4 * mod(floor(p.x) + floor(p.z), 2.0)) : tint;
col = albedo * (dif * vec3(1.1, 1.0, 0.9) + amb * vec3(0.12, 0.15, 0.2));
col += pow(max(dot(reflect(rd, n), l), 0.0), 32.0) * dif * 0.5;
col = mix(col, sky, 1.0 - exp(-0.01 * t * t));
}
fragColor = vec4(pow(col, vec3(0.4545)), 1.0);
}