Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 29 December 2010 - 02:58 PM
Posted 29 December 2010 - 05:21 PM
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;
}
Posted 30 December 2010 - 07:07 AM
POPULAR
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;
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;
Posted 30 December 2010 - 07:54 AM
Posted 30 December 2010 - 10:46 AM
[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);
}
Posted 23 June 2012 - 02:23 PM
Posted 23 June 2012 - 04:35 PM
Edited by MJP, 23 June 2012 - 04:35 PM.
Posted 23 June 2012 - 09:14 PM
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.
Posted 24 June 2012 - 05:43 AM
Posted 24 June 2012 - 07:04 AM
Posted 30 June 2012 - 02:58 PM
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.