Course contentsShow
← Unity Game Development
Lesson 613 of 8289. Rendering and VisualsFree lesson

Building Custom Lighting Models for Stylized Rendering

Override standard lighting calculations by creating custom light attenuation logic to achieve unique artistic styles like cel shading.

In this lesson, you will learn how to override standard PBR lighting calculations to create sharp, artistic transitions characteristic of cel-shaded visuals.

Breaking the Standard Light Model

In standard PBR (Physically Based Rendering), light intensity falls off smoothly across a surface based on the dot product between the surface normal and the light direction. This results in the soft, realistic gradients we’ve explored in previous lessons. For stylized rendering, we want to replace this continuous gradient with a "stepped" or "quantized" transition.

Implementing Toon Shading Logic

The core of a custom lighting model is manipulating the NdotL (Normal dot Light) value before it is multiplied by your albedo or lighting color. Instead of using the raw NdotL value, we pass it through a threshold function or a gradient ramp.

If you are using Shader Graph, you can achieve this by using the Smoothstep or Step node. Here is the logic in pseudocode:

// Standard calculation
float NdotL = dot(normal, lightDirection);

// Custom stylized attenuation
// We map the continuous gradient to a sharp threshold (e.g., 0.5)
float celShadedLight = smoothstep(0.48, 0.52, NdotL);

// Apply to final color
float3 finalColor = albedo * lightColor * celShadedLight;

Controlling the "Edge"

By adjusting the values inside the smoothstep (or using a texture ramp as a lookup table), you control how "hard" the edge of the shadow is. A narrow range (e.g., 0.49 to 0.51) creates a sharp anime-style line, while a wider range creates a soft-but-still-stylized look. By overriding this attenuation, you decouple the shader from the strict laws of physics, allowing you to force shadows to appear exactly where your artistic direction requires, regardless of the light intensity.

This technique is the foundation for creating everything from comic-book-inspired characters to vibrant, painterly environments, all while maintaining the performance benefits of real-time lighting.

Key Takeaway: By replacing smooth gradient light attenuation with a threshold-based step function, you can transform realistic surfaces into stylized, cel-shaded visuals.