MobileBloom.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma strict
  2. @script ExecuteInEditMode
  3. @script RequireComponent (Camera)
  4. @script AddComponentMenu ("Image Effects/Mobile Bloom V2")
  5. public var intensity : float = 0.7f;
  6. public var threshhold : float = 0.75f;
  7. public var blurWidth : float = 1.0f;
  8. public var extraBlurry : boolean = false;
  9. // image effects materials for internal use
  10. public var bloomMaterial : Material = null;
  11. private var supported : boolean = false;
  12. private var tempRtA : RenderTexture = null;
  13. private var tempRtB : RenderTexture = null;
  14. function Supported () : boolean {
  15. if(supported) return true;
  16. supported = (SystemInfo.supportsImageEffects && SystemInfo.supportsRenderTextures && bloomMaterial.shader.isSupported);
  17. return supported;
  18. }
  19. function CreateBuffers () {
  20. if (!tempRtA) {
  21. tempRtA = new RenderTexture (Screen.width / 4, Screen.height / 4, 0);
  22. tempRtA.hideFlags = HideFlags.DontSave;
  23. }
  24. if (!tempRtB) {
  25. tempRtB = new RenderTexture (Screen.width / 4, Screen.height / 4, 0);
  26. tempRtB.hideFlags = HideFlags.DontSave;
  27. }
  28. }
  29. function OnDisable () {
  30. if (tempRtA) {
  31. DestroyImmediate (tempRtA);
  32. tempRtA = null;
  33. }
  34. if (tempRtB) {
  35. DestroyImmediate (tempRtB);
  36. tempRtB = null;
  37. }
  38. }
  39. function EarlyOutIfNotSupported (source : RenderTexture, destination : RenderTexture) : boolean {
  40. if (!Supported ()) {
  41. enabled = false;
  42. Graphics.Blit (source, destination);
  43. return true;
  44. }
  45. return false;
  46. }
  47. function OnRenderImage (source : RenderTexture, destination : RenderTexture) {
  48. CreateBuffers ();
  49. if (EarlyOutIfNotSupported (source, destination))
  50. return;
  51. // prepare data
  52. bloomMaterial.SetVector ("_Parameter", Vector4 (0.0f, 0.0f, threshhold, intensity / (1.0f - threshhold)));
  53. // ds & blur
  54. var oneOverW : float = 1.0f / (source.width * 1.0f);
  55. var oneOverH : float = 1.0f / (source.height * 1.0f);
  56. bloomMaterial.SetVector("_OffsetsA", Vector4(1.5f*oneOverW,1.5f*oneOverH,-1.5f*oneOverW,1.5f*oneOverH));
  57. bloomMaterial.SetVector("_OffsetsB", Vector4(-1.5f*oneOverW,-1.5f*oneOverH,1.5f*oneOverW,-1.5f*oneOverH));
  58. Graphics.Blit (source, tempRtB, bloomMaterial, 1);
  59. oneOverW *= 4.0f * blurWidth;
  60. oneOverH *= 4.0f * blurWidth;
  61. bloomMaterial.SetVector("_OffsetsA", Vector4(1.5f*oneOverW,0.0f,-1.5f*oneOverW,0.0f));
  62. bloomMaterial.SetVector("_OffsetsB", Vector4(0.5f*oneOverW,0.0f,-0.5f*oneOverW,0.0f));
  63. Graphics.Blit (tempRtB, tempRtA, bloomMaterial, 2);
  64. bloomMaterial.SetVector("_OffsetsA", Vector4(0.0f,1.5f*oneOverH,0.0f,-1.5f*oneOverH));
  65. bloomMaterial.SetVector("_OffsetsB", Vector4(0.0f,0.5f*oneOverH,0.0f,-0.5f*oneOverH));
  66. Graphics.Blit (tempRtA, tempRtB, bloomMaterial, 2);
  67. if(extraBlurry) {
  68. bloomMaterial.SetVector("_OffsetsA", Vector4(1.5f*oneOverW,0.0f,-1.5f*oneOverW,0.0f));
  69. bloomMaterial.SetVector("_OffsetsB", Vector4(0.5f*oneOverW,0.0f,-0.5f*oneOverW,0.0f));
  70. Graphics.Blit (tempRtB, tempRtA, bloomMaterial, 2);
  71. bloomMaterial.SetVector("_OffsetsA", Vector4(0.0f,1.5f*oneOverH,0.0f,-1.5f*oneOverH));
  72. bloomMaterial.SetVector("_OffsetsB", Vector4(0.0f,0.5f*oneOverH,0.0f,-0.5f*oneOverH));
  73. Graphics.Blit (tempRtA, tempRtB, bloomMaterial, 2);
  74. }
  75. // bloomMaterial
  76. bloomMaterial.SetTexture ("_Bloom", tempRtB);
  77. Graphics.Blit (source, destination, bloomMaterial, 0);
  78. }