Ray marching; signed distance field

Drawing 3D forms by stepping through a math function instead of rasterizing triangles. It is the source of many blobby Shadertoy scenes.

3D shapes made from maththe blobby shader shapesrender without a meshhow do Shadertoy scenes workinfinite repeating 3D scene3D made of nothing but a fragment shadersphere tracingsinged distance field

See it

Live demo coming soon

What it is

A signed distance field, or SDF, is a function that returns how far a point is from a surface, with the sign distinguishing inside from outside. A ray marcher sends a ray through each pixel and advances it by that distance until it reaches the surface or gives up. Sphere tracing is the common distance-guided version used for SDF scenes.

Because the scene is functions instead of triangles, shapes can be combined, carved, repeated, twisted, and blended with a smooth minimum. That makes SDF ray marching a natural fit for liquid blobs, abstract motion, procedural tunnels, and the compact mathematical scenes common on Shadertoy, where Inigo Quilez's articles on SDF primitives and operators are the reference most people build from. Density ray marching uses a related loop for clouds and fog, but samples density rather than a surface distance.

Gotcha: cost is paid per pixel and per step. A low step limit clips or misses geometry; a tiny hit threshold causes flicker and burns steps. A function that underestimates distance is safe but slow, while one that overestimates can jump through thin surfaces. Shadows, reflections, and ambient occlusion each add more marches.

Ask AI for it

Write a Shadertoy-compatible GLSL fragment shader that ray marches a signed distance field with sphere tracing. Combine a sphere and rounded box using a polynomial smooth-min operation, repeat the pair along the z axis, and animate the blend with iTime. Use MAX_STEPS = 128, MAX_DISTANCE = 100.0, and SURFACE_EPSILON = 0.001; compute normals with central finite differences, add one soft shadow march and fog by travel distance, and color missed rays with a dark background. Expose a debug view that colors pixels by step count.

You might have meant

shadervertex vs fragment shadercamera projection matrixsurface normals backface cullingmesh geometry