Skip to content

Commit 6b6bf70

Browse files
- Split shadow setting relevant to the scene from the HDRenderLoop ShadowSettings structure to the CommonSettings component.
- Made sure everything worked fine with multi-editing.
1 parent 3bafbd0 commit 6b6bf70

4 files changed

Lines changed: 146 additions & 28 deletions

File tree

Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ private class Styles
3232
public readonly GUIContent shadowsAtlasWidth = new GUIContent("Atlas width");
3333
public readonly GUIContent shadowsAtlasHeight = new GUIContent("Atlas height");
3434

35-
public readonly GUIContent shadowsMaxShadowDistance = new GUIContent("Maximum shadow distance");
36-
public readonly GUIContent shadowsDirectionalLightCascadeCount = new GUIContent("Directional cascade count");
37-
public readonly GUIContent[] shadowsCascadeCounts = new GUIContent[] { new GUIContent("1"), new GUIContent("2"), new GUIContent("3"), new GUIContent("4") };
38-
public readonly int[] shadowsCascadeCountValues = new int[] { 1, 2, 3, 4 };
39-
public readonly GUIContent shadowsCascades = new GUIContent("Cascade values");
40-
4135
public readonly GUIContent tileLightLoopSettings = new GUIContent("Tile Light Loop Settings");
4236
public readonly string[] tileLightLoopDebugTileFlagStrings = new string[] { "Punctual Light", "Area Light", "Env Light"};
4337
public readonly GUIContent splitLightEvaluation = new GUIContent("Split light and reflection evaluation", "Toggle");
@@ -204,15 +198,7 @@ private void ShadowParametersUI(HDRenderLoop renderLoop)
204198
shadowParameters.enabled = EditorGUILayout.Toggle(styles.shadowsEnabled, shadowParameters.enabled);
205199
shadowParameters.shadowAtlasWidth = Mathf.Max(0, EditorGUILayout.IntField(styles.shadowsAtlasWidth, shadowParameters.shadowAtlasWidth));
206200
shadowParameters.shadowAtlasHeight = Mathf.Max(0, EditorGUILayout.IntField(styles.shadowsAtlasHeight, shadowParameters.shadowAtlasHeight));
207-
shadowParameters.maxShadowDistance = Mathf.Max(0, EditorGUILayout.FloatField(styles.shadowsMaxShadowDistance, shadowParameters.maxShadowDistance));
208-
shadowParameters.directionalLightCascadeCount = EditorGUILayout.IntPopup(styles.shadowsDirectionalLightCascadeCount, shadowParameters.directionalLightCascadeCount, styles.shadowsCascadeCounts, styles.shadowsCascadeCountValues);
209201

210-
EditorGUI.indentLevel++;
211-
for (int i = 0; i < shadowParameters.directionalLightCascadeCount - 1; i++)
212-
{
213-
shadowParameters.directionalLightCascades[i] = Mathf.Max(0, EditorGUILayout.FloatField(shadowParameters.directionalLightCascades[i]));
214-
}
215-
EditorGUI.indentLevel--;
216202
if (EditorGUI.EndChangeCheck())
217203
{
218204
EditorUtility.SetDirty(renderLoop); // Repaint

Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ public struct HDCamera
182182
public Matrix4x4 invViewProjectionMatrix;
183183
}
184184

185+
CommonSettings m_CommonSettings = null;
186+
public CommonSettings commonSettings
187+
{
188+
set { m_CommonSettings = value; }
189+
get { return m_CommonSettings; }
190+
}
191+
185192
public override void Build()
186193
{
187194
#if UNITY_EDITOR
@@ -628,6 +635,22 @@ public void PushGlobalParams(HDCamera hdCamera, RenderLoop renderLoop)
628635
m_lightLoop.PushGlobalParams(hdCamera.camera, renderLoop);
629636
}
630637

638+
void UpdateCommonSettings()
639+
{
640+
if(m_CommonSettings == null)
641+
{
642+
m_ShadowSettings.maxShadowDistance = ShadowSettings.Default.maxShadowDistance;
643+
m_ShadowSettings.directionalLightCascadeCount = ShadowSettings.Default.directionalLightCascadeCount;
644+
m_ShadowSettings.directionalLightCascades = ShadowSettings.Default.directionalLightCascades;
645+
}
646+
else
647+
{
648+
m_ShadowSettings.directionalLightCascadeCount = m_CommonSettings.shadowCascadeCount;
649+
m_ShadowSettings.directionalLightCascades = new Vector3(m_CommonSettings.shadowCascadeSplit0, m_CommonSettings.shadowCascadeSplit1, m_CommonSettings.shadowCascadeSplit2);
650+
m_ShadowSettings.maxShadowDistance = m_CommonSettings.shadowMaxDistance;
651+
}
652+
}
653+
631654
public override void Render(Camera[] cameras, RenderLoop renderLoop)
632655
{
633656
if (!m_LitRenderLoop.isInit)
@@ -638,6 +661,8 @@ public override void Render(Camera[] cameras, RenderLoop renderLoop)
638661
// Do anything we need to do upon a new frame.
639662
m_lightLoop.NewFrame();
640663

664+
UpdateCommonSettings();
665+
641666
// Set Frame constant buffer
642667
// TODO...
643668

Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings/CommonSettings.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,25 @@ namespace UnityEngine.Experimental.ScriptableRenderLoop
1212
public class CommonSettings
1313
: MonoBehaviour
1414
{
15-
[SerializeField]
16-
private string m_SkyRendererTypeName = "";
15+
[SerializeField] private string m_SkyRendererTypeName = ""; // Serialize a string because serialize a Type.
16+
17+
[SerializeField] float m_ShadowMaxDistance = ShadowSettings.Default.maxShadowDistance;
18+
[SerializeField] int m_ShadowCascadeCount = ShadowSettings.Default.directionalLightCascadeCount;
19+
[SerializeField] float m_ShadowCascadeSplit0 = ShadowSettings.Default.directionalLightCascades.x;
20+
[SerializeField] float m_ShadowCascadeSplit1 = ShadowSettings.Default.directionalLightCascades.y;
21+
[SerializeField] float m_ShadowCascadeSplit2 = ShadowSettings.Default.directionalLightCascades.z;
22+
1723
public Type skyRendererType
1824
{
1925
set { m_SkyRendererTypeName = value != null ? value.FullName : ""; OnSkyRendererChanged(); }
2026
get { return m_SkyRendererTypeName == "" ? null : Assembly.GetAssembly(typeof(CommonSettings)).GetType(m_SkyRendererTypeName); }
2127
}
2228

29+
public float shadowMaxDistance { set { m_ShadowMaxDistance = value; OnValidate(); } get { return m_ShadowMaxDistance; } }
30+
public int shadowCascadeCount { set { m_ShadowCascadeCount = value; OnValidate(); } get { return m_ShadowCascadeCount; } }
31+
public float shadowCascadeSplit0 { set { m_ShadowCascadeSplit0 = value; OnValidate(); } get { return m_ShadowCascadeSplit0; } }
32+
public float shadowCascadeSplit1 { set { m_ShadowCascadeSplit1 = value; OnValidate(); } get { return m_ShadowCascadeSplit1; } }
33+
public float shadowCascadeSplit2 { set { m_ShadowCascadeSplit2 = value; OnValidate(); } get { return m_ShadowCascadeSplit2; } }
2334

2435
void OnEnable()
2536
{
@@ -29,11 +40,33 @@ void OnEnable()
2940
return;
3041
}
3142

43+
if (renderLoop.commonSettings == null)
44+
renderLoop.commonSettings = this;
45+
else if (renderLoop.commonSettings != this)
46+
Debug.LogWarning("Only one CommonSettings can be setup at a time.");
47+
3248
OnSkyRendererChanged();
3349
}
3450

3551
void OnDisable()
3652
{
53+
HDRenderLoop renderLoop = Utilities.GetHDRenderLoop();
54+
if (renderLoop == null)
55+
{
56+
return;
57+
}
58+
59+
if (renderLoop.commonSettings == this)
60+
renderLoop.commonSettings = null;
61+
}
62+
63+
void OnValidate()
64+
{
65+
m_ShadowMaxDistance = Mathf.Max(0.0f, m_ShadowMaxDistance);
66+
m_ShadowCascadeCount = Math.Min(4, Math.Max(1, m_ShadowCascadeCount));
67+
m_ShadowCascadeSplit0 = Mathf.Min(1.0f, Mathf.Max(0.0f, m_ShadowCascadeSplit0));
68+
m_ShadowCascadeSplit1 = Mathf.Min(1.0f, Mathf.Max(0.0f, m_ShadowCascadeSplit1));
69+
m_ShadowCascadeSplit2 = Mathf.Min(1.0f, Mathf.Max(0.0f, m_ShadowCascadeSplit2));
3770
}
3871

3972
void OnSkyRendererChanged()

Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings/Editor/CommonSettingsEditor.cs

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,24 @@
99
namespace UnityEngine.Experimental.ScriptableRenderLoop
1010
{
1111
[CustomEditor(typeof(CommonSettings))]
12+
[CanEditMultipleObjects]
1213
public class CommonSettingsEditor
1314
: Editor
1415
{
1516
private class Styles
1617
{
1718
public readonly GUIContent none = new GUIContent("None");
19+
public readonly GUIContent sky = new GUIContent("Sky");
1820
public readonly GUIContent skyRenderer = new GUIContent("Sky Renderer");
21+
22+
public readonly GUIContent shadows = new GUIContent("Shadows");
23+
public readonly GUIContent maxShadowDistance = new GUIContent("Maximum shadow distance");
24+
public readonly GUIContent shadowsDirectionalLightCascadeCount = new GUIContent("Directional cascade count");
25+
public readonly GUIContent[] shadowsCascadeCounts = new GUIContent[] { new GUIContent("1"), new GUIContent("2"), new GUIContent("3"), new GUIContent("4") };
26+
public readonly int[] shadowsCascadeCountValues = new int[] { 1, 2, 3, 4 };
27+
public readonly GUIContent shadowsCascades = new GUIContent("Cascade values");
28+
public readonly GUIContent[] shadowSplits = new GUIContent[] { new GUIContent("Split 0"), new GUIContent("Split 1"), new GUIContent("Split 2") };
29+
1930
}
2031

2132
private static Styles s_Styles = null;
@@ -29,23 +40,42 @@ private static Styles styles
2940
}
3041
}
3142

32-
private List<Type> m_SkyRendererTypes;
43+
// Sky renderer
44+
List<Type> m_SkyRendererTypes = new List<Type>();
3345
private List<GUIContent> m_SkyRendererTypeNames = new List<GUIContent>();
46+
private List<string> m_SkyRendererFullTypeNames = new List<string>();
3447
private List<int> m_SkyRendererTypeValues = new List<int>();
3548

49+
private bool multipleEditing { get { return targets.Length > 1; } }
50+
51+
private SerializedProperty m_SkyRenderer;
52+
53+
private SerializedProperty m_ShadowMaxDistance;
54+
private SerializedProperty m_ShadowCascadeCount;
55+
private SerializedProperty[] m_ShadowCascadeSplits = new SerializedProperty[3];
56+
3657
void OnEnable()
3758
{
59+
m_SkyRenderer = serializedObject.FindProperty("m_SkyRendererTypeName");
60+
61+
m_ShadowMaxDistance = serializedObject.FindProperty("m_ShadowMaxDistance");
62+
m_ShadowCascadeCount = serializedObject.FindProperty("m_ShadowCascadeCount");
63+
for (int i = 0; i < 3; ++i)
64+
m_ShadowCascadeSplits[i] = serializedObject.FindProperty(string.Format("m_ShadowCascadeSplit{0}", i));
65+
3866
m_SkyRendererTypes = Assembly.GetAssembly(typeof(SkyRenderer))
39-
.GetTypes()
40-
.Where(t => t.IsSubclassOf(typeof(SkyRenderer)) && !t.IsGenericType)
41-
.ToList();
67+
.GetTypes()
68+
.Where(t => t.IsSubclassOf(typeof(SkyRenderer)) && !t.IsGenericType)
69+
.ToList();
4270

4371
// Prepare the list of available SkyRenderers for the IntPopup
4472
m_SkyRendererTypeNames.Clear();
73+
m_SkyRendererFullTypeNames.Clear();
4574
m_SkyRendererTypeValues.Clear();
46-
for(int i = 0 ; i < m_SkyRendererTypes.Count ; ++i)
75+
for (int i = 0; i < m_SkyRendererTypes.Count; ++i)
4776
{
4877
string longName = m_SkyRendererTypes[i].ToString();
78+
m_SkyRendererFullTypeNames.Add(longName);
4979
char[] separators = {'.'};
5080
string[] tokens = longName.Split(separators);
5181
m_SkyRendererTypeNames.Add(new GUIContent(tokens[tokens.Length - 1]));
@@ -54,32 +84,76 @@ void OnEnable()
5484

5585
// Add default null value.
5686
m_SkyRendererTypeNames.Add(styles.none);
87+
m_SkyRendererFullTypeNames.Add("");
5788
m_SkyRendererTypeValues.Add(m_SkyRendererTypeValues.Count);
5889
m_SkyRendererTypes.Add(null);
5990
}
6091

61-
public override void OnInspectorGUI()
92+
void OnSkyInspectorGUI()
6293
{
63-
serializedObject.Update();
94+
EditorGUILayout.LabelField(styles.sky);
95+
EditorGUI.indentLevel++;
6496

65-
CommonSettings settings = target as CommonSettings;
66-
// Retrieve the index of the current SkyRenderer
97+
// Retrieve the index of the current SkyRenderer. Won't be used in case of multiple editing with different values
6798
int index = -1;
68-
for(int i = 0 ; i < m_SkyRendererTypeValues.Count ; ++i )
99+
for (int i = 0; i < m_SkyRendererTypeNames.Count; ++i)
69100
{
70-
if(m_SkyRendererTypes[i] == settings.skyRendererType)
101+
if (m_SkyRendererFullTypeNames[i] == m_SkyRenderer.stringValue)
71102
{
72103
index = i;
73104
break;
74105
}
75106
}
76107

108+
EditorGUI.showMixedValue = m_SkyRenderer.hasMultipleDifferentValues;
77109
EditorGUI.BeginChangeCheck();
78110
int newValue = EditorGUILayout.IntPopup(styles.skyRenderer, index, m_SkyRendererTypeNames.ToArray(), m_SkyRendererTypeValues.ToArray());
111+
if (EditorGUI.EndChangeCheck())
112+
{
113+
m_SkyRenderer.stringValue = m_SkyRendererFullTypeNames[newValue];
114+
}
115+
EditorGUI.showMixedValue = false;
116+
117+
EditorGUI.indentLevel--;
118+
}
119+
120+
void OnShadowInspectorGUI()
121+
{
122+
EditorGUILayout.LabelField(styles.shadows);
123+
EditorGUI.indentLevel++;
124+
EditorGUILayout.PropertyField(m_ShadowMaxDistance, styles.maxShadowDistance);
125+
126+
EditorGUI.BeginChangeCheck();
127+
EditorGUI.showMixedValue = m_ShadowCascadeCount.hasMultipleDifferentValues;
128+
int newCascadeCount = EditorGUILayout.IntPopup(styles.shadowsDirectionalLightCascadeCount, m_ShadowCascadeCount.intValue, styles.shadowsCascadeCounts, styles.shadowsCascadeCountValues);
79129
if(EditorGUI.EndChangeCheck())
80130
{
81-
settings.skyRendererType = m_SkyRendererTypes[newValue];
131+
m_ShadowCascadeCount.intValue = newCascadeCount;
132+
}
133+
134+
// Compute max cascade count.
135+
int maxCascadeCount = 0;
136+
for (int i = 0; i < targets.Length; ++i)
137+
{
138+
CommonSettings settings = targets[i] as CommonSettings;
139+
maxCascadeCount = Math.Max(maxCascadeCount, settings.shadowCascadeCount);
140+
}
141+
142+
EditorGUI.indentLevel++;
143+
for (int i = 0; i < maxCascadeCount - 1; i++)
144+
{
145+
EditorGUILayout.PropertyField(m_ShadowCascadeSplits[i], styles.shadowSplits[i]);
82146
}
147+
EditorGUI.indentLevel--;
148+
EditorGUI.indentLevel--;
149+
}
150+
151+
public override void OnInspectorGUI()
152+
{
153+
serializedObject.Update();
154+
155+
OnSkyInspectorGUI();
156+
OnShadowInspectorGUI();
83157

84158
serializedObject.ApplyModifiedProperties();
85159
}

0 commit comments

Comments
 (0)