using System.Collections.Generic;
using UnityEngine.Serialization;
namespace UnityEngine.Rendering
{
///
/// A generic Volume component holding a .
///
[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;
///
/// Specifies whether to apply the Volume to the entire Scene or not.
///
[Tooltip("When enabled, the Volume is applied to the entire Scene.")]
public bool isGlobal
{
get => m_IsGlobal;
set => m_IsGlobal = value;
}
///
/// The Volume priority in the stack. A higher value means higher priority. This supports negative values.
///
[Tooltip("When multiple Volumes affect the same settings, Unity uses this value to determine which Volume to use. A Volume with the highest Priority value takes precedence.")]
public float priority = 0f;
///
/// The outer distance to start blending from. A value of 0 means no blending and Unity applies
/// the Volume overrides immediately upon entry.
///
[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;
///
/// The total weight of this volume in the Scene. 0 means no effect and 1 means full effect.
///
[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;
///
/// The shared Profile that this Volume uses.
/// Modifying sharedProfile changes every Volumes that uses this Profile and also changes
/// the Profile settings stored in the Project.
///
///
/// You should not modify Profiles that sharedProfile returns. If you want
/// to modify the Profile of a Volume, use instead.
///
///
public VolumeProfile sharedProfile = null;
///
/// Gets the first instantiated assigned to the Volume.
/// Modifying profile 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.
///
///
/// 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.
///
///
public VolumeProfile profile
{
get
{
if (m_InternalProfile == null)
{
m_InternalProfile = ScriptableObject.CreateInstance();
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 m_Colliders = new List();
///
/// The colliders of the volume if is false
///
public List colliders => m_Colliders;
internal VolumeProfile profileRef => m_InternalProfile == null ? sharedProfile : m_InternalProfile;
///
/// Checks if the Volume has an instantiated Profile or if it uses a shared Profile.
///
/// true if the profile has been instantiated.
///
///
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;
}
}
}
}