From 603a19ddf429b4cd446adb59f27e3b139c8ecc4a Mon Sep 17 00:00:00 2001 From: Robert Cupisz Date: Wed, 2 Nov 2016 17:07:52 +0100 Subject: [PATCH] Handle the reversed depth buffer in 5.5 --- Assets/AreaLight/Shaders/AreaLightShadows.cginc | 11 ++++++++++- Assets/VolumetricFog/Shaders/BlurShadowmap.shader | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Assets/AreaLight/Shaders/AreaLightShadows.cginc b/Assets/AreaLight/Shaders/AreaLightShadows.cginc index 1951656..31f5b67 100644 --- a/Assets/AreaLight/Shaders/AreaLightShadows.cginc +++ b/Assets/AreaLight/Shaders/AreaLightShadows.cginc @@ -67,6 +67,14 @@ half EdgeSmooth(half2 xy) return saturate(1 - smoothstep(-smooth, 0, d)); } +half ReverseZ(half z) +{ +#if UNITY_REVERSED_Z + return 1.0 - z; +#endif + return z; +} + half Shadow(half3 position) { half4 pClip = mul(_ShadowProjectionMatrix, half4(position, 1)); @@ -83,7 +91,7 @@ half Shadow(half3 position) for(int j = 0; j < 10; ++j) { half2 offset = poisson[j + 24] * _ShadowReceiverWidth; - float depth = _Shadowmap.SampleLevel(sampler_ShadowmapDummy, p.xy + offset, 0).r; + float depth = ReverseZ(_Shadowmap.SampleLevel(sampler_ShadowmapDummy, p.xy + offset, 0).r); dist += max(0.0, p.z - depth); } @@ -91,6 +99,7 @@ half Shadow(half3 position) dist *= _ShadowReceiverDistanceScale; p.z -= _ShadowBias/pClip.w; + p.z = ReverseZ(p.z); half shadow = 0; for(int i = 0; i < 32; ++i) { diff --git a/Assets/VolumetricFog/Shaders/BlurShadowmap.shader b/Assets/VolumetricFog/Shaders/BlurShadowmap.shader index 86d989c..79afb6b 100644 --- a/Assets/VolumetricFog/Shaders/BlurShadowmap.shader +++ b/Assets/VolumetricFog/Shaders/BlurShadowmap.shader @@ -69,6 +69,10 @@ Shader "Hidden/BlurShadowmap" { // The texture contains just 0. But we need to sample it somewhere for Unity to initialize the corresponding sampler. z.r += _ShadowmapDummy.Sample(sampler_ShadowmapDummy, 0).a; + #if UNITY_REVERSED_Z + z = 1.0 - z; + #endif + // Transform to linear z, 0 at near, 1 at far z = z * 2 - 1; z = _ZParams.x * (z + 1.0) / (z + _ZParams.y);