GLSL shader example

Ray Marching an SDF Sphere

Trace one ray per pixel, advancing by the distance field. A hit epsilon and maximum distance terminate the loop.

Live example

Edit the code and the preview updates as you type.

What to observe

Ray Marching an SDF Sphere.

Mechanism

Trace one ray per pixel, advancing by the distance field. A hit epsilon and maximum distance terminate the loop.

The controlling code

return length(p)-.75;

Experiment

Change the radius in scene(p)=length(p)-.75 from .75 to .5. The sphere silhouette becomes smaller, while the camera position and perspective stay fixed.

Debugging and cost

The hit test d<.002 and 80-step cap jointly decide when tracing stops. Increase the hit tolerance to .02 to see a less precise surface boundary; a larger tolerance is not the same as increasing the sphere's radius in scene.

Understand the technique

Built-in functions used

Learn it step by step