-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathCustomPassDrawer.cs
More file actions
302 lines (257 loc) · 12.2 KB
/
CustomPassDrawer.cs
File metadata and controls
302 lines (257 loc) · 12.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using UnityEditorInternal;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using System.Linq;
using System;
using System.Reflection;
namespace UnityEditor.Rendering.HighDefinition
{
/// <summary>
/// Custom UI class for custom passes
/// </summary>
[CustomPassDrawerAttribute(typeof(CustomPass))]
public class CustomPassDrawer
{
class Styles
{
public static float defaultLineSpace = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
public static float reorderableListHandleIndentWidth = 12;
public static GUIContent callback = new GUIContent("Event", "Chose the Callback position for this render pass object.");
public static GUIContent enabled = new GUIContent("Enabled", "Enable or Disable the custom pass");
public static GUIContent targetDepthBuffer = new GUIContent("Target Depth Buffer");
public static GUIContent targetColorBuffer = new GUIContent("Target Color Buffer");
public static GUIContent clearFlags = new GUIContent("Clear Flags", "Clear Flags used when the render targets will are bound, before the pass renders.");
}
/// <summary>
/// List of the elements you can show/hide in the default custom pass UI.
/// </summary>
[Flags]
public enum PassUIFlag
{
/// <summary>Hides all the default UI fields.</summary>
None = 0x00,
/// <summary>Shows the name field.</summary>
Name = 0x01,
/// <summary>Shows the target color buffer field.</summary>
TargetColorBuffer = 0x02,
/// <summary>Shows the target depth buffer field.</summary>
TargetDepthBuffer = 0x04,
/// <summary>Shows the clear flags field.</summary>
ClearFlags = 0x08,
/// <summary>Shows all the default UI fields.</summary>
All = ~0,
}
/// <summary>
/// Controls which field of the common pass UI is displayed.
/// </summary>
protected virtual PassUIFlag commonPassUIFlags => PassUIFlag.All;
/// <summary>
/// Get the current instance of the custom pass being displayed in the Custom Pass Volume Editor.
/// </summary>
protected CustomPass target => m_CustomPass;
bool firstTime = true;
// Serialized Properties
SerializedProperty m_Name;
SerializedProperty m_Enabled;
SerializedProperty m_TargetColorBuffer;
SerializedProperty m_TargetDepthBuffer;
SerializedProperty m_ClearFlags;
SerializedProperty m_PassFoldout;
List<SerializedProperty> m_CustomPassUserProperties = new List<SerializedProperty>();
CustomPass m_CustomPass;
Type m_PassType => m_CustomPass.GetType();
void FetchProperties(SerializedProperty property)
{
m_Name = property.FindPropertyRelative("m_Name");
m_Enabled = property.FindPropertyRelative("enabled");
m_TargetColorBuffer = property.FindPropertyRelative("targetColorBuffer");
m_TargetDepthBuffer = property.FindPropertyRelative("targetDepthBuffer");
m_ClearFlags = property.FindPropertyRelative("clearFlags");
m_PassFoldout = property.FindPropertyRelative("passFoldout");
}
void LoadUserProperties(SerializedProperty customPass)
{
// Store all fields in CustomPass so we can exclude them when retrieving the user custom pass type
var customPassFields = typeof(CustomPass).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
m_CustomPassUserProperties.Clear();
foreach (var field in m_PassType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
var serializeField = field.GetCustomAttribute<SerializeField>();
var hideInInspector = field.GetCustomAttribute<HideInInspector>();
var nonSerialized = field.GetCustomAttribute<NonSerializedAttribute>();
if (customPassFields.Any(f => f.Name == field.Name))
continue;
if (nonSerialized != null || hideInInspector != null)
continue;
if (!field.IsPublic && serializeField == null)
continue;
var prop = customPass.FindPropertyRelative(field.Name);
if (prop != null)
m_CustomPassUserProperties.Add(prop);
}
}
void InitInternal(SerializedProperty customPass)
{
FetchProperties(customPass);
Initialize(customPass);
LoadUserProperties(customPass);
firstTime = false;
}
/// <summary>
/// Use this function to initialize the local SerializedProperty you will use in your pass.
/// </summary>
/// <param name="customPass">Your custom pass instance represented as a SerializedProperty</param>
protected virtual void Initialize(SerializedProperty customPass) { }
internal void SetPass(CustomPass pass) => m_CustomPass = pass;
internal void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
rect.height = EditorGUIUtility.singleLineHeight;
EditorGUI.BeginChangeCheck();
if (firstTime)
InitInternal(property);
DoHeaderGUI(ref rect);
if (m_PassFoldout.boolValue)
{
EditorGUI.EndChangeCheck();
return;
}
EditorGUI.BeginDisabledGroup(!m_Enabled.boolValue);
{
DoCommonSettingsGUI(ref rect);
DoPassGUI(property, rect);
}
EditorGUI.EndDisabledGroup();
if (EditorGUI.EndChangeCheck())
property.serializedObject.ApplyModifiedProperties();
}
void DoCommonSettingsGUI(ref Rect rect)
{
if ((commonPassUIFlags & PassUIFlag.Name) != 0)
{
EditorGUI.BeginChangeCheck();
EditorGUI.PropertyField(rect, m_Name);
if (EditorGUI.EndChangeCheck())
m_CustomPass.name = m_Name.stringValue;
rect.y += Styles.defaultLineSpace;
}
if ((commonPassUIFlags & PassUIFlag.TargetColorBuffer) != 0)
{
EditorGUI.PropertyField(rect, m_TargetColorBuffer, Styles.targetColorBuffer);
rect.y += Styles.defaultLineSpace;
}
if ((commonPassUIFlags & PassUIFlag.TargetDepthBuffer) != 0)
{
EditorGUI.PropertyField(rect, m_TargetDepthBuffer, Styles.targetDepthBuffer);
rect.y += Styles.defaultLineSpace;
CustomPass.TargetBuffer requestedDepth = m_TargetDepthBuffer.GetEnumValue<CustomPass.TargetBuffer>();
if (m_CustomPass.getConstrainedDepthBuffer() != requestedDepth)
{
Rect helpBoxRect = rect;
float helpBoxHeight = EditorGUIUtility.singleLineHeight * 2;
helpBoxRect.height = helpBoxHeight;
EditorGUI.HelpBox(helpBoxRect, "Camera depth isn't supported when dynamic scaling is on. We will automatically fall back to not doing depth-testing for this pass.", MessageType.Warning);
rect.y += helpBoxHeight;
}
}
if ((commonPassUIFlags & PassUIFlag.ClearFlags) != 0)
{
EditorGUI.PropertyField(rect, m_ClearFlags, Styles.clearFlags);
rect.y += Styles.defaultLineSpace;
}
}
/// <summary>
/// Implement this function to draw your custom GUI.
/// </summary>
/// <param name="customPass">Your custom pass instance represented as a SerializedProperty</param>
/// <param name="rect">space available for you to draw the UI</param>
protected virtual void DoPassGUI(SerializedProperty customPass, Rect rect)
{
foreach (var prop in m_CustomPassUserProperties)
{
EditorGUI.PropertyField(rect, prop, true);
rect.y += EditorGUI.GetPropertyHeight(prop) + EditorGUIUtility.standardVerticalSpacing;
}
}
void DoHeaderGUI(ref Rect rect)
{
var enabledSize = EditorStyles.boldLabel.CalcSize(Styles.enabled) + new Vector2(Styles.reorderableListHandleIndentWidth, 0);
var headerRect = new Rect(rect.x + Styles.reorderableListHandleIndentWidth,
rect.y + EditorGUIUtility.standardVerticalSpacing,
rect.width - Styles.reorderableListHandleIndentWidth - enabledSize.x,
EditorGUIUtility.singleLineHeight);
rect.y += Styles.defaultLineSpace;
var enabledRect = headerRect;
enabledRect.x = rect.xMax - enabledSize.x;
enabledRect.width = enabledSize.x;
EditorGUI.BeginProperty(headerRect, GUIContent.none, m_PassFoldout);
{
EditorGUIUtility.labelWidth = headerRect.width;
m_PassFoldout.boolValue = EditorGUI.Foldout(headerRect, m_PassFoldout.boolValue, $"{m_Name.stringValue} ({m_PassType.Name})", true, EditorStyles.boldLabel);
EditorGUIUtility.labelWidth = 0;
}
EditorGUI.EndProperty();
EditorGUI.BeginProperty(enabledRect, Styles.enabled, m_Enabled);
{
EditorGUIUtility.labelWidth = enabledRect.width - 14;
m_Enabled.boolValue = EditorGUI.Toggle(enabledRect, Styles.enabled, m_Enabled.boolValue);
EditorGUIUtility.labelWidth = 0;
}
EditorGUI.EndProperty();
}
/// <summary>
/// Implement this functions if you implement DoPassGUI. The result of this function must match the number of lines displayed in your custom GUI.
/// Note that this height can be dynamic.
/// </summary>
/// <param name="customPass">Your custom pass instance represented as a SerializedProperty</param>
/// <returns>The height in pixels of tour custom pass GUI</returns>
protected virtual float GetPassHeight(SerializedProperty customPass)
{
float height = 0;
foreach (var prop in m_CustomPassUserProperties)
{
height += EditorGUI.GetPropertyHeight(prop);
height += EditorGUIUtility.standardVerticalSpacing;
}
return height;
}
internal float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
float height = Styles.defaultLineSpace;
if (firstTime)
InitInternal(property);
if (m_PassFoldout.boolValue)
return height;
if (!firstTime)
{
int lines = 4; // name + target buffers + clearFlags
if (commonPassUIFlags != PassUIFlag.All)
{
lines = 0;
for (int i = 0; i < 32; i++)
lines += (((int)commonPassUIFlags & (1 << i)) != 0) ? 1 : 0;
}
height += Styles.defaultLineSpace * lines;
// Add height for the help box if it will be shown
if ((commonPassUIFlags & PassUIFlag.TargetDepthBuffer) != 0 &&
m_CustomPass.getConstrainedDepthBuffer() != m_TargetDepthBuffer.GetEnumValue<CustomPass.TargetBuffer>())
{
height += EditorGUIUtility.singleLineHeight * 2; // Help box height
}
}
return height + GetPassHeight(property);
}
internal GUIContent[] GetMaterialPassNames(Material mat)
{
GUIContent[] passNames = new GUIContent[mat.passCount];
for (int i = 0; i < mat.passCount; i++)
{
string passName = mat.GetPassName(i);
passNames[i] = new GUIContent(string.IsNullOrEmpty(passName) ? i.ToString() : passName);
}
return passNames;
}
}
}