public static void SetTextureAlpha(this Texture2DArray dstArr, Texture2D src, Texture2D alpha, int dstCh, bool apply = true) /// Sets RGB to src RGB, and A to desaturated alpha RGB { if (alpha == null) { dstArr.SetTexture(src, dstCh, apply: apply); return; } //shortcut if alpha is null if (!src.IsReadable()) { src = src.ReadableClone(); } if (src.format.IsCompressed()) { src = src.UncompressedClone(); } if (src.width != dstArr.width || src.height != dstArr.height) { src = src.ResizedClone(dstArr.width, dstArr.height); } if (!alpha.IsReadable()) { alpha = alpha.ReadableClone(); } //if (!alpha.format.IsCompressed()) alpha = alpha.UncompressedClone(); //no need to change alpha format, we will read pixels anyways if (alpha.width != dstArr.width || alpha.height != dstArr.height) { alpha = alpha.ResizedClone(dstArr.width, dstArr.height); } Texture2D tmp = new Texture2D(src.width, src.height, TextureFormat.RGBA32, true, src.IsLinear()); int mipmapCount = src.mipmapCount; for (int m = 0; m < mipmapCount; m++) { Color[] srcColors = src.GetPixels(m); Color[] alphaColors = alpha.GetPixels(m); for (int i = 0; i < srcColors.Length; i++) { srcColors[i] = new Color(srcColors[i].r, srcColors[i].g, srcColors[i].b, alphaColors[i].r * 0.3f + alphaColors[i].r * 0.6f + alphaColors[i].r * 0.1f); } tmp.SetPixels(srcColors, m); } tmp.Apply(updateMipmaps: false); dstArr.SetTexture(tmp, dstCh, apply: apply); }
static public void Clear(this Texture2DArray texArr, int chNum) /// Fills channel with blank without removing it { Texture2D temp = new Texture2D(texArr.width, texArr.height, texArr.format, true, linear: texArr.IsLinear()); texArr.SetTexture(temp, chNum); }
static public void Switch(this Texture2DArray texArr, int num1, int num2) { if (num1 < 0 || num1 >= texArr.depth || num2 < 0 || num2 >= texArr.depth) { return; } Texture2D temp = texArr.GetTexture(num1); CopyTexture(texArr, num2, texArr, num1); texArr.SetTexture(temp, num2); }
public static Texture2DArray Add(Texture2DArray texArr, Texture2D tex) { Texture2DArray newArr = new Texture2DArray(texArr.width, texArr.height, texArr.depth + 1, texArr.format, true, linear: texArr.IsLinear()); newArr.name = texArr.name; CopyTextures(texArr, newArr, texArr.depth); newArr.SetTexture(tex, texArr.depth, apply: false); newArr.Apply(updateMipmaps: false); return(newArr); }
public void ApplySource(int ch) { #if UNITY_EDITOR if (srcArr[ch].alpha == null) { if (srcArr[ch].source != null) { texArr.SetTexture(srcArr[ch].source, ch, apply: true); } } else { if (srcArr[ch].source != null) { texArr.SetTextureAlpha(srcArr[ch].source, srcArr[ch].alpha, ch, apply: true); } } srcArr[ch].preview = null; //resetting preview not keeping it. It will be generated on next get #endif }
static public Texture2DArray Insert(Texture2DArray texArr, int pos, Texture2D tex) { bool linear = texArr.IsLinear(); if (texArr == null || texArr.depth == 0) { texArr = new Texture2DArray(tex.width, tex.height, 1, texArr.format, true, linear: linear); texArr.filterMode = FilterMode.Trilinear; texArr.SetTexture(tex, 0, apply: false); return(texArr); } if (pos > texArr.depth || pos < 0) { pos = texArr.depth; } Texture2DArray newArr = new Texture2DArray(texArr.width, texArr.height, texArr.depth + 1, texArr.format, true, linear: linear); newArr.name = texArr.name; if (pos != 0) { CopyTextures(texArr, newArr, pos); } if (pos != texArr.depth) { CopyTextures(texArr, pos, newArr, pos + 1, texArr.depth - pos); } if (tex != null) { newArr.SetTexture(tex, pos, apply: false); } newArr.Apply(updateMipmaps: false); return(newArr); }