yAtlas

Drawing the first frame…

0:00 / 11:47Prologue

The Order Beneath · No.03 · Rendered in real time · 11:47

Implicit

A short film about implicit shapes

Signed distance fields in six short chapters: ask one function how far the nearest surface is, and that answer alone is enough to combine shapes, draw them onto the screen step by step, repeat them without end and carve them at every scale. Every frame is computed by code; on the website, each one is drawn live in your browser as the film plays.

Runtime
11:47
Chapters
Six, with a prologue and a coda
Resolution
Matches the player, up to 4.4 MP
Frame rate
Up to your display’s refresh rate, depending on the device
Prerecorded frames
None
Languages
中文 · English

Try it yourself

Each of these moments in the film opens in the Studio, where you can try the idea yourself.

  1. Turn k yourself and watch two discs melt into oneIII · Combining shapes
  2. Move the ray's origin and aim, and count its stepsIV · Onto the screen
  3. Move the fan of rays and see which ones take the most stepsIV · Onto the screen
  4. Drag the cell size c up, then shrink it again, and watch one cube fill spaceV · Infinitely many cubes
  5. Bring the twist k back to zero, then turn it up and watch the tunnel windV · Infinitely many cubes
  6. Cut the levels yourself and count how many of its 27 pieces each cube keepsVI · At every scale
  7. Drag n from 0 up to 5 and watch the volume fallVI · At every scale

Chapters

Studio

This volume's pictures, in your hands: change a parameter and watch the picture follow.

Where it turns up

Places in the world where this volume's mathematics turns up. Each card links to a related Studio.

Transcript · 86
  1. Prologue

  2. IHow far to the edge

  3. IICorners and the skeleton

  4. IIICombining shapes

  5. IVOnto the screen

  6. VInfinitely many cubes

  7. VIAt every scale

  8. Coda

Plate notes · 5

I Distance

A signed distance field gives every point its distance to a shape, positive outside and negative inside; the shape is where the field is zero. Its level sets, gold outside and ice-blue inside, are the fronts of the shape: everything exactly so far from it. They drift outwards here, welling up from the skeleton and crossing the boundary. The dashed line is a slice, and the corner plots the field along it.

A circle
d(p)=|p|−rThe distance to a circle. Every level set is a circle too, spaced exactly as their values say: move one pixel along a radius and an exact distance field changes by exactly one pixel; away from the centre, |∇d|=1.
Corners
q=|p|−bd=|max(q,0)|+min(max(qx,qy),0)−rThe circle grows into a rounded box. Outside, the fronts round every corner off; inside, once they are deeper than the corner's radius, they turn sharp, and the corners run in along the corner bisectors, where each point is equally near two edges. Those bisectors, with the segment that joins them, are the skeleton.
Fronts
{p:d(p)=c}Move the bright front inwards, c falling below zero: the rounded corners shrink and turn sharp, the fronts meet along the skeleton, and the box burns out last along its middle segment (Blum's grassfire). Then a star grows out of its own deepest point.
A star
|∇d|=1A star is not convex, so its skeleton reaches outside as well, into every concave corner. The probe carries the largest circle around it with no boundary inside; its radius is |d|, and where it touches the boundary are the nearest points. Where the gradient exists there is just one, p−d∇d; on the skeleton there are more. At the end the star burns out and the circle grows back.

II Combining

Two shapes combine by combining their distances: the smaller of the two, counting negatives as smaller, is their union, the larger their intersection, and flipping a sign cuts one out of the other. The faint circles are the ingredients (a third joins for the smooth union); the bright line is the result, and the level sets show where the result is still a true distance and where it is only a bound. The corner plots the ingredients along the slice, and what the operation makes of them.

Union
d=min(dA,dB)The union is the smaller of the two values. Outside it is exact; inside, each circle's lines run on through the other, because there the result's size only bounds the true distance from below.
Intersection
d=max(dA,dB)The intersection is the larger of the two: exact inside, only a bound outside, where the corners at which the circles cross stay sharp however far out you go.
Difference
d=max(dA,−dB)Flipping the sign of dB turns its inside out, and the intersection with that cuts B away from A. A crescent waxes and wanes as B passes over.
Smooth union
h=max(k−|a−b|,0)/kd=min(a,b)−h2k/4Where the two distances are within k of each other the minimum is rounded off, and the shapes run together like drops of water. Near the seam the result is no longer an exact distance, but it still never changes faster than you move, so a ray can still step through it safely.

III Sphere tracing

To find where a ray first meets a surface, ask the field how far the nearest surface is and step exactly that far: nothing can be closer, so the step is safe. Far from everything the steps are long; grazing past an edge they shrink and crowd. The plate starts in a slice through the scene, the vertical plane of the camera, then puts one ray through every pixel and lights what they find.

One ray
ti+1=ti+d(o+tir)Each dashed circle is empty of surface, so the ray jumps to its edge. This ray skims the top of the torus and creeps into the crevice where the sphere meets the floor, and its steps grow tiny. The corner plots the distance along it: every step falls to zero down a line of slope −1, and the curve always stays above that line.
Many rays
d(o+tr)<εA whole fan of rays at once. Most arrive in a few steps; the ones that graze a surface take dozens, and the ones that miss run on until they reach the distance limit or run out of steps.
Every pixel
d(o+tr(u,v))=0Now one ray per pixel, coloured by how many steps it took: dark where it found something at once, gold and white along the silhouettes, where rays run close to a surface for a long way. The corner keeps the slice, with the camera in it.
Shading
n=∇d/|∇d|s=minikd(pi)/tiThe gradient of the field is the surface normal. A second trace towards the light gives the shadow: the narrower the gaps it passes, compared with how far it has come, the darker the point, which softens the shadow's edge. A few samples along the normal give the ambient occlusion. Both are approximations. The floor carries the distance to the objects in faint rings.

IV Repetition

A distance field does not have to be about space as it is. Take every point modulo a cell and the field only ever sees one cell, so one object is repeated without end; twist the space and the repetition twists with it; give each cell its index and each copy can be different. The camera flies down a corridor between the copies. The corner is a slice through the lattice at mid-height, seen from above, with the cells drawn in.

One object
d(p)=min(dframe(p),|p|−r)A wire cube with a gold bead at its centre, alone in the dark. Its field is the smaller of two distances.
Infinitely many
q=mod(p+c/2,c)−c/2Evaluate the same field at q instead of p and every cell holds a copy. The cell's side shrinks from ninety units to 2.6, so the copies walk in out of the fog, and the camera sets off down the corridor between them.
A twist
pxy↦R(kz)pxyd↦d/λ,λ=(kr+(k2r2+4)1/2)/2Rotate each slice of space by an angle that grows with z, about the corridor's own axis. At distance r from the axis the twist stretches space by up to λ, so the field is divided by λ; but λ changes from place to place, so that alone is not quite enough, and a little more is taken off. The margin has been checked numerically, for this scene only.
Each cell its own
θ(id)=Asin(ωt−0.7Σid)Every cell knows its index, so every copy can turn by its own angle; here a wave of turns runs through the lattice. A cell only sees its own object, so the step is also capped at the distance to the cell's wall plus the clearance every object keeps from its walls. At the end the camera has flown exactly twelve cells, which looks the same as none, and the copies walk back out into the fog.

V Every scale

The Menger sponge is one line of a distance field repeated across scales: fold space into cells a third of the size, cut out a cross, and do it again. Levels finer than a pixel are simply left out: leaving their holes uncut only makes the shape a little fatter, so the field still never overstates the distance. Each level has its own colour. The corner is the front face, a Sierpiński carpet, zooming with the camera.

A cube
d0(p)=box(p,1)The distance to a cube, turning slowly. Nothing has been taken out yet.
Remove the middle
a=mod(3mp,2)−1,r=|1−3|a||dm+1=max(dm,(mini≠jmax(ri,rj)−1)/3m+1)Fold space into cells, take each point's distance to the cross through the middle of its cell, and cut the cross out; after the cut the field is only a lower bound. Every level folds into cells a third of the size, so the same line carves the cube, then each of the twenty cubes left, then each of theirs.
Every scale
dimM=log20/log3≈2.727Each level keeps twenty of twenty-seven cubes, so the volume goes to nothing while the surface grows without end. What is left has dimension between a surface and a solid.
Zoom
M∩K=c+(M−c)/3The part of the sponge inside the corner cube K is the whole sponge a third of the size, so the sponge is itself the corner of one three times larger. The camera flies to the corner, the sponge becomes the corner of a larger one, and the zoom runs in by powers of three: every time it has zoomed in threefold, the picture is the one it started from.