On this page
Shadow Map Sampling
A shadow map is just a depth texture, and using it is just sampling it with a compare. This chapter is the texture side of shadowing. The theory, why it works and where the artifacts live, is in Foundations: Shadow Mapping. Here you learn what the map looks like as data and how a sample against it produces a yes or no.
// The whole use, as texture operations:
float4 lightPos = TransformWorldToShadowCoord(positionWS); // uv + depth
half storedDepth = SAMPLE_DEPTH_TEXTURE(_ShadowMap, sampler_ShadowMap, lightPos.xyz);
half inShadow = step(lightPos.w, storedDepth - bias) ? 1.0h : 0.0h;
What the map stores¶
The caster pass, the one that renders the scene from the light’s eye, writes depth into a texture instead of color. So the shadow map, read as a texture, is a grayscale image where each texel is a distance: how far the nearest thing was from the light at that spot. A dark texel is close to the light, a bright one is far. On a depth texture the channel is a raw depth value, not a color, so it is linear data with no sRGB, same habit as Normal Map Sampling.
Sampling it is a two part question. First, where in the map am I, and that is a UV, found by projecting the fragment into the light’s view. Second, how deep am I compared to what the map stored there, and that is the compare. Put together, a sample returns a number and your fragment’s own depth in light space, and the difference decides lit or shadow.
The compare, and the bias you must keep¶
The compare reads: if my depth in light space is less than what the light stored, something is between me and the light, so I am in shadow. One subtlety ruins a naive version. A surface stores its own depth in the map, so it compares against itself, ties, and flickers between the two answers frame to frame. That is the acne from the foundations chapter, and on the sampling side its cure is a bias: subtract a tiny amount from the stored depth before the compare, so a surface at exactly its recorded depth reads as lit.
half stored = SAMPLE_DEPTH_TEXTURE(_ShadowMap, sampler_ShadowMap, lightPos.xyz);
half ref = lightPos.w - _Bias;
half shadow = saturate(1.0h - step(ref, stored)); // 1 when in shadow
step(ref, stored) is one when the stored depth is in front of you, and the 1 - flips it to a 0 to 1 attenuation you multiply into the light. The foundations chapter covers the full artifact family, including peter-panning when the bias gets too large.
Soft edges: sampling a neighborhood¶
A single texel compare gives a hard edge, because the map is one depth sample per spot. Soft shadows, the penumbra, come from sampling a small patch of the map around your UV and averaging the answers. Three or five taps give a visible softening. A full percentage closer filter samples more, and the number of taps is the softness slider. The foundations chapter treats PCF as a light setting, and that is how most pipelines expose it, but the mechanism is exactly this: more samples, averaged, softer edge.
// A five tap cross, the cheap version of PCF:
half2 texel = 1.0 / _ShadowMapSize.xy;
half sum = 0;
sum += Compare(lightPos.xyz + texel * float2( 1, 1));
sum += Compare(lightPos.xyz + texel * float2(-1, 1));
sum += Compare(lightPos.xyz + texel * float2( 1, -1));
sum += Compare(lightPos.xyz + texel * float2(-1, -1));
sum += Compare(lightPos.xyz);
half shadow = sum / 5.0;
In a real project the pipeline hands you the finished attenuation, and you multiply. This chapter exists so that when the pipeline’s result surprises you, you can read the surprise in the same vocabulary: a UV, a stored depth, a compare, a bias.
What the shader does¶
The shader renders one caster and one receiver, sampling the shadow map with a compare and a bias property. The bias controls how much depth the receiver trusts, so the same scene can go from acne-ridden to clean by changing one value.
Illustrative schematic: Caster, receiver, and the depth map.
The pipeline provides the shadow coordinate and the bias, and this pass does the compare by hand so you can see the texture side. A tap count slider runs it from one hard sample to a small PCF.
Shader "DecompiledArt/Textures/Basics/ShadowMapSampling/ShadowMapSampling"
{
Properties
{
[MainColor] _BaseColor("Base Color", Color) = (0.7, 0.7, 0.6, 1)
_Ambient("Ambient", Range(0, 1)) = 0.2
_Bias("Bias", Range(0, 0.01)) = 0.001
_Taps("PCF Taps (0..4)", Range(0, 4)) = 1
}
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;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
half _Ambient;
half _Bias;
half _Taps;
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 L = GetMainLight(IN.shadowCoord);
// The pipeline's compare, for reference:
half pipelineShadow = L.shadowAttenuation;
// The manual texture side: the map is a depth texture.
float3 sUv = IN.shadowCoord.xyz;
float myD = IN.shadowCoord.w - _Bias;
half sum = 0;
half count = 1;
half2 texel = 1.0 / max(unity_ShadowMapSizes[0].xy, 1.0);
for (int i = 0; i <= (int)_Taps; i++)
{
half2 o = (i == 0) ? half2(0, 0)
: half2((i & 1) ? 1 : -1, (i & 2) ? 1 : -1);
float stored = SAMPLE_DEPTH_TEXTURE(_MainLightShadowmapTexture,
sampler_MainLightShadowmapTexture,
sUv + float2(o) * texel).r;
sum += (myD < stored) ? 1.0h : 0.0h;
count += 1.0h;
}
half manualShadow = 1.0h - sum / count;
half shadow = lerp(pipelineShadow, manualShadow, step(0.5h, _Taps));
half NdotL = saturate(dot(n, L.direction));
half3 color = _BaseColor.rgb * (L.color * NdotL * shadow + _Ambient);
return half4(color, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
Toggle the taps at zero and above to flip between the pipeline’s attenuation and the hand made compare, and nudge the bias to see acne form and fade.
🔗 Related Chapters¶
Foundations: Shadow Mapping • Lambertian
Related functions: Step • 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.