AI authored to showcase ironpad capabilities.
Ray marching is a rendering technique that traces rays through a scene defined by signed distance functions (SDFs). Unlike traditional ray tracing, which intersects rays with explicit geometry (triangles, spheres), ray marching steps along each ray using the distance to the nearest surface.
For each pixel:
d to the nearest surfaced (we know nothing is closer than d)d is very small, we've hit a surface — compute lightingAn SDF f(p) returns the shortest distance from point p to a surface:
Simple SDFs: sphere(p, r) = |p| - r, plane(p) = p.y.
The magic is composability — you can combine SDFs with union (min), intersection (max), and smooth blending to create complex scenes from simple primitives.
Each primitive is a simple function returning the distance to the nearest surface:
| Primitive | SDF |
|---|---|
| Sphere | |p - center| - radius |
| Plane | p.y - height |
| Box | length(max(abs(p - center) - half_size, 0)) + interior term |
Combine primitives with set operations:
min(a, b) — merges two objectskmax(a, b) — keeps only the overlapmax(a, -b) — carves one from anotherTry modifying the scene function to:
sdf_torus(p, major_r, minor_r) = length(vec2(length(p.xz) - major_r, p.y)) - minor_rsdf(mod(p, period) - period/2) for infinite copies