-
Notifications
You must be signed in to change notification settings - Fork 871
Expand file tree
/
Copy pathVolumeProfile.cs
More file actions
377 lines (339 loc) · 14.4 KB
/
VolumeProfile.cs
File metadata and controls
377 lines (339 loc) · 14.4 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.Assertions;
namespace UnityEngine.Rendering
{
/// <summary>
/// An Asset which holds a set of settings to use with a <see cref="Volume"/>.
/// </summary>
[CurrentPipelineHelpURL("Volume-Profile")]
[Icon("Packages/com.unity.render-pipelines.core/Editor/Icons/Processed/VolumeProfile Icon.asset")]
public sealed class VolumeProfile : ScriptableObject
{
/// <summary>
/// A list of every setting that this Volume Profile stores.
/// </summary>
public List<VolumeComponent> components = new List<VolumeComponent>();
/// <summary>
/// **Note**: For Internal Use Only<br/>
/// A dirty check used to redraw the profile inspector when something has changed. This is
/// currently only used in the editor.
/// </summary>
[Obsolete("This field was only public for editor access. #from(6000.0)")]
public bool isDirty
{
get => dirtyState != DirtyState.None;
set
{
if (value)
dirtyState |= DirtyState.Other;
else
dirtyState &= ~DirtyState.Other;
}
}
[Flags] internal enum DirtyState
{
None = 0,
DirtyByComponentChange = 1,
DirtyByProfileReset = 2,
Other = 4
}
internal DirtyState dirtyState;
void OnEnable()
{
// Make sure every setting is valid. If a profile holds a script that doesn't exist
// anymore, nuke it to keep the volume clean. Note that if you delete a script that is
// currently in use in a volume you'll still get a one-time error in the console, it's
// harmless and happens because Unity does a redraw of the editor (and thus the current
// frame) before the recompilation step.
components.RemoveAll(x => x == null);
}
// The lifetime of ScriptableObjects is different from MonoBehaviours. When the last reference to a
// VolumeProfile goes out of scope (e.g. when a scene containing Volume components is unloaded), Unity will call
// OnDisable() on the VolumeProfile. We need to release the internal resources in this case to avoid leaks.
internal void OnDisable()
{
if (components == null)
return;
for (int i = 0; i < components.Count; i++)
{
if (components[i] != null)
components[i].Release();
}
}
/// <summary>
/// Resets the dirty state of the Volume Profile. Unity uses this to force-refresh and redraw the
/// Volume Profile editor when you modify the Asset via script instead of the Inspector.
/// </summary>
public void Reset()
=> dirtyState |= DirtyState.DirtyByProfileReset;
/// <summary>
/// Adds a <see cref="VolumeComponent"/> to this Volume Profile.
/// </summary>
/// <remarks>
/// You can only have a single component of the same type per Volume Profile.
/// </remarks>
/// <typeparam name="T">A type of <see cref="VolumeComponent"/>.</typeparam>
/// <param name="overrides">Specifies whether Unity should automatically override all the settings when
/// you add a <see cref="VolumeComponent"/> to the Volume Profile.</param>
/// <returns>The instance for the given type that you added to the Volume Profile</returns>
/// <seealso cref="Add"/>
public T Add<T>(bool overrides = false)
where T : VolumeComponent
{
return (T)Add(typeof(T), overrides);
}
/// <summary>
/// Adds a <see cref="VolumeComponent"/> to this Volume Profile.
/// </summary>
/// <remarks>
/// You can only have a single component of the same type per Volume Profile.
/// </remarks>
/// <param name="type">A type that inherits from <see cref="VolumeComponent"/>.</param>
/// <param name="overrides">Specifies whether Unity should automatically override all the settings when
/// you add a <see cref="VolumeComponent"/> to the Volume Profile.</param>
/// <returns>The instance created for the given type that has been added to the profile</returns>
/// <seealso cref="Add{T}"/>
public VolumeComponent Add(Type type, bool overrides = false)
{
if (Has(type))
throw new InvalidOperationException("Component already exists in the volume");
var component = (VolumeComponent)CreateInstance(type);
#if UNITY_EDITOR
component.hideFlags = HideFlags.HideInInspector | HideFlags.HideInHierarchy;
component.name = type.Name;
#endif
component.SetAllOverridesTo(overrides);
components.Add(component);
dirtyState |= DirtyState.DirtyByComponentChange;
return component;
}
/// <summary>
/// Removes a <see cref="VolumeComponent"/> from this Volume Profile.
/// </summary>
/// <remarks>
/// This method does nothing if the type does not exist in the Volume Profile.
/// </remarks>
/// <typeparam name="T">A type of <see cref="VolumeComponent"/>.</typeparam>
/// <seealso cref="Remove"/>
public void Remove<T>()
where T : VolumeComponent
{
Remove(typeof(T));
}
/// <summary>
/// Removes a <see cref="VolumeComponent"/> from this Volume Profile.
/// </summary>
/// <remarks>
/// This method does nothing if the type does not exist in the Volume Profile.
/// </remarks>
/// <param name="type">A type that inherits from <see cref="VolumeComponent"/>.</param>
/// <seealso cref="Remove{T}"/>
public void Remove(Type type)
{
int toRemove = -1;
for (int i = 0; i < components.Count; i++)
{
if (components[i].GetType() == type)
{
toRemove = i;
break;
}
}
if (toRemove >= 0)
{
components.RemoveAt(toRemove);
dirtyState |= DirtyState.DirtyByComponentChange;
}
}
/// <summary>
/// Checks if this Volume Profile contains the <see cref="VolumeComponent"/> you pass in.
/// </summary>
/// <typeparam name="T">A type of <see cref="VolumeComponent"/>.</typeparam>
/// <returns><c>true</c> if the <see cref="VolumeComponent"/> exists in the Volume Profile,
/// <c>false</c> otherwise.</returns>
/// <seealso cref="Has"/>
/// <seealso cref="HasSubclassOf"/>
public bool Has<T>()
where T : VolumeComponent
{
return Has(typeof(T));
}
/// <summary>
/// Checks if this Volume Profile contains the <see cref="VolumeComponent"/> you pass in.
/// </summary>
/// <param name="type">A type that inherits from <see cref="VolumeComponent"/>.</param>
/// <returns><c>true</c> if the <see cref="VolumeComponent"/> exists in the Volume Profile,
/// <c>false</c> otherwise.</returns>
/// <seealso cref="Has{T}"/>
/// <seealso cref="HasSubclassOf"/>
public bool Has(Type type)
{
foreach (var component in components)
{
if (component.GetType() == type)
return true;
}
return false;
}
/// <summary>
/// Checks if this Volume Profile contains the <see cref="VolumeComponent"/>, which is a subclass of <paramref name="type"/>,
/// that you pass in.
/// </summary>
/// <param name="type">A type that inherits from <see cref="VolumeComponent"/>.</param>
/// <returns><c>true</c> if the <see cref="VolumeComponent"/> exists in the Volume Profile,
/// <c>false</c> otherwise.</returns>
/// <seealso cref="Has"/>
/// <seealso cref="Has{T}"/>
public bool HasSubclassOf(Type type)
{
foreach (var component in components)
{
if (component.GetType().IsSubclassOf(type))
return true;
}
return false;
}
/// <summary>
/// Gets the <see cref="VolumeComponent"/> of the specified type, if it exists.
/// </summary>
/// <typeparam name="T">A type of <see cref="VolumeComponent"/>.</typeparam>
/// <param name="component">The output argument that contains the <see cref="VolumeComponent"/>
/// or <c>null</c>.</param>
/// <returns><c>true</c> if the <see cref="VolumeComponent"/> is in the Volume Profile,
/// <c>false</c> otherwise.</returns>
/// <seealso cref="TryGet{T}(Type, out T)"/>
/// <seealso cref="TryGetSubclassOf{T}"/>
/// <seealso cref="TryGetAllSubclassOf{T}"/>
public bool TryGet<T>(out T component)
where T : VolumeComponent
{
return TryGet(typeof(T), out component);
}
/// <summary>
/// Gets the <see cref="VolumeComponent"/> of the specified type, if it exists.
/// </summary>
/// <typeparam name="T">A type of <see cref="VolumeComponent"/></typeparam>
/// <param name="type">A type that inherits from <see cref="VolumeComponent"/>.</param>
/// <param name="component">The output argument that contains the <see cref="VolumeComponent"/>
/// or <c>null</c>.</param>
/// <returns><c>true</c> if the <see cref="VolumeComponent"/> is in the Volume Profile,
/// <c>false</c> otherwise.</returns>
/// <seealso cref="TryGet{T}(out T)"/>
/// <seealso cref="TryGetSubclassOf{T}"/>
/// <seealso cref="TryGetAllSubclassOf{T}"/>
public bool TryGet<T>(Type type, out T component)
where T : VolumeComponent
{
component = null;
foreach (var comp in components)
{
if (comp.GetType() == type)
{
component = (T)comp;
return true;
}
}
return false;
}
/// <summary>
/// Gets the <see cref="VolumeComponent"/>, which is a subclass of <paramref name="type"/>, if
/// it exists.
/// </summary>
/// <typeparam name="T">A type of <see cref="VolumeComponent"/>.</typeparam>
/// <param name="type">A type that inherits from <see cref="VolumeComponent"/>.</param>
/// <param name="component">The output argument that contains the <see cref="VolumeComponent"/>
/// or <c>null</c>.</param>
/// <returns><c>true</c> if the <see cref="VolumeComponent"/> is in the Volume Profile,
/// <c>false</c> otherwise.</returns>
/// <seealso cref="TryGet{T}(Type, out T)"/>
/// <seealso cref="TryGet{T}(out T)"/>
/// <seealso cref="TryGetAllSubclassOf{T}"/>
public bool TryGetSubclassOf<T>(Type type, out T component)
where T : VolumeComponent
{
component = null;
foreach (var comp in components)
{
if (comp.GetType().IsSubclassOf(type))
{
component = (T)comp;
return true;
}
}
return false;
}
/// <summary>
/// Gets all the <see cref="VolumeComponent"/> that are subclasses of the specified type,
/// if there are any.
/// </summary>
/// <typeparam name="T">A type of <see cref="VolumeComponent"/>.</typeparam>
/// <param name="type">A type that inherits from <see cref="VolumeComponent"/>.</param>
/// <param name="result">The output list that contains all the <see cref="VolumeComponent"/>
/// if any. Note that Unity does not clear this list.</param>
/// <returns><c>true</c> if any <see cref="VolumeComponent"/> have been found in the profile,
/// <c>false</c> otherwise.</returns>
/// <seealso cref="TryGet{T}(Type, out T)"/>
/// <seealso cref="TryGet{T}(out T)"/>
/// <seealso cref="TryGetSubclassOf{T}"/>
public bool TryGetAllSubclassOf<T>(Type type, List<T> result)
where T : VolumeComponent
{
Assert.IsNotNull(components);
int count = result.Count;
foreach (var comp in components)
{
if (comp.GetType().IsSubclassOf(type))
result.Add((T)comp);
}
return count != result.Count;
}
/// <summary>
/// A custom hashing function that Unity uses to compare the state of parameters.
/// </summary>
/// <returns>A computed hash code for the current instance.</returns>
public override int GetHashCode()
{
unchecked
{
int hash = 17;
for (int i = 0; i < components.Count; i++)
hash = hash * 23 + components[i].GetHashCode();
return hash;
}
}
internal int GetComponentListHashCode()
{
unchecked
{
int hash = 17;
for (int i = 0; i < components.Count; i++)
hash = hash * 23 + components[i].GetType().GetHashCode();
return hash;
}
}
/// <summary>
/// Removes any components that were destroyed externally from the iternal list of components
/// </summary>
internal void Sanitize()
{
for (int i = components.Count - 1; i >= 0; i--)
if (components[i] == null)
components.RemoveAt(i);
}
#if UNITY_EDITOR
void OnValidate()
{
// Delay the callback because when undoing the deletion of a VolumeComponent from a profile,
// it's possible VolumeComponent.OnEnable() has not yet been called, resulting in a crash when trying to
// update the default state.
EditorApplication.delayCall += () =>
{
if (VolumeManager.instance.isInitialized)
VolumeManager.instance.OnVolumeProfileChanged(this);
};
}
#endif
}
}