using System;
using System.Collections.Generic;
namespace UnityEngine.Rendering
{
///
/// Holds the state of a Volume blending update. A global stack is
/// available by default in but you can also create your own using
/// if you need to update the manager with specific
/// settings and store the results for later use.
///
public sealed class VolumeStack : IDisposable
{
// Holds the state of _all_ component types you can possibly add on volumes
internal readonly Dictionary components = new();
// Holds the default value for every volume parameter for faster per-frame stack reset.
internal (VolumeParameter parameter, VolumeParameter defaultValue)[] defaultParameters;
internal bool requiresReset = true;
internal VolumeStack()
{
}
internal void Clear()
{
foreach (var component in components)
CoreUtils.Destroy(component.Value);
components.Clear();
if (defaultParameters != null)
{
foreach (var tuple in defaultParameters)
{
tuple.defaultValue?.Release();
}
defaultParameters = null;
}
}
internal void Reload(List componentDefaultStates)
{
Clear();
requiresReset = true;
List<(VolumeParameter parameter, VolumeParameter defaultValue)> defaultParametersList = new();
foreach (var defaultVolumeComponent in componentDefaultStates)
{
var type = defaultVolumeComponent.GetType();
var component = (VolumeComponent)ScriptableObject.CreateInstance(type);
components.Add(type, component);
for (int i = 0; i < component.parameterList.Count; i++)
{
defaultParametersList.Add(new()
{
parameter = component.parameters[i],
defaultValue = defaultVolumeComponent.parameterList[i].Clone() as VolumeParameter,
});
}
}
defaultParameters = defaultParametersList.ToArray();
}
///
/// Gets the current state of the of type
/// in the stack.
///
/// A type of .
/// The current state of the of type
/// in the stack.
public T GetComponent()
where T : VolumeComponent
{
var comp = GetComponent(typeof(T));
return (T)comp;
}
///
/// Gets the current state of the of the specified type in the
/// stack.
///
/// The type of to look for.
/// The current state of the of the specified type,
/// or null if the type is invalid.
public VolumeComponent GetComponent(Type type)
{
components.TryGetValue(type, out var comp);
return comp;
}
///
/// Cleans up the content of this stack. Once a VolumeStack is disposed, it souldn't
/// be used anymore.
///
public void Dispose()
{
Clear();
}
}
}