ChromaticAberrationComponent.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. namespace UnityEngine.PostProcessing
  3. {
  4. public sealed class ChromaticAberrationComponent : PostProcessingComponentRenderTexture<ChromaticAberrationModel>
  5. {
  6. public override bool active
  7. {
  8. get
  9. {
  10. return base.model.enabled && base.model.settings.intensity > 0f && !this.context.interrupted;
  11. }
  12. }
  13. public override void OnDisable()
  14. {
  15. GraphicsUtils.Destroy(this.m_SpectrumLut);
  16. this.m_SpectrumLut = null;
  17. }
  18. public override void Prepare(Material uberMaterial)
  19. {
  20. ChromaticAberrationModel.Settings settings = base.model.settings;
  21. Texture2D texture2D = settings.spectralTexture;
  22. if (texture2D == null)
  23. {
  24. if (this.m_SpectrumLut == null)
  25. {
  26. this.m_SpectrumLut = new Texture2D(3, 1, TextureFormat.RGB24, false)
  27. {
  28. name = "Chromatic Aberration Spectrum Lookup",
  29. filterMode = FilterMode.Bilinear,
  30. wrapMode = TextureWrapMode.Clamp,
  31. anisoLevel = 0,
  32. hideFlags = HideFlags.DontSave
  33. };
  34. Color[] pixels = new Color[]
  35. {
  36. new Color(1f, 0f, 0f),
  37. new Color(0f, 1f, 0f),
  38. new Color(0f, 0f, 1f)
  39. };
  40. this.m_SpectrumLut.SetPixels(pixels);
  41. this.m_SpectrumLut.Apply();
  42. }
  43. texture2D = this.m_SpectrumLut;
  44. }
  45. uberMaterial.EnableKeyword("CHROMATIC_ABERRATION");
  46. uberMaterial.SetFloat(ChromaticAberrationComponent.Uniforms._ChromaticAberration_Amount, settings.intensity * 0.03f);
  47. uberMaterial.SetTexture(ChromaticAberrationComponent.Uniforms._ChromaticAberration_Spectrum, texture2D);
  48. }
  49. private Texture2D m_SpectrumLut;
  50. private static class Uniforms
  51. {
  52. internal static readonly int _ChromaticAberration_Amount = Shader.PropertyToID("_ChromaticAberration_Amount");
  53. internal static readonly int _ChromaticAberration_Spectrum = Shader.PropertyToID("_ChromaticAberration_Spectrum");
  54. }
  55. }
  56. }