6
6
[ ExecuteInEditMode ]
7
7
public class PCSSLight : MonoBehaviour
8
8
{
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;
11
9
public int resolution = 4096 ;
12
10
public bool customShadowResolution = false ;
13
11
@@ -50,13 +48,23 @@ public class PCSSLight : MonoBehaviour
50
48
public RenderTexture shadowRenderTexture ;
51
49
public RenderTextureFormat format = RenderTextureFormat . RFloat ;
52
50
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 ;
55
59
private LightEvent lightEvent = LightEvent . AfterShadowMap ;
56
60
57
61
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 ;
59
65
private int shadowmapPropID ;
66
+ public ShaderVariantCollection shaderVariants ;
67
+ private List < string > keywords = new List < string > ( ) ;
60
68
61
69
private CommandBuffer copyShadowBuffer ;
62
70
[ HideInInspector ]
@@ -70,9 +78,7 @@ public void OnEnable ()
70
78
71
79
public void OnDisable ( )
72
80
{
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 ( ) ;
76
82
}
77
83
78
84
[ ContextMenu ( "Reinitialize" ) ]
@@ -89,6 +95,13 @@ public void Setup ()
89
95
_light . shadowCustomResolution = 0 ;
90
96
91
97
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
+ }
92
105
shadowmapPropID = Shader . PropertyToID ( "_ShadowMap" ) ;
93
106
94
107
copyShadowBuffer = new CommandBuffer ( ) ;
@@ -117,16 +130,26 @@ public void CreateShadowRenderTexture ()
117
130
shadowRenderTexture = new RenderTexture ( resolution , resolution , 0 , format ) ;
118
131
shadowRenderTexture . filterMode = filterMode ;
119
132
shadowRenderTexture . useMipMap = false ;
120
- shadowRenderTexture . antiAliasing = Mathf . Clamp ( MSAA , 1 , 8 ) ;
133
+ shadowRenderTexture . antiAliasing = ( int ) MSAA ;
121
134
}
122
135
123
136
[ ContextMenu ( "Reset Shadows To Default" ) ]
124
137
public void ResetShadowMode ( )
125
138
{
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 ) ;
126
144
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 ) ;
130
153
GraphicsSettings . SetShaderMode ( BuiltinShaderType . ScreenSpaceShadows , BuiltinShaderMode . Disabled ) ;
131
154
_light . shadowCustomResolution = 0 ;
132
155
DestroyImmediate ( shadowRenderTexture ) ;
@@ -142,12 +165,13 @@ public void ResetShadowMode ()
142
165
#region UpdateSettings
143
166
public void UpdateShaderValues ( )
144
167
{
168
+ keywords . Clear ( ) ;
145
169
Shader . SetGlobalInt ( "Blocker_Samples" , Blocker_SampleCount ) ;
146
170
Shader . SetGlobalInt ( "PCF_Samples" , PCF_SampleCount ) ;
147
171
148
172
if ( shadowRenderTexture )
149
173
{
150
- if ( shadowRenderTexture . format != format || shadowRenderTexture . antiAliasing != Mathf . Clamp ( MSAA , 1 , 8 ) )
174
+ if ( shadowRenderTexture . format != format || shadowRenderTexture . antiAliasing != ( int ) MSAA )
151
175
CreateShadowRenderTexture ( ) ;
152
176
else
153
177
{
@@ -186,6 +210,9 @@ public void UpdateShaderValues ()
186
210
187
211
SetFlag ( "POISSON_32" , maxSamples < 33 ) ;
188
212
SetFlag ( "POISSON_64" , maxSamples > 33 ) ;
213
+
214
+ if ( shaderVariants )
215
+ shaderVariants . Add ( new ShaderVariantCollection . ShaderVariant ( shader , PassType . Normal , keywords . ToArray ( ) ) ) ;
189
216
}
190
217
191
218
public void UpdateCommandBuffer ( )
@@ -202,8 +229,11 @@ public void UpdateCommandBuffer ()
202
229
203
230
public void SetFlag ( string shaderKeyword , bool value )
204
231
{
205
- if ( value )
206
- Shader . EnableKeyword ( shaderKeyword ) ;
232
+ if ( value )
233
+ {
234
+ Shader . EnableKeyword ( shaderKeyword ) ;
235
+ keywords . Add ( shaderKeyword ) ;
236
+ }
207
237
else
208
238
Shader . DisableKeyword ( shaderKeyword ) ;
209
239
}
0 commit comments