1. Ray Marching & SDFs

The Geometry of Rays and Distance Fields

The journey begins with the most concrete representation of 3D space: the Signed Distance Function (SDF).

A ray can be mathematically defined as r(t) = o + td, where o represents the camera origin and d the viewing direction for a specific pixel.

Sphere Tracing: We iteratively step along the ray. At each step, we query the SDF to find the distance to the nearest surface. We can safely step forward by exactly this distance without passing through any object.

// A differentiable sphere SDF
float sphereSDF(float3 p, float radius) {
    return length(p) - radius;
}

Adjust the epsilon and maximum steps to understand the trade-offs between performance and visual artifacts.