Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public static class Texture2DExpansion
- {
- public static Texture2D Blur(this Texture2D image, int blurSize)
- {
- Texture2D blurred = new Texture2D(image.width, image.height);
- // foreach pixel
- for (int xx = 0; xx < image.width; xx++)
- {
- for (int yy = 0; yy < image.height; yy++)
- {
- float avgR = 0, avgG = 0, avgB = 0, avgA = 0;
- int blurPixelCount = 0;
- // average the color
- for (int x = xx; (x < xx + blurSize && x < image.width); x++)
- {
- for (int y = yy; (y < yy + blurSize && y < image.height); y++)
- {
- Color pixel = image.GetPixel(x, y);
- avgR += pixel.r;
- avgG += pixel.g;
- avgB += pixel.b;
- avgA += pixel.a;
- blurPixelCount++;
- }
- }
- avgR = avgR / blurPixelCount;
- avgG = avgG / blurPixelCount;
- avgB = avgB / blurPixelCount;
- avgA = avgA / blurPixelCount;
- // after averaging, set pixel to that color
- for (int x = xx; x < xx + blurSize && x < image.width; x++)
- {
- for (int y = yy; y < yy + blurSize && y < image.height; y++)
- {
- blurred.SetPixel(x, y, new Color(avgR, avgG, avgB, avgA));
- }
- }
- }
- }
- blurred.Apply();
- return blurred;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement