Outlines
Outlines make objects readable: the ink lines of a cel-shaded character, the highlight around a selected unit in a strategy game, the glow around an item you can pick up. There are three families of techniques, and which one you use depends on what information you have:
| Technique | Works on | Where it runs | Good for |
|---|---|---|---|
| SDF outline | shapes you have a distance function for | the shape's own shader | 2D shapes, UI, text, raymarched scenes |
| inverted hull | meshes | an extra draw of the mesh, scaled along normals | character ink lines in toon games |
| edge detection | anything already rendered | a post-process pass over depth/normal buffers | whole-scene ink style, selection outlines |
1. Outlines from a distance field
With an SDF, an outline is a band of distance. You already saw abs(d) - thickness in Shapes with SDFs. For an outline that sits outside the shape (so the fill is not eaten into), use the range 0 < d < width:
float sdBox(vec2 p, vec2 b) {
vec2 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);
}
float sdStar5(vec2 p, float r, float rf) { // star SDF from Inigo Quilez
const vec2 k1 = vec2(0.809016994375, -0.587785252292);
const vec2 k2 = vec2(-k1.x, k1.y);
p.x = abs(p.x);
p -= 2.0 * max(dot(k1, p), 0.0) * k1;
p -= 2.0 * max(dot(k2, p), 0.0) * k2;
p.x = abs(p.x);
p.y -= r;
vec2 ba = rf * vec2(-k1.y, k1.x) - vec2(0, 1);
float h = clamp(dot(p, ba) / dot(ba, ba), 0.0, r);
return length(p - ba * h) * sign(p.y * ba.x - p.x * ba.y);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
float px = 1.0 / iResolution.y;
float width = 0.015 + 0.01 * sin(iTime * 3.0);
float d = min(sdStar5(p - vec2(-0.35, 0.0), 0.25, 0.45), sdBox(p - vec2(0.4, 0.0), vec2(0.18, 0.12)) - 0.03);
vec3 color = vec3(0.85, 0.88, 0.92);
float fill = 1.0 - smoothstep(-px, px, d);
float line = 1.0 - smoothstep(width - px, width + px, d); // everything closer than width
color = mix(color, vec3(0.1), line); // outline color underneath
color = mix(color, vec3(1.0, 0.75, 0.2), fill); // fill on top
float glow = exp(-max(d, 0.0) * 25.0) * 0.4; // optional soft glow
color += vec3(1.0, 0.6, 0.2) * glow * (1.0 - line);
fragColor = vec4(color, 1.0);
}Because the distance is exact, the line has the same thickness everywhere, including around the star's sharp points. This is how font renderers that use SDF text (signed distance field glyph atlases, as in TextMeshPro in Unity) draw outlines and glows at any size.
2. Silhouettes and creases in a raymarched scene
A raymarcher knows more than a mesh renderer: it can tell how close each ray came to a surface, even when it missed. A ray that passes very close to an object without hitting it is right next to the silhouette, so tracking the minimum distance along a missed ray gives an outer outline.
For edges inside the shape (where one surface overlaps another, or at sharp creases), use screen-space derivatives: fwidth(t) jumps where neighboring pixels hit surfaces at very different depths, and fwidth(n) jumps where the normal changes sharply.
float map(vec3 p) {
float s = length(p - vec3(-0.5, 0.0, 0.0)) - 0.6;
vec3 q = abs(p - vec3(0.7, -0.1, -0.4)) - vec3(0.45);
float box = length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
return min(min(s, box), p.y + 0.55);
}
vec3 calcNormal(vec3 p) {
vec2 e = vec2(0.001, 0.0);
return normalize(vec3(map(p + e.xyy) - map(p - e.xyy),
map(p + e.yxy) - map(p - e.yxy),
map(p + e.yyx) - map(p - e.yyx)));
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
float a = 0.3 * sin(iTime * 0.5);
vec3 ro = vec3(3.0 * sin(a), 1.0, 3.0 * cos(a));
vec3 fwd = normalize(vec3(0.0, -0.1, 0.0) - ro);
vec3 right = normalize(cross(fwd, vec3(0, 1, 0))), up = cross(right, fwd);
vec3 rd = normalize(p.x * right + p.y * up + 1.6 * fwd);
float t = 0.0;
for (int i = 0; i < 120; i++) {
float d = map(ro + rd * t);
if (d < 0.001 || t > 12.0) break;
t += d;
}
vec3 lightDir = normalize(vec3(0.5, 0.8, 0.4));
vec3 color = vec3(0.95, 0.93, 0.88);
vec3 n = vec3(0.0, 1.0, 0.0);
if (t < 12.0) {
vec3 pos = ro + rd * t;
n = calcNormal(pos);
float light = smoothstep(0.0, 0.05, dot(n, lightDir)) * 0.5 + 0.5; // two-tone
vec3 albedo = pos.y < -0.54 ? vec3(0.9, 0.88, 0.8) : vec3(0.4, 0.7, 0.9);
color = albedo * light;
}
// Edges: big jumps in depth (silhouettes) or in the normal (creases) between neighbor pixels.
float depthEdge = fwidth(min(t, 12.0)) / max(t, 1.0);
float normalEdge = length(fwidth(n));
float edge = max(smoothstep(0.02, 0.06, depthEdge), smoothstep(0.3, 0.6, normalEdge));
color = mix(color, vec3(0.08), edge);
fragColor = vec4(color, 1.0);
}Dividing the depth jump by t makes it relative: a 0.1 m step means a lot up close and little far away, where the floor's depth naturally changes fast from pixel to pixel. Without it, the distant floor would turn black.
Note:
fwidthcompares neighbors in a 2×2 pixel quad, so these lines are one to two pixels wide and slightly blocky. A proper post-process pass (technique 3) samples a wider neighborhood and gives cleaner, thicker lines.
3. The inverted hull (mesh outlines)
For meshes, the classic toon outline draws the model twice. The second draw pushes every vertex outward along its normal by the line width, renders only back faces (front-face culling), and uses a flat dark color. The enlarged back shell peeks out around the silhouette and looks like an ink line. This needs a vertex shader, so it cannot run in a fragment-only playground; here are the two shaders in GLSL:
// Outline pass, vertex shader: draw with front faces culled (gl.cullFace(gl.FRONT)).
uniform mat4 uModel, uView, uProj;
uniform float uWidth; // in object units
in vec3 aPosition;
in vec3 aNormal;
void main() {
vec3 pushed = aPosition + normalize(aNormal) * uWidth;
gl_Position = uProj * uView * uModel * vec4(pushed, 1.0);
}// Outline pass, fragment shader
precision mediump float;
uniform vec3 uOutlineColor;
out vec4 outColor;
void main() { outColor = vec4(uOutlineColor, 1.0); }Known issues and their standard fixes:
| Problem | Fix |
|---|---|
| line gets thicker far away / thinner up close | push in clip space and scale by w, so the width is constant in pixels |
| gaps at hard edges (a cube's corners split open) | bake smoothed normals into a spare vertex channel (a second UV set or vertex color) and push along those |
| lines appear where you don't want them | paint a per-vertex width mask (vertex color) to thin or hide the line |
In Three.js the same idea is a second mesh with side: THREE.BackSide and a ShaderMaterial whose vertex shader pushes along the normal; the Three.js ShaderMaterial lesson shows the setup. Engines have it too: many Unity toon packages and Godot's StandardMaterial3D (Grow together with a Next Pass material set to front-face culling) use exactly this trick.
4. Post-process edge detection
The most general technique renders the scene first, then runs a full-screen pass that looks at the depth and normal buffers and draws a line wherever neighboring pixels differ too much: a Sobel or Roberts-cross filter over a few samples. It outlines everything consistently, including intersections between objects, and the line width is under control.
It needs the scene in textures, which is what Render Targets is about. Selection outlines, like the ones the Unity and Unreal editors draw around the selected object, are usually a variant: render the selected object's mask into a small target, blur or dilate it, and draw where the dilated mask exists but the original does not.
Test yourself
With an SDF d, which pixels belong to an outline drawn just outside the shape?
- d < 0
- 0 < d < width
- d > width
- d == 0 only
In the inverted hull technique, which faces does the outline pass draw?
- Front faces
- Back faces of a mesh pushed outward along its normals
- Only the silhouette edges
- No faces; it is a post-process
Why divide the depth jump fwidth(t) by t in the raymarched edge example?
- To get a negative value
- So far surfaces, whose depth changes quickly per pixel, are not mistaken for edges
- To normalize the normal
- It is required by fwidth