using System; using System.Diagnostics; using UnityEngine.Serialization; namespace UnityEngine.Rendering.HighDefinition { /// /// Screen Space Reflection Algorithm /// public enum ScreenSpaceReflectionAlgorithm { /// Legacy SSR approximation. Approximation, /// Screen Space Reflection, Physically Based with Accumulation through multiple frame. PBRAccumulation } [GenerateHLSL] // Define if we use SSR, RTR, Mixed or none enum ReflectionsMode { Off, ScreenSpace, RayTraced, Mixed } /// /// Screen Space Reflection Algorithm Type volume parameter. /// [Serializable, DebuggerDisplay(k_DebuggerDisplay)] public sealed class SSRAlgoParameter : VolumeParameter { /// /// Screen Space Reflection Algorithm Type volume parameter constructor. /// /// SSR Algo Type parameter. /// Initial override state. public SSRAlgoParameter(ScreenSpaceReflectionAlgorithm value, bool overrideState = false) : base(value, overrideState) { } } /// /// A volume component that holds settings for screen space reflection and ray traced reflections. /// [Serializable, VolumeComponentMenu("Lighting/Screen Space Reflection")] [SupportedOnRenderPipeline(typeof(HDRenderPipelineAsset))] [HDRPHelpURL("Override-Screen-Space-Reflection")] public class ScreenSpaceReflection : VolumeComponentWithQuality { bool UsesRayTracingQualityMode() { // The default value is set to quality. So we should be in quality if not overriden or we have an override set to quality return (tracing.overrideState && tracing.value == RayCastingMode.RayTracing && (!mode.overrideState || (mode.overrideState && mode.value == RayTracingMode.Quality))); } bool UsesRayTracing() { var hdAsset = HDRenderPipeline.currentAsset; return hdAsset != null && hdAsset.currentPlatformRenderPipelineSettings.supportRayTracing && tracing.overrideState && tracing.value != RayCastingMode.RayMarching; } #region General /// Enable Screen Space Reflections. [Tooltip("Enable Screen Space Reflections.")] public BoolParameter enabled = new BoolParameter(true, BoolParameter.DisplayType.EnumPopup); /// Enable Transparent Screen Space Reflections. [Tooltip("Enable Transparent Screen Space Reflections.")] public BoolParameter enabledTransparent = new BoolParameter(true, BoolParameter.DisplayType.EnumPopup); /// /// [Tooltip("Controls the casting technique used to evaluate the effect.")] public RayCastingModeParameter tracing = new RayCastingModeParameter(RayCastingMode.RayMarching); // Shared Data /// /// Controls the smoothness value at which HDRP activates SSR and the smoothness-controlled fade out stops. /// public float minSmoothness { get { if ((UsesRayTracing() && (UsesRayTracingQualityMode() || !UsesQualitySettings())) || !UsesRayTracing()) return m_MinSmoothness.value; else return GetLightingQualitySettings().RTRMinSmoothness[(int)quality.value]; } set { m_MinSmoothness.value = value; } } [SerializeField, FormerlySerializedAs("minSmoothness")] private ClampedFloatParameter m_MinSmoothness = new ClampedFloatParameter(0.9f, 0.0f, 1.0f); /// /// Controls the smoothness value at which the smoothness-controlled fade out starts. The fade is in the range [Min Smoothness, Smoothness Fade Start] /// public float smoothnessFadeStart { get { if ((UsesRayTracing() && (UsesRayTracingQualityMode() || !UsesQualitySettings())) || !UsesRayTracing()) return m_SmoothnessFadeStart.value; else return GetLightingQualitySettings().RTRSmoothnessFadeStart[(int)quality.value]; } set { m_SmoothnessFadeStart.value = value; } } [SerializeField, FormerlySerializedAs("smoothnessFadeStart")] private ClampedFloatParameter m_SmoothnessFadeStart = new ClampedFloatParameter(0.9f, 0.0f, 1.0f); #endregion #region Ray Marching /// /// When enabled, SSR handles sky reflection for opaque objects (not supported for SSR on transparent). /// public BoolParameter reflectSky = new BoolParameter(true); /// Screen Space Reflections Algorithm used. public SSRAlgoParameter usedAlgorithm = new SSRAlgoParameter(ScreenSpaceReflectionAlgorithm.Approximation); // SSR Data /// /// Controls the distance at which HDRP fades out SSR near the edge of the screen. /// public ClampedFloatParameter depthBufferThickness = new ClampedFloatParameter(0.01f, 0, 1); /// /// Controls the typical thickness of objects the reflection rays may pass behind. /// public ClampedFloatParameter screenFadeDistance = new ClampedFloatParameter(0.1f, 0.0f, 1.0f); /// /// Controls the amount of accumulation (0 no accumulation, 1 just accumulate) /// public ClampedFloatParameter accumulationFactor = new ClampedFloatParameter(0.75f, 0.0f, 1.0f); /// /// For PBR: Controls the bias of accumulation (0 no bias, 1 bias ssr) /// [AdditionalProperty] public ClampedFloatParameter biasFactor = new ClampedFloatParameter(0.5f, 0.0f, 1.0f); /// /// Controls the likelihood history will be rejected based on the previous frame motion vectors of both the surface and the hit object in world space. /// // If change this value, must change on ScreenSpaceReflections.compute on 'float speed = saturate((speedDst + speedSrc) * 128.0f / (...)' [AdditionalProperty] public FloatParameter speedRejectionParam = new ClampedFloatParameter(0.5f, 0.0f, 1.0f); /// /// Controls the upper range of speed. The faster the objects or camera are moving, the higher this number should be. /// // If change this value, must change on ScreenSpaceReflections.compute on 'float speed = saturate((speedDst + speedSrc) * 128.0f / (...)' [AdditionalProperty] public ClampedFloatParameter speedRejectionScalerFactor = new ClampedFloatParameter(0.2f, 0.001f, 1f); /// /// When enabled, history can be partially rejected for moving objects which gives a smoother transition. When disabled, history is either kept or totally rejected. /// [AdditionalProperty] public BoolParameter speedSmoothReject = new BoolParameter(false); /// /// When enabled, speed rejection used world space motion of the reflecting surface. /// [AdditionalProperty] public BoolParameter speedSurfaceOnly = new BoolParameter(true); /// /// When enabled, speed rejection used world space motion of the hit surface by the SSR. /// [AdditionalProperty] public BoolParameter speedTargetOnly = new BoolParameter(true); /// /// When enabled, world space speed from Motion vector is used to reject samples. /// public BoolParameter enableWorldSpeedRejection = new BoolParameter(false); /// /// Sets the maximum number of steps HDRP uses for raytracing. Affects both correctness and performance. /// public int rayMaxIterations { get { if (!UsesQualitySettings()) return m_RayMaxIterations.value; else return GetLightingQualitySettings().SSRMaxRaySteps[(int)quality.value]; } set { m_RayMaxIterations.value = value; } } [SerializeField, FormerlySerializedAs("rayMaxIterations")] private MinIntParameter m_RayMaxIterations = new MinIntParameter(64, 0); #endregion #region Ray Tracing /// /// Controls which sources are used to fallback on when the traced ray misses. /// [FormerlySerializedAs("fallbackHierachy")] [AdditionalProperty] public RayTracingFallbackHierachyParameter rayMiss = new RayTracingFallbackHierachyParameter(RayTracingFallbackHierachy.ReflectionProbesAndSky); /// /// Controls the fallback hierarchy for lighting the last bounce. /// [AdditionalProperty] public RayTracingFallbackHierachyParameter lastBounceFallbackHierarchy = new RayTracingFallbackHierachyParameter(RayTracingFallbackHierachy.ReflectionProbesAndSky); /// /// Controls the dimmer applied to the ambient and legacy light probes. /// [Tooltip("Controls the dimmer applied to the ambient and legacy light probes.")] [AdditionalProperty] public ClampedFloatParameter ambientProbeDimmer = new ClampedFloatParameter(1.0f, 0.0f, 1.0f); /// /// Layer mask used to include the objects for screen space reflection. /// public LayerMaskParameter layerMask = new LayerMaskParameter(-1); /// /// Defines the LOD Bias for sampling all the textures. /// public ClampedFloatParameter textureLodBias = new ClampedFloatParameter(1.0f, 0.0f, 7.0f); /// /// Controls the length of reflection rays in meters. /// public float rayLength { get { if (!UsesQualitySettings() || UsesRayTracingQualityMode()) return m_RayLength.value; else return GetLightingQualitySettings().RTRRayLength[(int)quality.value]; } set { m_RayLength.value = value; } } [SerializeField, FormerlySerializedAs("rayLength")] private MinFloatParameter m_RayLength = new MinFloatParameter(50.0f, 0.01f); /// /// Clamps the exposed intensity, this only affects reflections on opaque objects. /// public float clampValue { get { return m_ClampValue.value; } set { m_ClampValue.value = value; } } [SerializeField, FormerlySerializedAs("clampValue")] [Tooltip("Clamps the exposed intensity, this only affects reflections on opaque objects.")] private MinFloatParameter m_ClampValue = new MinFloatParameter(100.0f, 0.001f); /// /// Enable denoising on the ray traced reflections. /// public bool denoise { get { if (!UsesQualitySettings() || UsesRayTracingQualityMode()) return m_Denoise.value; else return GetLightingQualitySettings().RTRDenoise[(int)quality.value]; } set { m_Denoise.value = value; } } [SerializeField, FormerlySerializedAs("denoise")] [Tooltip("Denoise the ray-traced reflection.")] private BoolParameter m_Denoise = new BoolParameter(true); /// /// Controls the radius of reflection denoiser. /// public float denoiserRadius { get { if (!UsesQualitySettings() || UsesRayTracingQualityMode()) return m_DenoiserRadius.value; else return GetLightingQualitySettings().RTRDenoiserRadiusDimmer[(int)quality.value]; } set { m_DenoiserRadius.value = value; } } [SerializeField] [Tooltip("Controls the radius of the ray traced reflection denoiser.")] private ClampedFloatParameter m_DenoiserRadius = new ClampedFloatParameter(0.75f, 0.0f, 1.0f); /// /// Controls the anti-flickering strength of the reflection denoiser. /// public float denoiserAntiFlickeringStrength { get { if (!UsesQualitySettings() || UsesRayTracingQualityMode()) return m_DenoiserAntiFlickeringStrength.value; else return GetLightingQualitySettings().RTRDenoiserAntiFlicker[(int)quality.value]; } set { m_DenoiserAntiFlickeringStrength.value = value; } } [SerializeField] [Tooltip("Controls the anti-flickering strength of the reflection denoiser.")] private ClampedFloatParameter m_DenoiserAntiFlickeringStrength = new ClampedFloatParameter(1.0f, 0.0f, 1.0f); /// /// Controls which version of the effect should be used. /// public RayTracingModeParameter mode = new RayTracingModeParameter(RayTracingMode.Quality); /// /// Defines if the effect should be evaluated at full resolution. /// public bool fullResolution { get { if (!UsesQualitySettings()) return m_FullResolution.value; else return GetLightingQualitySettings().RTRFullResolution[(int)quality.value]; } set { m_FullResolution.value = value; } } [SerializeField, FormerlySerializedAs("fullResolution")] [Tooltip("Full Resolution")] private BoolParameter m_FullResolution = new BoolParameter(false); // Quality /// /// Number of samples for reflections. /// public ClampedIntParameter sampleCount = new ClampedIntParameter(1, 1, 32); /// /// Number of bounces for reflection rays. /// public ClampedIntParameter bounceCount = new ClampedIntParameter(1, 1, 8); /// /// Sets the maximum number of steps HDRP uses for mixed tracing. Affects both correctness and performance. /// public int rayMaxIterationsRT { get { if (!UsesQualitySettings()) return m_RayMaxIterationsRT.value; else return GetLightingQualitySettings().RTRRayMaxIterations[(int)quality.value]; } set { m_RayMaxIterationsRT.value = value; } } [SerializeField, FormerlySerializedAs("rayMaxIterations")] private MinIntParameter m_RayMaxIterationsRT = new MinIntParameter(48, 0); #endregion internal static bool RayTracingActive(ScreenSpaceReflection volume) { return volume.tracing.value != RayCastingMode.RayMarching; } } }