Skip to content

Commit 740b4bd

Browse files
committed
The issue seems to be the shader variants, which are getting discarded during build. Started work on a shader variants collection to ensure they get loaded correctly
1 parent 69054d7 commit 740b4bd

18 files changed

+514
-147
lines changed

Assets/PCSS/Demo/Demo Assets/Palm Trees/Shaders/Vegetation Tesselation.shader

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Shader "Custom/Vegetation"
1+
Shader "Custom/Vegetation Tesselation"
22
{
33
Properties
44
{
Binary file not shown.
Binary file not shown.

Assets/PCSS/Demo/Demo Assets/Scripts/PCSSDemo.cs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public class PCSSDemo : MonoBehaviour
2626
[Space(10f)]
2727
public Dropdown shadowMode;
2828

29+
[Space(10f)]
30+
public Dropdown msaaMode;
31+
private Camera _camera;
32+
2933
private void Awake ()
3034
{
3135
blockerSlider.value = pcssScript.Blocker_SampleCount;
@@ -41,6 +45,7 @@ private void Awake ()
4145
SetSoftnessFalloff(softnessFalloffSlider.value);
4246

4347
// SetShadowMode(shadowMode.value);
48+
SetMSAAMode(msaaMode.value);
4449
}
4550

4651
public void SetBlockerSamples (float samplesFloat)
@@ -75,20 +80,46 @@ public void SetSoftnessFalloff (float softnessFalloff)
7580

7681
public void SetShadowMode (int mode)
7782
{
78-
switch (mode) {
79-
case(0):
80-
pcssScript.ResetShadowMode();
81-
pcssScript._light.shadows = LightShadows.Soft;
82-
pcssScript.Setup();
83-
break;
84-
case(1):
85-
pcssScript.ResetShadowMode ();
86-
pcssScript._light.shadows = LightShadows.Soft;
87-
break;
88-
case(2):
89-
pcssScript.ResetShadowMode();
90-
pcssScript._light.shadows = LightShadows.Hard;
91-
break;
83+
switch (mode)
84+
{
85+
case(0):
86+
pcssScript.ResetShadowMode();
87+
pcssScript._light.shadows = LightShadows.Soft;
88+
pcssScript.Setup();
89+
break;
90+
case(1):
91+
pcssScript.ResetShadowMode();
92+
pcssScript._light.shadows = LightShadows.Soft;
93+
break;
94+
case(2):
95+
pcssScript.ResetShadowMode();
96+
pcssScript._light.shadows = LightShadows.Hard;
97+
break;
98+
}
99+
}
100+
101+
public void SetMSAAMode (int mode)
102+
{
103+
if (!_camera)
104+
_camera = Camera.main;
105+
if (!_camera)
106+
return;
107+
108+
_camera.allowMSAA = mode > 0;
109+
switch (mode)
110+
{
111+
case(0):
112+
QualitySettings.antiAliasing = 0;
113+
break;
114+
case(1):
115+
QualitySettings.antiAliasing = 2;
116+
break;
117+
case(2):
118+
QualitySettings.antiAliasing = 4;
119+
break;
120+
case(3):
121+
QualitySettings.antiAliasing = 8;
122+
break;
92123
}
93124
}
94125
}

Assets/PCSS/Demo/PCSS Demo.unity

216 Bytes
Binary file not shown.

Assets/PCSS/Scripts/Attributes/Editor/PowRangeDrawer.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

Assets/PCSS/Scripts/Attributes/Editor/PowRangeDrawer.cs.meta

Lines changed: 0 additions & 13 deletions
This file was deleted.

Assets/PCSS/Scripts/Attributes/PowRangeAttribute.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

Assets/PCSS/Scripts/Attributes/PowRangeAttribute.cs.meta

Lines changed: 0 additions & 13 deletions
This file was deleted.

Assets/PCSS/Scripts/PCSSLight.cs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
[ExecuteInEditMode]
77
public 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
}

Assets/PCSS/Scripts/SetCameraDepthMode.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
[ExecuteInEditMode]
66
public class SetCameraDepthMode : MonoBehaviour
77
{
8+
public bool enabled;
89
public DepthTextureMode depthMode = DepthTextureMode.Depth;
910
public DepthTextureMode depthMode2 = DepthTextureMode.None;
10-
[PowRange(0, 8, 2, true)]
11-
public int MSAA = 0;
11+
public enum antiAliasing
12+
{
13+
None = 0,
14+
Two = 2,
15+
Four = 4,
16+
Eight = 8,
17+
}
18+
public antiAliasing MSAA = antiAliasing.None;
1219
private Camera _camera;
1320

1421
void Awake ()
@@ -19,13 +26,16 @@ void Awake ()
1926
[ContextMenu("Set Depth Mode")]
2027
public void SetDepthMode ()
2128
{
29+
if (!enabled)
30+
return;
31+
2232
if (!_camera)
2333
_camera = GetComponent<Camera>();
2434
if (!_camera)
2535
return;
2636

2737
_camera.depthTextureMode = depthMode | depthMode2;
28-
QualitySettings.antiAliasing = MSAA;
29-
_camera.allowMSAA = MSAA > 0;
38+
QualitySettings.antiAliasing = (int)MSAA;
39+
_camera.allowMSAA = (MSAA != antiAliasing.None);
3040
}
3141
}

0 commit comments

Comments
 (0)