**0**

# XNA 4.0 - shimmering shadow maps

###
#1
Members - Reputation: **102**

Posted 29 December 2010 - 02:58 PM

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

###
#3
Members - Reputation: **102**

Posted 29 December 2010 - 05:21 PM

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[i];

if (cornersWS[i].Y < 0)

cornersWS[i].Y = 0.0f;

if (low > cornersWS[i].Y)

low = cornersWS[i].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;

}

###
#4
Moderators - Reputation: **17038**

Posted 30 December 2010 - 07:07 AM

Anyway the simplest and easiest way to avoid shimmering is to create a "stable" projection, using the method described in the article "Stable Cascaded Shadow Maps" from ShaderX6. Like the name suggests it's meant for cascaded shadow maps, but the same idea can be applied to a single orthographic projection.

The first step is to make it so that the width and height of your projection don't change every frame due to camera rotations...in other words we want our method for determining the projection to be rotation-invariant. With the light-space bounding box method we get a tighter fit on the view frustum, but as the frustum rotates the projection will grow and shrink causing shimmering. The easiest way to fix that is to use a bounding sphere instead of a bounding box. One really nice thing about using a sphere is that since it's rotationally invariant, we can pretty much do all of our calculations in world space. To make a bounding sphere, you can use the BoundingSphere class. You can either use CreateFromPoints give it the array of frustum corners in world space, or you can just skip that and go right for CreateFromFrustum.

Once you have a bounding sphere, the center point of the sphere is your "look at" point for the shadow caster camera. To figure out the position of the shadow caster camera, you use the same procedure as before and back up from the look at point in the opposite direction of the light. At a minimum you'll want to back up an amount equal to the bounding sphere radius (plus a small offset for the near clip plane of the orthographic projection), but in most cases you'll probably need to back up further to make sure you don't miss any potential shadow casters that are outside the field of view. You can just add a constant amount if you want, or if you want to get fancier you can figure out how far you need to pull back based on the bounding spheres of objects in your scene. Then for the width and height of your projection, you just use the radius of the bounding sphere. It should look something like this:

BoundingSphere sphere = BoundingSphere.CreateFromFrustum(pCamera.ViewFrustum);

const float ExtraBackup = 20.0f;

const float NearClip = 1.0f;

float backupDist = ExtraBackup + NearClip + sphere.Radius;

Vector3 shadowCamPos = sphere.Center + direction * backupDist;

Matrix shadowViewMatrix = Matrix.CreateLookAt(shadowCamPos, sphere.Center, Vector3.Up);

float bounds = sphere.Radius * 2.0f;

flat farClip = backup + sphere.Radius;

Matrix shadowProjMatrix = Matrix.CreateOrthographic(bounds, bounds, NearClip, farClip);

Matrix shadowMatrix = shadowViewMatrix * shadowProjMatrix;

So that should give you a projection based off a bounding sphere, where with width and height don't change when the camera rotates. I would try that out first, make sure you've got it working. Once it works okay, we have another issue to deal with. When the camera translates, the shadow caster camera will translate with it. This causes shimmering as the geometry in the shadow map moves at sub-texel increments. To fix this, we need to round the shadow caster camera's movement so that it moves in whole-texel increments. Basically this is done by projecting an arbitrary point into shadowmap-space (I'll use the world-space origin), then adding a small translation so that the arbitrary point ends up at an integer location. So it'll be like "Project (0,0,0), it ends up at shadowmap texels (64.7, 22.9), so we add (0.3, 0.1) to round it up to (65, 23)"

float ShadowMapSize = 1024.0f; // Set this to the size of your shadow map

Vector3 shadowOrigin = Vector3.Transform(Vector3.Zero, shadowMatrix);

shadowOrigin *= (ShadowMapSize / 2.0f);

Vector2 roundedOrigin = Vector2(Math.Round(shadowOrigin .x), Math.Round(shadowOrigin .y));

Vector2 rounding = roundedOrigin - shadowOrigin;

rounding /= (ShadowMapSize / 2.0f);

Matrix roundMatrix = Matrix.CreateTranslation(rounding.x, rounding.y, 0.0f);

shadowMatrix *= roundMatrix;

If you use that shadow matrix for rendering geometry to the shadow map and for doing the shadow map lookup, you shouldn't have any jittering. If you still have some, you can try rounding the radius of the bounding sphere to keep it from having small fluctuations (or you can just calculate it once and use the same value every frame). Also I used Math.Round up in that code which takes and returns a double, so you'll have add in the necessary casting.

###
#5
Members - Reputation: **102**

Posted 30 December 2010 - 07:54 AM

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.

###
#6
Members - Reputation: **102**

Posted 30 December 2010 - 10:46 AM

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);

}

###
#7
Members - Reputation: **225**

Posted 23 June 2012 - 02:23 PM

Vector3 shadowCamPos = sphere.Center + direction * backupDist;

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

###
#8
Moderators - Reputation: **17038**

Posted 23 June 2012 - 04:35 PM

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, 23 June 2012 - 04:35 PM.**

###
#9
Members - Reputation: **225**

Posted 23 June 2012 - 09:14 PM

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[i] = 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, 23 June 2012 - 09:20 PM.**

###
#10
Moderators - Reputation: **7804**

Posted 24 June 2012 - 05:43 AM

Primary Website:

RuinValor - Birth of Talos

Social Links:

IndieDB - RuinValor

Greenlight - RuinValor

Facebook - RuinValor

Twitter - Riuthamus

Youtube - RuinValor

###
#12
Moderators - Reputation: **7804**

Posted 24 June 2012 - 07:04 AM

Primary Website:

RuinValor - Birth of Talos

Social Links:

IndieDB - RuinValor

Greenlight - RuinValor

Facebook - RuinValor

Twitter - Riuthamus

Youtube - RuinValor

###
#13
Members - Reputation: **118**

Posted 30 June 2012 - 02:58 PM

If you are interested here's a playlist that details my shadows development:

http://www.youtube.com/playlist?list=PL2E3759AF24FE2E9E&feature=plcp

Or have a look at this video: