This lesson ties the course together: you describe a scene as a single distance function, fire a ray through every pixel and shade what each ray finds. This lesson pairs with Coding Adventure: Ray Marching and Coding Adventure: Clouds by Sebastian Lague, linked under Watch & read. Judging by their titles and chapter lists, the first covers signed distances, marching rays, combining shapes and lighting, and the second builds up from noise textures to rays travelling through soft volumes. You build the core of that pipeline in NumPy and grade it against exact geometry.
A signed distance function (SDF) takes a point and returns how far it is from the nearest surface: positive outside, negative inside, zero on the surface. A sphere is |p - c| - r. A plane y = h is p_y - h. A box needs two parts: with q = |p - c| - half, the outside part is |max(q, 0)| and the inside part is min(max(q_x, q_y, q_z), 0). The inside part is the one people drop, and then every point inside reads as 0 instead of a negative depth.
import numpy as np
def box(p, half):
q = np.abs(p) - half
return np.linalg.norm(np.maximum(q, 0)), q.max() # (outside part, raw largest q)
half = np.array([1.0, 0.5, 0.75])
print([round(float(v), 3) for v in box(np.array([2.0, 0.0, 0.0]), half)])
print([round(float(v), 3) for v in box(np.array([0.7, 0.1, 0.0]), half)])The first point is 1 outside the face x = 1, so both numbers are 1.0. The second is inside: the outside part is 0 and the largest q is -0.3, the depth below the nearest face. The sum of the outside part and min(q.max, 0) covers both cases with one formula.
Shapes combine by taking the smaller distance: union is the elementwise minimum. smooth_union(a, b, k) rounds the seam: with h = max(k - |a - b|, 0) / k it returns min(a, b) - h²k/4. Two shapes that touch at distance 0 give -k/4 there, and far apart shapes are unchanged. repeat folds space into cells, p - period · round(p / period), so one shape appears on a lattice for free.
Ray marching rests on one fact: if the field says d at a point, no surface is closer than d in any direction, so the ray can move d along its direction without crossing anything. March repeatedly: evaluate the field at origin + t · direction, stop with a hit when the value drops below eps, otherwise add that value to t. A ray that passes max_dist, or uses all max_steps, is a miss and gets t = inf. The direction must have length 1, or t is not a distance.
import numpy as np
o, d = np.array([0.0, 3.0, 0.0]), np.array([0.6, -0.8, 0.0])
t = 0.0
for step in range(5):
dist = (o + t * d)[1] # distance to the floor y = 0
print(step, round(t, 4), round(dist, 4))
t += distThe floor is 3 below the start but 3 / 0.8 = 3.75 away along the ray. The field says 3, then 0.6, 0.12, 0.024: each step covers 0.8 of what is left, and the last 0.2 is what remains. A flatter ray covers a smaller share per step, so it needs many more.
for dy in (0.8, 0.5, 0.2):
remaining, steps = 3.0 / dy, 0
while True:
steps += 1
if remaining * dy < 1e-4: # the field value is dy times the distance left
break
remaining *= 1 - dy
print(dy, steps)The rest is shading. normal takes central differences of the field along each axis and normalises the result. soft_shadow marches from a surface point toward the light and keeps the smallest value of k · d / t seen on the way: a ray that grazes an occluder gives a small ratio, which makes a soft edge, and a ray that enters it gives zero. camera_rays builds one unit direction per pixel centre from the eye, the target and a vertical field of view, with y up. render calls them in turn.
Two mistakes are worth looking for in your own code. The first is a box that returns 0 inside: the picture still looks right from outside, which is why only a test that samples interior points catches it. The second is a march that replaces t with the latest field value instead of adding it. The first step is identical in both versions, so a ray straight at a sphere passes; only a ray that needs several steps shows the difference. Other classic slips are a direction that is not unit length, a normal that is not normalised, and a shadow ray that starts exactly on the surface and shades itself.
The tests compare against exact answers: analytic ray-sphere and ray-floor hits, a brute-force distance to a dense sample of a box surface, Lambert's law on a sphere, a mirror-symmetric image of a mirror-symmetric world, and a sampled check that no field changes faster than the distance travelled, which is what makes marching safe. They do not judge whether a picture looks good. The soft shadow has no closed form to compare with, so it is checked only for ordering and limits. A repeated shape is exact only while it fits inside its cell, and none of the tests covers a scene much bigger than a few shapes.