• ### Popular Now

• 10
• 12
• 12
• 14
• 16

This topic is 902 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

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.

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;

}

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)
{
}

return percentLit /= 9.0f;

You can do the hardware PCF using a SamplerComparisonState :

D3D11_SAMPLER_DESC ShadowSamplerDesc;
ShadowSamplerDesc.MaxLOD           = D3D11_FLOAT32_MAX;

ShadowMap.SampleCmpLevelZero(ShadowSampler, uv, LightDepth);