On this page
Shadow Mapping
A shadow is simply the absence of a particular light. Shadow mapping is the standard, engine-agnostic way real-time graphics produce that absence, and it is one of the most useful tricks in all of graphics. Everything fancier, cascades, soft penumbras, contact shadows, is built on the same core compare.
// The whole idea, in one question the fragment stage asks:
// am I in front of what the light saw? No -> lit. Yes -> shadow.
The idea: give the light a memory¶
Imagine you could stand where the light stands and look out at the scene. Everything in its line of sight is lit. Anything that hides behind something else, anything the light cannot see directly, is in shadow. Shadow mapping makes exactly that viewpoint real. It renders the scene once from the light’s own point of view and stores the depth of everything it sees. That stored depth image is the shadow map, and it is the light’s memory of what was in front of it.
Then, while you render the scene the normal way from the camera, every fragment asks a single question about that memory: am I closer to the light than the depth the light recorded at my spot? If you are closer, the light’s line of sight to you is blocked and you are in shadow. If you are not, you are lit. That one depth comparison, run per pixel, is the entire technique.
Two passes, two viewpoints¶
Shadow mapping works with two passes that see the same scene from two different places. The caster pass renders the scene as the light sees it and writes depth into the shadow map. For a directional light that view is orthographic, a flat, even projection. For a point or spot light it is perspective, because those lights radiate out. The receiver pass is the one you usually write: it shades the scene normally from the camera, but for each fragment it also figures out where that fragment sits in the light’s view and compares it against the stored depth.
In a forward pipeline both roles exist for your material. The engine generates the caster automatically for objects that should cast, and your fragment shader does the receiving by asking the lighting system for a shadow result. You will rarely write the caster by hand, but you need to understand it, because it is where the shadow map is born and where the quality knobs live.
Bias, acne, and soft edges¶
A raw depth comparison has a famous failure. A flat surface lying exactly at the depth the light recorded will compare against its own depth, tie, and flicker between lit and shadowed as the camera moves. That crawling, speckled pattern is called shadow acne, and its cure is a small bias, a nudge that says “treat a surface exactly at the recorded depth as lit.” Push the bias too far, though, and the shadow detaches from the object and floats nearby. That is peter-panning. Modern setups also use a normal bias, which offsets the sample a little along the surface normal before the compare, fixing most acne without pulling the shadow off the wall.
The other quality knob is edge softness. Comparing a single texel of the shadow map gives a hard, aliased edge. Percentage-closer filtering samples a small neighborhood of texels around your spot and averages the lit and shadow results, turning the hard edge into a soft penumbra. Both the bias and the PCF are set on the light, not in your shader, which is why the receiving code stays simple while the visual quality is controlled from the lighting settings.
One map, or six¶
The shape of the shadow map depends on the light. A directional light shines from one direction and needs a single orthographic map, the cheap and common case. A spot light is a perspective view and also needs one map. A point light, though, radiates in every direction at once, so it must be seen from six viewpoints and gets a cube of six shadow maps. That is why a scene full of point lights with shadows is far more expensive than the same scene with directional lights, and it is the first thing to check when shadow cost surprises you.
What the shader does¶
The shader renders two objects from the light’s point of view: the front one casts, the back one sits in its shadow. The Shadow Strength property scales how much of the light’s stored depth the receiver trusts, so the same geometry moves between lit and shadowed purely by changing that weight.
Illustrative schematic: Caster and receiver from the light's point of view.
This is the receiver. The vertex stage projects each vertex into light space, the fragment stage pulls the main light together with its shadow attenuation, and a single multiply applies the result. The caster pass and the bias and PCF are provided by the pipeline and the light, so the shader stays clean.
Shader "DecompiledArt/Foundations/ShadowMapping/ShadowMapping"
{
Properties
{
[MainColor] _BaseColor("Base Color", Color) = (0.6, 0.7, 0.5, 1.0)
_Ambient("Ambient", Range(0.0, 1.0)) = 0.2
_ShadowStrength("Shadow Strength", Range(0.0, 1.0)) = 1.0
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }
Pass
{
Name "ForwardOnly"
Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
half3 normalWS : TEXCOORD0;
float4 shadowCoord : TEXCOORD1; // where this vertex sits in light space
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
half _Ambient;
half _ShadowStrength;
CBUFFER_END
Varyings Vert(Attributes IN)
{
Varyings OUT;
VertexPositionInputs p = GetVertexPositionInputs(IN.positionOS);
OUT.positionCS = p.positionCS;
OUT.normalWS = TransformObjectToWorldNormal(IN.normalOS);
OUT.shadowCoord = TransformWorldToShadowCoord(p.positionWS);
return OUT;
}
half4 Frag(Varyings IN) : SV_Target
{
half3 n = normalize(IN.normalWS);
Light mainLight = GetMainLight(IN.shadowCoord);
half NdotL = saturate(dot(n, mainLight.direction));
// The pipeline already compared this pixel against the light's
// depth image and handed back a 0..1 attenuation.
half shadow = lerp(1.0h, mainLight.shadowAttenuation, _ShadowStrength);
half3 col = _BaseColor.rgb * (mainLight.color * NdotL * shadow + _Ambient);
return half4(col, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
The one line that does the work is the attenuation. Everything about where the shadow falls, how hard its edge is, and how much it bites is decided by the pipeline when it built the shadow map and ran the compare. Your shader just has to ask for it and multiply.
🔗 Related Chapters¶
The Render Pipeline • Lambertian • Blinn-Phong
Related functions: Dot • Saturate
❤️ Support Shader Story¶
If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.
Your support helps keep this library open and free for everyone.