Medium Mesh
Toon shading
Cartoon look: 3 flat brightness bands and a black outline at the silhouette.
About the Toon shading shader
Cartoon look: 3 flat brightness bands and a black outline at the silhouette. Vertex and fragment shaders on real 3D meshes: moving vertices, lighting, toon shading, holograms and dissolves, as used in Unity, Unreal and Godot.
How this challenge works
You write a fragment shader that draws the picture above, live in your browser. A match bar shows how close you are, and the score combines accuracy, code size (the par is 277 characters in Unity (Built-in)) and GPU speed. Work in HLSL, GLSL, WGSL, Metal or Godot and export the result when you are done.
What it is built from
- Light
normalize(float3(0.5, ?, ?))- Bands
band = floor(diffuse * ?) / ?- Color
float3(0.95, 0.75, 0.2) * (? + ? * band)- Outline
multiply by step(?, saturate(dot(i_normal, i_viewDir)))
The exact numbers are left out (shown as ?), but every colour is given. Measure the rest with the pixel readout on both pictures and Diff.
How to make a toon shading shader
- floor() on the diffuse value quantizes it into steps.
- The silhouette is where the normal is nearly perpendicular to the view.
Techniques used
- Diffuse lighting: brightness from the angle between the surface and the light.
- step: a hard 0-or-1 cutoff for crisp shapes and thresholds.
Toon shading shader code, explained step by step
Try to build it yourself first, because that is how it sticks. When you want to check your work or you are stuck, this is a complete working solution cut into parts, with the idea behind each part. The parts are in HLSL (Unity and Unreal); the full code below comes in every language.
Part 1: Choose the colours
Inside float4 frag(float2 uv)
float diff = max(dot(i_normal, normalize(float3(0.5, 0.7, 0.6))), 0.0);
float band = floor(diff * 3.0) / 3.0;
float3 col = float3(0.95, 0.75, 0.2) * (0.25 + 0.75 * band); - floor() picks the cell a point is in and frac() is the position inside that cell. Together they repeat a pattern.
- The dot product of the surface normal and the light direction is the diffuse light: bright where the surface faces the light, dark where it faces away.
- normalize() scales a direction to length 1, so only its direction matters.
- A mesh shader receives the position, surface direction and view direction of each pixel (i_worldPos, i_normal, i_viewDir).
Part 2: Colour the pixel and return it
Still inside frag()
col *= step(0.25, saturate(dot(i_normal, i_viewDir)));
return float4(col, 1.0); - step(a, x) is a hard cutoff: 1 once x passes a, 0 before.
- The dot product of the surface normal and the light direction is the diffuse light: bright where the surface faces the light, dark where it faces away.
- saturate() clamps a value into the 0 to 1 range.
- A mesh shader receives the position, surface direction and view direction of each pixel (i_worldPos, i_normal, i_viewDir).
- The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).
The full code
HLSL Unity, Unreal
float4 frag(float2 uv)
{
float diff = max(dot(i_normal, normalize(float3(0.5, 0.7, 0.6))), 0.0);
float band = floor(diff * 3.0) / 3.0;
float3 col = float3(0.95, 0.75, 0.2) * (0.25 + 0.75 * band);
col *= step(0.25, saturate(dot(i_normal, i_viewDir)));
return float4(col, 1.0);
} GLSL three.js, Shadertoy, OpenGL
vec4 frag(vec2 uv)
{
float diff = max(dot(i_normal, normalize(vec3(0.5, 0.7, 0.6))), 0.0);
float band = floor(diff * 3.0) / 3.0;
vec3 col = vec3(0.95, 0.75, 0.2) * (0.25 + 0.75 * band);
col *= step(0.25, clamp(dot(i_normal, i_viewDir), 0.0, 1.0));
return vec4(col, 1.0);
} Solve it in your language
Unity (Built-in), Unity (URP), Unreal, Shadertoy / WebGL, OpenGL (LWJGL, raylib), three.js, GameMaker, LÖVE (Lua), Godot, Apple (iOS, macOS), WebGPU / Bevy.
Start codingQuestions about this shader
How do I make a toon shading shader?
Cartoon look: 3 flat brightness bands and a black outline at the silhouette. A good first step: floor() on the diffuse value quantizes it into steps. In ShaderBattle you build it in the browser with a live match bar, so you see how close you are after every change.
What techniques does the toon shading shader use?
It is built from diffuse lighting, step. Each one is a small idea you can reuse in other effects.
Does this toon shading shader work in Unity, Unreal, Godot and Shadertoy?
Yes. The idea is the same in every shader language. ShaderBattle shows the code in HLSL (Unity, Unreal), GLSL (Shadertoy, three.js, OpenGL), WGSL (WebGPU), Metal and Godot, and exports a ready-to-paste version.