public virtual Sprite MakeSprite(Vector2Int textureSize, float pixelsPerUnit, List <Color> colorsToBlendTo, List <Vector2Int> pixelsToBlendFrom) { Texture2D texture = (Texture2D)GameManager.Clone(Texture2D.whiteTexture); texture.Resize(textureSize.x, textureSize.y); for (int x = 0; x < textureSize.x; x++) { for (int y = 0; y < textureSize.y; y++) { Vector2Int pixel = new Vector2Int(x, y); Color color; int indexOfPixel = pixelsToBlendFrom.IndexOf(pixel); if (indexOfPixel != -1) { List <Color> colors = new List <Color>(); do { colors.Add(colorsToBlendTo[indexOfPixel]); colorsToBlendTo.RemoveAt(indexOfPixel); pixelsToBlendFrom.RemoveAt(indexOfPixel); indexOfPixel = pixelsToBlendFrom.IndexOf(pixel); } while (indexOfPixel != -1); color = ColorExtensions.GetAverage(colors.ToArray()); } else { color = Evaluate(((Vector2)pixel).Divide((Vector2)textureSize)); } texture.SetPixel(x, y, color); } } texture.Apply(); return(Sprite.Create(texture, Rect.MinMaxRect(0, 0, textureSize.x, textureSize.y), Vector2.one / 2, pixelsPerUnit)); }
public virtual Color GetColor(Vector2Int position) { List <Color> colors = new List <Color>(); for (int x = Mathf.Clamp(position.x - blurRadius, 0, size.x); x <= Mathf.Clamp(position.x + blurRadius, 0, size.x - 1); x++) { for (int y = Mathf.Clamp(position.y - blurRadius, 0, size.y); y <= Mathf.Clamp(position.y + blurRadius, 0, size.y - 1); y++) { if (new Vector2Int(x, y) != position) { colors.Add(this.colors[x + y * size.x]); } } } return(ColorExtensions.GetAverage(colors.ToArray())); }
void Blur(int blurRange) { Color[] newColors = new Color[colors.Length]; for (int x = 0; x < textureSize.x; x++) { for (int y = 0; y < textureSize.y; y++) { List <Color> _colors = new List <Color>(); for (int x2 = Mathf.Clamp(x - blurRange, 0, textureSize.x - 1); x2 <= Mathf.Clamp(x + blurRange, 0, textureSize.x - 1); x2++) { for (int y2 = Mathf.Clamp(y - blurRange, 0, textureSize.y - 1); y2 <= Mathf.Clamp(y + blurRange, 0, textureSize.y - 1); y2++) { _colors.Add(colors[x2 + y2 * textureSize.x]); } } newColors[x + y * textureSize.x] = ColorExtensions.GetAverage(_colors.ToArray()); } } colors = newColors; }