BloomOptimized.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.ImageEffects
  4. {
  5. [ExecuteInEditMode]
  6. [RequireComponent (typeof(Camera))]
  7. [AddComponentMenu ("Image Effects/Bloom and Glow/Bloom (Optimized)")]
  8. public class BloomOptimized : PostEffectsBase
  9. {
  10. public enum Resolution
  11. {
  12. Low = 0,
  13. High = 1,
  14. }
  15. public enum BlurType
  16. {
  17. Standard = 0,
  18. Sgx = 1,
  19. }
  20. [Range(0.0f, 1.5f)]
  21. public float threshold = 0.25f;
  22. [Range(0.0f, 2.5f)]
  23. public float intensity = 0.75f;
  24. [Range(0.25f, 5.5f)]
  25. public float blurSize = 1.0f;
  26. Resolution resolution = Resolution.Low;
  27. [Range(1, 4)]
  28. public int blurIterations = 1;
  29. public BlurType blurType= BlurType.Standard;
  30. public Shader fastBloomShader = null;
  31. private Material fastBloomMaterial = null;
  32. public override bool CheckResources ()
  33. {
  34. CheckSupport (false);
  35. fastBloomMaterial = CheckShaderAndCreateMaterial (fastBloomShader, fastBloomMaterial);
  36. if (!isSupported)
  37. ReportAutoDisable ();
  38. return isSupported;
  39. }
  40. void OnDisable ()
  41. {
  42. if (fastBloomMaterial)
  43. DestroyImmediate (fastBloomMaterial);
  44. }
  45. void OnRenderImage (RenderTexture source, RenderTexture destination)
  46. {
  47. if (CheckResources() == false)
  48. {
  49. Graphics.Blit (source, destination);
  50. return;
  51. }
  52. int divider = resolution == Resolution.Low ? 4 : 2;
  53. float widthMod = resolution == Resolution.Low ? 0.5f : 1.0f;
  54. fastBloomMaterial.SetVector ("_Parameter", new Vector4 (blurSize * widthMod, 0.0f, threshold, intensity));
  55. source.filterMode = FilterMode.Bilinear;
  56. var rtW= source.width/divider;
  57. var rtH= source.height/divider;
  58. // downsample
  59. RenderTexture rt = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
  60. rt.filterMode = FilterMode.Bilinear;
  61. Graphics.Blit (source, rt, fastBloomMaterial, 1);
  62. var passOffs= blurType == BlurType.Standard ? 0 : 2;
  63. for(int i = 0; i < blurIterations; i++)
  64. {
  65. fastBloomMaterial.SetVector ("_Parameter", new Vector4 (blurSize * widthMod + (i*1.0f), 0.0f, threshold, intensity));
  66. // vertical blur
  67. RenderTexture rt2 = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
  68. rt2.filterMode = FilterMode.Bilinear;
  69. Graphics.Blit (rt, rt2, fastBloomMaterial, 2 + passOffs);
  70. RenderTexture.ReleaseTemporary (rt);
  71. rt = rt2;
  72. // horizontal blur
  73. rt2 = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
  74. rt2.filterMode = FilterMode.Bilinear;
  75. Graphics.Blit (rt, rt2, fastBloomMaterial, 3 + passOffs);
  76. RenderTexture.ReleaseTemporary (rt);
  77. rt = rt2;
  78. }
  79. fastBloomMaterial.SetTexture ("_Bloom", rt);
  80. Graphics.Blit (source, destination, fastBloomMaterial, 0);
  81. RenderTexture.ReleaseTemporary (rt);
  82. }
  83. }
  84. }