Skip to content

Commit ed6c6b8

Browse files
committed
Added an exponential falloff to the PCF filtering radius as an attempt to make the shadows better transition from hard to soft. Still not entirely sold on it, but I think it's better.
If you want to remove it, it's the line after 'penumbra' is declared in the PCSS_Main function of the PCSS shader
1 parent cb0a7d7 commit ed6c6b8

File tree

8 files changed

+34
-17
lines changed

8 files changed

+34
-17
lines changed
48 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
36 Bytes
Binary file not shown.

Assets/PCSS/Demo/Demo Assets/Standard Assets/Characters/FirstPersonCharacter/Scripts/FirstPersonController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ private void GetInput(out float speed)
236236

237237
private void RotateView()
238238
{
239-
m_MouseLook.LookRotation (transform, m_Camera.transform);
239+
if(!UnityEngine.VR.VRSettings.enabled)
240+
m_MouseLook.LookRotation (transform, m_Camera.transform);
240241
}
241242

242243

Assets/PCSS/Demo/PCSS Demo.unity

-1.63 KB
Binary file not shown.

Assets/PCSS/Scripts/PCSSLight.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public class PCSSLight : MonoBehaviour
2323
[Space(20f)]
2424
[Range(0f, 7.5f)]
2525
public float Softness = 1f;
26+
[Range(0f, 5f)]
27+
public float SoftnessFalloff = 4f;
28+
//[Range(0f, 1f)]
29+
//public float NearPlane = .1f;
2630

2731
[Space(20f)]
2832
[Range(0f, 0.15f)]
@@ -53,7 +57,7 @@ public class PCSSLight : MonoBehaviour
5357
private CommandBuffer copyShadowBuffer;
5458
private Light _light;
5559

56-
60+
#region Initialization
5761
public void OnEnable ()
5862
{
5963
Setup();
@@ -101,6 +105,13 @@ public void Setup ()
101105
UpdateCommandBuffer();
102106
}
103107

108+
public void CreateShadowRenderTexture ()
109+
{
110+
shadowRenderTexture = new RenderTexture(resolution, resolution, 0, format);
111+
shadowRenderTexture.filterMode = filterMode;
112+
shadowRenderTexture.useMipMap = false;
113+
}
114+
104115
[ContextMenu("Reset Shadows To Default")]
105116
public void ResetShadowMode ()
106117
{
@@ -115,7 +126,9 @@ public void ResetShadowMode ()
115126

116127
_light.RemoveCommandBuffer(LightEvent.AfterShadowMap, copyShadowBuffer);
117128
}
129+
#endregion
118130

131+
#region UpdateSettings
119132
public void UpdateShaderValues ()
120133
{
121134
Shader.SetGlobalInt("Blocker_Samples", Blocker_SampleCount);
@@ -126,7 +139,9 @@ public void UpdateShaderValues ()
126139
else
127140
shadowRenderTexture.filterMode = filterMode;
128141

129-
Shader.SetGlobalFloat("Softness", Softness * Softness / 32f / Mathf.Sqrt(QualitySettings.shadowDistance));
142+
Shader.SetGlobalFloat("Softness", Softness / 64f / Mathf.Sqrt(QualitySettings.shadowDistance));
143+
Shader.SetGlobalFloat("SoftnessFalloff", Mathf.Exp(SoftnessFalloff));
144+
//Shader.SetGlobalFloat("NearPlane", NearPlane);
130145

131146
Shader.SetGlobalFloat("RECEIVER_PLANE_MIN_FRACTIONAL_ERROR", MaxStaticGradientBias);
132147
Shader.SetGlobalFloat("Blocker_GradientBias", Blocker_GradientBias);
@@ -153,6 +168,7 @@ public void UpdateShaderValues ()
153168
int maxSamples = Mathf.Max(Blocker_SampleCount, PCF_SampleCount);
154169

155170
SetFlag("POISSON_32", maxSamples < 33);
171+
SetFlag("POISSON_64", maxSamples > 33);
156172
}
157173

158174
public void UpdateCommandBuffer ()
@@ -165,13 +181,7 @@ public void UpdateCommandBuffer ()
165181
copyShadowBuffer.Blit(BuiltinRenderTextureType.CurrentActive, shadowRenderTexture);
166182
copyShadowBuffer.SetGlobalTexture(shadowmapPropID, shadowRenderTexture);
167183
}
168-
169-
public void CreateShadowRenderTexture ()
170-
{
171-
shadowRenderTexture = new RenderTexture(resolution, resolution, 0, format);
172-
shadowRenderTexture.filterMode = filterMode;
173-
shadowRenderTexture.useMipMap = false;
174-
}
184+
#endregion
175185

176186
public void SetFlag (string shaderKeyword, bool value)
177187
{

Assets/PCSS/Shaders/PCSS.shader

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ uniform float Blocker_Rotation = .5;
251251
uniform float PCF_Rotation = .5;
252252

253253
uniform float Softness = 1.0;
254+
uniform float SoftnessFalloff = 1.0;
255+
//uniform float NearPlane = .1;
256+
254257
uniform float Blocker_GradientBias = 0.0;
255258
uniform float PCF_GradientBias = 1.0;
256259
uniform float CascadeBlendDistance = .5;
@@ -502,11 +505,15 @@ float PCF_Filter(float2 uv, float depth, float filterRadiusUV, float2 receiverPl
502505
=========================================================================================================================================
503506
*/
504507

505-
506508
float PCSS_Main(float4 coords, float2 receiverPlaneDepthBias, float random)
507509
{
508510
float2 uv = coords.xy;
509511
float depth = coords.z;
512+
float zAwareDepth = depth;
513+
514+
#if defined(UNITY_REVERSED_Z)
515+
zAwareDepth = 1.0 - depth;
516+
#endif
510517

511518
//float rotationAngle = random * 6.283185307179586476925286766559;
512519
float rotationAngle = random * 3.1415926;
@@ -518,6 +525,7 @@ float PCSS_Main(float4 coords, float2 receiverPlaneDepthBias, float random)
518525

519526
// STEP 1: blocker search
520527
//float searchSize = Softness * (depth - _LightShadowData.w) / depth;
528+
//float searchSize = Softness * saturate(zAwareDepth - NearPlane) / zAwareDepth;
521529
float2 blockerInfo = FindBlocker(uv, depth, Softness, receiverPlaneDepthBias, rotationTrig);
522530

523531
if (blockerInfo.y < 1)
@@ -527,11 +535,9 @@ float PCSS_Main(float4 coords, float2 receiverPlaneDepthBias, float random)
527535
}
528536

529537
// STEP 2: penumbra size
530-
#if defined(UNITY_REVERSED_Z)
531-
float penumbra = (1.0 - depth) - blockerInfo.x;
532-
#else
533-
float penumbra = depth - blockerInfo.x;
534-
#endif
538+
//float penumbra = zAwareDepth * zAwareDepth - blockerInfo.x * blockerInfo.x;
539+
float penumbra = zAwareDepth - blockerInfo.x;
540+
penumbra = 1.0 - pow(1.0 - penumbra, SoftnessFalloff);
535541

536542
float filterRadiusUV = penumbra * Softness;
537543
//filterRadiusUV *= filterRadiusUV;
@@ -584,7 +590,7 @@ fixed4 frag_pcss (v2f i) : SV_Target
584590
#if defined(USE_NOISE_TEX)
585591
float random = tex2D(_NoiseTexture, i.uv.xy * NoiseCoords.xy * _ScreenParams.xy).a;
586592
random = mad(random, 2.0, -1.0);
587-
random = sign(random) * (1.0 - sqrt(1.0 - abs(random)));
593+
//random = sign(random) * (1.0 - sqrt(1.0 - abs(random)));
588594
#else
589595
float random = ValueNoise(wpos.xyz);
590596
#endif

0 commit comments

Comments
 (0)