public static void AssignImageToTexture(Bitmap sourceImage, AssetsChanger.Texture2DObject targetTexture, int targetWidth, int targetHeight, int targetMips = Int32.MaxValue, TextureConversionFormat format = TextureConversionFormat.Auto) { //right now "Auto" is always RGB24. Probably will automatically decide based on image size, but doing ETC2 sucks at the moment int mips; byte[] textureBytes; if (format == TextureConversionFormat.ETC2_RGB) { textureBytes = ConvertToETC2AndMipmap(sourceImage, targetWidth, targetHeight, targetMips, out mips); targetTexture.TextureFormat = Texture2DObject.TextureFormatType.ETC2_RGB; } else //always fall back to RGB24 { textureBytes = ConvertToRGBAndMipmap(sourceImage, targetWidth, targetHeight, targetMips, out mips); targetTexture.TextureFormat = AssetsChanger.Texture2DObject.TextureFormatType.RGB24; } targetTexture.ForcedFallbackFormat = 4; targetTexture.DownscaleFallback = false; targetTexture.Width = targetWidth; targetTexture.Height = targetHeight; targetTexture.CompleteImageSize = textureBytes.Length; targetTexture.MipCount = mips; targetTexture.IsReadable = false; targetTexture.StreamingMipmaps = false; targetTexture.StreamingMipmapsPriority = 0; targetTexture.ImageCount = 1; targetTexture.TextureDimension = 2; targetTexture.TextureSettings = new GLTextureSettings() { FilterMode = 2, Aniso = 1, MipBias = -1, WrapU = 1, WrapV = 1, WrapW = 0 }; targetTexture.LightmapFormat = 6; targetTexture.ColorSpace = 1; targetTexture.ImageData = textureBytes; targetTexture.StreamData = new StreamingInfo() { offset = 0, size = 0, path = "" }; }
public static Bitmap TextureToBitmap(AssetsChanger.Texture2DObject texture) { //does unity store mips of compressed textures individually, or does it compress the entire thing? switch (texture.TextureFormat) { case AssetsChanger.Texture2DObject.TextureFormatType.RGB24: return(ConvertFromRGB24ToBitmap(texture.ImageData, texture.Width, texture.Height, RotateFlipType.RotateNoneFlipXY)); //not really sure if it can do all these or not case AssetsChanger.Texture2DObject.TextureFormatType.ETC2_RGB: case AssetsChanger.Texture2DObject.TextureFormatType.ETC_RGB4: case AssetsChanger.Texture2DObject.TextureFormatType.ETC2_RGBA8: case AssetsChanger.Texture2DObject.TextureFormatType.ETC2_RGBA1: return(ConvertETC2ToBitmap(texture.ImageData, texture.Width, texture.Height)); default: throw new NotImplementedException($"Texture type {texture.ToString()} isn't currently supported."); } }