BloomComponent.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. namespace UnityEngine.PostProcessing
  3. {
  4. public sealed class BloomComponent : PostProcessingComponentRenderTexture<BloomModel>
  5. {
  6. public override bool active
  7. {
  8. get
  9. {
  10. return base.model.enabled && base.model.settings.bloom.intensity > 0f && !this.context.interrupted;
  11. }
  12. }
  13. public void Prepare(RenderTexture source, Material uberMaterial, Texture autoExposure)
  14. {
  15. BloomModel.BloomSettings bloom = base.model.settings.bloom;
  16. BloomModel.LensDirtSettings lensDirt = base.model.settings.lensDirt;
  17. Material material = this.context.materialFactory.Get("Hidden/Post FX/Bloom");
  18. material.shaderKeywords = null;
  19. material.SetTexture(BloomComponent.Uniforms._AutoExposure, autoExposure);
  20. int width = this.context.width / 2;
  21. int num = this.context.height / 2;
  22. bool isMobilePlatform = Application.isMobilePlatform;
  23. RenderTextureFormat format = (!isMobilePlatform) ? RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default;
  24. float num2 = Mathf.Log((float)num, 2f) + bloom.radius - 8f;
  25. int num3 = (int)num2;
  26. int num4 = Mathf.Clamp(num3, 1, 16);
  27. float thresholdLinear = bloom.thresholdLinear;
  28. material.SetFloat(BloomComponent.Uniforms._Threshold, thresholdLinear);
  29. float num5 = thresholdLinear * bloom.softKnee + 1E-05f;
  30. Vector3 v = new Vector3(thresholdLinear - num5, num5 * 2f, 0.25f / num5);
  31. material.SetVector(BloomComponent.Uniforms._Curve, v);
  32. material.SetFloat(BloomComponent.Uniforms._PrefilterOffs, (!bloom.antiFlicker) ? 0f : -0.5f);
  33. float num6 = 0.5f + num2 - (float)num3;
  34. material.SetFloat(BloomComponent.Uniforms._SampleScale, num6);
  35. if (bloom.antiFlicker)
  36. {
  37. material.EnableKeyword("ANTI_FLICKER");
  38. }
  39. RenderTexture renderTexture = this.context.renderTextureFactory.Get(width, num, 0, format, RenderTextureReadWrite.Default, FilterMode.Bilinear, TextureWrapMode.Clamp, "FactoryTempTexture");
  40. Graphics.Blit(source, renderTexture, material, 0);
  41. RenderTexture renderTexture2 = renderTexture;
  42. for (int i = 0; i < num4; i++)
  43. {
  44. this.m_BlurBuffer1[i] = this.context.renderTextureFactory.Get(renderTexture2.width / 2, renderTexture2.height / 2, 0, format, RenderTextureReadWrite.Default, FilterMode.Bilinear, TextureWrapMode.Clamp, "FactoryTempTexture");
  45. int pass = (i != 0) ? 2 : 1;
  46. Graphics.Blit(renderTexture2, this.m_BlurBuffer1[i], material, pass);
  47. renderTexture2 = this.m_BlurBuffer1[i];
  48. }
  49. for (int j = num4 - 2; j >= 0; j--)
  50. {
  51. RenderTexture renderTexture3 = this.m_BlurBuffer1[j];
  52. material.SetTexture(BloomComponent.Uniforms._BaseTex, renderTexture3);
  53. this.m_BlurBuffer2[j] = this.context.renderTextureFactory.Get(renderTexture3.width, renderTexture3.height, 0, format, RenderTextureReadWrite.Default, FilterMode.Bilinear, TextureWrapMode.Clamp, "FactoryTempTexture");
  54. Graphics.Blit(renderTexture2, this.m_BlurBuffer2[j], material, 3);
  55. renderTexture2 = this.m_BlurBuffer2[j];
  56. }
  57. RenderTexture renderTexture4 = renderTexture2;
  58. for (int k = 0; k < 16; k++)
  59. {
  60. if (this.m_BlurBuffer1[k] != null)
  61. {
  62. this.context.renderTextureFactory.Release(this.m_BlurBuffer1[k]);
  63. }
  64. if (this.m_BlurBuffer2[k] != null && this.m_BlurBuffer2[k] != renderTexture4)
  65. {
  66. this.context.renderTextureFactory.Release(this.m_BlurBuffer2[k]);
  67. }
  68. this.m_BlurBuffer1[k] = null;
  69. this.m_BlurBuffer2[k] = null;
  70. }
  71. this.context.renderTextureFactory.Release(renderTexture);
  72. uberMaterial.SetTexture(BloomComponent.Uniforms._BloomTex, renderTexture4);
  73. uberMaterial.SetVector(BloomComponent.Uniforms._Bloom_Settings, new Vector2(num6, bloom.intensity));
  74. if (lensDirt.intensity > 0f && lensDirt.texture != null)
  75. {
  76. uberMaterial.SetTexture(BloomComponent.Uniforms._Bloom_DirtTex, lensDirt.texture);
  77. uberMaterial.SetFloat(BloomComponent.Uniforms._Bloom_DirtIntensity, lensDirt.intensity);
  78. uberMaterial.EnableKeyword("BLOOM_LENS_DIRT");
  79. }
  80. else
  81. {
  82. uberMaterial.EnableKeyword("BLOOM");
  83. }
  84. }
  85. private const int k_MaxPyramidBlurLevel = 16;
  86. private readonly RenderTexture[] m_BlurBuffer1 = new RenderTexture[16];
  87. private readonly RenderTexture[] m_BlurBuffer2 = new RenderTexture[16];
  88. private static class Uniforms
  89. {
  90. internal static readonly int _AutoExposure = Shader.PropertyToID("_AutoExposure");
  91. internal static readonly int _Threshold = Shader.PropertyToID("_Threshold");
  92. internal static readonly int _Curve = Shader.PropertyToID("_Curve");
  93. internal static readonly int _PrefilterOffs = Shader.PropertyToID("_PrefilterOffs");
  94. internal static readonly int _SampleScale = Shader.PropertyToID("_SampleScale");
  95. internal static readonly int _BaseTex = Shader.PropertyToID("_BaseTex");
  96. internal static readonly int _BloomTex = Shader.PropertyToID("_BloomTex");
  97. internal static readonly int _Bloom_Settings = Shader.PropertyToID("_Bloom_Settings");
  98. internal static readonly int _Bloom_DirtTex = Shader.PropertyToID("_Bloom_DirtTex");
  99. internal static readonly int _Bloom_DirtIntensity = Shader.PropertyToID("_Bloom_DirtIntensity");
  100. }
  101. }
  102. }