public static void Encode(Texture2D src, Texture2D dst, bool gpu = false, YCgACoFormat format = YCgACoFormat.CgACoY_DontChange, int quality = 100) { var pixels = src.GetPixels(); Resize(src, dst, format); var resized = src.width != dst.width || src.height != dst.height; if (gpu) { // TODO: Force mipmap and trilinear when resized. var shader = Shader.Find("Hidden/RGBA to CgACoY"); var mat = new Material(shader); var temp = RenderTexture.GetTemporary(dst.width, dst.height); Graphics.Blit(src, temp, mat); dst.ReadPixels(new Rect(0, 0, dst.width, dst.height), 0, 0); RenderTexture.ReleaseTemporary(temp); Object.DestroyImmediate(mat); } else { if (resized) { var srcPixels = pixels; pixels = dst.GetPixels(); Shrink(srcPixels, pixels, src.width, dst.width); } RGBAToCgACoY(pixels, pixels); dst.SetPixels(pixels); } Compress(dst, format, quality); }
static void Resize(Texture src, Texture2D dst, YCgACoFormat format) { var width = src.width; var height = src.height; switch (format) { case YCgACoFormat.CgACo_RGB24_Half_6bpp: case YCgACoFormat.CgACo_RGB565_Half_4bpp: width /= 2; height /= 2; break; } if (dst.width != width || dst.height != height) { dst.Resize(width, height); } }
static void Compress(Texture2D dst, YCgACoFormat format, int quality) { var texFormat = dst.format; switch (format) { case YCgACoFormat.Y_Alpha8_8bpp: texFormat = TextureFormat.Alpha8; break; case YCgACoFormat.CgACo_PVRTC_4bpp: texFormat = TextureFormat.PVRTC_RGB4; break; case YCgACoFormat.CgACo_PVRTC_2bpp: texFormat = TextureFormat.PVRTC_RGBA2; break; case YCgACoFormat.CgACo_ETC_4bpp: texFormat = TextureFormat.ETC_RGB4; break; case YCgACoFormat.CgACo_DXT_4bpp: texFormat = TextureFormat.DXT1; break; case YCgACoFormat.CgACo_RGB24_Half_6bpp: texFormat = TextureFormat.RGB24; break; case YCgACoFormat.CgACo_RGB565_Half_4bpp: case YCgACoFormat.CgACo_RGB565_16bpp: texFormat = TextureFormat.RGB565; break; case YCgACoFormat.CgACoY_DontChange: return; } EditorUtility.CompressTexture(dst, texFormat, quality); }
static void Resize(Texture src, Texture2D dst, YCgACoFormat format) { var width = src.width; var height = src.height; switch (format) { case YCgACoFormat.CgACo_RGB24_Half_6bpp: case YCgACoFormat.CgACo_RGB565_Half_4bpp: width /= 2; height /= 2; break; } if (dst.width != width || dst.height != height) dst.Resize(width, height); }