public static Texture2D Copy(this Texture2D source, TextureFormat format = TextureFormat.RGBA32, CopyFilterMode filter = CopyFilterMode.Source, bool getSafePixels = false) { if (source == null) { return(null); } Color[] pixels = (getSafePixels == true) ? source.GetSafePixels() : source.GetPixels(); var result = new Texture2D(source.width, source.height, format, false); result.name = source.name; switch (filter) { case CopyFilterMode.Source: result.filterMode = source.filterMode; break; case CopyFilterMode.Point: result.filterMode = FilterMode.Point; break; case CopyFilterMode.Bilinear: result.filterMode = FilterMode.Bilinear; break; case CopyFilterMode.Trilinear: result.filterMode = FilterMode.Trilinear; break; } if (pixels != null) { result.SetPixels(pixels); result.Apply(); } return(result); }
public static bool HasAlpha(this Texture2D source, bool getSafePixels = false) { bool hasAlpha = source.alphaIsTransparency; Color[] apixels = (getSafePixels == true) ? source.GetSafePixels(new Rect(0, 0, source.width, source.height)) : source.GetPixels(0, 0, source.width, source.height); for (int index = 0; index < apixels.Length; index++) { hasAlpha |= apixels[index].a <= 0.99999f; } return(hasAlpha); }
public static void InvertAlpha(this Texture2D source, bool getSafePixels = false) { if (source == null) { return; } Color[] colors = (getSafePixels == true) ? source.GetSafePixels(): source.GetPixels(); for (int index = 0; index < colors.Length; index++) { colors[index].a = (1.0f - colors[index].a); } source.SetPixels(colors); source.Apply(); }
public static void ScaleAlpha(this Texture2D source, float scale, bool getSafePixels = false) { if (source == null) { return; } Color[] colors = (getSafePixels == true) ? source.GetSafePixels(): source.GetPixels(); for (int index = 0; index < colors.Length; index++) { colors[index].a = Mathf.Clamp((colors[index].a * scale), 0.0f, 1.0f); } source.SetPixels(colors); source.Apply(); }
public static void MakeGrayscale(this Texture2D source, bool getSafePixels = false) { if (source == null) { return; } Color[] colors = (getSafePixels == true) ? source.GetSafePixels(): source.GetPixels(); Color[] pixels = new Color[source.width * source.height]; for (int index = 0; index < pixels.Length; index++) { float gray = colors[index].grayscale; pixels[index] = new Color(gray, gray, gray, 1.0f); } source.SetPixels(pixels); source.Apply(); }
public static void MakeAlpha(this Texture2D source, float alpha = 1.0f, bool getSafePixels = false) { if (source == null) { return; } Color[] colors = (getSafePixels == true) ? source.GetSafePixels(): source.GetPixels(); Color[] pixels = new Color[source.width * source.height]; for (int index = 0; index < pixels.Length; index++) { pixels[index].r = Mathf.Clamp(colors[index].r, 0.0f, 1.0f); pixels[index].g = Mathf.Clamp(colors[index].g, 0.0f, 1.0f); pixels[index].b = Mathf.Clamp(colors[index].b, 0.0f, 1.0f); pixels[index].a = alpha; } source.SetPixels(pixels); source.Apply(); }