using System; using UnityEngine.Serialization; namespace UnityEngine.Rendering.PostProcessing { /// /// This class holds settings for the Chromatic Aberration effect. /// [Serializable] [PostProcess(typeof(ChromaticAberrationRenderer), "Unity/Chromatic Aberration")] public sealed class ChromaticAberration : PostProcessEffectSettings { /// /// A texture used for custom fringing color (it will use a default one when null). /// [Tooltip("Shifts the hue of chromatic aberrations.")] public TextureParameter spectralLut = new TextureParameter { value = null }; /// /// The amount of tangential distortion. /// [Range(0f, 1f), Tooltip("Amount of tangential distortion.")] public FloatParameter intensity = new FloatParameter { value = 0f }; /// /// If true, it will use a faster variant of the effect for improved performances. /// [FormerlySerializedAs("mobileOptimized")] [Tooltip("Boost performances by lowering the effect quality. This settings is meant to be used on mobile and other low-end platforms but can also provide a nice performance boost on desktops and consoles.")] public BoolParameter fastMode = new BoolParameter { value = false }; /// /// Returns true if the effect is currently enabled and supported. /// /// The current post-processing render context /// true if the effect is currently enabled and supported public override bool IsEnabledAndSupported(PostProcessRenderContext context) { return enabled.value && intensity.value > 0f; } } [UnityEngine.Scripting.Preserve] internal sealed class ChromaticAberrationRenderer : PostProcessEffectRenderer { Texture2D m_InternalSpectralLut; public override void Render(PostProcessRenderContext context) { var spectralLut = settings.spectralLut.value; if (spectralLut == null) { if (m_InternalSpectralLut == null) { m_InternalSpectralLut = new Texture2D(3, 1, TextureFormat.RGB24, false) { name = "Chromatic Aberration Spectrum Lookup", filterMode = FilterMode.Bilinear, wrapMode = TextureWrapMode.Clamp, anisoLevel = 0, hideFlags = HideFlags.DontSave }; m_InternalSpectralLut.SetPixels(new[] { new Color(1f, 0f, 0f), new Color(0f, 1f, 0f), new Color(0f, 0f, 1f) }); m_InternalSpectralLut.Apply(); } spectralLut = m_InternalSpectralLut; } var sheet = context.uberSheet; bool fastMode = settings.fastMode || SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLES2; sheet.EnableKeyword(fastMode ? "CHROMATIC_ABERRATION_LOW" : "CHROMATIC_ABERRATION" ); sheet.properties.SetFloat(ShaderIDs.ChromaticAberration_Amount, settings.intensity * 0.05f); sheet.properties.SetTexture(ShaderIDs.ChromaticAberration_SpectralLut, spectralLut); } public override void Release() { RuntimeUtilities.Destroy(m_InternalSpectralLut); m_InternalSpectralLut = null; } } }