Blur.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.ImageEffects
  4. {
  5. [ExecuteInEditMode]
  6. [AddComponentMenu("Image Effects/Blur/Blur")]
  7. public class Blur : MonoBehaviour
  8. {
  9. /// Blur iterations - larger number means more blur.
  10. [Range(0,10)]
  11. public int iterations = 3;
  12. /// Blur spread for each iteration. Lower values
  13. /// give better looking blur, but require more iterations to
  14. /// get large blurs. Value is usually between 0.5 and 1.0.
  15. [Range(0.0f,1.0f)]
  16. public float blurSpread = 0.6f;
  17. // --------------------------------------------------------
  18. // The blur iteration shader.
  19. // Basically it just takes 4 texture samples and averages them.
  20. // By applying it repeatedly and spreading out sample locations
  21. // we get a Gaussian blur approximation.
  22. public Shader blurShader = null;
  23. static Material m_Material = null;
  24. protected Material material {
  25. get {
  26. if (m_Material == null) {
  27. m_Material = new Material(blurShader);
  28. m_Material.hideFlags = HideFlags.DontSave;
  29. }
  30. return m_Material;
  31. }
  32. }
  33. protected void OnDisable() {
  34. if ( m_Material ) {
  35. DestroyImmediate( m_Material );
  36. }
  37. }
  38. // --------------------------------------------------------
  39. protected void Start()
  40. {
  41. // Disable if we don't support image effects
  42. if (!SystemInfo.supportsImageEffects) {
  43. enabled = false;
  44. return;
  45. }
  46. // Disable if the shader can't run on the users graphics card
  47. if (!blurShader || !material.shader.isSupported) {
  48. enabled = false;
  49. return;
  50. }
  51. }
  52. // Performs one blur iteration.
  53. public void FourTapCone (RenderTexture source, RenderTexture dest, int iteration)
  54. {
  55. float off = 0.5f + iteration*blurSpread;
  56. Graphics.BlitMultiTap (source, dest, material,
  57. new Vector2(-off, -off),
  58. new Vector2(-off, off),
  59. new Vector2( off, off),
  60. new Vector2( off, -off)
  61. );
  62. }
  63. // Downsamples the texture to a quarter resolution.
  64. private void DownSample4x (RenderTexture source, RenderTexture dest)
  65. {
  66. float off = 1.0f;
  67. Graphics.BlitMultiTap (source, dest, material,
  68. new Vector2(-off, -off),
  69. new Vector2(-off, off),
  70. new Vector2( off, off),
  71. new Vector2( off, -off)
  72. );
  73. }
  74. // Called by the camera to apply the image effect
  75. void OnRenderImage (RenderTexture source, RenderTexture destination) {
  76. int rtW = source.width/4;
  77. int rtH = source.height/4;
  78. RenderTexture buffer = RenderTexture.GetTemporary(rtW, rtH, 0);
  79. // Copy source to the 4x4 smaller texture.
  80. DownSample4x (source, buffer);
  81. // Blur the small texture
  82. for(int i = 0; i < iterations; i++)
  83. {
  84. RenderTexture buffer2 = RenderTexture.GetTemporary(rtW, rtH, 0);
  85. FourTapCone (buffer, buffer2, i);
  86. RenderTexture.ReleaseTemporary(buffer);
  87. buffer = buffer2;
  88. }
  89. Graphics.Blit(buffer, destination);
  90. RenderTexture.ReleaseTemporary(buffer);
  91. }
  92. }
  93. }