diff --git a/Effects/FXB/DeferredPBREffect.fxb b/Effects/FXB/DeferredPBREffect.fxb index 17baa97..a9961cf 100644 Binary files a/Effects/FXB/DeferredPBREffect.fxb and b/Effects/FXB/DeferredPBREffect.fxb differ diff --git a/Effects/HLSL/DeferredPBREffect.fx b/Effects/HLSL/DeferredPBREffect.fx index d6e2d9c..65c1eb9 100644 --- a/Effects/HLSL/DeferredPBREffect.fx +++ b/Effects/HLSL/DeferredPBREffect.fx @@ -75,21 +75,27 @@ float GeometrySmith(float3 N, float3 V, float3 L, float roughness) float ComputeShadow(float4 positionLightSpace) { - float bias = 0.01; + float bias = 0.001; //0.005; // maps to [-1, 1] float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w; // maps to [0, 1] - projectionCoords = (projectionCoords * 0.5) + 0.5; + projectionCoords.x = (projectionCoords.x * 0.5) + 0.5; + projectionCoords.y = (projectionCoords.y * 0.5) + 0.5; projectionCoords.y *= -1; float closestDepth = SAMPLE_TEXTURE(shadowMap, projectionCoords.xy).r; float currentDepth = projectionCoords.z; - float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0; - - return shadow; + if (currentDepth - bias > closestDepth) + { + return 1.0; + } + else + { + return 0.0; + } } float3 ComputeLight( diff --git a/Effects/SimpleDepthEffect.cs b/Effects/SimpleDepthEffect.cs index 9443540..1854e3c 100644 --- a/Effects/SimpleDepthEffect.cs +++ b/Effects/SimpleDepthEffect.cs @@ -52,8 +52,8 @@ namespace Kav { if ((dirtyFlags & EffectDirtyFlags.WorldViewProj) != 0) { - Matrix.Multiply(ref model, ref view, out Matrix modelView); - Matrix.Multiply(ref modelView, ref projection, out Matrix worldViewProj); + Matrix.Multiply(ref view, ref projection, out Matrix viewProjection); + Matrix.Multiply(ref model, ref viewProjection, out Matrix worldViewProj); modelViewProjectionParam.SetValue(worldViewProj); diff --git a/Renderer.cs b/Renderer.cs index 99c26d3..058a383 100644 --- a/Renderer.cs +++ b/Renderer.cs @@ -181,32 +181,43 @@ namespace Kav var right = Vector3.Cross(Vector3.Up, directionalLight.Direction); var up = Vector3.Cross(directionalLight.Direction, right); - - var lightRotation = Matrix.CreateLookAt(Vector3.Zero, -directionalLight.Direction, up); - var cameraBoundingFrustum = new BoundingFrustum(camera.View * camera.Projection); - Vector3[] frustumCorners = cameraBoundingFrustum.GetCorners(); + + Vector3 frustumCenter = Vector3.Zero; for (var i = 0; i < frustumCorners.Length; i++) { - frustumCorners[i] = Vector3.Transform(frustumCorners[i], lightRotation); + frustumCenter += frustumCorners[i]; + } + frustumCenter /= 8f; + + var lightView = Matrix.CreateLookAt(frustumCenter, frustumCenter - directionalLight.Direction, camera.View.Right); + + for (var i = 0; i < frustumCorners.Length; i++) + { + frustumCorners[i] = Vector3.Transform(frustumCorners[i], lightView); } BoundingBox lightBox = BoundingBox.CreateFromPoints(frustumCorners); Vector3 boxSize = lightBox.Max - lightBox.Min; Vector3 halfBoxSize = boxSize * 0.5f; - Vector3 lightPosition = lightBox.Min + halfBoxSize; - lightPosition.Z = lightBox.Min.Z; - lightPosition = Vector3.Transform(lightPosition, Matrix.Invert(lightRotation)); + Vector3 lightPosition = frustumCenter + directionalLight.Direction * -lightBox.Min.Z; + //lightPosition.Z = lightBox.Min.Z; + //lightPosition = Vector3.Transform(lightPosition, Matrix.Invert(lightRotation)); + + SimpleDepthEffect.View = Matrix.CreateLookAt(lightPosition, frustumCenter, camera.View.Right); + SimpleDepthEffect.Projection = Matrix.CreateOrthographicOffCenter(lightBox.Min.X, lightBox.Max.X, lightBox.Min.Y, lightBox.Max.Y, 0, lightBox.Max.Z - lightBox.Min.Z); - //SimpleDepthEffect.View = Matrix.CreateLookAt(lightPosition, lightPosition - directionalLight.Direction, up); - //SimpleDepthEffect.Projection = Matrix.CreateOrthographic(boxSize.X, boxSize.Y, -boxSize.Z, boxSize.Z); - - SimpleDepthEffect.View = directionalLight.View; - SimpleDepthEffect.Projection = directionalLight.Projection; + //SimpleDepthEffect.View = directionalLight.View; + //SimpleDepthEffect.Projection = directionalLight.Projection; DeferredPBREffect.LightSpaceMatrix = SimpleDepthEffect.View * SimpleDepthEffect.Projection; + var globalShadowView = Matrix.CreateLookAt(frustumCenter + directionalLight.Direction * -0.5f, frustumCenter, camera.View.Right); + var globalShadowProjection = Matrix.CreateOrthographic(1f, 1f, 0f, 1f); + //var texScaleBias = Matrix.CreateScale(0.5f, -0.5f, 1f) * Matrix.CreateTranslation(0.5f, 0.5f, 0f); + //DeferredPBREffect.LightSpaceMatrix = globalShadowView * globalShadowProjection; // * texScaleBias; + foreach (var (model, transform) in modelTransforms) { foreach (var modelMesh in model.Meshes)