Introduction

SDF part 1 showed how single shapes are described by their distance functions. The real power of SDFs is how easily they combine. This post covers:

  • Union, intersection and subtraction with min and max
  • The famous smooth union, which melts shapes together like clay
  • Bending space: repeating, mirroring and twisting shapes by changing the point before measuring
  • When the results stop being true distances, and why that matters

Joining and Cutting Shapes

Say a and b are the distances from a point to two shapes. Then:

  • Union (either shape): min(a, b). The nearer of the two edges is the one that counts.
  • Intersection (only where both overlap): max(a, b). A point is inside only if it’s inside both, meaning both distances are negative.
  • Subtraction (cut a out of b): max(b, -a). Flipping the sign of a turns its inside into its outside, then intersecting keeps “inside b but outside a”.

Drag the circle and the box by their centres and try each one. Hover over or tap to measure the distance.

A circle and a box combined with distance functions

The shader code for this picture, with the current numbers filled in:

Smooth union

A plain union leaves a sharp crease where the two shapes meet, because min switches suddenly from one distance to the other. Smooth union blends between them near the crease. Where the two distances are very different, it behaves just like min. Where they’re close (near the join), it takes a little less than either, which pulls the surface outwards and fills the crease with a smooth curve:

float smoothUnion(float a, float b, float k) {
    float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);   // how much to favour a over b
    return mix(b, a, h) - k * h * (1.0 - h);             // blend, then bulge outwards a little
}

Here a and b are the two distances, as before, and k is how wide the blend is, in distance units. The value h is a number from 0 to 1 saying how much to favour a: 1 where a is much smaller than b, 0 where b is much smaller, and in between only near the join (where a and b are within k of each other). mix(b, a, h) is the lerp from Interpolation. The last part, k × h × (1 − h), is zero away from the join and biggest (k ÷ 4) right in the middle of it, where h = 0.5: that’s what pulls the surface outwards and fills the crease. Choose Smooth union and drag the shapes towards each other: they reach out and merge like drops of liquid. It’s used for organic shapes, blobby characters and lava lamps, and there are smooth versions of intersection and subtraction too.

Are these still true distances?

Switch to Distance field and look closely. For a union, the distances outside are still exact. But inside an intersection or subtraction, and near a smooth union’s blend, the contours aren’t quite evenly spaced: the numbers there are no longer the true distance to the edge, only a value that’s guaranteed never to be more than it. The edge itself is still in exactly the right place, so for drawing a shape it doesn’t matter. It does matter for effects that rely on the exact distance, like outlines of a fixed width, or for ray marching, where a too-big distance would make a ray jump through a surface.

Bending Space

SDF part 1 moved shapes by changing the point before measuring. The same idea gets much more powerful when the change isn’t just a shift. Choose a mode below.

Changing the point before measuring: repeat, mirror, copy round a circle, wobble

The shader code for this picture, with the current numbers filled in:

Hover over or tap the picture: the pink dot is your point, and the pink ring is where the code moves it to before measuring the one real shape.

In all of these, p is the point being measured, (x, y), and p.x and p.y are its two numbers.

  • Repeat: p - size * round(p / size), where size is the width of one cell and round rounds to the nearest whole number, moves every point into the middle cell, by subtracting a whole number of cell widths. Every cell then measures the same single shape, so you get an endless grid of copies for the cost of one. As they animate, they all turn together, because they’re really the same shape.
  • Mirror: p.x = abs(p.x) makes every point on the left behave like its mirror image on the right. Characters, logos and buildings are often symmetrical, so this halves the work.
  • Copies round a circle: turn each point’s angle into an angle within one slice (using the angles from Sine and Cosine; atan(p.y, p.x) is the function that gives a point’s angle from its x and y), keeping its distance from the centre the same, then measure once: a flower, a gear or a clock face.
  • Wobble: shift each point sideways by a sine wave before measuring, and the shape wobbles. Twisting, bending and stretching work the same way.

These are called domain operations, because they change the space the shape is measured in rather than the shape itself. Repetition and mirroring keep the distances exact (as long as the shape fits inside its cell). Wobbling, twisting and stretching don’t: they squash and stretch space, so distances get distorted, and ray marchers have to take smaller steps to stay safe.

Under the hood

Domain repetition is one of the reasons the demoscene (programmers who make astonishing animations in tiny programs) loves SDFs: a single round instruction turns one column into an endless temple, at almost no cost. The graphics card doesn’t know or care that there are “copies”; each pixel just does a little arithmetic on its own coordinates and measures one shape. Compare that with triangles, where a thousand copies means a thousand times the vertex work (or a technique called instancing to reduce it).

Questions You Might Have

Can the shapes be 3D?

Yes. Every operation here (min, max, smooth union, repeating, mirroring) works the same with 3D points and 3D distance functions such as spheres and boxes. SDF part 4 builds a 3D scene exactly this way.

Summary

  • Union = min(a, b), intersection = max(a, b), subtraction = max(b, -a).
  • Smooth union blends near the join, melting shapes together; k sets how far.
  • Combining can make the inside values approximate, though the edge stays exact.
  • Domain operations change the point before measuring: repeat, mirror, copy round a circle, wobble.

Next: crisp text from tiny textures.

Further Reading

  • Inigo Quilez, Smooth minimum: several versions of smooth union, explained and compared.
  • Inigo Quilez, Domain repetition: repeating shapes correctly, including the pitfalls.
  • Mercury, hg_sdf: a well-known library of SDF shapes and operators from the demoscene.