-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathHDLightingWindowEnvironmentSection.cs
More file actions
242 lines (201 loc) · 10.2 KB
/
HDLightingWindowEnvironmentSection.cs
File metadata and controls
242 lines (201 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
using UnityEngine.Rendering.HighDefinition;
using UnityEditor;
using UnityEditor.Rendering;
using UnityEditor.SceneManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace UnityEditor.Rendering.HighDefinition
{
[ScriptableRenderPipelineExtension(typeof(HDRenderPipelineAsset))]
class HDLightingWindowEnvironmentSectionEditor : LightingWindowEnvironmentSection
{
class Styles
{
public static readonly GUIStyle inspectorTitle = "IN Title";
}
class SerializedStaticLightingSky
{
SerializedObject serializedObject;
public SerializedProperty skyUniqueID;
public SerializedProperty cloudUniqueID;
public SerializedProperty volumetricCloudsToggle;
public VolumeProfile volumeProfile
{
get => (serializedObject.targetObject as StaticLightingSky).profile;
set => (serializedObject.targetObject as StaticLightingSky).profile = value;
}
public SerializedStaticLightingSky(StaticLightingSky staticLightingSky)
{
serializedObject = new SerializedObject(staticLightingSky);
skyUniqueID = serializedObject.FindProperty("m_StaticLightingSkyUniqueID");
cloudUniqueID = serializedObject.FindProperty("m_StaticLightingCloudsUniqueID");
volumetricCloudsToggle = serializedObject.FindProperty("m_StaticLightingVolumetricClouds");
}
public void Apply() => serializedObject.ApplyModifiedProperties();
public void Update() => serializedObject.Update();
public bool valid
=> serializedObject != null
&& !serializedObject.Equals(null)
&& serializedObject.targetObject != null
&& !serializedObject.targetObject.Equals(null);
}
SerializedStaticLightingSky m_SerializedActiveSceneLightingSky;
List<GUIContent> m_SkyClassNames = null;
List<int> m_SkyUniqueIDs = null;
List<GUIContent> m_CloudClassNames = null;
List<int> m_CloudUniqueIDs = null;
const string k_ToggleValueKey = "HDRP:LightingWindowEnvironemntSection:Header";
bool m_ToggleValue = true;
bool toggleValue
{
get => m_ToggleValue;
set
{
m_ToggleValue = value;
EditorPrefs.SetBool(k_ToggleValueKey, value);
}
}
static MethodInfo k_FoldoutTitlebar;
public override void OnEnable()
{
m_SerializedActiveSceneLightingSky = new SerializedStaticLightingSky(GetStaticLightingSkyForScene(EditorSceneManager.GetActiveScene()));
if (EditorPrefs.HasKey(k_ToggleValueKey))
m_ToggleValue = EditorPrefs.GetBool(k_ToggleValueKey);
EditorSceneManager.activeSceneChanged += OnActiveSceneChange;
}
public override void OnDisable()
=> EditorSceneManager.activeSceneChanged -= OnActiveSceneChange;
void OnActiveSceneChange(Scene current, Scene next)
=> m_SerializedActiveSceneLightingSky = new SerializedStaticLightingSky(GetStaticLightingSkyForScene(next));
StaticLightingSky GetStaticLightingSkyForScene(Scene scene)
{
StaticLightingSky result = null;
foreach (var go in scene.GetRootGameObjects())
{
result = go.GetComponent<StaticLightingSky>();
if (result != null)
break;
}
//Perhaps it is an old scene. Search everywhere
if (result == null)
{
var candidates = GameObject.FindObjectsOfType<StaticLightingSky>().Where(sls => sls.gameObject.scene == scene);
if (candidates.Count() > 0)
result = candidates.First();
}
//Perhaps it not exist yet
if (result == null)
{
var go = new GameObject("StaticLightingSky", new[] { typeof(StaticLightingSky) });
go.hideFlags = HideFlags.HideInHierarchy;
result = go.GetComponent<StaticLightingSky>();
}
return result;
}
public override void OnInspectorGUI()
{
WorkarroundWhileActiveSceneChangedHookIsNotCalled();
m_SerializedActiveSceneLightingSky.Update();
EditorGUI.BeginChangeCheck();
//Volume can have changed. Check available sky
UpdateIntPopupData(ref m_SkyClassNames, ref m_SkyUniqueIDs, SkyManager.skyTypesDict, m_SerializedActiveSceneLightingSky.skyUniqueID);
UpdateIntPopupData(ref m_CloudClassNames, ref m_CloudUniqueIDs, SkyManager.cloudTypesDict, m_SerializedActiveSceneLightingSky.cloudUniqueID);
DrawGUI();
if (EditorGUI.EndChangeCheck())
{
m_SerializedActiveSceneLightingSky.Apply();
var hdrp = HDRenderPipeline.currentPipeline;
if (hdrp != null)
{
hdrp.RequestStaticSkyUpdate();
SceneView.RepaintAll();
}
}
}
void DrawGUI()
{
if (k_FoldoutTitlebar == null)
{
var flags = BindingFlags.NonPublic | BindingFlags.Static;
Type[] args = new Type[] { typeof(Rect), typeof(GUIContent), typeof(bool), typeof(bool) };
k_FoldoutTitlebar = typeof(EditorGUI).GetMethod("FoldoutTitlebar", flags, null, args, null);
}
var labelRect = GUILayoutUtility.GetRect(GUIContent.none, Styles.inspectorTitle, GUILayout.ExpandWidth(true));
var label = EditorGUIUtility.TrTextContent("Environment (HDRP)", "Sky lighting environment for active Scene");
toggleValue = (bool)k_FoldoutTitlebar.Invoke(null, new object[] { labelRect, label, toggleValue, true });
if (m_ToggleValue)
{
++EditorGUI.indentLevel;
//cannot use SerializeProperty due to logic in the property
var profile = m_SerializedActiveSceneLightingSky.volumeProfile;
var newProfile = EditorGUILayout.ObjectField(EditorGUIUtility.TrTextContent("Profile"), profile, typeof(VolumeProfile), allowSceneObjects: false) as VolumeProfile;
if (profile != newProfile)
{
m_SerializedActiveSceneLightingSky.volumeProfile = newProfile;
}
using (new EditorGUI.DisabledScope(m_SkyClassNames.Count == 1)) // Only "None"
{
EditorGUILayout.IntPopup(m_SerializedActiveSceneLightingSky.skyUniqueID, m_SkyClassNames.ToArray(), m_SkyUniqueIDs.ToArray(), EditorGUIUtility.TrTextContent("Static Lighting Sky", "Specify which kind of sky you want to use for static ambient in the referenced profile for active scene."));
}
if (m_SkyClassNames.Count > 1 && m_SerializedActiveSceneLightingSky.skyUniqueID.intValue > 0)
EditorGUILayout.HelpBox("Note that depending on the static lighting sky type, you might need to regenerate the lighting after changing the properties of the main directional light.", MessageType.Info);
using (new EditorGUI.DisabledScope(m_CloudClassNames.Count == 1)) // Only "None"
{
EditorGUILayout.IntPopup(m_SerializedActiveSceneLightingSky.cloudUniqueID, m_CloudClassNames.ToArray(), m_CloudUniqueIDs.ToArray(), EditorGUIUtility.TrTextContent("Static Lighting Background Clouds", "Specify which kind of background clouds you want to use for static ambient in the referenced profile for active scene."));
}
EditorGUILayout.PropertyField(m_SerializedActiveSceneLightingSky.volumetricCloudsToggle, EditorGUIUtility.TrTextContent("Static Lighting Volumetric Clouds", "Specify if volumetric clouds should be used for static ambient in the referenced profile for active scene."));
--EditorGUI.indentLevel;
}
}
void UpdateIntPopupData(ref List<GUIContent> classNames, ref List<int> uniqueIds, Dictionary<int, Type> typesDict, SerializedProperty idProperty)
{
if (classNames == null)
{
classNames = new List<GUIContent>();
uniqueIds = new List<int>();
}
// We always reinit because the content can change depending on the volume and we are not always notified when this happens (like for undo/redo for example)
classNames.Clear();
uniqueIds.Clear();
// Add special "None" case.
classNames.Add(new GUIContent("None"));
uniqueIds.Add(0);
VolumeProfile profile = m_SerializedActiveSceneLightingSky.volumeProfile;
if (profile != null)
{
var currentID = idProperty.intValue;
bool foundID = currentID == 0;
foreach (KeyValuePair<int, Type> kvp in typesDict)
{
if (profile.TryGet(kvp.Value, out VolumeComponent comp) && comp.active)
{
classNames.Add(new GUIContent(kvp.Value.Name.ToString()));
uniqueIds.Add(kvp.Key);
foundID |= (currentID == kvp.Key);
}
}
if (!foundID) // Selected volume component has been deleted
{
idProperty.intValue = 0;
GUI.changed = true;
}
}
}
Scene m_ActiveScene;
void WorkarroundWhileActiveSceneChangedHookIsNotCalled()
{
Scene currentActiveScene = EditorSceneManager.GetActiveScene();
if (m_ActiveScene != currentActiveScene
|| !(m_SerializedActiveSceneLightingSky?.valid ?? false))
{
m_SerializedActiveSceneLightingSky = new SerializedStaticLightingSky(GetStaticLightingSkyForScene(currentActiveScene));
m_ActiveScene = currentActiveScene;
}
}
}
}