Raymarching with SDFs
First appeared 1996 · John C. Hart — Sphere tracing of signed distance fields — Hart (1996); popularized in real time by Íñigo Quílez (2008–). · timeline ↗
community / folk Name — Descriptive: "ray marching" of signed distance fields; the principled form is John C. Hart's "sphere tracing" (1996). "Ray marching" is a community term, not a personal eponym.
In one line A rendering technique that marches a ray forward in steps governed by a Signed Distance Function (SDF) to draw implicit surfaces — widely run in a fragment shader for real-time 3D scenes built from math.
The code
The actual sketch behind the demo above — sandboxed and reproducible. This is the whole thing.
raymarching-sdf/index.html
// Minimal raymarching of a smin'd union of 3 spheres + a ground plane,
// rendered in a single WebGL2 fragment shader.
const SEED = Math.random();
const c = document.getElementById('c');
const dpr = Math.min(devicePixelRatio || 1, 2);
const size = Math.min(innerWidth, innerHeight);
c.width = Math.floor(size * dpr);
c.height = Math.floor(size * dpr);
const gl = c.getContext('webgl2');
if (!gl) {
c.outerHTML = '<p style="color:#fff;font-family:system-ui">WebGL2 unavailable</p>';
} else {
const vs = `#version 300 es
in vec2 a; void main(){ gl_Position = vec4(a, 0.0, 1.0); }`;
const fs = `#version 300 es
precision highp float;
out vec4 fragColor;
uniform vec2 u_resolution;
uniform float u_seed;
// SDFs
float sdSphere(vec3 p, float r){ return length(p) - r; }
float sdPlane (vec3 p, float y){ return p.y - y; }
// polynomial smooth min by IQ
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);
}
// simple hash → [0,1)
float hash(float n){ return fract(sin(n)*43758.5453123); }
vec3 spherePos(int i){
float s = u_seed * 100.0;
return vec3(
(hash(float(i)*7.1 + s) - 0.5) * 1.6,
-0.2 + hash(float(i)*11.3 + s) * 0.7,
(hash(float(i)*13.7 + s) - 0.5) * 1.6
);
}
float sphereR(int i){ return 0.45 + hash(float(i)*17.0 + u_seed*100.0)*0.45; }
float map(vec3 p){
float d = sdPlane(p, -0.9);
for (int i = 0; i < 3; i++) {
d = smin(d, sdSphere(p - spherePos(i), sphereR(i)), 0.35);
}
return d;
}
vec3 normal(vec3 p){
vec2 e = vec2(0.0015, 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)
));
}
void main(){
vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution) / u_resolution.y;
// camera
vec3 ro = vec3(0.0, 0.4, 3.5);
vec3 rd = normalize(vec3(uv, -1.4));
float t = 0.0;
float d = 0.0;
vec3 p;
bool hit = false;
for (int i = 0; i < 96; i++) {
p = ro + rd * t;
d = map(p);
if (d < 0.001) { hit = true; break; }
if (t > 16.0) break;
t += d;
}
vec3 col;
if (hit) {
vec3 n = normal(p);
vec3 lDir = normalize(vec3(0.7, 0.9, 0.4));
float diff = clamp(dot(n, lDir), 0.0, 1.0);
float ao = 0.5 + 0.5*n.y;
vec3 base = 0.5 + 0.5*sin(vec3(0.0, 2.1, 4.2) + p.y*1.5 + u_seed*6.28);
col = base * (0.25 + 0.85*diff) * ao;
// sky-fog
col = mix(col, vec3(0.07,0.08,0.11), 1.0 - exp(-0.06 * t));
} else {
// sky gradient
col = mix(vec3(0.05,0.06,0.10), vec3(0.18,0.21,0.28), 0.5 + 0.5*rd.y);
}
// gamma
col = pow(col, vec3(1.0/2.2));
fragColor = vec4(col, 1.0);
}`;
function shader(type, src){
const sh = gl.createShader(type);
gl.shaderSource(sh, src);
gl.compileShader(sh);
if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(sh));
}
return sh;
}
const prog = gl.createProgram();
gl.attachShader(prog, shader(gl.VERTEX_SHADER, vs));
gl.attachShader(prog, shader(gl.FRAGMENT_SHADER, fs));
gl.linkProgram(prog);
gl.useProgram(prog);
const buf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
-1,-1, 1,-1, -1,1, -1,1, 1,-1, 1,1
]), gl.STATIC_DRAW);
const loc = gl.getAttribLocation(prog, 'a');
gl.enableVertexAttribArray(loc);
gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
gl.uniform2f(gl.getUniformLocation(prog, 'u_resolution'), c.width, c.height);
gl.uniform1f(gl.getUniformLocation(prog, 'u_seed'), SEED);
gl.viewport(0, 0, c.width, c.height);
gl.drawArrays(gl.TRIANGLES, 0, 6);
}How it works
An SDF returns, for any point, the distance to the nearest surface (negative inside). For each pixel, cast a ray from the camera and "sphere-trace": 1. Evaluate the SDF at the ray's current point — this is the largest safe step that cannot overshoot a surface. 2. Advance the ray by that distance. 3. Repeat until the distance is below a small epsilon (a hit) or a max step/distance is exceeded (a miss). At a hit, the surface normal comes from the gradient of the SDF (finite differences), enabling lighting. Scenes are composed by combining primitive SDFs with min/max (union/intersection), smooth-min (blending), and by transforming/repeating coordinates — all in the fragment shader.
Parameters
maxSteps: number — Maximum march iterations per ray before it is abandoned.epsilon: number — Surface-hit threshold; the distance under which the ray is treated as a hit.maxDistance: number — Far clip; the distance after which the ray is treated as missing the scene.stepScale: number — Fraction of the SDF distance advanced each step (a relaxation factor ≤ 1 for safe marching).
Lineage
Frequently combined with
- Fragment Shader (GLSL) · 20 works
- Perlin / Simplex Noise · 10 works
- 3D Mesh / Geometry · 6 works
- Audio-Reactive · 4 works
- Flow Field / Vector Field · 4 works
- Bezier / Catmull-Rom Splines · 4 works
- Instanced Geometry · 3 works
- Voronoi Diagram · 2 works
- Boids / Flocking · 2 works
- Plotter-style Hatching · 2 works
- HSL / Hue Cycle · 1 work
- Marching Squares / Contours · 1 work
Works using this algorithm (36)
Last updated 2026-07-16