BloomComponent.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. namespace UnityEngine.PostProcessing
  2. {
  3. public sealed class BloomComponent : PostProcessingComponentRenderTexture<BloomModel>
  4. {
  5. static class Uniforms
  6. {
  7. internal static readonly int _AutoExposure = Shader.PropertyToID("_AutoExposure");
  8. internal static readonly int _Threshold = Shader.PropertyToID("_Threshold");
  9. internal static readonly int _Curve = Shader.PropertyToID("_Curve");
  10. internal static readonly int _PrefilterOffs = Shader.PropertyToID("_PrefilterOffs");
  11. internal static readonly int _SampleScale = Shader.PropertyToID("_SampleScale");
  12. internal static readonly int _BaseTex = Shader.PropertyToID("_BaseTex");
  13. internal static readonly int _BloomTex = Shader.PropertyToID("_BloomTex");
  14. internal static readonly int _Bloom_Settings = Shader.PropertyToID("_Bloom_Settings");
  15. internal static readonly int _Bloom_DirtTex = Shader.PropertyToID("_Bloom_DirtTex");
  16. internal static readonly int _Bloom_DirtIntensity = Shader.PropertyToID("_Bloom_DirtIntensity");
  17. }
  18. const int k_MaxPyramidBlurLevel = 16;
  19. readonly RenderTexture[] m_BlurBuffer1 = new RenderTexture[k_MaxPyramidBlurLevel];
  20. readonly RenderTexture[] m_BlurBuffer2 = new RenderTexture[k_MaxPyramidBlurLevel];
  21. public override bool active
  22. {
  23. get
  24. {
  25. return model.enabled
  26. && model.settings.bloom.intensity > 0f
  27. && !context.interrupted;
  28. }
  29. }
  30. public void Prepare(RenderTexture source, Material uberMaterial, Texture autoExposure)
  31. {
  32. var bloom = model.settings.bloom;
  33. var lensDirt = model.settings.lensDirt;
  34. var material = context.materialFactory.Get("Hidden/Post FX/Bloom");
  35. material.shaderKeywords = null;
  36. // Apply auto exposure before the prefiltering pass
  37. material.SetTexture(Uniforms._AutoExposure, autoExposure);
  38. // Do bloom on a half-res buffer, full-res doesn't bring much and kills performances on
  39. // fillrate limited platforms
  40. var tw = context.width / 2;
  41. var th = context.height / 2;
  42. // Blur buffer format
  43. // TODO: Extend the use of RGBM to the whole chain for mobile platforms
  44. var useRGBM = Application.isMobilePlatform;
  45. var rtFormat = useRGBM
  46. ? RenderTextureFormat.Default
  47. : RenderTextureFormat.DefaultHDR;
  48. // Determine the iteration count
  49. float logh = Mathf.Log(th, 2f) + bloom.radius - 8f;
  50. int logh_i = (int)logh;
  51. int iterations = Mathf.Clamp(logh_i, 1, k_MaxPyramidBlurLevel);
  52. // Uupdate the shader properties
  53. float lthresh = bloom.thresholdLinear;
  54. material.SetFloat(Uniforms._Threshold, lthresh);
  55. float knee = lthresh * bloom.softKnee + 1e-5f;
  56. var curve = new Vector3(lthresh - knee, knee * 2f, 0.25f / knee);
  57. material.SetVector(Uniforms._Curve, curve);
  58. material.SetFloat(Uniforms._PrefilterOffs, bloom.antiFlicker ? -0.5f : 0f);
  59. float sampleScale = 0.5f + logh - logh_i;
  60. material.SetFloat(Uniforms._SampleScale, sampleScale);
  61. // TODO: Probably can disable antiFlicker if TAA is enabled - need to do some testing
  62. if (bloom.antiFlicker)
  63. material.EnableKeyword("ANTI_FLICKER");
  64. // Prefilter pass
  65. var prefiltered = context.renderTextureFactory.Get(tw, th, 0, rtFormat);
  66. Graphics.Blit(source, prefiltered, material, 0);
  67. // Construct a mip pyramid
  68. var last = prefiltered;
  69. for (int level = 0; level < iterations; level++)
  70. {
  71. m_BlurBuffer1[level] = context.renderTextureFactory.Get(
  72. last.width / 2, last.height / 2, 0, rtFormat
  73. );
  74. int pass = (level == 0) ? 1 : 2;
  75. Graphics.Blit(last, m_BlurBuffer1[level], material, pass);
  76. last = m_BlurBuffer1[level];
  77. }
  78. // Upsample and combine loop
  79. for (int level = iterations - 2; level >= 0; level--)
  80. {
  81. var baseTex = m_BlurBuffer1[level];
  82. material.SetTexture(Uniforms._BaseTex, baseTex);
  83. m_BlurBuffer2[level] = context.renderTextureFactory.Get(
  84. baseTex.width, baseTex.height, 0, rtFormat
  85. );
  86. Graphics.Blit(last, m_BlurBuffer2[level], material, 3);
  87. last = m_BlurBuffer2[level];
  88. }
  89. var bloomTex = last;
  90. // Release the temporary buffers
  91. for (int i = 0; i < k_MaxPyramidBlurLevel; i++)
  92. {
  93. if (m_BlurBuffer1[i] != null)
  94. context.renderTextureFactory.Release(m_BlurBuffer1[i]);
  95. if (m_BlurBuffer2[i] != null && m_BlurBuffer2[i] != bloomTex)
  96. context.renderTextureFactory.Release(m_BlurBuffer2[i]);
  97. m_BlurBuffer1[i] = null;
  98. m_BlurBuffer2[i] = null;
  99. }
  100. context.renderTextureFactory.Release(prefiltered);
  101. // Push everything to the uber material
  102. uberMaterial.SetTexture(Uniforms._BloomTex, bloomTex);
  103. uberMaterial.SetVector(Uniforms._Bloom_Settings, new Vector2(sampleScale, bloom.intensity));
  104. if (lensDirt.intensity > 0f && lensDirt.texture != null)
  105. {
  106. uberMaterial.SetTexture(Uniforms._Bloom_DirtTex, lensDirt.texture);
  107. uberMaterial.SetFloat(Uniforms._Bloom_DirtIntensity, lensDirt.intensity);
  108. uberMaterial.EnableKeyword("BLOOM_LENS_DIRT");
  109. }
  110. else
  111. {
  112. uberMaterial.EnableKeyword("BLOOM");
  113. }
  114. }
  115. }
  116. }