# Understanding the shadow matrix

This topic is 1172 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all,

While learning d3d11, I've come to the topic 'shadowing'.

To make sure I understand the theory, I've written out the explanation below.

My question:

- do you think I'm on the right track/ do I understand it correctly?

(I'd like to be sure and not just assume and go on with the code I have)

anyone?

##### Share on other sites

What shadowing method are you shooting for? Mapping, volumes, cascading ect...

Edit: Basically each pixel needs to be transformed to be viewed from the angle of the light. If the pixel depth is closer than the transformed depth, the pixel is in the dark, if they are the same, it is lit

Edited by ExErvus

##### Share on other sites

Here an explanation of shadow mapping :

1) You generate a distance map from the light position, it's called shadow map, it's like depth map but for the light and not the camera.

2) You compare in the pixel shader the shadow map with the distance of the pixel in light space, this is the distance of the pixel view from the light.

If the distance of the pixel in light space is upper than the value from the shadow map, that mean the point is behind something.

If the point is behind something that mean the pixel is in shadow.

For the compare, you have to use an epsilon value because of floating point precision.

Here a the HLSL shader code for hard shadow (shadow without filtering) :

float ComputHardShadowFactor(in SURFACE_DATA SurfaceData)
{
// Transform world space to light space.
float4 PositionLightCS = mul(SurfaceData.PositionW, SunViewProj);

// Light depth in NDC space.
float LightDepth = PositionLightCS.z - 0.006f;

// Transform from NDC space to texture space.
float2 ShadowTexCoord = 0.5f * float2(PositionLightCS.x, -PositionLightCS.y) + 0.5f;

// Shadow depth test.
return LightDepth <= ShadowMapSample;
}

Here the lighting calcule :

Light.Intensity * Light.Color * (SurfaceData.DiffuseColor * NdotL + SpecFactor) * ShadowFactor;
Edited by Alundra

##### Share on other sites
Thanks, this clears things up.
Combined with the shadow theory and matrix from my d3d11 (luna) book, I should be able to figure things out.

##### Share on other sites

Luna book uses 3x3 averaging algorithm to filter the shadow :

float percentLit = 0.0f;

const float2 offsets[9] =
{
float2(-dx,  -dx), float2(0.0f,  -dx), float2(dx,  -dx),
float2(-dx, 0.0f), float2(0.0f, 0.0f), float2(dx, 0.0f),
float2(-dx,  +dx), float2(0.0f,  +dx), float2(dx,  +dx)
};

[unroll]
for(int i = 0; i < 9; ++i)
{
shadowPosH.xy + offsets[i], depth).r;
}

return percentLit /= 9.0f;

You can do the hardware PCF using a SamplerComparisonState :

D3D11_SAMPLER_DESC ShadowSamplerDesc;
ShadowSamplerDesc.BorderColor[ 0 ] = 0.0f;
ShadowSamplerDesc.BorderColor[ 1 ] = 0.0f;
ShadowSamplerDesc.BorderColor[ 2 ] = 0.0f;
ShadowSamplerDesc.BorderColor[ 3 ] = 0.0f;
ShadowSamplerDesc.MaxLOD           = D3D11_FLOAT32_MAX;

In the shader :

ShadowMap.SampleCmpLevelZero(ShadowSampler, uv, LightDepth);
Edited by Alundra

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634112
• Total Posts
3015566
×