---
id: "msdf-sdf-text-rendering"
title: "SDF and multi-channel SDF text rendering"
aliases: [MSDF, msdfgen, distance field fonts, Valve alpha-tested magnification]
domain: "geometry"
stage: "geometry"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, mobile-gpu, unity, unreal, godot]
year_introduced: 2007
inputs: [curves]
outputs: [texture]
style: "both"
dynamic: "dynamic"
cost: "cheap"
authority: "Green, 2007"
url: "https://texelatlas.vercel.app/t/msdf-sdf-text-rendering/"
sources_verified_at: "2026-11-29"
---
# SDF and multi-channel SDF text rendering (Green, 2007)

> Bakes glyph or icon outlines into low-resolution distance textures; a shader thresholds the sampled distance, giving crisp edges at any scale, and the multi-channel form keeps sharp corners.

## Reach for it when

- text looks blurry when zoomed in on a texture
- glyph corners get rounded in sdf font atlas
- need crisp ui text at any scale in a shader
- outline and glow on text without extra passes

## How it works

For each glyph, compute a signed distance to the outline at every texel of a small atlas cell, clamped to a pixel range and remapped to 0..1 with 0.5 on the edge. At draw time the shader samples bilinearly, which interpolates distances far better than it interpolates coverage, then applies a smoothstep around 0.5 with a width taken from screen-space derivatives for antialiasing. Outlines, glows and drop shadows are just different thresholds. A single channel rounds off sharp corners because distance is a poor description there. Multi-channel SDFs colour the outline edges so that each RGB channel stores the distance to a subset of edges, with adjacent edges sharing a channel, and the shader takes the median of the three. The median reproduces the sharp corner while far from corners all three agree. A fourth channel often holds a true SDF for effects.

## When to use it

Choose it for UI, labels and world-space text that must stay sharp under zoom and perspective without big atlases. Prefer plain bitmap fonts when scale is fixed, and [Exact Euclidean Distance Transform](https://texelatlas.vercel.app/t/exact-euclidean-distance-transform.md) or [Jump Flooding Algorithm](https://texelatlas.vercel.app/t/jump-flooding-algorithm.md) when you only need to generate a single-channel field from a raster. Prefer direct GPU curve rendering (Slug-style) for huge glyphs or extreme magnification.

## Cost

cheap — One texture sample and a few ALU per fragment; the atlas is generated offline or at load.
Measured GPU Weight: 1.57× baseline (feather) — 0.0517 ms median / 0.0539 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 1.15× baseline (feather) — 0.0183 ms median (2.83× faster on L4).
Ampere A100 scaling: 1.01× baseline (feather) — 0.029 ms median (1.78× faster on A100).

## Failure modes

- Single-channel SDF rounds corners and drops thin serifs at low atlas resolution
- MSDF can show stray colour fringes or blobs where overlapping contours confuse the channel assignment unless the font is preprocessed
- Very small text loses hinting, so stems blur compared with a hinted rasteriser
- Atlas range too small clips outlines and glows; too large wastes precision and softens edges

## Engine support

- babylonjs: official — `import { TextRenderer } from "@babylonjs/addons" (msdfText)` (evidence: https://github.com/BabylonJS/Babylon.js/blob/9.38.0/packages/dev/addons/src/msdfText/textRenderer.ts, checked 2026-12-01)
- godot: builtin — `FontFile.multichannel_signed_distance_field = true (Label3D/Label/RichTextLabel)` (evidence: https://github.com/godotengine/godot/blob/4.7.2-stable/doc/classes/FontFile.xml, checked 2026-12-01) — Also SDF-style rendering of 3D text via Label3D.
- threejs: plugin — `npm i troika-three-text` (evidence: https://github.com/protectwise/troika/blob/main/packages/troika-three-text/README.md, checked 2026-12-01)
- unity: official — `TextMeshPro SDF font assets (Font Asset Creator)` (evidence: https://docs.unity3d.com/Packages/com.unity.ugui@2.0/manual/TextMeshPro/FontAssetsCreator.html, checked 2026-12-01) — Single-channel SDF; MSDF is not built in.
- unreal: builtin — `SlateCore FontSdfSettings (FFontSdfSettings) for signed distance field glyph rendering` (evidence: https://dev.epicgames.com/documentation/en-us/unreal-engine/python-api/class/FontSdfSettings, checked 2026-12-01)

## Dataflow

- Inputs: curves
- Outputs: texture
- Platforms: webgl2, webgpu, desktop-gpu, mobile-gpu, unity, unreal, godot

## Presets

### Median-of-three decode of a procedural glyph (glsl-fragment; rendered 2026-11-29)

```json
{
  "zoom_min": 0.55,
  "zoom_max": 1.6,
  "px_range": 6
}
```

```glsl
// GLSL ES 3.0, Shadertoy-style. Procedural 5-edge glyph encoded as a 3-channel signed distance,
// then decoded with the median trick. Sharp corners survive at any zoom.
const float ZOOM_MIN = 0.55;
const float ZOOM_MAX = 1.6;
const float PX_RANGE = 6.0;

float median(float a, float b, float c) { return max(min(a, b), min(max(a, b), c)); }

vec3 glyphMSDF(vec2 p) {
  vec2 P[5];
  P[0] = vec2(-0.5, -0.45); P[1] = vec2(0.5, -0.45); P[2] = vec2(0.5, 0.15);
  P[3] = vec2(0.0, 0.6);    P[4] = vec2(-0.5, 0.15);
  vec3 ch = vec3(-1e3);
  for (int i = 0; i < 5; i++) {
    vec2 a = P[i], b = P[(i + 1) % 5];
    vec2 e = normalize(b - a);
    vec2 n = vec2(e.y, -e.x);              // outward normal for counter-clockwise winding
    float s = dot(p - a, n);               // signed distance to the infinite edge line
    int col = (i == 4) ? 1 : (i % 3);      // edge colours 0,1,2,0,1
    for (int k = 0; k < 2; k++) {
      int c = (col + k) % 3;
      ch[c] = max(ch[c], s);
    }
  }
  return ch;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
  float zoom = mix(ZOOM_MIN, ZOOM_MAX, 0.5 + 0.5 * sin(iTime));
  float ang = 0.35 * sin(iTime * 0.7);
  mat2 R = mat2(cos(ang), -sin(ang), sin(ang), cos(ang));
  vec2 g = R * uv / zoom;
  vec3 m = glyphMSDF(g);
  float sd = -median(m.r, m.g, m.b);       // positive inside
  float screenPx = PX_RANGE * 0.5 / (iResolution.y * 0.5) * zoom; // unit-space size of one pixel-ish
  float aa = fwidth(sd) * 0.75 + 1e-4;
  float fill = smoothstep(-aa, aa, sd);
  float outline = smoothstep(-aa, aa, sd + 0.05) - fill;
  vec3 bg = mix(vec3(0.10, 0.11, 0.16), vec3(0.22, 0.2, 0.3), uv.y + 0.5);
  vec3 col = bg;
  col = mix(col, vec3(0.05, 0.05, 0.08), outline);
  col = mix(col, mix(vec3(1.0, 0.85, 0.4), vec3(1.0, 0.45, 0.3), g.y + 0.5), fill);
  fragColor = vec4(col, 1.0);
}

```

Attribution: written for this index

## Combines with

- [Exact Euclidean Distance Transform](https://texelatlas.vercel.app/t/exact-euclidean-distance-transform.md): Produces the single-channel distance field from a rasterised glyph that the alpha channel or simple SDF text uses.

## Sources

1. [primary] Chris Green (2007) Improved alpha-tested magnification for vector textures and special effects. ACM SIGGRAPH 2007 courses. https://doi.org/10.1145/1281500.1281665 (verified via crossref on 2026-11-29)
2. [implementation] Chlumsky (2016) Chlumsky/msdfgen. https://github.com/Chlumsky/msdfgen License: MIT. (verified via github on 2026-11-29)
3. [implementation] Chlumsky (2020) Chlumsky/msdf-atlas-gen. https://github.com/Chlumsky/msdf-atlas-gen License: MIT. (verified via github on 2026-11-29)
4. [implementation] protectwise (2019) protectwise/troika. https://github.com/protectwise/troika License: MIT. (verified via github on 2026-11-29)

## Cite this entry

Texel. "SDF and multi-channel SDF text rendering." https://texelatlas.vercel.app/t/msdf-sdf-text-rendering/ (accessed 2026-12-01).
