using System;
using UnityEngine.Serialization;
namespace UnityEngine.Rendering.HighDefinition
{
///
/// Focusing modes for the depth of field effect.
///
///
public enum DepthOfFieldMode
{
///
/// Disables depth of field.
///
Off,
///
/// Uses the physical Camera to set focusing properties.
///
UsePhysicalCamera,
///
/// Uses custom distance values to set the focus.
///
Manual
}
///
/// The resolution at which HDRP processes the depth of field effect.
///
///
public enum DepthOfFieldResolution : int
{
///
/// Quarter resolution.
///
Quarter = 4,
///
/// Half resolution.
///
Half = 2,
///
/// Full resolution. Should only be set for beauty shots or film uses.
///
Full = 1
}
///
/// A volume component that holds settings for the Depth Of Field effect.
///
[Serializable, VolumeComponentMenu("Post-processing/Depth Of Field")]
[HelpURL(Documentation.baseURL + Documentation.version + Documentation.subURL + "Post-Processing-Depth-of-Field" + Documentation.endURL)]
public sealed class DepthOfField : VolumeComponentWithQuality, IPostProcessComponent
{
///
/// Specifies the mode that HDRP uses to set the focus for the depth of field effect.
///
///
[Tooltip("Specifies the mode that HDRP uses to set the focus for the depth of field effect.")]
public DepthOfFieldModeParameter focusMode = new DepthOfFieldModeParameter(DepthOfFieldMode.Off);
// -------------------------------------------
// Physical settings
//
///
/// Sets the distance to the focus point from the Camera.
///
[Tooltip("Sets the distance to the focus point from the Camera.")]
public MinFloatParameter focusDistance = new MinFloatParameter(10f, 0.1f);
// -------------------------------------------
// Manual settings
// Note: because they can't mathematically be mapped to physical settings, interpolating
// between manual & physical is not supported
//
///
/// Sets the distance from the Camera at which the near field blur begins to decrease in intensity.
///
[Tooltip("Sets the distance from the Camera at which the near field blur begins to decrease in intensity.")]
public MinFloatParameter nearFocusStart = new MinFloatParameter(0f, 0f);
///
/// Sets the distance from the Camera at which the near field does not blur anymore.
///
[Tooltip("Sets the distance from the Camera at which the near field does not blur anymore.")]
public MinFloatParameter nearFocusEnd = new MinFloatParameter(4f, 0f);
///
/// Sets the distance from the Camera at which the far field starts blurring.
///
[Tooltip("Sets the distance from the Camera at which the far field starts blurring.")]
public MinFloatParameter farFocusStart = new MinFloatParameter(10f, 0f);
///
/// Sets the distance from the Camera at which the far field blur reaches its maximum blur radius.
///
[Tooltip("Sets the distance from the Camera at which the far field blur reaches its maximum blur radius.")]
public MinFloatParameter farFocusEnd = new MinFloatParameter(20f, 0f);
// -------------------------------------------
// Shared settings
//
///
/// Sets the number of samples to use for the near field.
///
public int nearSampleCount
{
get
{
if (!UsesQualitySettings())
{
return m_NearSampleCount.value;
}
else
{
int qualityLevel = (int)quality.levelAndOverride.level;
return GetPostProcessingQualitySettings().NearBlurSampleCount[qualityLevel];
}
}
set { m_NearSampleCount.value = value; }
}
///
/// Sets the maximum radius the near blur can reach.
///
public float nearMaxBlur
{
get
{
if (!UsesQualitySettings())
{
return m_NearMaxBlur.value;
}
else
{
int qualityLevel = (int)quality.levelAndOverride.level;
return GetPostProcessingQualitySettings().NearBlurMaxRadius[qualityLevel];
}
}
set { m_NearMaxBlur.value = value; }
}
///
/// Sets the number of samples to use for the far field.
///
public int farSampleCount
{
get
{
if (!UsesQualitySettings())
{
return m_FarSampleCount.value;
}
else
{
int qualityLevel = (int)quality.levelAndOverride.level;
return GetPostProcessingQualitySettings().FarBlurSampleCount[qualityLevel];
}
}
set { m_FarSampleCount.value = value; }
}
///
/// Sets the maximum radius the far blur can reach.
///
public float farMaxBlur
{
get
{
if (!UsesQualitySettings())
{
return m_FarMaxBlur.value;
}
else
{
int qualityLevel = (int)quality.levelAndOverride.level;
return GetPostProcessingQualitySettings().FarBlurMaxRadius[qualityLevel];
}
}
set { m_FarMaxBlur.value = value; }
}
///
/// When enabled, HDRP uses bicubic filtering instead of bilinear filtering for the depth of field effect.
///
public bool highQualityFiltering
{
get
{
if (!UsesQualitySettings())
{
return m_HighQualityFiltering.value;
}
else
{
int qualityLevel = (int)quality.levelAndOverride.level;
return GetPostProcessingQualitySettings().DoFHighQualityFiltering[qualityLevel];
}
}
set { m_HighQualityFiltering.value = value; }
}
///
/// When enabled, HDRP uses a more accurate but slower physically based method to compute the depth of field effect.
///
public bool physicallyBased
{
get
{
if (!UsesQualitySettings())
{
return m_PhysicallyBased.value;
}
else
{
int qualityLevel = (int)quality.levelAndOverride.level;
return GetPostProcessingQualitySettings().DoFPhysicallyBased[qualityLevel];
}
}
set { m_PhysicallyBased.value = value; }
}
///
/// Specifies the resolution at which HDRP processes the depth of field effect.
///
///
public DepthOfFieldResolution resolution
{
get
{
if (!UsesQualitySettings())
{
return m_Resolution.value;
}
else
{
int qualityLevel = (int)quality.levelAndOverride.level;
return GetPostProcessingQualitySettings().DoFResolution[qualityLevel];
}
}
set
{
m_Resolution.value = value;
}
}
[Tooltip("Sets the number of samples to use for the near field.")]
[SerializeField, FormerlySerializedAs("nearSampleCount")]
ClampedIntParameter m_NearSampleCount = new ClampedIntParameter(5, 3, 8);
[SerializeField, FormerlySerializedAs("nearMaxBlur")]
[Tooltip("Sets the maximum radius the near blur can reach.")]
ClampedFloatParameter m_NearMaxBlur = new ClampedFloatParameter(4f, 0f, 8f);
[Tooltip("Sets the number of samples to use for the far field.")]
[SerializeField, FormerlySerializedAs("farSampleCount")]
ClampedIntParameter m_FarSampleCount = new ClampedIntParameter(7, 3, 16);
[Tooltip("Sets the maximum radius the far blur can reach.")]
[SerializeField, FormerlySerializedAs("farMaxBlur")]
ClampedFloatParameter m_FarMaxBlur = new ClampedFloatParameter(8f, 0f, 16f);
// -------------------------------------------
// Advanced settings
//
[AdditionalProperty]
[Tooltip("When enabled, HDRP uses bicubic filtering instead of bilinear filtering for the depth of field effect.")]
[SerializeField, FormerlySerializedAs("highQualityFiltering")]
BoolParameter m_HighQualityFiltering = new BoolParameter(true);
[AdditionalProperty]
[Tooltip("Specifies the resolution at which HDRP processes the depth of field effect.")]
[SerializeField, FormerlySerializedAs("resolution")]
DepthOfFieldResolutionParameter m_Resolution = new DepthOfFieldResolutionParameter(DepthOfFieldResolution.Half);
[AdditionalProperty]
[SerializeField]
BoolParameter m_PhysicallyBased = new BoolParameter(false);
///
/// Tells if the effect needs to be rendered or not.
///
/// true if the effect should be rendered, false otherwise.
public bool IsActive()
{
return focusMode.value != DepthOfFieldMode.Off && (IsNearLayerActive() || IsFarLayerActive());
}
///
/// Returns the state of the near field blur.
/// This is only relevant when is set.
///
/// true if the near field blur is active and visible.
public bool IsNearLayerActive() => nearMaxBlur > 0f && nearFocusEnd.value > 0f;
///
/// Returns the state of the far field blur.
/// This is only relevant when is set.
///
/// true if the far field blur is active and visible.
public bool IsFarLayerActive() => farMaxBlur > 0f;
}
///
/// A that holds a value.
///
[Serializable]
public sealed class DepthOfFieldModeParameter : VolumeParameter
{
///
/// Creates a new instance.
///
/// The initial value to store in the parameter.
/// The initial override state for the parameter.
public DepthOfFieldModeParameter(DepthOfFieldMode value, bool overrideState = false) : base(value, overrideState) {}
}
///
/// A that holds a value.
///
[Serializable]
public sealed class DepthOfFieldResolutionParameter : VolumeParameter
{
///
/// Creates a new instance.
///
/// The initial value to store in the parameter.
/// The initial override state for the parameter.
public DepthOfFieldResolutionParameter(DepthOfFieldResolution value, bool overrideState = false) : base(value, overrideState) {}
}
}