From f153209bbc7cc74a521f523e86001ede5451ece9 Mon Sep 17 00:00:00 2001 From: max Date: Sun, 3 Apr 2022 17:47:44 +0200 Subject: [PATCH] Dir light test --- .../Scripts/FogLight.DirectionalShadow.cs | 17 ++-- Assets/VolumetricFog/Scripts/VolumetricFog.cs | 11 ++- .../Shaders/ApplyToOpaque.shader | 86 ++++++++++--------- .../Shaders/BlurShadowmap.shader | 17 ++-- .../Shaders/InjectLightingAndDensity.compute | 16 +++- .../VolumetricFog/Shaders/VolumetricFog.cginc | 2 +- 6 files changed, 91 insertions(+), 58 deletions(-) diff --git a/Assets/VolumetricFog/Scripts/FogLight.DirectionalShadow.cs b/Assets/VolumetricFog/Scripts/FogLight.DirectionalShadow.cs index 05fbb98..e476b7d 100644 --- a/Assets/VolumetricFog/Scripts/FogLight.DirectionalShadow.cs +++ b/Assets/VolumetricFog/Scripts/FogLight.DirectionalShadow.cs @@ -79,7 +79,7 @@ public partial class FogLight : LightOverride if (temp == null || temp.Length != downsampleSteps - 1) temp = new int[downsampleSteps - 1]; - for (int i = 0, currentRes = startRes/2; i < downsampleSteps; i++) + for (int i = 0, currentRes = startRes / 2; i < downsampleSteps; i++) { m_BufGrabShadowmap.SetGlobalVector("_TexelSize", new Vector4(0.5f/currentRes, 0.5f/currentRes, 0, 0)); @@ -113,10 +113,17 @@ public partial class FogLight : LightOverride currentRes /= 2; } - //var directionalShadowmapBlurred = Shader.PropertyToID("_DirectionalShadowmapBlurred"); - //m_BufGrabShadowmap.GetTemporaryRT(directionalShadowmapBlurred, 1024, 1024, 0, FilterMode.Bilinear, RenderTextureFormat.RFloat, RenderTextureReadWrite.Linear); - //m_BufGrabShadowmap.Blit(shadowmap, m_Shadowmap); - //m_BufGrabShadowmap.SetGlobalTexture(directionalShadowmapBlurred, directionalShadowmapBlurred); + RenderTexture blur = RenderTexture.GetTemporary(targetRes, targetRes, 0, format, RenderTextureReadWrite.Linear); + m_BufGrabShadowmap.SetGlobalVector("_TexelSize", new Vector4(1.0f / targetRes, 1.0f / targetRes, 0, 0)); + m_BufGrabShadowmap.SetGlobalFloat("_BlurSize", m_BlurSize); + + for (int i = 0; i < m_BlurIterations; i++) + { + m_BufGrabShadowmap.Blit(m_Shadowmap, blur, m_BlurShadowmapMaterial, 2); + m_BufGrabShadowmap.Blit(blur, m_Shadowmap, m_BlurShadowmapMaterial, 3); + } + + blur.Release(); } private Vector4 GetZParams() diff --git a/Assets/VolumetricFog/Scripts/VolumetricFog.cs b/Assets/VolumetricFog/Scripts/VolumetricFog.cs index b03eccc..0a95d15 100644 --- a/Assets/VolumetricFog/Scripts/VolumetricFog.cs +++ b/Assets/VolumetricFog/Scripts/VolumetricFog.cs @@ -74,6 +74,13 @@ public class VolumetricFog : MonoBehaviour [Range(0.0f, 1.0f)] public float m_Z = 1.0f; + [SerializeField] + private float m_dirLightOffset; + [SerializeField] + private float m_vsmBias; + [SerializeField] + private float m_dirBias; + struct PointLightParams { public Vector3 pos; @@ -292,7 +299,9 @@ public class VolumetricFog : MonoBehaviour m_dirLightDir[1] = dir.y; m_dirLightDir[2] = dir.z; m_InjectLightingAndDensity.SetFloats("_DirLightDir", m_dirLightDir); - + m_InjectLightingAndDensity.SetFloat("_DirLightOffset", m_dirLightOffset); + m_InjectLightingAndDensity.SetFloat("_VSMBias", m_vsmBias); + m_InjectLightingAndDensity.SetFloat("_DirBias", m_dirBias); } float[] m_fogParams; diff --git a/Assets/VolumetricFog/Shaders/ApplyToOpaque.shader b/Assets/VolumetricFog/Shaders/ApplyToOpaque.shader index 212fc62..642395e 100644 --- a/Assets/VolumetricFog/Shaders/ApplyToOpaque.shader +++ b/Assets/VolumetricFog/Shaders/ApplyToOpaque.shader @@ -1,48 +1,50 @@ Shader "Hidden/ApplyToOpaque" { -SubShader { -Pass { - ZTest Always Cull Off ZWrite Off - Blend Off + SubShader { + Pass { + ZTest Always Cull Off ZWrite Off + Blend Off -CGPROGRAM - #pragma target 3.0 - #include "UnityCG.cginc" - #include "VolumetricFog.cginc" - #pragma vertex vert - #pragma fragment frag + CGPROGRAM + #pragma target 3.0 + #include "UnityCG.cginc" + #include "VolumetricFog.cginc" + #pragma vertex vert + #pragma fragment frag - sampler2D _CameraDepthTexture; - sampler2D _MainTex; + sampler2D _CameraDepthTexture; + sampler2D _MainTex; - struct v2f - { - float4 pos : SV_POSITION; - float2 uv : TEXCOORD0; - }; + struct v2f + { + float4 pos : SV_POSITION; + float2 uv : TEXCOORD0; + }; - v2f vert (appdata_img v) - { - v2f o; - o.pos = v.vertex; - o.pos.xy = o.pos.xy * 2 - 1; - o.uv = v.texcoord.xy; - - #if UNITY_UV_STARTS_AT_TOP - if (_ProjectionParams.x < 0) - o.uv.y = 1-o.uv.y; - #endif - - return o; - } - - half4 frag (v2f i) : SV_Target - { - half linear01Depth = Linear01Depth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv)); - half4 fog = Fog(linear01Depth, i.uv); - return tex2D(_MainTex, i.uv) * fog.a + fog; - } + v2f vert(appdata_img v) + { + v2f o; + o.pos = v.vertex; + o.pos.xy = o.pos.xy * 2 - 1; + o.uv = v.texcoord.xy; -ENDCG -} -} -} + #if UNITY_UV_STARTS_AT_TOP + if (_ProjectionParams.x < 0) + o.uv.y = 1 - o.uv.y; + #endif + + return o; + } + + half4 frag(v2f i) : SV_Target + { + //return half4(i.uv, 0, 1); + + half linear01Depth = Linear01Depth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv)); + half4 fog = Fog(linear01Depth, i.uv); + return tex2D(_MainTex, i.uv) * fog.a + fog; + } + + ENDCG + } + } +} \ No newline at end of file diff --git a/Assets/VolumetricFog/Shaders/BlurShadowmap.shader b/Assets/VolumetricFog/Shaders/BlurShadowmap.shader index a55b658..a4e374b 100644 --- a/Assets/VolumetricFog/Shaders/BlurShadowmap.shader +++ b/Assets/VolumetricFog/Shaders/BlurShadowmap.shader @@ -130,7 +130,7 @@ Shader "Hidden/BlurShadowmap" { struct v2f { float4 pos : SV_POSITION; - half4 uv : TEXCOORD0; + half2 uv : TEXCOORD0; half2 offs : TEXCOORD1; }; @@ -139,9 +139,11 @@ Shader "Hidden/BlurShadowmap" { v2f vertBlurHorizontal (appdata_img v) { v2f o; - o.pos = v.vertex; + + o.pos = UnityObjectToClipPos(v.vertex); + //o.pos = v.vertex; - o.uv = half4(v.texcoord.xy,1,1); + o.uv = v.texcoord; o.offs = _TexelSize.xy * half2(1.0, 0.0) * _BlurSize; return o; @@ -150,16 +152,19 @@ Shader "Hidden/BlurShadowmap" { v2f vertBlurVertical (appdata_img v) { v2f o; - o.pos = v.vertex; + + o.pos = UnityObjectToClipPos(v.vertex); + //o.pos = v.vertex; - o.uv = half4(v.texcoord.xy, 1, 1); + o.uv = v.texcoord; o.offs = _TexelSize.xy * half2(0.0, 1.0) * _BlurSize; return o; } - float4 fragBlur8 (v2f i) : SV_Target + float4 fragBlur8(v2f i) : SV_Target { + //half2 coords = i.uv.xy; half2 coords = i.uv.xy - i.offs * 5.0; float4 color = 0; diff --git a/Assets/VolumetricFog/Shaders/InjectLightingAndDensity.compute b/Assets/VolumetricFog/Shaders/InjectLightingAndDensity.compute index cece694..ce286af 100644 --- a/Assets/VolumetricFog/Shaders/InjectLightingAndDensity.compute +++ b/Assets/VolumetricFog/Shaders/InjectLightingAndDensity.compute @@ -33,6 +33,10 @@ float3 _DirLightDir; #ifdef DIR_LIGHT_SHADOWS float _DirLightShadows; float _ESMExponentDirLight; +float _DirLightOffset; +float _VSMBias; +float _DirBias; + struct ShadowParams { float4x4 worldToShadow[4]; @@ -258,7 +262,6 @@ float ChebyshevUpperBound(float2 moments, float mean) { // Compute variance float variance = moments.y - (moments.x * moments.x); - float _VSMBias = 0.001f; variance = max(variance, _VSMBias * mean * mean); // Compute probabilistic upper bound @@ -298,12 +301,19 @@ float3 DirectionalLight(float3 pos) if (_DirLightShadows > 0.0) { float4 cascadeWeights = getCascadeWeights_splitSpheres(pos); - float4 samplePos = getShadowCoord(pos, cascadeWeights).xyzw; + + float3 spos = pos + (_DirLightDir * _DirLightOffset); + + float4 samplePos = getShadowCoord(spos, cascadeWeights).xyzw; //--- //att *= _DirectionalShadowmap.SampleLevel(sampler_DirectionalShadowmap, samplePos.xy, 0).r < samplePos.z; //--- float2 shadowmap = _DirectionalShadowmap.SampleLevel(sampler_DirectionalShadowmap, samplePos.xy, 0).xy; - att *= ChebyshevUpperBound(shadowmap.xy, samplePos.z / samplePos.w); + float shadow = ChebyshevUpperBound(shadowmap.xy, samplePos.z / samplePos.w); + + shadow = saturate(lerp(shadow, 1.0, _DirBias)); + + att = shadow; //--- //float depth = exp(-40.0 * samplePos.z); //att = saturate(shadowmap.r * depth); diff --git a/Assets/VolumetricFog/Shaders/VolumetricFog.cginc b/Assets/VolumetricFog/Shaders/VolumetricFog.cginc index 49c47c0..ff62547 100644 --- a/Assets/VolumetricFog/Shaders/VolumetricFog.cginc +++ b/Assets/VolumetricFog/Shaders/VolumetricFog.cginc @@ -29,6 +29,6 @@ half4 Fog(half linear01Depth, half2 screenuv) return half4(0, 0, 0, 1); half3 uvw = half3(screenuv.x, screenuv.y, z); - uvw.xy += cellNoise(uvw.xy * _Screen_TexelSize.zw) * _VolumeScatter_TexelSize.xy * 0.8; + //uvw.xy += cellNoise(uvw.xy * _Screen_TexelSize.zw) * _VolumeScatter_TexelSize.xy * 0.8; return tex3D(_VolumeScatter, uvw); }