Advertisement
morphism

Blur Texture

Jun 30th, 2020
1,455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.54 KB | None | 0 0
  1.     class LinearBlur
  2.         {
  3.             private float _rSum = 0;
  4.             private float _gSum = 0;
  5.             private float _bSum = 0;
  6.      
  7.             private Texture2D _sourceImage;
  8.             private int _sourceWidth;
  9.             private int _sourceHeight;
  10.             private int _windowSize;
  11.      
  12.             public Texture2D Blur(Texture2D image, int radius, int iterations)
  13.             {
  14.                 _windowSize = radius * 2 + 1;
  15.                 _sourceWidth = image.width;
  16.                 _sourceHeight = image.height;
  17.      
  18.                 var tex = image;
  19.      
  20.                 for (var i = 0; i < iterations; i++)
  21.                 {
  22.                     tex = OneDimensialBlur(tex, radius, true);
  23.                     tex = OneDimensialBlur(tex, radius, false);
  24.                 }
  25.      
  26.                 return tex;
  27.             }
  28.      
  29.             private Texture2D OneDimensialBlur(Texture2D image, int radius, bool horizontal)
  30.             {
  31.                 _sourceImage = image;
  32.      
  33.                 var blurred = new Texture2D(image.width, image.height, image.format, false);
  34.      
  35.                 if (horizontal)
  36.                 {
  37.                     for (int imgY = 0; imgY < _sourceHeight; ++imgY)
  38.                     {
  39.                         ResetSum();
  40.      
  41.                         for (int imgX = 0; imgX < _sourceWidth; imgX++)
  42.                         {
  43.                             if (imgX == 0)
  44.                                 for (int x = radius * -1; x <= radius; ++x)
  45.                                     AddPixel(GetPixelWithXCheck(x, imgY));
  46.                             else
  47.                             {
  48.                                 var toExclude = GetPixelWithXCheck(imgX - radius - 1, imgY);
  49.                                 var toInclude = GetPixelWithXCheck(imgX + radius, imgY);
  50.      
  51.                                 SubstPixel(toExclude);
  52.                                 AddPixel(toInclude);
  53.                             }
  54.      
  55.                             blurred.SetPixel(imgX, imgY, CalcPixelFromSum());
  56.                         }
  57.                     }
  58.                 }
  59.      
  60.                 else
  61.                 {
  62.                     for (int imgX = 0; imgX < _sourceWidth; imgX++)
  63.                     {
  64.                         ResetSum();
  65.      
  66.                         for (int imgY = 0; imgY < _sourceHeight; ++imgY)
  67.                         {
  68.                             if (imgY == 0)
  69.                                 for (int y = radius * -1; y <= radius; ++y)
  70.                                     AddPixel(GetPixelWithYCheck(imgX, y));
  71.                             else
  72.                             {
  73.                                 var toExclude = GetPixelWithYCheck(imgX, imgY - radius - 1);
  74.                                 var toInclude = GetPixelWithYCheck(imgX, imgY + radius);
  75.      
  76.                                 SubstPixel(toExclude);
  77.                                 AddPixel(toInclude);
  78.                             }
  79.      
  80.                             blurred.SetPixel(imgX, imgY, CalcPixelFromSum());
  81.                         }
  82.                     }
  83.                 }
  84.      
  85.                 blurred.Apply();
  86.                 return blurred;
  87.             }
  88.      
  89.             private Color GetPixelWithXCheck(int x, int y)
  90.             {
  91.                 if (x <= 0) return _sourceImage.GetPixel(0, y);
  92.                 if (x >= _sourceWidth) return _sourceImage.GetPixel(_sourceWidth - 1, y);
  93.                 return _sourceImage.GetPixel(x, y);
  94.             }
  95.      
  96.             private Color GetPixelWithYCheck(int x, int y)
  97.             {
  98.                 if (y <= 0) return _sourceImage.GetPixel(x, 0);
  99.                 if (y >= _sourceHeight) return _sourceImage.GetPixel(x, _sourceHeight - 1);
  100.                 return _sourceImage.GetPixel(x, y);
  101.             }
  102.      
  103.             private void AddPixel(Color pixel)
  104.             {
  105.                 _rSum += pixel.r;
  106.                 _gSum += pixel.g;
  107.                 _bSum += pixel.b;
  108.             }
  109.      
  110.             private void SubstPixel(Color pixel)
  111.             {
  112.                 _rSum -= pixel.r;
  113.                 _gSum -= pixel.g;
  114.                 _bSum -= pixel.b;
  115.             }
  116.      
  117.             private void ResetSum()
  118.             {
  119.                 _rSum = 0.0f;
  120.                 _gSum = 0.0f;
  121.                 _bSum = 0.0f;
  122.             }
  123.      
  124.             Color CalcPixelFromSum()
  125.             {
  126.                 return new Color(_rSum / _windowSize, _gSum / _windowSize, _bSum / _windowSize);
  127.             }
  128.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement