• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
NewtonsBit

XNA 4.0 - shimmering shadow maps

12 posts in this topic

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?
1

Share this post


Link to post
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.
0

Share this post


Link to post
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[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;
}




0

Share this post


Link to post
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.
0

Share this post


Link to post
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);
}

0

Share this post


Link to post
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.
0

Share this post


Link to post
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. [url="http://www.youtube.com/watch?v=Lj5K996QYCs&feature=youtu.be"]http://www.youtube.com/watch?v=Lj5K996QYCs&feature=youtu.be[/url]
[CODE]
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();
}
}
[/CODE]

[CODE]
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();
}
}

[/CODE] Edited by mgoss
0

Share this post


Link to post
Share on other sites
Well excuse me for skimming... im an artist! no a coder! :P poo head! back on topic, lets find an answer so we can actually move on from shadows!
0

Share this post


Link to post
Share on other sites
I would very much like to thank you for the explanation and code. I had shadows that would shimmer whenever I rotated or moved. Now they are rock solid. The difference is very satisfying :)

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

[url="http://www.youtube.com/playlist?list=PL2E3759AF24FE2E9E&feature=plcp"]http://www.youtube.com/playlist?list=PL2E3759AF24FE2E9E&feature=plcp[/url]

Or have a look at this video:

[url="http://youtu.be/zS90RwFxKlQ"]http://youtu.be/zS90RwFxKlQ[/url]
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0