# Artifact in case of creation of shadowmapping

When the light source moves away from the object, it creates unnecessary shadow:
[ATTACH=CONFIG]1154[/ATTACH]ok
[ATTACH=CONFIG]1155[/ATTACH]ok
[ATTACH=CONFIG]1156[/ATTACH]problem

``````vec3 computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
{
vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
vec2 uv = depth.xy;

if((depth.x < 0.0)||(depth.x > 1.0) || (depth.y < 0.0) || (depth.y > 1.0) || (depth.z < 0.0) || (depth.z > 1.0))
{
return vec3(1.0);
}else
{
float texelSize = 1.0/1024.0;
vec3 colour = vec3(0.0);int count = 0;float shadow;
for (int y = -2; y <= 2; ++y)
{
for (int x = -2; x <= 2; ++x)
{
vec2 offset = uv + vec2(float(x) * texelSize, float(y) * texelSize);
if((offset.x >= 0.0) && (offset.x <= 1.0) && (offset.y >= -0.0) && (offset.y <= 0.999))
{
if ( depth.z > shadow )
{
colour+=vec3(1.0)*0.8;
}
else
{
colour+=vec3(1.0);
}
count++;
}
}
}
colour /= float(count);
return colour;
}
}
``````

How to correct it?
live example

Is the shadow map large enough to cover the visible area?

When you render into the shadow map, are you setting the viewport to cover its full extent?

the problem is created in a cycle:

``````vec3 computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
{
vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
vec2 uv = depth.xy;
if((depth.x < 0.0)||(depth.x > 1.0) || (depth.y < 0.0) || (depth.y > 1.0) || (depth.z < 0.0) || (depth.z > 1.0))
{
return vec3(1.0);
}else
{
float texelSize = 1.0/1024.0;
vec3 colour = vec3(0.0);int count = 0;float shadow;
vec2 offset = uv ;
if((offset.x >= 0.0) && (offset.x <= 1.0) && (offset.y >= -0.0) && (offset.y <= 0.999))
{
{
colour+=vec3(1.0)*0.8;
}
else
{
colour+=vec3(1.0);
}
}
return colour;
}
}
``````

-Problems aren’t present, but shadows aren’t smoothed

That the artifact is not full intensity shadow is a good clue.

Some ideas to help you nail it down. Apply these cumulatively, not just flip each on and off in isolation:

• You can’t robustly clip after the perspective divide (with the perspective frustum for your point light). Clip before.
• Are you clearing the shadow map to 1 before you render the scene into it? If not, do so.
• For testing, disable anistropic filtering.
• For testing, set NEAREST on your MIN and MAG filters (i.e. disable PCF)
• What wrap behavior do you have set on your shadow map. Change it to CLAMP_TO_EDGE.
• Are you casting anything besides the human figure (e.g. the ground plane) into the shadow map? Disable that.
• When the light source gets close to the ground plane, how are you computing the bounds of the shadow frustum. Is the size exploding?
• You’re sampling textures with implicit LOD inside conditionals. You shouldn’t do that. Read this:
** Derivatives I: Derivatives and Discontinuities (http://hacksoflife.blogspot.com/2011/01/derivatives-i-discontinuities-and.html)
** Derivatives II: Conditional Texture Fetches (http://hacksoflife.blogspot.com/2011/01/derivatives-ii-conditional-texture.html)
• In your second shader, if you change (offset.y <= 0.999) to (offset.y <= 1.0 ), does the problem re-appear?
• Is it possible your math to back-project the eye-space vertices of the ground plane back into light (shadow) space isn’t right? Try munging the eye-space depth value to 0, 0.5, or 1?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.