Domain repetition is not always free

Built on repetition of SDFs by Inigo Quilez. Sources for this reference

Folding a point into one cell tiles a shape across all of space for the cost of a round. Every reference says so, and every reference demonstrates it with a sphere. The sphere is the one case where the claim is exactly true.

vec2 q = p - c * floor(0.5 + p / c);   // fold into the nearest cell
return sdSphere(q, r);           // evaluate once

Why it is exact for a sphere

On a cubic lattice, rounding each coordinate independently lands on the Euclidean-nearest lattice point, because the axes are separable. For a shape that is radially symmetric and centered in its cell, the nearest instance is the one at the nearest center. So the distance you get back is the true distance. Nothing is approximated.

Circles. The red channel is the error against a neighbor-checked field, and it never fires.

And why it breaks for everything else

The fold is exact exactly when the shape fits inside its own Voronoi cell, meaning it never reaches further than half the spacing from its center. Cross that line and the reasoning collapses: the nearest cell center is still the nearest center, but the nearest surface can belong to the box next door. The fold answers with its own cell's box, which is further away, so the field overestimates. A wide thin box that still fits inside the half-cell is perfectly safe, which is worth saying because "wide and thin" is the wrong tell and it is the one I reached for first.

Boxes reaching past the half-cell. Left: the naive fold, overestimate in red. Right: neighbor-checked, correct.

Look at where the red lives: it is concentrated on the cell walls, which is exactly the place a tiled scene draws attention to. And notice the distance bands on the left kinking as they cross a boundary. A correct distance field has smooth level sets. Those kinks are the error, visible without any special instrumentation.

Overestimating is the dangerous direction

A sphere trace advances by the value the field returns. If that value is too small you waste steps. If it is too large you march past the surface, and the ray tunnels straight through geometry that was really there. So this is not a quality issue you can tune away with more iterations. It is a correctness issue, and it gets worse as the shape gets less symmetric.

The fix, and its cost

Evaluate the neighboring cells too and take the minimum. In 2D that is nine evaluations instead of one, in 3D twenty-seven, which is why nobody does it blindly. The useful middle ground: only the neighbors the shape can actually reach into, which for a shape bounded by half the cell size is none at all.

Rules of thumb

  1. The test is one number: does the shape reach further than half the spacing from its center?
  2. If it does not, the fold is exact. Shape, aspect ratio and symmetry do not matter.
  3. If it does, the fold overestimates near the seams, which is the overstep direction.
  4. Kinked distance bands at cell walls are the symptom. Look for them.
  5. Write the fold with round, never mod. HLSL fmod is a truncated remainder and breaks the whole negative side of the lattice.

All 61 notes How to use them Credits