Shaders From Zero · Episode 15
Stylized Toon Shader
Stylized Toon Shader is Episode 15 of Shaders From Zero, a MasterAllArts free interactive lesson for artists learning creative 3D on the web.
Part of the MasterAllArts Creative 3D Roadmap .
Toon shading is the gateway to non-photoreal rendering. The trick: take a smooth gradient of light and chop it into bands. Take a smooth gradient of color and chop it into a palette. Add a hard rim. Add a hard outline. Three quantization steps and a sphere stops looking 3D-renderable and starts looking drawn.
This is also the first episode in the series that does real lighting math — we fake a directional light with a hard-coded uniform and compute NdotL by hand.
Layer 1 — Smooth Lambert
We don't use Three.js's lights. Instead, hard-code a light direction as a uniform and compute the dot product with the surface normal — the classic Lambert diffuse term.
// VERTEX
varying vec3 vNormal;
void main() {
vNormal = normalize(normalMatrix * normal);
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
// FRAGMENT
varying vec3 vNormal;
uniform vec3 uLightDir;
uniform vec3 uBaseColor;
void main() {
vec3 N = normalize(vNormal);
vec3 L = normalize(uLightDir);
float NdotL = max(dot(N, L), 0.0);
gl_FragColor = vec4(uBaseColor * NdotL, 1.0);
}
The result: a smooth diffuse gradient. Looks like a clay ball under a single light. Photoreal-ish, boring.
Layer 2 — Quantize the lighting
Take NdotL (a value in [0,1]) and snap it to 3 levels.
Now the sphere has 3 distinct shadow bands.
float NdotL = max(dot(N, L), 0.0);
float lit = 0.2; // shadow
if (NdotL > 0.33) lit = 0.5; // midtone
if (NdotL > 0.66) lit = 1.0; // lit
gl_FragColor = vec4(uBaseColor * lit, 1.0);
Or use step if you prefer the GLSL-native form:
float lit = 0.2 + 0.3 * step(0.33, NdotL) + 0.5 * step(0.66, NdotL);
The cel-shaded look starts here. Hard transitions read as "drawn," soft transitions read as "rendered."
Layer 3 — Posterize the colors
Instead of multiplying one base color by 3 brightness levels (which gives 3 shades of the same hue), pick 3 hand-chosen palette colors:
vec3 shadow = vec3(0.2, 0.3, 0.55);
vec3 midtone = vec3(0.45, 0.6, 0.85);
vec3 lit = vec3(0.95, 0.95, 0.85);
vec3 col = lit;
if (NdotL < 0.66) col = midtone;
if (NdotL < 0.33) col = shadow;
gl_FragColor = vec4(col, 1.0);
This is what makes it look DRAWN instead of RENDERED. The eye reads "three distinct colors" as cel paint.
Layer 4 — Rim light
The classic anime "rim light" — a bright edge facing AWAY from the viewer. Looks like a spotlight from behind the character.
It's just the fresnel pattern from Episode 09, but used additively and hard-quantized.
// vertex must also pass vViewDir
float NdotV = max(dot(N, normalize(vViewDir)), 0.0);
float rim = pow(1.0 - NdotV, 3.0);
rim = smoothstep(0.6, 0.9, rim); // hard quantization
col += rim * vec3(1.0, 0.85, 0.6);
The smoothstep(0.6, 0.9, rim) is the key — it gives a HARD edge
on the rim contribution. Smooth rim = video-game shader. Hard rim = anime cel.
Layer 5 — Outline
Two common approaches:
- Inverted-mesh outline — render the mesh inflated with reversed backface culling. Robust at all distances, requires a second draw.
- Fresnel-based outline — use a very-high-power fresnel as a silhouette mask, paint it black. One pass, scales poorly with camera distance.
We use the fresnel approach because it works within a single
ShaderMaterial:
float silhouette = 1.0 - NdotV;
float outline = step(0.9, silhouette);
col = mix(col, vec3(0.0), outline);
The step(0.9, ...) means: if the silhouette mask is above 0.9 (very
grazing angle), paint pure black. Gives a clean black silhouette outline.
Tune the threshold. Too high → no outline. Too low → murky thick edge.
Real toon shaders ship outlines as a post-processing pass that uses the depth buffer to detect edges — much more robust at all distances and around interior detail. That's Episode 16 territory. The fresnel approach we use here is the simplest one that works without post-processing.
Putting it together
// FRAGMENT (final)
varying vec3 vNormal;
varying vec3 vViewDir;
uniform vec3 uLightDir;
void main() {
vec3 N = normalize(vNormal);
vec3 L = normalize(uLightDir);
vec3 V = normalize(vViewDir);
float NdotL = max(dot(N, L), 0.0);
vec3 shadow = vec3(0.2, 0.3, 0.55);
vec3 midtone = vec3(0.45, 0.6, 0.85);
vec3 lit = vec3(0.95, 0.95, 0.85);
vec3 col = lit;
if (NdotL < 0.66) col = midtone;
if (NdotL < 0.33) col = shadow;
float NdotV = max(dot(N, V), 0.0);
float rim = pow(1.0 - NdotV, 3.0);
rim = smoothstep(0.6, 0.9, rim);
col += rim * vec3(1.0, 0.85, 0.6);
float silhouette = 1.0 - NdotV;
float outline = step(0.9, silhouette);
col = mix(col, vec3(0.0), outline);
gl_FragColor = vec4(col, 1.0);
}
Common pitfalls
- Normals not transformed. Use
normalMatrix * normalso the normal stays correct under any model transformation. - Light direction in the wrong space. We compute in
view-space (because that's what
normalMatrixoutputs). If you want world-space lighting that's stable as the camera moves, transform the light direction byviewMatrixfirst. - Outline threshold too low. Looks like the sphere is wearing
a thick muddy halo. Keep
step(0.9, ...)or higher. - Rim hard-quantization missing. Smooth rim looks like a
Unity asset. Hard rim looks like anime.
smoothstep(0.6, 0.9, rim)gives the hard edge.
Exercises
- Two-tone (just shadow + lit, no midtone). Drop the middle band.
- 5 bands. Lengthier
ifladder. The smoother the gradient, the more it looks rendered again. - Animate the light direction so the highlight sweeps across the sphere with
uTime. ComputeuLightDirin JS:(Math.cos(t), 0.7, Math.sin(t)).
What's next
Episode 16 — Post-Processing Suite. The season finale.
EffectComposer, bloom, chromatic aberration, vignette, pixelation.
The five-pass pipeline you'll build for every cinematic 3D project after this.