ChromaticAberration.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using UnityEngine.Serialization;
  3. namespace UnityEngine.Rendering.PostProcessing
  4. {
  5. /// <summary>
  6. /// This class holds settings for the Chromatic Aberration effect.
  7. /// </summary>
  8. [Serializable]
  9. [PostProcess(typeof(ChromaticAberrationRenderer), "Unity/Chromatic Aberration")]
  10. public sealed class ChromaticAberration : PostProcessEffectSettings
  11. {
  12. /// <summary>
  13. /// A texture used for custom fringing color (it will use a default one when <c>null</c>).
  14. /// </summary>
  15. [Tooltip("Shifts the hue of chromatic aberrations.")]
  16. public TextureParameter spectralLut = new TextureParameter { value = null };
  17. /// <summary>
  18. /// The amount of tangential distortion.
  19. /// </summary>
  20. [Range(0f, 1f), Tooltip("Amount of tangential distortion.")]
  21. public FloatParameter intensity = new FloatParameter { value = 0f };
  22. /// <summary>
  23. /// If <c>true</c>, it will use a faster variant of the effect for improved performances.
  24. /// </summary>
  25. [FormerlySerializedAs("mobileOptimized")]
  26. [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.")]
  27. public BoolParameter fastMode = new BoolParameter { value = false };
  28. /// <inheritdoc />
  29. public override bool IsEnabledAndSupported(PostProcessRenderContext context)
  30. {
  31. return enabled.value
  32. && intensity.value > 0f;
  33. }
  34. }
  35. internal sealed class ChromaticAberrationRenderer : PostProcessEffectRenderer<ChromaticAberration>
  36. {
  37. Texture2D m_InternalSpectralLut;
  38. public override void Render(PostProcessRenderContext context)
  39. {
  40. var spectralLut = settings.spectralLut.value;
  41. if (spectralLut == null)
  42. {
  43. if (m_InternalSpectralLut == null)
  44. {
  45. m_InternalSpectralLut = new Texture2D(3, 1, TextureFormat.RGB24, false)
  46. {
  47. name = "Chromatic Aberration Spectrum Lookup",
  48. filterMode = FilterMode.Bilinear,
  49. wrapMode = TextureWrapMode.Clamp,
  50. anisoLevel = 0,
  51. hideFlags = HideFlags.DontSave
  52. };
  53. m_InternalSpectralLut.SetPixels(new []
  54. {
  55. new Color(1f, 0f, 0f),
  56. new Color(0f, 1f, 0f),
  57. new Color(0f, 0f, 1f)
  58. });
  59. m_InternalSpectralLut.Apply();
  60. }
  61. spectralLut = m_InternalSpectralLut;
  62. }
  63. var sheet = context.uberSheet;
  64. bool fastMode = settings.fastMode || SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLES2;
  65. sheet.EnableKeyword(fastMode
  66. ? "CHROMATIC_ABERRATION_LOW"
  67. : "CHROMATIC_ABERRATION"
  68. );
  69. sheet.properties.SetFloat(ShaderIDs.ChromaticAberration_Amount, settings.intensity * 0.05f);
  70. sheet.properties.SetTexture(ShaderIDs.ChromaticAberration_SpectralLut, spectralLut);
  71. }
  72. public override void Release()
  73. {
  74. RuntimeUtilities.Destroy(m_InternalSpectralLut);
  75. m_InternalSpectralLut = null;
  76. }
  77. }
  78. }