66[ ExecuteInEditMode ]
77public class PCSSLight : MonoBehaviour
88{
9- // [Tooltip("Disable when building, as Unity 2017 seems to have issues with the 'ResetShadowMode()' being called during 'OnDisable()' in builds, though it works fine in the editor")]
10- // public bool resetOnDisable = false;
119 public int resolution = 4096 ;
1210 public bool customShadowResolution = false ;
1311
@@ -50,13 +48,23 @@ public class PCSSLight : MonoBehaviour
5048 public RenderTexture shadowRenderTexture ;
5149 public RenderTextureFormat format = RenderTextureFormat . RFloat ;
5250 public FilterMode filterMode = FilterMode . Bilinear ;
53- [ PowRange ( 0 , 8 , 2 , true ) ]
54- public int MSAA = 0 ;
51+ public enum antiAliasing
52+ {
53+ None = 1 ,
54+ Two = 2 ,
55+ Four = 4 ,
56+ Eight = 8 ,
57+ }
58+ public antiAliasing MSAA = antiAliasing . None ;
5559 private LightEvent lightEvent = LightEvent . AfterShadowMap ;
5660
5761 public string shaderName = "Hidden/PCSS" ;
58- private Shader shader ;
62+ public Shader shader ;
63+ public string builtinShaderName = "Hidden/Built-In-ScreenSpaceShadows" ;
64+ public Shader builtinShader ;
5965 private int shadowmapPropID ;
66+ public ShaderVariantCollection shaderVariants ;
67+ private List < string > keywords = new List < string > ( ) ;
6068
6169 private CommandBuffer copyShadowBuffer ;
6270 [ HideInInspector ]
@@ -70,9 +78,7 @@ public void OnEnable ()
7078
7179 public void OnDisable ( )
7280 {
73- //Unity 2017 seems to have issues with the 'ResetShadowMode()' being called during 'OnDisable()' in builds, though it works fine in the editor
74- if ( Application . isEditor )
75- ResetShadowMode ( ) ;
81+ ResetShadowMode ( ) ;
7682 }
7783
7884 [ ContextMenu ( "Reinitialize" ) ]
@@ -89,6 +95,13 @@ public void Setup ()
8995 _light . shadowCustomResolution = 0 ;
9096
9197 shader = Shader . Find ( shaderName ) ;
98+ if ( ! Application . isEditor )
99+ {
100+ if ( shader )
101+ Debug . LogErrorFormat ( "Custom Shadow Shader Found: {0} | Supported {1}" , shader . name , shader . isSupported ) ;
102+ else
103+ Debug . LogError ( "Custom Shadow Shader Not Found!!!" ) ;
104+ }
92105 shadowmapPropID = Shader . PropertyToID ( "_ShadowMap" ) ;
93106
94107 copyShadowBuffer = new CommandBuffer ( ) ;
@@ -117,16 +130,26 @@ public void CreateShadowRenderTexture ()
117130 shadowRenderTexture = new RenderTexture ( resolution , resolution , 0 , format ) ;
118131 shadowRenderTexture . filterMode = filterMode ;
119132 shadowRenderTexture . useMipMap = false ;
120- shadowRenderTexture . antiAliasing = Mathf . Clamp ( MSAA , 1 , 8 ) ;
133+ shadowRenderTexture . antiAliasing = ( int ) MSAA ;
121134 }
122135
123136 [ ContextMenu ( "Reset Shadows To Default" ) ]
124137 public void ResetShadowMode ( )
125138 {
139+ // //Unity 2017 seems to have issues with the 'ResetShadowMode()' being called during 'OnDisable()' in builds, though it works fine in the editor
140+ // if (!Application.isEditor)
141+ // return;
142+
143+ builtinShader = Shader . Find ( builtinShaderName ) ;
126144 if ( ! Application . isEditor )
127- return ;
128-
129- GraphicsSettings . SetCustomShader ( BuiltinShaderType . ScreenSpaceShadows , Shader . Find ( "Hidden/Internal-ScreenSpaceShadows" ) ) ;
145+ {
146+ if ( builtinShader )
147+ Debug . LogErrorFormat ( "Built-In Shadow Shader Found: {0} | Supported {1}" , builtinShader . name , builtinShader . isSupported ) ;
148+ else
149+ Debug . LogError ( "Shadow Shader Not Found!!!" ) ;
150+ }
151+
152+ GraphicsSettings . SetCustomShader ( BuiltinShaderType . ScreenSpaceShadows , builtinShader ) ;
130153 GraphicsSettings . SetShaderMode ( BuiltinShaderType . ScreenSpaceShadows , BuiltinShaderMode . Disabled ) ;
131154 _light . shadowCustomResolution = 0 ;
132155 DestroyImmediate ( shadowRenderTexture ) ;
@@ -142,12 +165,13 @@ public void ResetShadowMode ()
142165 #region UpdateSettings
143166 public void UpdateShaderValues ( )
144167 {
168+ keywords . Clear ( ) ;
145169 Shader . SetGlobalInt ( "Blocker_Samples" , Blocker_SampleCount ) ;
146170 Shader . SetGlobalInt ( "PCF_Samples" , PCF_SampleCount ) ;
147171
148172 if ( shadowRenderTexture )
149173 {
150- if ( shadowRenderTexture . format != format || shadowRenderTexture . antiAliasing != Mathf . Clamp ( MSAA , 1 , 8 ) )
174+ if ( shadowRenderTexture . format != format || shadowRenderTexture . antiAliasing != ( int ) MSAA )
151175 CreateShadowRenderTexture ( ) ;
152176 else
153177 {
@@ -186,6 +210,9 @@ public void UpdateShaderValues ()
186210
187211 SetFlag ( "POISSON_32" , maxSamples < 33 ) ;
188212 SetFlag ( "POISSON_64" , maxSamples > 33 ) ;
213+
214+ if ( shaderVariants )
215+ shaderVariants . Add ( new ShaderVariantCollection . ShaderVariant ( shader , PassType . Normal , keywords . ToArray ( ) ) ) ;
189216 }
190217
191218 public void UpdateCommandBuffer ( )
@@ -202,8 +229,11 @@ public void UpdateCommandBuffer ()
202229
203230 public void SetFlag ( string shaderKeyword , bool value )
204231 {
205- if ( value )
206- Shader . EnableKeyword ( shaderKeyword ) ;
232+ if ( value )
233+ {
234+ Shader . EnableKeyword ( shaderKeyword ) ;
235+ keywords . Add ( shaderKeyword ) ;
236+ }
207237 else
208238 Shader . DisableKeyword ( shaderKeyword ) ;
209239 }
0 commit comments