🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Shadow mapping problem

Started by
1 comment, last by fleabay 3 years, 3 months ago

Hi !

My shadow mapping works for some parts of the scene, except for pixels that are outside the light which generate the shadow map. These pixels are in shadow whereas they should not be. Can you help me ?

This is some code :

half shadowing(psIn input)

{

float bias;

float4 color;

float2 projectTexCoord;

float depthValue;

float lightDepthValue;

float lightIntensity;

float4 textureColor;

// Set the bias value for fixing the floating point precision issues.

bias = 0.00050f;

//bias = 0.0001;

// Set the default output color to the ambient light value for all pixels.

//color = ambientColor;

// Calculate the projected texture coordinates.

projectTexCoord.x = input.posInLightSpace.x / input.posInLightSpace.w * 0.5f + 0.5f;

projectTexCoord.y = -input.posInLightSpace.y / input.posInLightSpace.w * 0.5f + 0.5f;

// Determine if the projected coordinates are in the 0 to 1 range. If so then this pixel is in the view of the light.

if ((saturate(projectTexCoord.x) == projectTexCoord.x) && (saturate(projectTexCoord.y) == projectTexCoord.y) )

{

// Sample the shadow map depth value from the depth texture using the sampler at the projected texture coordinate location.

depthValue = shadowTex2D.Sample(shadowMap, projectTexCoord).r;

// Calculate the depth of the light.

lightDepthValue = input.posInLightSpace.z / input.posInLightSpace.w;

// Subtract the bias from the lightDepthValue.

lightDepthValue -= bias;

// Compare the depth of the shadow map value and the depth of the light to determine whether to shadow or to light this pixel.

// If the light is in front of the object then light the pixel, if not then shadow this pixel since an object (occluder) is casting a shadow on it.

if (lightDepthValue < depthValue)

{

return 1.0;

}

else

return 0.2;

}

return 1.0;

}

Advertisement

Test. This is just a repost of the same code.

half shadowing(psIn input)
{
   float bias;
   float4 color;
   float2 projectTexCoord;
   float depthValue;
   float lightDepthValue;
   float lightIntensity;
   float4 textureColor;

   // Set the bias value for fixing the floating point precision issues.
   bias = 0.00050f;
   //bias = 0.0001;

   // Set the default output color to the ambient light value for all pixels.
   //color = ambientColor;

   // Calculate the projected texture coordinates.
   projectTexCoord.x = input.posInLightSpace.x / input.posInLightSpace.w * 0.5f + 0.5f;
   projectTexCoord.y = -input.posInLightSpace.y / input.posInLightSpace.w * 0.5f + 0.5f;

   // Determine if the projected coordinates are in the 0 to 1 range. If so then this pixel is in the view of the light.
   if ((saturate(projectTexCoord.x) == projectTexCoord.x) && (saturate(projectTexCoord.y) == projectTexCoord.y))
   {
      // Sample the shadow map depth value from the depth texture using the sampler at the projected texture coordinate location.
      depthValue = shadowTex2D.Sample(shadowMap, projectTexCoord).r;
      // Calculate the depth of the light.
      lightDepthValue = input.posInLightSpace.z / input.posInLightSpace.w;
      // Subtract the bias from the lightDepthValue.
      lightDepthValue -= bias;
      // Compare the depth of the shadow map value and the depth of the light to determine whether to shadow or to light this pixel.
      // If the light is in front of the object then light the pixel, if not then shadow this pixel since an object (occluder) is casting a shadow on it.
      if (lightDepthValue < depthValue)
      {
         return 1.0;
      }
      else
         return 0.2;
   }
   return 1.0;
}

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

This topic is closed to new replies.

Advertisement