From 345ffaa247854a29670e264240500fa11322fe17 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 16 Sep 2020 16:49:08 -0700 Subject: [PATCH] fix fragment depth being translated to clip space --- Effects/FXB/DeferredPBREffect.fxb | Bin 52140 -> 52160 bytes Effects/HLSL/DeferredPBREffect.fx | 16 +++++++++---- Effects/SimpleDepthEffect.cs | 4 ++-- Renderer.cs | 37 +++++++++++++++++++----------- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/Effects/FXB/DeferredPBREffect.fxb b/Effects/FXB/DeferredPBREffect.fxb index 17baa97a597c24b1e7d8cb6a8fef55e8eb125d54..a9961cf37ec65f66aecf53a97bebf2335aa4df03 100644 GIT binary patch delta 2588 zcma*pQAkr!7zgn0o_p1;`yjZ7i4uz-7%WZ5VzQVOBNc;C7!FMcK^P2C31TsjI}E*Q zA$5XD5)L66TZC~z)S;+}bNbM$;e#(eEPUumP<*(aGkpKP3kSaQzu);i|9_;^&6K)y zs4Gp@U)@vHJX|xr)&)>81=Z>Ci^bsV_SvI*Yo#EFPv+Ai7o)fvt>Qek?YAN8t}CP| zCLeXX)M>E!q}!u__1*n}xlORs5dcjA#n8Yu6!Au`{MnT&vAU1Q3Xj!2u%b6#B5T&A zha{cfMx~iMB+2xmQoWBP)i$D1u8AaN?xRxRAxZMLpwdBrB<%%JsqYC%iiA+<^chJi zb)Zt>IZ2AYKqbAKBss#UG~Yv#=6X@d*H4nXuTUvJK$2DlQK>UZlG%?@=i*OOpJbQ0d?^N!m-HQr{Pn z6#0rurwb&hw1`THZzL(6K_xv$k{mx#X?}?$%~`1ATP8`~RaDC7Nz%#&Ds^s=r1qT= zq^t1%yA9l5xaE|c=mwk^6TGY78ma_fGLa3b0zhS;mS-k5wCe`NAY0C4tCE@Ak^fD| zrsWZD83RpZ;p9_cIH6k1B|~$Zq?x#1`$?zutDrN{*w%}la$`F#iaN&aVw)1?ZTkr& J%YX0xy8^)KDv$sG delta 2604 zcma*p-%C?r7zgm@eRs@l2Nc@H5q~f!guyI>eqc;y%BY2b7z|fSkRTY0$Pi>Ph%>C* zW@2@MNfI4|aBL#NflvohQT~`0(N#uQ!7dgCT?ziUIKAim1J85u!t=cE_x)UdKjs~a zdB^gu%vz0~vbX+1`1et85TGs#jk;+*3QZi&2WLL-cXzJU?E9R!2Y?`e+9S@D2PY@i zj1EsoVma-h;&EgFv`NLwBF2I;&6OOOdK5rnz*GrLm5D!AyN^?UbY4$cM`G-l8Zv>UfcS+L9JyhzClcaD0m44kP zNxP${lo%sP@o`i-PLia96e>+UCP|qzDz!WzNllZeRLYX1!gEv#%#ft6SE#h}nj~%I zQ7QI@Bt_q%(%w6gRGUYo)O(VYETEEHB1z6ARGM2RNwWqj`9F{(-zqAV%Oq)K9hLe& zk)-gKQKYMA|2EDjRv&UhRec6k{R9BFcF~PAWwZSj2g!bWD&Fb<931NH-o!;-5?#tA zc5BW+QP04UF8*;BI8_%!(tRTDhDI^}RV8Ot=VG^P7ZlTO 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)