{
 "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",
 "subdomain": "distance-field vector textures",
 "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",
 "summary": "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.",
 "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": "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` or `jump-flooding-algorithm` 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": {
  "class": "cheap",
  "notes": "One texture sample and a few ALU per fragment; the atlas is generated offline or at load."
 },
 "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/1281500.1281665",
   "type": "paper",
   "title": "Improved alpha-tested magnification for vector textures and special effects",
   "authors": [
    "Chris Green"
   ],
   "year": 2007,
   "venue": "ACM SIGGRAPH 2007 courses",
   "url": "https://doi.org/10.1145/1281500.1281665",
   "ids": {
    "doi": "10.1145/1281500.1281665"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1281500.1281665",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1281500.1281665",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1281500.1281665",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1281500.1281665",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/1281500.1281665",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "green2007improved"
  },
  {
   "role": "implementation",
   "key": "gh:chlumsky/msdfgen",
   "type": "code",
   "title": "Chlumsky/msdfgen",
   "description": "Multi-channel signed distance field generator",
   "authors": [
    "Chlumsky"
   ],
   "year": 2016,
   "venue": "GitHub",
   "url": "https://github.com/Chlumsky/msdfgen",
   "ids": {
    "repo": "Chlumsky/msdfgen"
   },
   "license": "MIT",
   "extra": {
    "stars": 4977,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-11T19:32:22Z",
    "language": "C++",
    "homepage": null,
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/Chlumsky/msdfgen",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/Chlumsky/msdfgen",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/Chlumsky/msdfgen",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/Chlumsky/msdfgen",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/Chlumsky/msdfgen",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/Chlumsky/msdfgen",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "chlumsky2016chlumsky"
  },
  {
   "role": "implementation",
   "key": "gh:chlumsky/msdf-atlas-gen",
   "type": "code",
   "title": "Chlumsky/msdf-atlas-gen",
   "description": "MSDF font atlas generator",
   "authors": [
    "Chlumsky"
   ],
   "year": 2020,
   "venue": "GitHub",
   "url": "https://github.com/Chlumsky/msdf-atlas-gen",
   "ids": {
    "repo": "Chlumsky/msdf-atlas-gen"
   },
   "license": "MIT",
   "extra": {
    "stars": 1358,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-05-16T16:29:59Z",
    "language": "C++",
    "homepage": null,
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/Chlumsky/msdf-atlas-gen",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/Chlumsky/msdf-atlas-gen",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/Chlumsky/msdf-atlas-gen",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/Chlumsky/msdf-atlas-gen",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/Chlumsky/msdf-atlas-gen",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/Chlumsky/msdf-atlas-gen",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "chlumsky2020chlumsky"
  },
  {
   "role": "implementation",
   "key": "gh:protectwise/troika",
   "type": "code",
   "title": "protectwise/troika",
   "description": "A JavaScript framework for interactive 3D and 2D visualizations",
   "authors": [
    "protectwise"
   ],
   "year": 2019,
   "venue": "GitHub",
   "url": "https://github.com/protectwise/troika",
   "ids": {
    "repo": "protectwise/troika"
   },
   "license": "MIT",
   "extra": {
    "stars": 1999,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-28T16:31:20Z",
    "language": "JavaScript",
    "homepage": null,
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/protectwise/troika",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/protectwise/troika",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/protectwise/troika",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/protectwise/troika",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/protectwise/troika",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/protectwise/troika",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "protectwise2019protectwise"
  }
 ],
 "presets": [
  {
   "id": "msdf-median-glyph",
   "title": "Median-of-three decode of a procedural glyph",
   "runtime": "glsl-fragment",
   "params": {
    "zoom_min": 0.55,
    "zoom_max": 1.6,
    "px_range": 6
   },
   "code": "// GLSL ES 3.0, Shadertoy-style. Procedural 5-edge glyph encoded as a 3-channel signed distance,\n// then decoded with the median trick. Sharp corners survive at any zoom.\nconst float ZOOM_MIN = 0.55;\nconst float ZOOM_MAX = 1.6;\nconst float PX_RANGE = 6.0;\n\nfloat median(float a, float b, float c) { return max(min(a, b), min(max(a, b), c)); }\n\nvec3 glyphMSDF(vec2 p) {\n  vec2 P[5];\n  P[0] = vec2(-0.5, -0.45); P[1] = vec2(0.5, -0.45); P[2] = vec2(0.5, 0.15);\n  P[3] = vec2(0.0, 0.6);    P[4] = vec2(-0.5, 0.15);\n  vec3 ch = vec3(-1e3);\n  for (int i = 0; i < 5; i++) {\n    vec2 a = P[i], b = P[(i + 1) % 5];\n    vec2 e = normalize(b - a);\n    vec2 n = vec2(e.y, -e.x);              // outward normal for counter-clockwise winding\n    float s = dot(p - a, n);               // signed distance to the infinite edge line\n    int col = (i == 4) ? 1 : (i % 3);      // edge colours 0,1,2,0,1\n    for (int k = 0; k < 2; k++) {\n      int c = (col + k) % 3;\n      ch[c] = max(ch[c], s);\n    }\n  }\n  return ch;\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float zoom = mix(ZOOM_MIN, ZOOM_MAX, 0.5 + 0.5 * sin(iTime));\n  float ang = 0.35 * sin(iTime * 0.7);\n  mat2 R = mat2(cos(ang), -sin(ang), sin(ang), cos(ang));\n  vec2 g = R * uv / zoom;\n  vec3 m = glyphMSDF(g);\n  float sd = -median(m.r, m.g, m.b);       // positive inside\n  float screenPx = PX_RANGE * 0.5 / (iResolution.y * 0.5) * zoom; // unit-space size of one pixel-ish\n  float aa = fwidth(sd) * 0.75 + 1e-4;\n  float fill = smoothstep(-aa, aa, sd);\n  float outline = smoothstep(-aa, aa, sd + 0.05) - fill;\n  vec3 bg = mix(vec3(0.10, 0.11, 0.16), vec3(0.22, 0.2, 0.3), uv.y + 0.5);\n  vec3 col = bg;\n  col = mix(col, vec3(0.05, 0.05, 0.08), outline);\n  col = mix(col, mix(vec3(1.0, 0.85, 0.4), vec3(1.0, 0.45, 0.3), g.y + 0.5), fill);\n  fragColor = vec4(col, 1.0);\n}\n",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "msdf-sdf-text-rendering",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.7028,
      "std": 0.053,
      "unique_colors": 14
     },
     "image": "/specimens/msdf-median-glyph.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "exact-euclidean-distance-transform",
  "jump-flooding-algorithm",
  "sdf-csg-smooth-min"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "exact-euclidean-distance-transform",
   "why": "Produces the single-channel distance field from a rasterised glyph that the alpha channel or simple SDF text uses."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/geometry/msdf-sdf-text-rendering.json"
 },
 "solves": [
  "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"
 ],
 "authority": "Green, 2007",
 "url": "https://texelatlas.vercel.app/t/msdf-sdf-text-rendering/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 1.57,
  "tier": "feather",
  "p50_ms": 0.0517,
  "p95_ms": 0.0539,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "msdf-median-glyph",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 1.15,
   "tier": "feather",
   "p50_ms": 0.0183,
   "p95_ms": 0.0184,
   "speedup_x": 2.83,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 1.01,
   "tier": "feather",
   "p50_ms": 0.029,
   "p95_ms": 0.0291,
   "speedup_x": 1.78,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}