Advertisement Jump to content
Sign in to follow this  

Problem with stable shadow mapping.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using a basic orthographic projection to create shadow mapping for a direction light in XNA.

I ran into the (I assume common) problem of the shimmering edges of the shadows as is described in this post:

I implemented the solution described in the post and it works great. HOWEVER, It seems to cut some of the shadows off. I've attached an image showing the problem:


The code I'm using to calculate the matrices can be found below. Playing with a few of the variables, it seems that this gap in the shadow is down to the ExtraBackup variable being too high. If I reduce it from 20 to 0, the shadows are nearly back to normal. However, this obviously creates different problems as not all onscreen models will be caught in the bounding sphere. Someone help!!!!

public void UpdateShadows(Vector3 lightDir, Matrix viewProjection)

BoundingFrustum frustum = new BoundingFrustum(viewProjection);

BoundingSphere sphere = BoundingSphere.CreateFromFrustum(frustum);
const float ExtraBackup = 20.0f;
const float NearClip = 1.0f;
float backupDist = ExtraBackup + NearClip + sphere.Radius;
Vector3 shadowCamPos = sphere.Center - (lightDir * backupDist);
lightViewMatrix = Matrix.CreateLookAt(shadowCamPos, sphere.Center, Vector3.Up);
float bounds = sphere.Radius * 2.0f;
float farClip = backupDist + sphere.Radius;
lightProjectionMatrix = Matrix.CreateOrthographic(bounds, bounds, NearClip, farClip);
lightViewProjectionMatrix = lightViewMatrix * lightProjectionMatrix;

//Make shadows stable under camera translation
float ShadowMapSize = 1024.0f;
Vector3 shadowOrigin = Vector3.Transform(Vector3.Zero, lightViewProjectionMatrix);
shadowOrigin *= (ShadowMapSize / 2.0f);
Vector2 roundedOrigin =new Vector2((float)Math.Round(shadowOrigin.X), (float)Math.Round(shadowOrigin.Y));
Vector2 rounding = roundedOrigin - new Vector2(shadowOrigin.X, shadowOrigin.Y); ;
rounding /= (ShadowMapSize / 2.0f);
Matrix roundMatrix = Matrix.CreateTranslation(rounding.X, rounding.Y, 0.0f);
lightViewProjectionMatrix *= roundMatrix;

Share this post

Link to post
Share on other sites
Ok well I've tried playing with the near and far planes, and I've tried altering the depth bias. No luck so far.

Any advice is welcome!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!