Advertisement
BlueBear

Blur.cs

Dec 25th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public static class Texture2DExpansion
  4. {
  5.     public static Texture2D Blur(this Texture2D image, int blurSize)
  6.     {
  7.             Texture2D blurred = new Texture2D(image.width, image.height);
  8.             // foreach pixel
  9.         for (int xx = 0; xx < image.width; xx++)
  10.             {
  11.                     for (int yy = 0; yy < image.height; yy++)
  12.                     {
  13.                         float avgR = 0, avgG = 0, avgB = 0, avgA = 0;
  14.                         int blurPixelCount = 0;
  15.                         // average the color
  16.                         for (int x = xx; (x < xx + blurSize && x < image.width); x++)
  17.                         {
  18.                                 for (int y = yy; (y < yy + blurSize && y < image.height); y++)
  19.                                 {
  20.                                     Color pixel = image.GetPixel(x, y);
  21.                                     avgR += pixel.r;
  22.                                     avgG += pixel.g;
  23.                                     avgB += pixel.b;
  24.                                     avgA += pixel.a;
  25.                                     blurPixelCount++;
  26.                                 }
  27.                         }
  28.                         avgR = avgR / blurPixelCount;
  29.                         avgG = avgG / blurPixelCount;
  30.                         avgB = avgB / blurPixelCount;
  31.                 avgA = avgA / blurPixelCount;
  32.                 // after averaging, set pixel to that color
  33.  
  34.                         for (int x = xx; x < xx + blurSize && x < image.width; x++)
  35.                 {
  36.                                 for (int y = yy; y < yy + blurSize && y < image.height; y++)
  37.                         {
  38.                                     blurred.SetPixel(x, y, new Color(avgR, avgG, avgB, avgA));
  39.                         }
  40.                 }
  41.                 }
  42.             }
  43.             blurred.Apply();
  44.             return blurred;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement