ChromaticAberrationComponent.cs 2.2 KB

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