Signed Distance Functions (SDFs) are mathematical functions that return the signed distance from any point in space to the nearest surface of a shape.

Sphere SDF

float sdSphere(vec3 p, float r) { return length(p) - r; }

Box SDF

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); }