Guest User

OptimizedBloom

a guest
Feb 2nd, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. namespace UnitySampleAssets.ImageEffects
  5. {
  6. [ExecuteInEditMode]
  7. [RequireComponent (typeof(Camera))]
  8. [AddComponentMenu ("Image Effects/Bloom and Glow/Bloom (Optimized)")]
  9. class BloomOptimized : PostEffectsBase
  10. {
  11.  
  12. public enum Resolution {
  13. Low = 0,
  14. High = 1,
  15. }
  16.  
  17. public enum BlurType {
  18. Standard = 0,
  19. Sgx = 1,
  20. }
  21.  
  22. [Range(0.0f, 1.5f)]
  23. public float threshhold = 0.25f;
  24. [Range(0.0f, 2.5f)]
  25. public float intensity = 0.75f;
  26.  
  27. [Range(0.25f, 5.5f)]
  28. public float blurSize = 1.0f;
  29.  
  30. Resolution resolution = Resolution.Low;
  31. [Range(1, 4)]
  32. public int blurIterations = 1;
  33.  
  34. public BlurType blurType= BlurType.Standard;
  35.  
  36. public Shader fastBloomShader = null;
  37. private Material fastBloomMaterial = null;
  38.  
  39.  
  40. public override bool CheckResources () {
  41. CheckSupport (false);
  42.  
  43. fastBloomMaterial = CheckShaderAndCreateMaterial (fastBloomShader, fastBloomMaterial);
  44.  
  45. if (!isSupported)
  46. ReportAutoDisable ();
  47. return isSupported;
  48. }
  49.  
  50. void OnDisable () {
  51. if (fastBloomMaterial)
  52. DestroyImmediate (fastBloomMaterial);
  53. }
  54.  
  55. void OnRenderImage (RenderTexture source, RenderTexture destination) {
  56. if (CheckResources() == false) {
  57. Graphics.Blit (source, destination);
  58. return;
  59. }
  60.  
  61. int divider = resolution == Resolution.Low ? 4 : 2;
  62. float widthMod = resolution == Resolution.Low ? 0.5f : 1.0f;
  63.  
  64. fastBloomMaterial.SetVector ("_Parameter", new Vector4 (blurSize * widthMod, 0.0f, threshhold, intensity));
  65. source.filterMode = FilterMode.Bilinear;
  66.  
  67. var rtW= source.width/divider;
  68. var rtH= source.height/divider;
  69.  
  70. // downsample
  71. RenderTexture rt = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
  72. rt.filterMode = FilterMode.Bilinear;
  73. Graphics.Blit (source, rt, fastBloomMaterial, 1);
  74.  
  75. var passOffs= blurType == BlurType.Standard ? 0 : 2;
  76.  
  77. for(int i = 0; i < blurIterations; i++) {
  78. fastBloomMaterial.SetVector ("_Parameter", new Vector4 (blurSize * widthMod + (i*1.0f), 0.0f, threshhold, intensity));
  79.  
  80. // vertical blur
  81. RenderTexture rt2 = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
  82. rt2.filterMode = FilterMode.Bilinear;
  83. Graphics.Blit (rt, rt2, fastBloomMaterial, 2 + passOffs);
  84. RenderTexture.ReleaseTemporary (rt);
  85. rt = rt2;
  86.  
  87. // horizontal blur
  88. rt2 = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
  89. rt2.filterMode = FilterMode.Bilinear;
  90. Graphics.Blit (rt, rt2, fastBloomMaterial, 3 + passOffs);
  91. RenderTexture.ReleaseTemporary (rt);
  92. rt = rt2;
  93. }
  94.  
  95. fastBloomMaterial.SetTexture ("_Bloom", rt);
  96.  
  97. Graphics.Blit (source, destination, fastBloomMaterial, 0);
  98.  
  99. RenderTexture.ReleaseTemporary (rt);
  100. }
  101. }
  102. }
Add Comment
Please, Sign In to add comment