A game about forced loneliness, made by TACStudios
1using System; 2using System.Collections.Generic; 3 4namespace UnityEngine.Rendering 5{ 6 /// <summary> 7 /// Holds the state of a Volume blending update. A global stack is 8 /// available by default in <see cref="VolumeManager"/> but you can also create your own using 9 /// <see cref="VolumeManager.CreateStack"/> if you need to update the manager with specific 10 /// settings and store the results for later use. 11 /// </summary> 12 public sealed class VolumeStack : IDisposable 13 { 14 // Holds the state of _all_ component types you can possibly add on volumes 15 internal readonly Dictionary<Type, VolumeComponent> components = new(); 16 17 // Flat list of every volume parameter for faster per-frame stack reset. 18 internal VolumeParameter[] parameters; 19 20 // Flag indicating that some properties have received overrides, therefore they must be reset in the next update. 21 internal bool requiresReset = true; 22 23 // Flag indicating that default state has changed, therefore all properties in the stack must be reset in the next update. 24 internal bool requiresResetForAllProperties = true; 25 26 internal VolumeStack() 27 { 28 } 29 30 internal void Clear() 31 { 32 foreach (var component in components) 33 CoreUtils.Destroy(component.Value); 34 35 components.Clear(); 36 37 parameters = null; 38 } 39 40 internal void Reload(Type[] componentTypes) 41 { 42 Clear(); 43 44 requiresReset = true; 45 requiresResetForAllProperties = true; 46 47 List<VolumeParameter> parametersList = new(); 48 foreach (var type in componentTypes) 49 { 50 var component = (VolumeComponent)ScriptableObject.CreateInstance(type); 51 components.Add(type, component); 52 parametersList.AddRange(component.parameters); 53 } 54 55 parameters = parametersList.ToArray(); 56 57 isValid = true; 58 } 59 60 /// <summary> 61 /// Gets the current state of the <see cref="VolumeComponent"/> of type <typeparamref name="T"/> 62 /// in the stack. 63 /// </summary> 64 /// <typeparam name="T">A type of <see cref="VolumeComponent"/>.</typeparam> 65 /// <returns>The current state of the <see cref="VolumeComponent"/> of type <typeparamref name="T"/> 66 /// in the stack.</returns> 67 public T GetComponent<T>() 68 where T : VolumeComponent 69 { 70 var comp = GetComponent(typeof(T)); 71 return (T)comp; 72 } 73 74 /// <summary> 75 /// Gets the current state of the <see cref="VolumeComponent"/> of the specified type in the 76 /// stack. 77 /// </summary> 78 /// <param name="type">The type of <see cref="VolumeComponent"/> to look for.</param> 79 /// <returns>The current state of the <see cref="VolumeComponent"/> of the specified type, 80 /// or <c>null</c> if the type is invalid.</returns> 81 public VolumeComponent GetComponent(Type type) 82 { 83 components.TryGetValue(type, out var comp); 84 return comp; 85 } 86 87 /// <summary> 88 /// Cleans up the content of this stack. Once a <c>VolumeStack</c> is disposed, it shouldn't 89 /// be used anymore. 90 /// </summary> 91 public void Dispose() 92 { 93 Clear(); 94 95 isValid = false; 96 } 97 98 /// <summary> 99 /// Check if the stack is in valid state and can be used. 100 /// </summary> 101 public bool isValid { get; private set; } 102 } 103}