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.
///
[InspectorName("Physical Camera")]
UsePhysicalCamera,
///
/// Uses custom distance values to set the focus.
///
[InspectorName("Manual Ranges")]
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
}
///
/// Options for the source of the focus distance HDRP uses in the depth of field calculations.
///
public enum FocusDistanceMode
{
///
/// Uses the focus distance from the Volume component.
///
Volume,
///
/// Uses the focus distance from the physical camera.
///
Camera
}
///
/// A volume component that holds settings for the Depth Of Field effect.
///
[Serializable, VolumeComponentMenu("Post-processing/Depth Of Field")]
[SupportedOnRenderPipeline(typeof(HDRenderPipelineAsset))]
[HDRPHelpURL("Post-Processing-Depth-of-Field")]
public sealed class DepthOfField : VolumeComponentWithQuality, IPostProcessComponent
{
// Sampling ratios for adaptive sampling.
// X: ratio of the sharp part tiles of PBR dof that have high variance of CoC.
// Y: ratio of the blurry / sharp tiles that have low variance of CoC.
internal static Vector2 s_HighQualityAdaptiveSamplingWeights = new Vector2(4.0f, 1.0f);
internal static Vector2 s_LowQualityAdaptiveSamplingWeights = new Vector2(1.0f, 0.75f);
///
/// 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
//
///
/// The distance to the focus plane from the Camera.
///
[Tooltip("The distance to the focus plane from the Camera.")]
public MinFloatParameter focusDistance = new MinFloatParameter(10f, 0.1f);
///
/// Specifies where to read the focus distance from.
///
[Tooltip("Specifies where to read the focus distance from..")]
public FocusDistanceModeParameter focusDistanceMode = new FocusDistanceModeParameter(FocusDistanceMode.Volume);
// -------------------------------------------
// 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.
///
[Header("Near Range")]
[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.
///
[Header("Far Range")]
[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; }
}
///
/// Adjust near blur CoC based on depth distance when manual, non-physical mode is used.
///
public bool limitManualRangeNearBlur
{
get
{
if (!UsesQualitySettings())
return m_LimitManualRangeNearBlur.value;
return GetPostProcessingQualitySettings().LimitManualRangeNearBlur[quality.levelAndOverride.level];
}
set => m_LimitManualRangeNearBlur.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;
}
}
[Header("Near Blur")]
[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);
[Header("Far Blur")]
[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
//
[Header("Advanced Tweaks")]
[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]
[Tooltip("When enabled, HDRP uses bicubic instead of bilinear filtering for the depth of field effect. Also conceals tiling artifacts in the physically-based mode.")]
[SerializeField, FormerlySerializedAs("highQualityFiltering")]
BoolParameter m_HighQualityFiltering = new BoolParameter(true);
[AdditionalProperty]
[Tooltip("When enabled, HDRP uses a more accurate but slower physically based algorithm to compute the depth of field effect.")]
[SerializeField]
BoolParameter m_PhysicallyBased = new BoolParameter(false);
[AdditionalProperty]
[Tooltip("Adjust near blur CoC based on depth distance when manual, non-physical mode is used.")]
[SerializeField]
BoolParameter m_LimitManualRangeNearBlur = 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) { }
}
///
/// A that holds a value.
///
[Serializable]
public sealed class FocusDistanceModeParameter : VolumeParameter
{
///
/// Creates a new instance.
///
/// The initial value to store in the parameter.
/// The initial override state for the parameter.
public FocusDistanceModeParameter(FocusDistanceMode value, bool overrideState = false) : base(value, overrideState) { }
}
}