Blur.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 the shader can't run on the users graphics card
  42. if (!blurShader || !material.shader.isSupported) {
  43. enabled = false;
  44. return;
  45. }
  46. }
  47. // Performs one blur iteration.
  48. public void FourTapCone (RenderTexture source, RenderTexture dest, int iteration)
  49. {
  50. float off = 0.5f + iteration*blurSpread;
  51. Graphics.BlitMultiTap (source, dest, material,
  52. new Vector2(-off, -off),
  53. new Vector2(-off, off),
  54. new Vector2( off, off),
  55. new Vector2( off, -off)
  56. );
  57. }
  58. // Downsamples the texture to a quarter resolution.
  59. private void DownSample4x (RenderTexture source, RenderTexture dest)
  60. {
  61. float off = 1.0f;
  62. Graphics.BlitMultiTap (source, dest, material,
  63. new Vector2(-off, -off),
  64. new Vector2(-off, off),
  65. new Vector2( off, off),
  66. new Vector2( off, -off)
  67. );
  68. }
  69. // Called by the camera to apply the image effect
  70. void OnRenderImage (RenderTexture source, RenderTexture destination) {
  71. int rtW = source.width/4;
  72. int rtH = source.height/4;
  73. RenderTexture buffer = RenderTexture.GetTemporary(rtW, rtH, 0);
  74. // Copy source to the 4x4 smaller texture.
  75. DownSample4x (source, buffer);
  76. // Blur the small texture
  77. for(int i = 0; i < iterations; i++)
  78. {
  79. RenderTexture buffer2 = RenderTexture.GetTemporary(rtW, rtH, 0);
  80. FourTapCone (buffer, buffer2, i);
  81. RenderTexture.ReleaseTemporary(buffer);
  82. buffer = buffer2;
  83. }
  84. Graphics.Blit(buffer, destination);
  85. RenderTexture.ReleaseTemporary(buffer);
  86. }
  87. }
  88. }