# XNA 4.0 - shimmering shadow maps

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

## Recommended Posts

I've been experiencing shadow map shimmering in my game right now and I've been looking at ways to solve it. I've been reading through this article and it has some really great things in it. but I can't quite wrap my head around what it's talking about on shimmering shadows. It says to move the light in texel-sized increments. I know what a texel is, but I have no idea how to convert where my shadow map is to a texel increment.

Can anyone translate that into non-GPU-wizard talk?

##### Share on other sites
Yeah it's a pretty simple concept, but it's kinda hard to explain. How are you setting up your shadow maps now? I can try and talk you through how to modify it to get rid of the shimmering.

##### Share on other sites
I'm taking my scene perspective camera and creating a region that encapsulates the whole view frustum of said camera. Most of the code was lifted from a forum post on this site =]

Here's the code:

        public static OrthogonalCamera DLShadowCamera(PerspectiveCamera pCamera, float high, Vector3 direction)        {            // Calculate the world space location centroid            Vector3[] cornersWS = pCamera.ViewFrustum.GetCorners();            Vector3 centroid = new Vector3(0,0,0);                        float low = high;            for (int i = 0; i < cornersWS.Length; i++)            {                centroid += cornersWS;                if (cornersWS.Y < 0)                    cornersWS.Y = 0.0f;                if (low > cornersWS.Y)                    low = cornersWS.Y;            }            centroid /= cornersWS.Length;            centroid.Y = (high - low) / 2;            // Calculate the position of the direction light.             float distance = centroid.Y / direction.Y;            Matrix lightViewMatrix = Matrix.CreateLookAt(centroid + (direction * distance), centroid, Vector3.Up);            // Calculate the light space locations of the 8 corners of the             // (world space) box. The lightViewMatrix is used to             // transform each world space frustum corner into light space.             // fake point[0].Y and point[1].Y to the high and low of the world space            cornersWS[0].Y = high;            cornersWS[1].Y = low;            Vector3[] cornersLS = new Vector3[cornersWS.Length];            Vector3.Transform(cornersWS, ref lightViewMatrix, cornersLS);            // Calculate the bounding box for the light space frustum             // corners. The bounding box is used to construct the proper             // orthographic projection matrix for the directional light.             BoundingBox box = BoundingBox.CreateFromPoints(cornersLS);            OrthogonalCamera camera = new OrthogonalCamera(centroid + (direction * distance), centroid, box.Min.X, box.Max.X, box.Min.Y, box.Max.Y, -box.Max.Z, -box.Min.Z);            return camera;        }

##### Share on other sites
Alright, I get it. Thanks Matt.

I don't think I'll be able to use a Bounding Sphere as I have some VERY tall geometry (100 times taller than a player) that will always need to be covered by the bounding region. But I get your point on keeping the size of the projection a constant. I should be able to do that with a pseudo fit-to-scene projection.

##### Share on other sites
Got it working on the first try. Truly a fantastic explanation matt.

In case anyone else is following a long and is lazier than I am, here's the result of the discussion:

[source = csharp]        public static OrthogonalCamera DLShadowCamera(PerspectiveCamera pCamera, float high, float low, Vector2 boxSize, int texSize, Vector3 direction)        {            // SOURCE: http://www.gamedev.net/community/forums/topic.asp?topic_id=591684            // CREATE A BOX CENTERED AROUND THE pCAMERA POSITION:                      Vector3 min = pCamera.Position - new Vector3(boxSize.X / 2, 0, boxSize.Y / 2);            min.Y = low;            Vector3 max = pCamera.Position + new Vector3(boxSize.X / 2, 0, boxSize.Y / 2);            max.Y = high;            BoundingBox boxWS = new BoundingBox(min, max);            // CREATE A VIEW MATRIX OF THE SHADOW CAMERA            Vector3 shadowCamPos = pCamera.Position;            shadowCamPos.Y = high - low;            Matrix shadowViewMatrix = Matrix.CreateLookAt(shadowCamPos, (shadowCamPos + (direction * 10)), Vector3.Up);            // TRANSFORM THE BOX INTO LIGHTSPACE COORDINATES:            Vector3[] cornersWS = boxWS.GetCorners();            Vector3[] cornersLS = new Vector3[cornersWS.Length];            Vector3.Transform(cornersWS, ref shadowViewMatrix, cornersLS);            BoundingBox box = BoundingBox.CreateFromPoints(cornersLS);            // CREATE PROJECTION MATRIX            Matrix shadowProjMatrix = Matrix.CreateOrthographicOffCenter(box.Min.X, box.Max.X, box.Min.Y, box.Max.Y, -box.Max.Z, -box.Min.Z);            Matrix shadowViewProjMatrix = shadowViewMatrix * shadowProjMatrix;            Vector3 shadowOrigin = Vector3.Transform(Vector3.Zero, shadowViewProjMatrix);            shadowOrigin *= (texSize / 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 /= (texSize / 2.0f);            Matrix roundMatrix = Matrix.CreateTranslation(new Vector3(rounding.X, rounding.Y, 0.0f));            shadowViewProjMatrix *= roundMatrix;            return new OrthogonalCamera(shadowCamPos, shadowViewProjMatrix);        }

##### Share on other sites
Hate to revive an old post but I'm implementing CSM using bounding spheres. However, I have a question.

Vector3 shadowCamPos = sphere.Center + direction * backupDist;

Is the direction the light direction? Also when the camera faces a particular direction, the shadows disappear.

##### Share on other sites
Yes, that direction would be direction towards the directional light, basically what you would use for N dot L.

I'm not sure why your shadows would disappear...I would try to break into the debugger when that happens and step through your code for creating the shadow matrices. Capturing in PIX/Nsight/PerfStudio might be helpful too. Edited by MJP

##### Share on other sites
Here an XNA version of the code. I prototype in XNA before I port it to SlimDx. I tired pix and stepping through debugging. I didn't see anything unusual.

Here a video of what's happening.

 const int SHADOW_RESOLUTION = 4096; static SpriteBatch spriteBatch = Engine.SpriteBatch; static int width = Engine.BackBufferWidth; static int height = Engine.BackBufferHeight; static RenderTarget2D depthMap = new RenderTarget2D(Engine.Graphics.GraphicsDevice, width, height, false, SurfaceFormat.Rg32, DepthFormat.Depth24); static RenderTarget2D shadowMap = new RenderTarget2D(Engine.Graphics.GraphicsDevice, SHADOW_RESOLUTION, SHADOW_RESOLUTION, false, SurfaceFormat.Rg32, DepthFormat.Depth24); public static RenderTarget2D shadowOcclusion = new RenderTarget2D(Engine.Graphics.GraphicsDevice, width, height, false, SurfaceFormat.Color, DepthFormat.Depth24); static Effect depthMapEffect = Engine.Game.Content.Load<Effect>("Effects\\Shadows\\DepthMap"); static Effect shadowMapEffect = Engine.Game.Content.Load<Effect>("Effects\\Shadows\\ShadowMap"); static Effect shadowOcclusionEffect = Engine.Game.Content.Load<Effect>("Effects\\Shadows\\ShadowOcclusion2"); static Matrix shadowMatrix = Matrix.Identity; static Vector3[] farFrustumCornersVS = new Vector3[4]; static Vector3[] frustumCornersVS = new Vector3[8]; public static void Draw() { RenderDepthMap(); ShadowMatrix(); RenderShadowMap(); RenderShadowOcclusion(); } public static void Initialize() { shadowOcclusionEffect.Parameters["OcclusionTextureSize"].SetValue(new Vector2(width, height)); shadowOcclusionEffect.Parameters["ShadowMapSize"].SetValue(new Vector2(shadowMap.Width, shadowMap.Height)); depthMapEffect.Parameters["FarPlane"].SetValue(Camera.FarPlaneDistance); } private static void RenderDepthMap() { Engine.Graphics.GraphicsDevice.SetRenderTarget(depthMap); Engine.Graphics.GraphicsDevice.Clear(ClearOptions.Target, Color.White, 1.0f, 0); depthMapEffect.Parameters["ViewProjection"].SetValue(Camera.ViewProjection); BlockRender.Draw(depthMapEffect); Engine.Graphics.GraphicsDevice.SetRenderTarget(null); } private static void ShadowMatrix() { BoundingSphere sphere = BoundingSphere.CreateFromFrustum(Camera.BoundingFrustum); float NearClipOffset = 500.0f; float NearClip = 1.0f; float backupDist = NearClipOffset + NearClip + sphere.Radius; //View Vector3 position = sphere.Center + (new Vector3(1, 1, 1) * backupDist); Matrix view = Matrix.CreateLookAt(position, sphere.Center, Vector3.Up); //Projection float bounds = sphere.Radius * 2.0f; float farClip = 2000 + sphere.Radius; Matrix projection = Matrix.CreateOrthographic(bounds, bounds, NearClip, farClip); shadowMatrix = view * projection; Vector3 shadowOrigin = Vector3.Transform(Vector3.Zero, shadowMatrix); shadowOrigin *= (SHADOW_RESOLUTION / 2.0f); Vector3 roundedOrigin = new Vector3((float)Math.Round(shadowOrigin.X), (float)Math.Round(shadowOrigin.Y), 0); Vector3 rounding = roundedOrigin - shadowOrigin; rounding /= (SHADOW_RESOLUTION / 2.0f); Matrix roundMatrix = Matrix.CreateTranslation(rounding.X, rounding.Y, 0.0f); shadowMatrix *= roundMatrix; } private static void RenderShadowMap() { Engine.Graphics.GraphicsDevice.SetRenderTarget(shadowMap); Engine.Graphics.GraphicsDevice.Clear(ClearOptions.Target, Color.White, 1.0f, 0); shadowMapEffect.Parameters["ViewProjection"].SetValue(shadowMatrix); BlockRender.Draw(shadowMapEffect); Engine.Graphics.GraphicsDevice.SetRenderTargets(null); } private static void RenderShadowOcclusion() { Engine.Graphics.GraphicsDevice.SetRenderTarget(shadowOcclusion); Engine.Graphics.GraphicsDevice.Clear(ClearOptions.Target, Color.White, 1.0f, 0); Matrix viewMatrix = Camera.View; Vector3.Transform(Camera.BoundingFrustum.GetCorners(), ref viewMatrix, frustumCornersVS); for (int i = 0; i < 4; i++) farFrustumCornersVS = frustumCornersVS[i + 4]; Matrix inverseView = Camera.World; shadowOcclusionEffect.Parameters["ViewProjection"].SetValue(Camera.ViewProjection); shadowOcclusionEffect.Parameters["InverseView"].SetValue(inverseView); shadowOcclusionEffect.Parameters["LightViewProjection"].SetValue(shadowMatrix); shadowOcclusionEffect.Parameters["FrustumCornersVS"].SetValue(farFrustumCornersVS); shadowOcclusionEffect.Parameters["ShadowMap"].SetValue(shadowMap); shadowOcclusionEffect.Parameters["DepthTexture"].SetValue(depthMap); shadowOcclusionEffect.CurrentTechnique.Passes[0].Apply(); DeferredRenderer.FullScreenQuad.Draw(); } public static void DrawRenderTargets() { spriteBatch.Begin(0, BlendState.Opaque, SamplerState.PointClamp, null, null); { spriteBatch.Draw(shadowMap, new Rectangle(0, 0, 256, 256), Color.White); spriteBatch.Draw(depthMap, new Rectangle(0, 256, 256, 256), Color.White); spriteBatch.Draw(shadowOcclusion, new Rectangle(0, 512, 256, 256), Color.White); } spriteBatch.End(); } } 

 float4x4 World; float4x4 ViewProjection; float4x4 InverseView; float4x4 LightViewProjection; float2 ShadowMapSize; float2 OcclusionTextureSize; float3 FrustumCornersVS [4]; static const float ShadowBias = 0.01f; static const float DarkenFactor = 100.0f; texture DepthTexture; sampler2D DepthTextureSampler = sampler_state { Texture = <DepthTexture>; MinFilter = Point; MagFilter = Point; MipFilter = None; }; texture ShadowMap; sampler2D ShadowMapSampler = sampler_state { Texture = <ShadowMap>; MinFilter = Anisotropic; MagFilter = Anisotropic; MipFilter = None; MaxAnisotropy = 16; }; float2 sampleShadowMap(float2 UV) { if (UV.x < 0 || UV.x > 1 || UV.y < 0 || UV.y > 1) { return float2(1, 1); } return tex2D(ShadowMapSampler, UV).rg; } struct Shadow_PSIn { float4 Position : POSITION; float3 TexCoordAndCornerIndex : TEXCOORD0; }; struct Shadow_VSOut { float4 Position : POSITION; float2 TexCoord : TEXCOORD0; float3 FrustumCornerVS : TEXCOORD1; }; // Vertex shader for rendering the full-screen quad used for calculating // the shadow occlusion factor. Shadow_VSOut ShadowTermVS (Shadow_PSIn input) { Shadow_VSOut output; // Offset the position by half a pixel to correctly align texels to pixels output.Position.x = input.Position.x - (1.0f / OcclusionTextureSize.x); output.Position.y = input.Position.y + (1.0f / OcclusionTextureSize.y); output.Position.z = input.Position.z; output.Position.w = 1.0f; // Pass along the texture coordiante and the position of the frustum corner output.TexCoord = input.TexCoordAndCornerIndex.xy; output.FrustumCornerVS = FrustumCornersVS[input.TexCoordAndCornerIndex.z]; return output; } // Pixel shader for computing the shadow occlusion factor float4 ShadowTermPS(Shadow_VSOut input) : COLOR0 { // Reconstruct view-space position from the depth buffer float pixelDepth = tex2D(DepthTextureSampler, input.TexCoord).r; float4 position = float4(pixelDepth * input.FrustumCornerVS, 1.0f); // Determine the depth of the pixel with respect to the light float4x4 inverseLVP = mul(InverseView, LightViewProjection); float4 positionLight = mul(position, inverseLVP); float lightDepth = (positionLight.z / positionLight.w) - ShadowBias; // Transform from light space to shadow map texture space. float2 shadowTexCoord = 0.5 * positionLight.xy / positionLight.w + float2(0.5f, 0.5f); shadowTexCoord.x = shadowTexCoord.x; shadowTexCoord.y = 1.0f - shadowTexCoord.y; // Offset the coordinate by half a texel so we sample it correctly shadowTexCoord += (0.5f / ShadowMapSize); float shadow = 1; float2 shadowMap = sampleShadowMap(shadowTexCoord); lightDepth += 0.01f; if (lightDepth < 1) { // Check if we're in shadow //float lit_factor = (lightDepth < shadowMap.x); //// Variance shadow mapping //float E_x2 = shadowMap.y; //float Ex_2 = shadowMap.x * shadowMap.x; //float variance = min(max(E_x2 - Ex_2, 0.0) + 1.0f / 5000, 1); //float m_d = (shadowMap.x - lightDepth); //float p = variance / (variance + m_d * m_d); //shadow = clamp(max(lit_factor, p), 0.35, 1.0f); //Calculate the Shadow Factor float shadowFactor = exp((DarkenFactor * 0.5f) * (shadowMap - lightDepth)); shadowFactor = clamp(shadowFactor, 0.45, 1.0); shadowFactor = saturate(shadowFactor); shadow = shadowFactor; } return float4(shadow, shadow, shadow, 0); } technique ShadowOcclusion { pass p0 { VertexShader = compile vs_3_0 ShadowTermVS(); PixelShader = compile ps_3_0 ShadowTermPS(); } }  Edited by mgoss

##### Share on other sites
bro, have you updated your repo lately? that looks like your using our old ass version....

• ### 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.)

• 15
• 11
• 9
• 9
• 40
• ### Forum Statistics

• Total Topics
634130
• Total Posts
3015704
×