TExcelTAD
Home › Shaders & Graphics Programming › Toon and Cel Shading

Toon and Cel Shading

Realistic lighting is a smooth gradient from light to dark. Toon shading (cel shading) throws most of that gradient away: it quantizes the lighting into a few flat bands, like the painted cels of hand-drawn animation. The Legend of Zelda: The Wind Waker, Borderlands, Genshin Impact and Hi-Fi Rush all start from this idea, then add rim light, outlines and hand-painted ramps on top.

The examples in this lesson light a small raymarched scene. If raymarching is new to you, read Raymarching SDF Scenes first, or just treat march and calcNormal as "give me the surface point and its normal" and focus on the lighting function.

The base: Lambert

Every toon shader starts from the ordinary diffuse term NdotL = dot(normal, lightDir), which runs from -1 (facing away) to 1 (facing the light). Toon shading is just a different way of turning that number into brightness:

Style Brightness from NdotL
realistic (Lambert) max(NdotL, 0.0)
two-tone cel step(0.0, NdotL) → 0 or 1
N bands floor(max(NdotL, 0.0) * N) / N
soft-edged bands smoothstep(t - w, t + w, NdotL) per band
ramp texture texture(ramp, vec2(NdotL * 0.5 + 0.5, 0.5))

Bands, side by side

The left sphere uses smooth Lambert, the right one three hard bands. The light circles the scene:

glsl · try it yourself
float sdSphere(vec3 p, float r) { return length(p) - r; }
float map(vec3 p) {
    float a = sdSphere(p - vec3(-0.75, 0.0, 0.0), 0.6);
    float b = sdSphere(p - vec3(0.75, 0.0, 0.0), 0.6);
    return min(a, min(b, p.y + 0.6));            // two spheres on a floor
}
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)));
}
float march(vec3 ro, vec3 rd) {
    float t = 0.0;
    for (int i = 0; i < 100; i++) {
        float d = map(ro + rd * t);
        if (d < 0.001 || t > 20.0) break;
        t += d;
    }
    return t;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    vec3 ro = vec3(0.0, 0.4, 3.0);
    vec3 rd = normalize(vec3(p, -1.5));
    vec3 lightDir = normalize(vec3(cos(iTime), 0.8, sin(iTime)));

    vec3 color = vec3(0.6, 0.75, 0.9);           // sky
    float t = march(ro, rd);
    if (t < 20.0) {
        vec3 pos = ro + rd * t;
        vec3 n = calcNormal(pos);
        float ndl = dot(n, lightDir);
        float light = pos.x < 0.0 ? max(ndl, 0.0)                          // smooth
                                  : floor(max(ndl, 0.0) * 3.0) / 3.0;      // 3 bands
        vec3 albedo = pos.y < -0.59 ? vec3(0.5, 0.6, 0.4) : vec3(0.95, 0.45, 0.35);
        color = albedo * (0.25 + 0.75 * light);
    }
    fragColor = vec4(color, 1.0);
}

Notice that the floor follows the same rule as the spheres: a toon look must be applied to the whole scene, or the mix of styles looks like a bug.

Smooth band edges and colored shadows

Hard floor bands alias: the boundary is a jagged, one-pixel staircase. The standard fix is a narrow smoothstep at each threshold, with its width taken from fwidth, the screen-space rate of change of a value (how much it changes from one pixel to the next). That gives an edge exactly one pixel soft at any distance.

Real cel shading also never shades to grey. Artists pick a shadow color, usually a cooler, more saturated version of the base, and blend toward it:

glsl · try it yourself
float map(vec3 p) {
    float s = length(p - vec3(0.0, 0.1, 0.0)) - 0.8;
    float torus = length(vec2(length(p.xz) - 1.2, p.y + 0.5)) - 0.12;
    return min(min(s, torus), p.y + 0.7);
}
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)));
}
float march(vec3 ro, vec3 rd) {
    float t = 0.0;
    for (int i = 0; i < 100; i++) {
        float d = map(ro + rd * t);
        if (d < 0.001 || t > 20.0) break;
        t += d;
    }
    return t;
}

float band(float x, float threshold) {
    float w = fwidth(x);                          // one pixel worth of change
    return smoothstep(threshold - w, threshold + w, x);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    float a = iTime * 0.3;
    vec3 ro = vec3(3.2 * sin(a), 1.2, 3.2 * cos(a));
    vec3 fwd = normalize(-ro), right = normalize(cross(fwd, vec3(0, 1, 0))), up = cross(right, fwd);
    vec3 rd = normalize(p.x * right + p.y * up + 1.6 * fwd);
    vec3 lightDir = normalize(vec3(0.6, 0.8, 0.3));

    vec3 color = vec3(0.98, 0.93, 0.8);
    float t = march(ro, rd);
    if (t < 20.0) {
        vec3 pos = ro + rd * t;
        float ndl = dot(calcNormal(pos), lightDir);
        bool floorHit = pos.y < -0.69;
        vec3 lit    = floorHit ? vec3(0.95, 0.85, 0.6) : vec3(0.3, 0.75, 0.6);
        vec3 shadow = floorHit ? vec3(0.65, 0.5, 0.55) : vec3(0.15, 0.3, 0.5);
        vec3 col = mix(shadow, lit, band(ndl, 0.05));
        col = mix(col, lit * 1.25, band(ndl, 0.85) * (floorHit ? 0.0 : 1.0));   // highlight band
        color = col;
    }
    fragColor = vec4(color, 1.0);
}

fwidth(x) is abs(dFdx(x)) + abs(dFdy(x)). The GPU runs fragments in 2×2 blocks ("quads"), so it can compare a value with its neighbor pixel's. These derivative functions are only available in fragment shaders, and they are the reason a quad is always shaded together even at a triangle's edge.

Toon specular

A toon highlight is a hard-edged blob rather than a smooth glow. Take the Blinn-Phong term (see Specular: Phong and Blinn-Phong), then threshold it:

glsl
vec3 h = normalize(lightDir + viewDir);
float spec = pow(max(dot(n, h), 0.0), 64.0);
float toonSpec = band(spec, 0.5);   // a crisp white spot

Toon shaders almost always pair the banded diffuse with a rim light and an outline; those are the next lessons, Rim Light and Fresnel and Outlines.

In engines

Engine How toon shading is usually built
Unity URP a custom Lit shader or Shader Graph with a Custom Function node that reads the main light (GetMainLight()), then a ramp; Unity's Toon Shader package (com.unity.toonshader, the successor of the Unity-Chan Toon Shader) is a complete, open-source implementation
Unreal an Unlit material that computes its own bands from a light direction passed in through a Material Parameter Collection, or a post-process material that re-bands the lit scene
Godot a spatial shader with a light() function that quantizes dot(NORMAL, LIGHT); it runs once per light, so bands work with multiple lights

Tip: Make the band thresholds and colors uniforms, not constants. Artists will want to tune them per character, and a ramp texture (a 256×1 gradient) is even more flexible: it lets them paint the bands.

Test yourself

What does floor(max(ndl, 0.0) * 3.0) / 3.0 do to the lighting?

  1. Makes it smoother
  2. Quantizes it into a few flat bands
  3. Inverts it
  4. Adds a highlight

Why use fwidth when smoothing a band edge?

  1. It returns the light direction
  2. It gives how much the value changes over one pixel, so the edge is about one pixel soft at any distance
  3. It is required by WebGL2
  4. It normalizes vectors

Why do toon shaders usually blend toward a chosen shadow color rather than toward black?

  1. Black is not allowed in shaders
  2. A hue-shifted shadow color looks painted and keeps the character readable
  3. It is faster
  4. To avoid NaNs