GLSL shader example

Three-Band Toon Shading

Calculate a continuous diffuse term, then quantize its intensity. Lighting bands move with the light because intensity, not screen position, is quantized.

Live example

Edit the code and the preview updates as you type.

What to observe

Three-Band Toon Shading.

Mechanism

Calculate a continuous diffuse term, then quantize its intensity. Lighting bands move with the light because intensity, not screen position, is quantized.

The controlling code

float r=dot(p,p);if(r<.5){vec3 n=normalize(vec3(p,sqrt(.5-r)));float l=max(dot(n,normalize(vec3(cos(u_time*.4),.5,1.))),0.);float band=floor(l*(2.+floor(u_parameter*4.)))/(2.+floor(u_parameter*4.));c=vec3(.2,.65,.95)*(.2+.8*band);}

Experiment

Set u_parameter to 0 and then 1, changing the divisor 2.+floor(u_parameter*4.) from 2 to 6. More brightness plateaus appear on the lit sphere, while their boundaries still follow the moving light.

Debugging and cost

The shader quantizes the diffuse value l after computing a sphere normal, without a texture lookup. Replace floor(l*(2.+floor(u_parameter*4.)))/(2.+floor(u_parameter*4.)) with l to restore continuous shading and isolate the quantization step.

Built-in functions used

Learn it step by step