This commit is contained in:
2024-09-20 20:30:10 +02:00
commit 4fabf1a6fd
29169 changed files with 1706941 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
using System.Collections.Generic;
namespace UnityEngine.Rendering
{
/// <summary>
/// An interface for Volumes
/// </summary>
public interface IVolume
{
/// <summary>
/// Specifies whether to apply the Volume to the entire Scene or not.
/// </summary>
bool isGlobal { get; set; }
/// <summary>
/// The colliders of the volume if <see cref="isGlobal"/> is false
/// </summary>
List<Collider> colliders { get; }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ff60b2884be449c4c84d8b77cdee9422
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,319 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Unity.Collections;
using UnityEngine.Assertions;
namespace UnityEngine.Rendering
{
/// <summary>
/// A helper function for interpolating AnimationCurves together. In general, curves can not be directly blended
/// because they will have keypoints at different places. InterpAnimationCurve traverses through the keypoints.
/// If both curves have a keypoint at the same time, they keypoints are trivially lerped together. However
/// if one curve has a keypoint at a time that is missing in the other curve (which is the most common case),
/// InterpAnimationCurve calculates a synthetic keypoint at that time based on value and derivative, and interpolates
/// the resulting keys.
/// Note that this function should only be called by internal rendering code. It creates a small pool of animation
/// curves and reuses them to avoid creating garbage. The number of curves needed is quite small, since curves only need
/// to be used when interpolating multiple volumes together with different curve parameters. The underlying interp
/// function isn't allowed to fail, so in the case where we run out of memory we fall back to returning a single keyframe.
/// </summary>
///
/// <example>Example:
/// <code>
/// {
/// AnimationCurve curve0 = new AnimationCurve();
/// curve0.AddKey(new Keyframe(0.0f, 3.0f));
/// curve0.AddKey(new Keyframe(4.0f, 2.0f));
///
/// AnimationCurve curve1 = new AnimationCurve();
/// curve1.AddKey(new Keyframe(0.0f, 0.0f));
/// curve1.AddKey(new Keyframe(2.0f, 1.0f));
/// curve1.AddKey(new Keyframe(4.0f, 4.0f));
///
/// float t = 0.5f;
/// KeyframeUtility.InterpAnimationCurve(curve0, curve1, t);
///
/// // curve0 now stores the resulting interpolated curve
/// }
/// </code>
/// </example>
public class KeyframeUtility
{
/// <summary>
/// Helper function to remove all control points for an animation curve. Since animation curves are reused in a pool,
/// this function clears existing keys so the curve is ready for reuse.
/// </summary>
/// <param name="curve">The curve to reset.</param>
static public void ResetAnimationCurve(AnimationCurve curve)
{
curve.ClearKeys();
}
static private Keyframe LerpSingleKeyframe(Keyframe lhs, Keyframe rhs, float t)
{
var ret = new Keyframe();
ret.time = Mathf.Lerp(lhs.time, rhs.time, t);
ret.value = Mathf.Lerp(lhs.value, rhs.value, t);
ret.inTangent = Mathf.Lerp(lhs.inTangent, rhs.inTangent, t);
ret.outTangent = Mathf.Lerp(lhs.outTangent, rhs.outTangent, t);
ret.inWeight = Mathf.Lerp(lhs.inWeight, rhs.inWeight, t);
ret.outWeight = Mathf.Lerp(lhs.outWeight, rhs.outWeight, t);
// it's not possible to lerp the weightedMode, so use the lhs mode.
ret.weightedMode = lhs.weightedMode;
// Note: ret.tangentMode is deprecated, so we will use the value from the constructor
return ret;
}
/// In an animation curve, the inTangent and outTangent don't match the edge of the curve. For example,
/// the first key might have inTangent=3.0f but the actual incoming tangent is 0.0 because the curve is
/// clamped outside the time domain. So this helper fetches a key, but zeroes out the inTangent of the first
/// key and the outTangent of the last key.
static private Keyframe GetKeyframeAndClampEdge([DisallowNull] NativeArray<Keyframe> keys, int index)
{
var lastKeyIndex = keys.Length - 1;
if (index < 0 || index > lastKeyIndex)
{
Debug.LogWarning("Invalid index in GetKeyframeAndClampEdge. This is likely a bug.");
return new Keyframe();
}
var currKey = keys[index];
if (index == 0)
{
currKey.inTangent = 0.0f;
}
if (index == lastKeyIndex)
{
currKey.outTangent = 0.0f;
}
return currKey;
}
/// Fetch a key from the keys list. If index<0, then expand the first key backwards to startTime. If index>=keys.length,
/// then extend the last key to endTime. Keys must be a valid array with at least one element.
static private Keyframe FetchKeyFromIndexClampEdge([DisallowNull] NativeArray<Keyframe> keys, int index, float segmentStartTime, float segmentEndTime)
{
float startTime = Mathf.Min(segmentStartTime, keys[0].time);
float endTime = Mathf.Max(segmentEndTime, keys[keys.Length - 1].time);
float startValue = keys[0].value;
float endValue = keys[keys.Length - 1].value;
// In practice, we are lerping animcurves for post processing curves that are always clamping at the begining and the end,
// so we are not implementing the other wrap modes like Loop, PingPong, etc.
Keyframe ret;
if (index < 0)
{
// when you are at a time either before the curve start time the value is clamped to the start time and the input tangent is ignored.
ret = new Keyframe(startTime, startValue, 0.0f, 0.0f);
}
else if (index >= keys.Length)
{
// if we are after the end of the curve, there slope is always zero just like before the start of a curve
var lastKey = keys[keys.Length - 1];
ret = new Keyframe(endTime, endValue, 0.0f, 0.0f);
}
else
{
// only remaining case is that we have a proper index
ret = GetKeyframeAndClampEdge(keys, index);
}
return ret;
}
/// Given a desiredTime, interpoloate between two keys to find the value and derivative. This function assumes that lhsKey.time <= desiredTime <= rhsKey.time,
/// but will return a reasonable float value if that's not the case.
static private void EvalCurveSegmentAndDeriv(out float dstValue, out float dstDeriv, Keyframe lhsKey, Keyframe rhsKey, float desiredTime)
{
// This is the same epsilon used internally
const float epsilon = 0.0001f;
float currTime = Mathf.Clamp(desiredTime, lhsKey.time, rhsKey.time);
// (lhsKey.time <= rhsKey.time) should always be true. But theoretically, if garbage values get passed in, the value would
// be clamped here to epsilon, and we would still end up with a reasonable value for dx.
float dx = Mathf.Max(rhsKey.time - lhsKey.time, epsilon);
float dy = rhsKey.value - lhsKey.value;
float length = 1.0f / dx;
float lengthSqr = length * length;
float m1 = lhsKey.outTangent;
float m2 = rhsKey.inTangent;
float d1 = m1 * dx;
float d2 = m2 * dx;
// Note: The coeffecients are calculated to match what the editor does internally. These coeffeceients expect a
// t in the range of [0,dx]. We could change the function to accept a range between [0,1], but then this logic would
// be different from internal editor logic which could cause subtle bugs later.
float c0 = (d1 + d2 - dy - dy) * lengthSqr * length;
float c1 = (dy + dy + dy - d1 - d1 - d2) * lengthSqr;
float c2 = m1;
float c3 = lhsKey.value;
float t = Mathf.Clamp(currTime - lhsKey.time, 0.0f, dx);
dstValue = (t * (t * (t * c0 + c1) + c2)) + c3;
dstDeriv = (t * (3.0f * t * c0 + 2.0f * c1)) + c2;
}
/// lhsIndex and rhsIndex are the indices in the keys array. The lhsIndex/rhsIndex may be -1, in which it creates a synthetic first key
/// at startTime, or beyond the length of the array, in which case it creates a synthetic key at endTime.
static private Keyframe EvalKeyAtTime([DisallowNull] NativeArray<Keyframe> keys, int lhsIndex, int rhsIndex, float startTime, float endTime, float currTime)
{
var lhsKey = KeyframeUtility.FetchKeyFromIndexClampEdge(keys, lhsIndex, startTime, endTime);
var rhsKey = KeyframeUtility.FetchKeyFromIndexClampEdge(keys, rhsIndex, startTime, endTime);
float currValue;
float currDeriv;
KeyframeUtility.EvalCurveSegmentAndDeriv(out currValue, out currDeriv, lhsKey, rhsKey, currTime);
return new Keyframe(currTime, currValue, currDeriv, currDeriv);
}
/// <summary>
/// Interpolates two AnimationCurves. Since both curves likely have control points at different places
/// in the curve, this method will create a new curve from the union of times between both curves. However, to avoid creating
/// garbage, this function will always replace the keys of lhsAndResultCurve with the final result, and return lhsAndResultCurve.
/// </summary>
/// <param name="lhsAndResultCurve">The start value. Additionaly, this instance will be reused and returned as the result.</param>
/// <param name="rhsCurve">The end value.</param>
/// <param name="t">The interpolation factor in range [0,1].</param>
static public void InterpAnimationCurve(ref AnimationCurve lhsAndResultCurve, [DisallowNull] AnimationCurve rhsCurve, float t)
{
if (t <= 0.0f || rhsCurve.length == 0)
{
// no op. lhsAndResultCurve is already the result
}
else if (t >= 1.0f || lhsAndResultCurve.length == 0)
{
// In this case the obvious solution would be to return the rhsCurve. BUT (!) the lhsCurve and rhsCurve are different. This function is
// called by:
// stateParam.Interp(stateParam, toParam, interpFactor);
//
// stateParam (lhsCurve) is a temporary in/out parameter, but toParam (rhsCurve) might point to the original component, so it's unsafe to
// change that data. Thus, we need to copy the keys from the rhsCurve to the lhsCurve instead of returning rhsCurve.
lhsAndResultCurve.CopyFrom(rhsCurve);
}
else
{
// Note: If we reached this code, we are guaranteed that both lhsCurve and rhsCurve are valid with at least 1 key
// create a native array for the temp keys to avoid GC
var lhsCurveKeys = new NativeArray<Keyframe>(lhsAndResultCurve.length, Allocator.Temp);
var rhsCurveKeys = new NativeArray<Keyframe>(rhsCurve.length, Allocator.Temp);
for (int i = 0; i < lhsAndResultCurve.length; i++)
{
lhsCurveKeys[i] = lhsAndResultCurve[i];
}
for (int i = 0; i < rhsCurve.length; i++)
{
rhsCurveKeys[i] = rhsCurve[i];
}
float startTime = Mathf.Min(lhsCurveKeys[0].time, rhsCurveKeys[0].time);
float endTime = Mathf.Max(lhsCurveKeys[lhsAndResultCurve.length - 1].time, rhsCurveKeys[rhsCurve.length - 1].time);
// we don't know how many keys the resulting curve will have (because we will compact keys that are at the exact
// same time), but in most cases we will need the worst case number of keys. So allocate the worst case.
int maxNumKeys = lhsAndResultCurve.length + rhsCurve.length;
int currNumKeys = 0;
var dstKeys = new NativeArray<Keyframe>(maxNumKeys, Allocator.Temp);
int lhsKeyCurr = 0;
int rhsKeyCurr = 0;
while (lhsKeyCurr < lhsCurveKeys.Length || rhsKeyCurr < rhsCurveKeys.Length)
{
// the index is considered invalid once it goes off the end of the array
bool lhsValid = lhsKeyCurr < lhsCurveKeys.Length;
bool rhsValid = rhsKeyCurr < rhsCurveKeys.Length;
// it's actually impossible for lhsKey/rhsKey to be uninitialized, but have to
// add initialize here to prevent compiler erros
var lhsKey = new Keyframe();
var rhsKey = new Keyframe();
if (lhsValid && rhsValid)
{
lhsKey = GetKeyframeAndClampEdge(lhsCurveKeys, lhsKeyCurr);
rhsKey = GetKeyframeAndClampEdge(rhsCurveKeys, rhsKeyCurr);
if (lhsKey.time == rhsKey.time)
{
lhsKeyCurr++;
rhsKeyCurr++;
}
else if (lhsKey.time < rhsKey.time)
{
// in this case:
// rhsKey[curr-1].time <= lhsKey.time <= rhsKey[curr].time
// so interpolate rhsKey at the lhsKey.time.
rhsKey = KeyframeUtility.EvalKeyAtTime(rhsCurveKeys, rhsKeyCurr - 1, rhsKeyCurr, startTime, endTime, lhsKey.time);
lhsKeyCurr++;
}
else
{
// only case left is (lhsKey.time > rhsKey.time)
Assert.IsTrue(lhsKey.time > rhsKey.time);
// this is the reverse of the lhs key case
// lhsKey[curr-1].time <= rhsKey.time <= lhsKey[curr].time
// so interpolate lhsKey at the rhsKey.time.
lhsKey = KeyframeUtility.EvalKeyAtTime(lhsCurveKeys, lhsKeyCurr - 1, lhsKeyCurr, startTime, endTime, rhsKey.time);
rhsKeyCurr++;
}
}
else if (lhsValid)
{
// we are still processing lhsKeys, but we are out of rhsKeys, so increment lhs and evaluate rhs
lhsKey = GetKeyframeAndClampEdge(lhsCurveKeys, lhsKeyCurr);
// rhs will be evaluated between the last rhs key and the extrapolated rhs key at the end time
rhsKey = KeyframeUtility.EvalKeyAtTime(rhsCurveKeys, rhsKeyCurr - 1, rhsKeyCurr, startTime, endTime, lhsKey.time);
lhsKeyCurr++;
}
else
{
// either lhsValid is True, rhsValid is True, or they are both True. So to miss the first two cases,
// right here rhsValid must be true.
Assert.IsTrue(rhsValid);
// we still have rhsKeys to lerp, but we are out of lhsKeys, to increment rhs and evaluate lhs
rhsKey = GetKeyframeAndClampEdge(rhsCurveKeys, rhsKeyCurr);
// lhs will be evaluated between the last lhs key and the extrapolated lhs key at the end time
lhsKey = KeyframeUtility.EvalKeyAtTime(lhsCurveKeys, lhsKeyCurr - 1, lhsKeyCurr, startTime, endTime, rhsKey.time);
rhsKeyCurr++;
}
var dstKey = KeyframeUtility.LerpSingleKeyframe(lhsKey, rhsKey, t);
dstKeys[currNumKeys] = dstKey;
currNumKeys++;
}
// Replace the keys in lhsAndResultCurve with our interpolated curve.
KeyframeUtility.ResetAnimationCurve(lhsAndResultCurve);
for (int i = 0; i < currNumKeys; i++)
{
lhsAndResultCurve.AddKey(dstKeys[i]);
}
dstKeys.Dispose();
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1e28576c73484c844b4b56a5352abe6f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,165 @@
using System.Collections.Generic;
using UnityEngine.Serialization;
namespace UnityEngine.Rendering
{
/// <summary>
/// A generic Volume component holding a <see cref="VolumeProfile"/>.
/// </summary>
[CoreRPHelpURL("Volumes", "com.unity.render-pipelines.high-definition")]
[ExecuteAlways]
[AddComponentMenu("Miscellaneous/Volume")]
public class Volume : MonoBehaviour, IVolume
{
[SerializeField, FormerlySerializedAs("isGlobal")]
private bool m_IsGlobal = true;
/// <summary>
/// Specifies whether to apply the Volume to the entire Scene or not.
/// </summary>
[Tooltip("When enabled, the Volume is applied to the entire Scene.")]
public bool isGlobal
{
get => m_IsGlobal;
set => m_IsGlobal = value;
}
/// <summary>
/// A value which determines which Volume is being used when Volumes have an equal amount of influence on the Scene. Volumes with a higher priority will override lower ones.
/// </summary>
[Tooltip("A value which determines which Volume is being used when Volumes have an equal amount of influence on the Scene. Volumes with a higher priority will override lower ones.")]
[Delayed]
public float priority = 0f;
/// <summary>
/// The outer distance to start blending from. A value of 0 means no blending and Unity applies
/// the Volume overrides immediately upon entry.
/// </summary>
[Tooltip("Sets the outer distance to start blending from. A value of 0 means no blending and Unity applies the Volume overrides immediately upon entry.")]
public float blendDistance = 0f;
/// <summary>
/// The total weight of this volume in the Scene. 0 means no effect and 1 means full effect.
/// </summary>
[Range(0f, 1f), Tooltip("Sets the total weight of this Volume in the Scene. 0 means no effect and 1 means full effect.")]
public float weight = 1f;
/// <summary>
/// The shared Profile that this Volume uses.
/// Modifying <c>sharedProfile</c> changes every Volumes that uses this Profile and also changes
/// the Profile settings stored in the Project.
/// </summary>
/// <remarks>
/// You should not modify Profiles that <c>sharedProfile</c> returns. If you want
/// to modify the Profile of a Volume, use <see cref="profile"/> instead.
/// </remarks>
/// <seealso cref="profile"/>
public VolumeProfile sharedProfile = null;
/// <summary>
/// Gets the first instantiated <see cref="VolumeProfile"/> assigned to the Volume.
/// Modifying <c>profile</c> changes the Profile for this Volume only. If another Volume
/// uses the same Profile, this clones the shared Profile and starts using it from now on.
/// </summary>
/// <remarks>
/// This property automatically instantiates the Profile and make it unique to this Volume
/// so you can safely edit it via scripting at runtime without changing the original Asset
/// in the Project.
/// Note that if you pass your own Profile, you must destroy it when you finish using it.
/// </remarks>
/// <seealso cref="sharedProfile"/>
public VolumeProfile profile
{
get
{
if (m_InternalProfile == null)
{
m_InternalProfile = ScriptableObject.CreateInstance<VolumeProfile>();
if (sharedProfile != null)
{
m_InternalProfile.name = sharedProfile.name;
foreach (var item in sharedProfile.components)
{
var itemCopy = Instantiate(item);
m_InternalProfile.components.Add(itemCopy);
}
}
}
return m_InternalProfile;
}
set => m_InternalProfile = value;
}
internal List<Collider> m_Colliders = new List<Collider>();
/// <summary>
/// The colliders of the volume if <see cref="isGlobal"/> is false
/// </summary>
public List<Collider> colliders => m_Colliders;
internal VolumeProfile profileRef => m_InternalProfile == null ? sharedProfile : m_InternalProfile;
/// <summary>
/// Checks if the Volume has an instantiated Profile or if it uses a shared Profile.
/// </summary>
/// <returns><c>true</c> if the profile has been instantiated.</returns>
/// <seealso cref="profile"/>
/// <seealso cref="sharedProfile"/>
public bool HasInstantiatedProfile() => m_InternalProfile != null;
// Needed for state tracking (see the comments in Update)
int m_PreviousLayer;
float m_PreviousPriority;
VolumeProfile m_InternalProfile;
void OnEnable()
{
m_PreviousLayer = gameObject.layer;
VolumeManager.instance.Register(this, m_PreviousLayer);
GetComponents(m_Colliders);
}
void OnDisable()
{
VolumeManager.instance.Unregister(this, gameObject.layer);
}
void Update()
{
// Unfortunately we need to track the current layer to update the volume manager in
// real-time as the user could change it at any time in the editor or at runtime.
// Because no event is raised when the layer changes, we have to track it on every
// frame :/
UpdateLayer();
// Same for priority. We could use a property instead, but it doesn't play nice with the
// serialization system. Using a custom Attribute/PropertyDrawer for a property is
// possible but it doesn't work with Undo/Redo in the editor, which makes it useless for
// our case.
if (priority != m_PreviousPriority)
{
VolumeManager.instance.SetLayerDirty(gameObject.layer);
m_PreviousPriority = priority;
}
#if UNITY_EDITOR
// In the editor, we refresh the list of colliders at every frame because it's frequent to add/remove them
GetComponents(m_Colliders);
#endif
}
internal void UpdateLayer()
{
int layer = gameObject.layer;
if (layer != m_PreviousLayer)
{
VolumeManager.instance.UpdateVolumeLayer(this, m_PreviousLayer, layer);
m_PreviousLayer = layer;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 172515602e62fb746b5d573b38a5fe58
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 188dfe7e559f13248ba2c41eb5a59328, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,42 @@
#if UNITY_EDITOR
using System;
using UnityEditor;
namespace UnityEngine.Rendering
{
public partial class VolumeComponent : IApplyRevertPropertyContextMenuItemProvider
{
public bool TryGetRevertMethodForFieldName(SerializedProperty property, out Action<SerializedProperty> revertMethod)
{
revertMethod = property =>
{
var defaultVolumeComponent = VolumeManager.instance.GetDefaultVolumeComponent(property.serializedObject.targetObject.GetType());
Undo.RecordObject(property.serializedObject.targetObject, $"Revert property {property.propertyPath} from {property.serializedObject}");
SerializedObject serializedObject = new SerializedObject(defaultVolumeComponent);
var serializedProperty = serializedObject.FindProperty(property.propertyPath);
property.serializedObject.CopyFromSerializedProperty(serializedProperty);
property.serializedObject.ApplyModifiedProperties();
};
return true;
}
public string GetSourceTerm()
{
return "Property";
}
public bool TryGetApplyMethodForFieldName(SerializedProperty property, out Action<SerializedProperty> applyMethod)
{
applyMethod = null;
return false;
}
public string GetSourceName(Component comp)
{
return string.Empty;
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9c62ee15c68690246a39d8c29726cb19
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,344 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
namespace UnityEngine.Rendering
{
/// <summary>
/// This attribute allows you to add commands to the <strong>Add Override</strong> popup menu
/// on Volumes.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class VolumeComponentMenu : Attribute
{
/// <summary>
/// The name of the entry in the override list. You can use slashes to create sub-menus.
/// </summary>
public readonly string menu;
// TODO: Add support for component icons
/// <summary>
/// Creates a new <seealso cref="VolumeComponentMenu"/> instance.
/// </summary>
/// <param name="menu">The name of the entry in the override list. You can use slashes to
/// create sub-menus.</param>
public VolumeComponentMenu(string menu)
{
this.menu = menu;
}
}
/// <summary>
/// This attribute allows you to add commands to the <strong>Add Override</strong> popup menu
/// on Volumes and specify for which render pipelines will be supported
/// </summary>
public class VolumeComponentMenuForRenderPipeline : VolumeComponentMenu
{
/// <summary>
/// The list of pipeline types that the target class supports
/// </summary>
public Type[] pipelineTypes { get; }
/// <summary>
/// Creates a new <seealso cref="VolumeComponentMenuForRenderPipeline"/> instance.
/// </summary>
/// <param name="menu">The name of the entry in the override list. You can use slashes to
/// create sub-menus.</param>
/// <param name="pipelineTypes">The list of pipeline types that the target class supports</param>
public VolumeComponentMenuForRenderPipeline(string menu, params Type[] pipelineTypes)
: base(menu)
{
if (pipelineTypes == null)
throw new Exception("Specify a list of supported pipeline");
// Make sure that we only allow the class types that inherit from the render pipeline
foreach (var t in pipelineTypes)
{
if (!typeof(RenderPipeline).IsAssignableFrom(t))
throw new Exception(
$"You can only specify types that inherit from {typeof(RenderPipeline)}, please check {t}");
}
this.pipelineTypes = pipelineTypes;
}
}
/// <summary>
/// An attribute to hide the volume component to be added through `Add Override` button on the volume component list
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
[Obsolete("VolumeComponentDeprecated has been deprecated (UnityUpgradable) -> [UnityEngine] UnityEngine.HideInInspector", false)]
public sealed class VolumeComponentDeprecated : Attribute
{
}
/// <summary>
/// The base class for all the components that can be part of a <see cref="VolumeProfile"/>.
/// The Volume framework automatically handles and interpolates any <see cref="VolumeParameter"/> members found in this class.
/// </summary>
/// <example>
/// <code>
/// using UnityEngine.Rendering;
///
/// [Serializable, VolumeComponentMenuForRenderPipeline("Custom/Example Component")]
/// public class ExampleComponent : VolumeComponent
/// {
/// public ClampedFloatParameter intensity = new ClampedFloatParameter(0f, 0f, 1f);
/// }
/// </code>
/// </example>
[Serializable]
public partial class VolumeComponent : ScriptableObject
{
/// <summary>
/// Local attribute for VolumeComponent fields only.
/// It handles relative indentation of a property for inspector.
/// </summary>
public sealed class Indent : PropertyAttribute
{
/// <summary> Relative indent amount registered in this atribute </summary>
public readonly int relativeAmount;
/// <summary> Constructor </summary>
/// <param name="relativeAmount">Relative indent change to use</param>
public Indent(int relativeAmount = 1)
=> this.relativeAmount = relativeAmount;
}
/// <summary>
/// The active state of the set of parameters defined in this class. You can use this to
/// quickly turn on or off all the overrides at once.
/// </summary>
public bool active = true;
/// <summary>
/// The name displayed in the component header. If you do not set a name, Unity generates one from
/// the class name automatically.
/// </summary>
public string displayName { get; protected set; } = "";
/// <summary>
/// The backing storage of <see cref="parameters"/>. Use this for performance-critical work.
/// </summary>
internal readonly List<VolumeParameter> parameterList = new();
ReadOnlyCollection<VolumeParameter> m_ParameterReadOnlyCollection;
/// <summary>
/// A read-only collection of all the <see cref="VolumeParameter"/>s defined in this class.
/// </summary>
public ReadOnlyCollection<VolumeParameter> parameters
{
get
{
if (m_ParameterReadOnlyCollection == null)
m_ParameterReadOnlyCollection = parameterList.AsReadOnly();
return m_ParameterReadOnlyCollection;
}
}
/// <summary>
/// Extracts all the <see cref="VolumeParameter"/>s defined in this class and nested classes.
/// </summary>
/// <param name="o">The object to find the parameters</param>
/// <param name="parameters">The list filled with the parameters.</param>
/// <param name="filter">If you want to filter the parameters</param>
internal static void FindParameters(object o, List<VolumeParameter> parameters, Func<FieldInfo, bool> filter = null)
{
if (o == null)
return;
var fields = o.GetType()
.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.OrderBy(t => t.MetadataToken); // Guaranteed order
foreach (var field in fields)
{
if (field.FieldType.IsSubclassOf(typeof(VolumeParameter)))
{
if (filter?.Invoke(field) ?? true)
{
VolumeParameter volumeParameter = (VolumeParameter)field.GetValue(o);
parameters.Add(volumeParameter);
}
}
else if (!field.FieldType.IsArray && field.FieldType.IsClass)
FindParameters(field.GetValue(o), parameters, filter);
}
}
/// <summary>
/// Unity calls this method when it loads the class.
/// </summary>
/// <remarks>
/// If you want to override this method, you must call <c>base.OnEnable()</c>.
/// </remarks>
protected virtual void OnEnable()
{
// Automatically grab all fields of type VolumeParameter for this instance
parameterList.Clear();
FindParameters(this, parameterList);
foreach (var parameter in parameterList)
{
if (parameter != null)
parameter.OnEnable();
else
Debug.LogWarning("Volume Component " + GetType().Name + " contains a null parameter; please make sure all parameters are initialized to a default value. Until this is fixed the null parameters will not be considered by the system.");
}
}
/// <summary>
/// Unity calls this method when the object goes out of scope.
/// </summary>
protected virtual void OnDisable()
{
foreach (var parameter in parameterList)
{
if (parameter != null)
parameter.OnDisable();
}
}
/// <summary>
/// Interpolates a <see cref="VolumeComponent"/> with this component by an interpolation
/// factor and puts the result back into the given <see cref="VolumeComponent"/>.
/// </summary>
/// <remarks>
/// You can override this method to do your own blending. Either loop through the
/// <see cref="parameters"/> list or reference direct fields. You should only use
/// <see cref="VolumeParameter.SetValue"/> to set parameter values and not assign
/// directly to the state object. you should also manually check
/// <see cref="VolumeParameter.overrideState"/> before you set any values.
/// </remarks>
/// <param name="state">The internal component to interpolate from. You must store
/// the result of the interpolation in this same component.</param>
/// <param name="interpFactor">The interpolation factor in range [0,1].</param>
/// <example>
/// Below is the default implementation for blending:
/// <code>
/// public virtual void Override(VolumeComponent state, float interpFactor)
/// {
/// int count = parameters.Count;
///
/// for (int i = 0; i &lt; count; i++)
/// {
/// var stateParam = state.parameters[i];
/// var toParam = parameters[i];
///
/// // Keep track of the override state for debugging purpose
/// stateParam.overrideState = toParam.overrideState;
///
/// if (toParam.overrideState)
/// stateParam.Interp(stateParam, toParam, interpFactor);
/// }
/// }
/// </code>
/// </example>
public virtual void Override(VolumeComponent state, float interpFactor)
{
int count = parameterList.Count;
for (int i = 0; i < count; i++)
{
var stateParam = state.parameterList[i];
var toParam = parameterList[i];
if (toParam.overrideState)
{
// Keep track of the override state for debugging purpose
stateParam.overrideState = toParam.overrideState;
stateParam.Interp(stateParam, toParam, interpFactor);
}
}
}
/// <summary>
/// Sets the state of all the overrides on this component to a given value.
/// </summary>
/// <param name="state">The value to set the state of the overrides to.</param>
public void SetAllOverridesTo(bool state)
{
SetOverridesTo(parameterList, state);
}
/// <summary>
/// Sets the override state of the given parameters on this component to a given value.
/// </summary>
/// <param name="state">The value to set the state of the overrides to.</param>
internal void SetOverridesTo(IEnumerable<VolumeParameter> enumerable, bool state)
{
foreach (var prop in enumerable)
{
prop.overrideState = state;
var t = prop.GetType();
if (VolumeParameter.IsObjectParameter(t))
{
// This method won't be called a lot but this is sub-optimal, fix me
var innerParams = (ReadOnlyCollection<VolumeParameter>)
t.GetProperty("parameters", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(prop, null);
if (innerParams != null)
SetOverridesTo(innerParams, state);
}
}
}
/// <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
{
//return parameters.Aggregate(17, (i, p) => i * 23 + p.GetHash());
int hash = 17;
for (int i = 0; i < parameterList.Count; i++)
hash = hash * 23 + parameterList[i].GetHashCode();
return hash;
}
}
/// <summary>
/// Returns true if any of the volume properites has been overridden.
/// </summary>
/// <returns>True if any of the volume properites has been overridden.</returns>
public bool AnyPropertiesIsOverridden()
{
for (int i = 0; i < parameterList.Count; ++i)
{
if (parameterList[i].overrideState) return true;
}
return false;
}
/// <summary>
/// Unity calls this method before the object is destroyed.
/// </summary>
protected virtual void OnDestroy() => Release();
/// <summary>
/// Releases all the allocated resources.
/// </summary>
public void Release()
{
if (parameterList == null)
return;
for (int i = 0; i < parameterList.Count; i++)
{
if (parameterList[i] != null)
parameterList[i].Release();
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 556096baecf4aeb4e9f0d3efce77e08f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,622 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEngine.Assertions;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.Rendering
{
using UnityObject = UnityEngine.Object;
/// <summary>
/// A global manager that tracks all the Volumes in the currently loaded Scenes and does all the
/// interpolation work.
/// </summary>
public sealed class VolumeManager
{
static readonly Lazy<VolumeManager> s_Instance = new Lazy<VolumeManager>(() => new VolumeManager());
/// <summary>
/// The current singleton instance of <see cref="VolumeManager"/>.
/// </summary>
public static VolumeManager instance => s_Instance.Value;
/// <summary>
/// A reference to the main <see cref="VolumeStack"/>.
/// </summary>
/// <seealso cref="VolumeStack"/>
public VolumeStack stack { get; set; }
/// <summary>
/// The current list of all available types that derive from <see cref="VolumeComponent"/>.
/// </summary>
[Obsolete("Please use baseComponentTypeArray instead.")]
public IEnumerable<Type> baseComponentTypes
{
get => baseComponentTypeArray;
private set => baseComponentTypeArray = value.ToArray();
}
static readonly Dictionary<Type, List<(string, Type)>> s_SupportedVolumeComponentsForRenderPipeline = new();
internal static List<(string, Type)> GetSupportedVolumeComponents(Type currentPipelineType)
{
if (s_SupportedVolumeComponentsForRenderPipeline.TryGetValue(currentPipelineType,
out var supportedVolumeComponents))
return supportedVolumeComponents;
supportedVolumeComponents = FilterVolumeComponentTypes(
VolumeManager.instance.baseComponentTypeArray, currentPipelineType);
s_SupportedVolumeComponentsForRenderPipeline[currentPipelineType] = supportedVolumeComponents;
return supportedVolumeComponents;
}
static List<(string, Type)> FilterVolumeComponentTypes(Type[] types, Type currentPipelineType)
{
var volumes = new List<(string, Type)>();
foreach (var t in types)
{
string path = string.Empty;
var attrs = t.GetCustomAttributes(false);
bool skipComponent = false;
// Look for the attributes of this volume component and decide how is added and if it needs to be skipped
foreach (var attr in attrs)
{
switch (attr)
{
case VolumeComponentMenu attrMenu:
{
path = attrMenu.menu;
if (attrMenu is VolumeComponentMenuForRenderPipeline supportedOn)
skipComponent |= !supportedOn.pipelineTypes.Contains(currentPipelineType);
break;
}
case HideInInspector attrHide:
case ObsoleteAttribute attrDeprecated:
skipComponent = true;
break;
}
}
if (skipComponent)
continue;
// If no attribute or in case something went wrong when grabbing it, fallback to a
// beautified class name
if (string.IsNullOrEmpty(path))
{
#if UNITY_EDITOR
path = ObjectNames.NicifyVariableName(t.Name);
#else
path = t.Name;
#endif
}
volumes.Add((path, t));
}
return volumes
.OrderBy(i => i.Item1)
.ToList();
}
/// <summary>
/// The current list of all available types that derive from <see cref="VolumeComponent"/>.
/// </summary>
public Type[] baseComponentTypeArray { get; private set; }
// Max amount of layers available in Unity
const int k_MaxLayerCount = 32;
// Cached lists of all volumes (sorted by priority) by layer mask
readonly Dictionary<int, List<Volume>> m_SortedVolumes;
// Holds all the registered volumes
readonly List<Volume> m_Volumes;
// Keep track of sorting states for layer masks
readonly Dictionary<int, bool> m_SortNeeded;
// Internal list of default state for each component type - this is used to reset component
// states on update instead of having to implement a Reset method on all components (which
// would be error-prone)
readonly List<VolumeComponent> m_ComponentsDefaultState;
internal VolumeComponent GetDefaultVolumeComponent(Type volumeComponentType)
{
foreach (VolumeComponent component in m_ComponentsDefaultState)
{
if (component.GetType() == volumeComponentType)
return component;
}
return null;
}
// Recycled list used for volume traversal
readonly List<Collider> m_TempColliders;
// The default stack the volume manager uses.
// We cache this as users able to change the stack through code and
// we want to be able to switch to the default one through the ResetMainStack() function.
VolumeStack m_DefaultStack = null;
VolumeManager()
{
m_SortedVolumes = new Dictionary<int, List<Volume>>();
m_Volumes = new List<Volume>();
m_SortNeeded = new Dictionary<int, bool>();
m_TempColliders = new List<Collider>(8);
m_ComponentsDefaultState = new List<VolumeComponent>();
ReloadBaseTypes();
m_DefaultStack = CreateStack();
stack = m_DefaultStack;
}
/// <summary>
/// Creates and returns a new <see cref="VolumeStack"/> to use when you need to store
/// the result of the Volume blending pass in a separate stack.
/// </summary>
/// <returns></returns>
/// <seealso cref="VolumeStack"/>
/// <seealso cref="Update(VolumeStack,Transform,LayerMask)"/>
public VolumeStack CreateStack()
{
var stack = new VolumeStack();
stack.Reload(m_ComponentsDefaultState);
return stack;
}
/// <summary>
/// Resets the main stack to be the default one.
/// Call this function if you've assigned the main stack to something other than the default one.
/// </summary>
public void ResetMainStack()
{
stack = m_DefaultStack;
}
/// <summary>
/// Destroy a Volume Stack
/// </summary>
/// <param name="stack">Volume Stack that needs to be destroyed.</param>
public void DestroyStack(VolumeStack stack)
{
stack.Dispose();
}
// This will be called only once at runtime and everytime script reload kicks-in in the
// editor as we need to keep track of any compatible component in the project
void ReloadBaseTypes()
{
m_ComponentsDefaultState.Clear();
// Grab all the component types we can find
baseComponentTypeArray = CoreUtils.GetAllTypesDerivedFrom<VolumeComponent>()
.Where(t => !t.IsAbstract).ToArray();
var flags = System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic;
// Keep an instance of each type to be used in a virtual lowest priority global volume
// so that we have a default state to fallback to when exiting volumes
foreach (var type in baseComponentTypeArray)
{
type.GetMethod("Init", flags)?.Invoke(null, null);
var inst = (VolumeComponent)ScriptableObject.CreateInstance(type);
m_ComponentsDefaultState.Add(inst);
}
}
/// <summary>
/// Registers a new Volume in the manager. Unity does this automatically when a new Volume is
/// enabled, or its layer changes, but you can use this function to force-register a Volume
/// that is currently disabled.
/// </summary>
/// <param name="volume">The volume to register.</param>
/// <param name="layer">The LayerMask that this volume is in.</param>
/// <seealso cref="Unregister"/>
public void Register(Volume volume, int layer)
{
m_Volumes.Add(volume);
// Look for existing cached layer masks and add it there if needed
foreach (var kvp in m_SortedVolumes)
{
// We add the volume to sorted lists only if the layer match and if it doesn't contain the volume already.
if ((kvp.Key & (1 << layer)) != 0 && !kvp.Value.Contains(volume))
kvp.Value.Add(volume);
}
SetLayerDirty(layer);
}
/// <summary>
/// Unregisters a Volume from the manager. Unity does this automatically when a Volume is
/// disabled or goes out of scope, but you can use this function to force-unregister a Volume
/// that you added manually while it was disabled.
/// </summary>
/// <param name="volume">The Volume to unregister.</param>
/// <param name="layer">The LayerMask that this Volume is in.</param>
/// <seealso cref="Register"/>
public void Unregister(Volume volume, int layer)
{
m_Volumes.Remove(volume);
foreach (var kvp in m_SortedVolumes)
{
// Skip layer masks this volume doesn't belong to
if ((kvp.Key & (1 << layer)) == 0)
continue;
kvp.Value.Remove(volume);
}
}
/// <summary>
/// Checks if a <see cref="VolumeComponent"/> is active in a given LayerMask.
/// </summary>
/// <typeparam name="T">A type derived from <see cref="VolumeComponent"/></typeparam>
/// <param name="layerMask">The LayerMask to check against</param>
/// <returns><c>true</c> if the component is active in the LayerMask, <c>false</c>
/// otherwise.</returns>
public bool IsComponentActiveInMask<T>(LayerMask layerMask)
where T : VolumeComponent
{
int mask = layerMask.value;
foreach (var kvp in m_SortedVolumes)
{
if (kvp.Key != mask)
continue;
foreach (var volume in kvp.Value)
{
if (!volume.enabled || volume.profileRef == null)
continue;
if (volume.profileRef.TryGet(out T component) && component.active)
return true;
}
}
return false;
}
internal void SetLayerDirty(int layer)
{
Assert.IsTrue(layer >= 0 && layer <= k_MaxLayerCount, "Invalid layer bit");
foreach (var kvp in m_SortedVolumes)
{
var mask = kvp.Key;
if ((mask & (1 << layer)) != 0)
m_SortNeeded[mask] = true;
}
}
internal void UpdateVolumeLayer(Volume volume, int prevLayer, int newLayer)
{
Assert.IsTrue(prevLayer >= 0 && prevLayer <= k_MaxLayerCount, "Invalid layer bit");
Unregister(volume, prevLayer);
Register(volume, newLayer);
}
// Go through all listed components and lerp overridden values in the global state
void OverrideData(VolumeStack stack, List<VolumeComponent> components, float interpFactor)
{
var numComponents = components.Count;
for (int i = 0; i < numComponents; i++)
{
var component = components[i];
if (!component.active)
continue;
var state = stack.GetComponent(component.GetType());
component.Override(state, interpFactor);
}
}
// Faster version of OverrideData to force replace values in the global state
internal void ReplaceData(VolumeStack stack)
{
var resetParameters = stack.defaultParameters;
var resetParametersCount = resetParameters.Length;
for (int i = 0; i < resetParametersCount; i++)
{
var resetParam = resetParameters[i];
var targetParam = resetParam.parameter;
targetParam.overrideState = false;
targetParam.SetValue(resetParam.defaultValue);
}
}
/// <summary>
/// Checks the state of the base type library. This is only used in the editor to handle
/// entering and exiting of play mode and domain reload.
/// </summary>
[Conditional("UNITY_EDITOR")]
public void CheckBaseTypes()
{
// Editor specific hack to work around serialization doing funky things when exiting
if (m_ComponentsDefaultState == null || (m_ComponentsDefaultState.Count > 0 && m_ComponentsDefaultState[0] == null))
ReloadBaseTypes();
}
/// <summary>
/// Checks the state of a given stack. This is only used in the editor to handle entering
/// and exiting of play mode and domain reload.
/// </summary>
/// <param name="stack">The stack to check.</param>
[Conditional("UNITY_EDITOR")]
public void CheckStack(VolumeStack stack)
{
// The editor doesn't reload the domain when exiting play mode but still kills every
// object created while in play mode, like stacks' component states
var components = stack.components;
if (components == null)
{
stack.Reload(m_ComponentsDefaultState);
return;
}
foreach (var kvp in components)
{
if (kvp.Key == null || kvp.Value == null)
{
stack.Reload(m_ComponentsDefaultState);
return;
}
}
}
// Returns true if must execute Update() in full, and false if we can early exit.
bool CheckUpdateRequired(VolumeStack stack)
{
if (m_Volumes.Count == 0)
{
if (stack.requiresReset)
{
// Update the stack one more time in case there was a volume that just ceased to exist. This ensures
// the stack will return to default values correctly.
stack.requiresReset = false;
return true;
}
// There were no volumes last frame either, and stack has been returned to defaults, so no update is
// needed and we can early exit from Update().
return false;
}
stack.requiresReset = true; // Stack must be reset every frame whenever there are volumes present
return true;
}
/// <summary>
/// Updates the global state of the Volume manager. Unity usually calls this once per Camera
/// in the Update loop before rendering happens.
/// </summary>
/// <param name="trigger">A reference Transform to consider for positional Volume blending
/// </param>
/// <param name="layerMask">The LayerMask that the Volume manager uses to filter Volumes that it should consider
/// for blending.</param>
public void Update(Transform trigger, LayerMask layerMask)
{
Update(stack, trigger, layerMask);
}
/// <summary>
/// Updates the Volume manager and stores the result in a custom <see cref="VolumeStack"/>.
/// </summary>
/// <param name="stack">The stack to store the blending result into.</param>
/// <param name="trigger">A reference Transform to consider for positional Volume blending.
/// </param>
/// <param name="layerMask">The LayerMask that Unity uses to filter Volumes that it should consider
/// for blending.</param>
/// <seealso cref="VolumeStack"/>
public void Update(VolumeStack stack, Transform trigger, LayerMask layerMask)
{
Assert.IsNotNull(stack);
CheckBaseTypes();
CheckStack(stack);
if (!CheckUpdateRequired(stack))
return;
// Start by resetting the global state to default values
ReplaceData(stack);
bool onlyGlobal = trigger == null;
var triggerPos = onlyGlobal ? Vector3.zero : trigger.position;
// Sort the cached volume list(s) for the given layer mask if needed and return it
var volumes = GrabVolumes(layerMask);
Camera camera = null;
// Behavior should be fine even if camera is null
if (!onlyGlobal)
trigger.TryGetComponent<Camera>(out camera);
// Traverse all volumes
int numVolumes = volumes.Count;
for (int i = 0; i < numVolumes; i++)
{
Volume volume = volumes[i];
if (volume == null)
continue;
#if UNITY_EDITOR
// Skip volumes that aren't in the scene currently displayed in the scene view
if (!IsVolumeRenderedByCamera(volume, camera))
continue;
#endif
// Skip disabled volumes and volumes without any data or weight
if (!volume.enabled || volume.profileRef == null || volume.weight <= 0f)
continue;
// Global volumes always have influence
if (volume.isGlobal)
{
OverrideData(stack, volume.profileRef.components, Mathf.Clamp01(volume.weight));
continue;
}
if (onlyGlobal)
continue;
// If volume isn't global and has no collider, skip it as it's useless
var colliders = m_TempColliders;
volume.GetComponents(colliders);
if (colliders.Count == 0)
continue;
// Find closest distance to volume, 0 means it's inside it
float closestDistanceSqr = float.PositiveInfinity;
int numColliders = colliders.Count;
for (int c = 0; c < numColliders; c++)
{
var collider = colliders[c];
if (!collider.enabled)
continue;
var closestPoint = collider.ClosestPoint(triggerPos);
var d = (closestPoint - triggerPos).sqrMagnitude;
if (d < closestDistanceSqr)
closestDistanceSqr = d;
}
colliders.Clear();
float blendDistSqr = volume.blendDistance * volume.blendDistance;
// Volume has no influence, ignore it
// Note: Volume doesn't do anything when `closestDistanceSqr = blendDistSqr` but we
// can't use a >= comparison as blendDistSqr could be set to 0 in which case
// volume would have total influence
if (closestDistanceSqr > blendDistSqr)
continue;
// Volume has influence
float interpFactor = 1f;
if (blendDistSqr > 0f)
interpFactor = 1f - (closestDistanceSqr / blendDistSqr);
// No need to clamp01 the interpolation factor as it'll always be in [0;1[ range
OverrideData(stack, volume.profileRef.components, interpFactor * Mathf.Clamp01(volume.weight));
}
}
/// <summary>
/// Get all volumes on a given layer mask sorted by influence.
/// </summary>
/// <param name="layerMask">The LayerMask that Unity uses to filter Volumes that it should consider.</param>
/// <returns>An array of volume.</returns>
public Volume[] GetVolumes(LayerMask layerMask)
{
var volumes = GrabVolumes(layerMask);
volumes.RemoveAll(v => v == null);
return volumes.ToArray();
}
List<Volume> GrabVolumes(LayerMask mask)
{
List<Volume> list;
if (!m_SortedVolumes.TryGetValue(mask, out list))
{
// New layer mask detected, create a new list and cache all the volumes that belong
// to this mask in it
list = new List<Volume>();
var numVolumes = m_Volumes.Count;
for (int i = 0; i < numVolumes; i++)
{
var volume = m_Volumes[i];
if ((mask & (1 << volume.gameObject.layer)) == 0)
continue;
list.Add(volume);
m_SortNeeded[mask] = true;
}
m_SortedVolumes.Add(mask, list);
}
// Check sorting state
bool sortNeeded;
if (m_SortNeeded.TryGetValue(mask, out sortNeeded) && sortNeeded)
{
m_SortNeeded[mask] = false;
SortByPriority(list);
}
return list;
}
// Stable insertion sort. Faster than List<T>.Sort() for our needs.
static void SortByPriority(List<Volume> volumes)
{
Assert.IsNotNull(volumes, "Trying to sort volumes of non-initialized layer");
for (int i = 1; i < volumes.Count; i++)
{
var temp = volumes[i];
int j = i - 1;
// Sort order is ascending
while (j >= 0 && volumes[j].priority > temp.priority)
{
volumes[j + 1] = volumes[j];
j--;
}
volumes[j + 1] = temp;
}
}
static bool IsVolumeRenderedByCamera(Volume volume, Camera camera)
{
#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
// GameObject for default global volume may not belong to any scene, following check prevents it from being culled
if (!volume.gameObject.scene.IsValid())
return true;
// IsGameObjectRenderedByCamera does not behave correctly when camera is null so we have to catch it here.
return camera == null ? true : UnityEditor.SceneManagement.StageUtility.IsGameObjectRenderedByCamera(volume.gameObject, camera);
#else
return true;
#endif
}
}
/// <summary>
/// A scope in which a Camera filters a Volume.
/// </summary>
[Obsolete("VolumeIsolationScope is deprecated, it does not have any effect anymore.")]
public struct VolumeIsolationScope : IDisposable
{
/// <summary>
/// Constructs a scope in which a Camera filters a Volume.
/// </summary>
/// <param name="unused">Unused parameter.</param>
public VolumeIsolationScope(bool unused) { }
/// <summary>
/// Stops the Camera from filtering a Volume.
/// </summary>
void IDisposable.Dispose() { }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f709aacbf8d0b0048889f52315d67d58
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d53242bc2649d524aa42f2a84eb077b2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,343 @@
using System;
using System.Collections.Generic;
using UnityEngine.Assertions;
namespace UnityEngine.Rendering
{
/// <summary>
/// An Asset which holds a set of settings to use with a <see cref="Volume"/>.
/// </summary>
[CoreRPHelpURL("Volume-Profile", "com.unity.render-pipelines.high-definition")]
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>
[NonSerialized]
public bool isDirty = true; // Editor only, doesn't have any use outside of it
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()
{
isDirty = true;
}
/// <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>
/// <see 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);
isDirty = true;
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);
isDirty = true;
}
}
/// <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 <seealso 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 <seealso 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 <seealso 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);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d7fd9488000d3734a9e00ee676215985
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: f20112bdeec2e8d4d9f80e8390e37263, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
namespace UnityEngine.Rendering
{
/// <summary>
/// Holds the state of a Volume blending update. A global stack is
/// available by default in <see cref="VolumeManager"/> but you can also create your own using
/// <see cref="VolumeManager.CreateStack"/> if you need to update the manager with specific
/// settings and store the results for later use.
/// </summary>
public sealed class VolumeStack : IDisposable
{
// Holds the state of _all_ component types you can possibly add on volumes
internal readonly Dictionary<Type, VolumeComponent> 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<VolumeComponent> 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();
}
/// <summary>
/// Gets the current state of the <see cref="VolumeComponent"/> of type <typeparamref name="T"/>
/// in the stack.
/// </summary>
/// <typeparam name="T">A type of <see cref="VolumeComponent"/>.</typeparam>
/// <returns>The current state of the <see cref="VolumeComponent"/> of type <typeparamref name="T"/>
/// in the stack.</returns>
public T GetComponent<T>()
where T : VolumeComponent
{
var comp = GetComponent(typeof(T));
return (T)comp;
}
/// <summary>
/// Gets the current state of the <see cref="VolumeComponent"/> of the specified type in the
/// stack.
/// </summary>
/// <param name="type">The type of <see cref="VolumeComponent"/> to look for.</param>
/// <returns>The current state of the <see cref="VolumeComponent"/> of the specified type,
/// or <c>null</c> if the type is invalid.</returns>
public VolumeComponent GetComponent(Type type)
{
components.TryGetValue(type, out var comp);
return comp;
}
/// <summary>
/// Cleans up the content of this stack. Once a <c>VolumeStack</c> is disposed, it souldn't
/// be used anymore.
/// </summary>
public void Dispose()
{
Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7af226ce5122db0409963decbabf9d9b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: