Introduction

SDF parts 1 and 2 described shapes with formulas. But some shapes, like the letters of a font, are far too complicated to write as a formula. This post shows how games get the benefits of distance fields anyway, by storing the distances in a texture. It covers:

  • Why ordinary text textures look blurry when magnified
  • Storing a distance field texture instead, and why that works so well
  • How such a texture is made
  • The effects it makes easy: outlines, glows, shadows and bolder or thinner text

The Problem with Letter Textures

Games draw text by copying letters from a texture onto little rectangles. If the texture stores the letter itself (how much of each texel the letter covers), that works at its original size. Magnify it, though, and bilinear filtering blends neighbouring texels into a soft, blurry blob with wobbly edges. You could store a bigger texture, but games need text at every size, and a huge texture for every letter of every font is a lot of memory.

Store the Distance Instead

In 2007, Valve published a neat fix that’s now used everywhere: in each texel, store the signed distance from that texel to the letter’s edge instead. The letter’s edge is wherever the distance is zero.

Both modes below use a texture of exactly the same size: just 48 × 48 texels for one letter. Zoom in.

The same 48 × 48 texture size, storing coverage or distance

The fragment shader's code for this view, with the current numbers filled in:

Choose The raw distance texture to see what’s actually stored: each texel drawn as a flat grey square, with no blending. Hover over or tap a texel to read its number. In the code, uv is the position on the texture, from 0 to 1 across (Textures and UVs), texture(uTex, uv).r reads the red channel (the only one this texture has) with bilinear filtering, and v is the value read. smoothstep is from Interpolation.

Why it works

Bilinear filtering blends neighbouring texels in straight lines (lerp). Blending colours that way smears an edge into a gradient. But think about how the distance to an edge changes as you move across it: if the edge is fairly straight, the distance changes at a steady rate, in a straight line. So blending distances in straight lines gives almost exactly the right distance in between. The shader then draws the edge where the distance crosses zero, blended over about one screen pixel using fwidth (SDF part 1), so it’s crisp at any zoom.

The weak spots are sharp corners. Near a corner, the distance doesn’t change in a straight line (the distance field turns into curves, as SDF part 1’s box showed), so magnified corners come out slightly rounded. Zoom right in on the tips of the S to see it.

Making the Texture

A distance field texture is made ahead of time, from a high-resolution drawing of each letter:

  1. Draw the letter large (this demo draws it at 512 × 512 when the page loads).
  2. Find the pixels on its edge.
  3. For each texel of the small texture, find the distance to the nearest edge pixel, negative if the texel is inside the letter.
  4. Store it as a number from 0 to 1, where 0.5 means “on the edge”, smaller is outside and larger is inside. Only distances up to a set spread (here about 4 texels) are stored; anything further is clamped, so 8 bits per texel is plenty.

A whole font’s letters are packed into one texture, called an atlas, along with a note of where each letter is.

Effects for Free

Because every texel knows how far it is from the edge, many effects become simple comparisons:

  • Weight: drawing the edge at 0.5 gives the normal letter. Move the Weight slider to draw it at a different value and the letter gets bolder or thinner, from the same texture. Games use this for emphasis, and to make text readable at small sizes.
  • Outline: colour everything between the edge and a little further out. Tick Outline.
  • Glow: fade a colour in as the distance outside the letter shrinks. Tick Glow.
  • Drop shadow: read the texture again at a small offset and draw a soft dark shape underneath. Tick Drop shadow.

With a normal letter texture, each of these would need a separate pre-made image or an expensive blur.

Under the hood

A refinement called multi-channel SDF (MSDF) uses the red, green and blue channels of the texture to store three slightly different distance fields, and takes the middle value of the three in the shader. Cleverly arranged, this preserves sharp corners, fixing the main weakness of the basic technique. Many game engines and user interface libraries now draw all their text with SDFs or MSDFs, from a single small texture per font. The same idea is used for icons, decals and even the edges of clouds of particles, anywhere a shape needs to look sharp at many sizes.

Questions You Might Have

Do I have to make the distance textures myself?

No. Tools generate them from ordinary font files, along with the atlas layout; Valve’s original paper describes the process, and open-source generators such as msdfgen also make the multi-channel version that keeps sharp corners.

Summary

  • Ordinary letter textures blur when magnified.
  • A distance field texture stores the distance to the edge, which filters almost perfectly, so edges stay crisp.
  • Sharp corners are slightly rounded; MSDF fixes that with three channels.
  • Outlines, glows, shadows and bold or thin text are just comparisons on the distance.

Next: ray marching, where distance functions draw entire 3D worlds.

Further Reading