public static TextureInfoWrapper DDSToTexture(TexInfo Texture, bool mipmaps, bool isCompressed, bool hasAlpha) { TextureConverter.InitImageBuffer(); FileStream imgStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read); imgStream.Position = 0; imgStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE); imgStream.Close(); TextureFormat format = TextureFormat.DXT1; if (hasAlpha && isCompressed) { format = TextureFormat.DXT5; } else if (hasAlpha) { format = TextureFormat.RGBA32; } else if (!isCompressed) { format = TextureFormat.RGB24; } Texture2D newTex = new Texture2D(Texture.width, Texture.height, format, mipmaps); newTex.LoadRawTextureData(imageBuffer); newTex.Apply(false, Texture.makeNotReadable); newTex.name = Texture.name; TextureInfoWrapper newTexInfo = new TextureInfoWrapper(newTex, Texture.isNormalMap, !Texture.makeNotReadable, isCompressed); newTexInfo.name = Texture.name; return(newTexInfo); }
public static void IMGToTexture(TexInfo Texture, bool mipmaps, bool isNormalFormat) { TextureInfoWrapper texture = Texture.texture; TextureConverter.InitImageBuffer(); FileStream imgStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read); imgStream.Position = 0; imgStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE); imgStream.Close(); Texture2D tex = texture.texture; tex.LoadImage(imageBuffer); bool convertToNormalFormat = texture.isNormalMap && !isNormalFormat ? true : false; bool hasMipmaps = tex.mipmapCount == 1 ? false : true; if (Texture.loadOriginalFirst) { Texture.Resize(tex.width, tex.height); } TextureFormat format = tex.format; if (texture.isNormalMap) { format = TextureFormat.ARGB32; } if (Texture.needsResize) { TextureConverter.Resize(texture, Texture.resizeWidth, Texture.resizeHeight, mipmaps, convertToNormalFormat); } else if (convertToNormalFormat || hasMipmaps != mipmaps || format != tex.format) { Color32[] pixels = tex.GetPixels32(); if (convertToNormalFormat) { for (int i = 0; i < pixels.Length; i++) { pixels[i].a = pixels[i].r; pixels[i].r = pixels[i].g; pixels[i].b = pixels[i].g; } } if (tex.format != format || hasMipmaps != mipmaps) { tex.Resize(tex.width, tex.height, format, mipmaps); } tex.SetPixels32(pixels); tex.Apply(mipmaps); } }
public static void TGAToTexture(TexInfo Texture, bool mipmaps) { GameDatabase.TextureInfo texture = Texture.texture; TextureConverter.InitImageBuffer(); FileStream tgaStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read); tgaStream.Position = 0; tgaStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE); tgaStream.Close(); byte imgType = imageBuffer[2]; int width = imageBuffer[12] | (imageBuffer[13] << 8); int height = imageBuffer[14] | (imageBuffer[15] << 8); if (Texture.loadOriginalFirst) { Texture.Resize(width, height); } int depth = imageBuffer[16]; bool alpha = depth == 32 ? true : false; TextureFormat texFormat = depth == 32 ? TextureFormat.RGBA32 : TextureFormat.RGB24; if (texture.isNormalMap) { texFormat = TextureFormat.ARGB32; } bool convertToNormalFormat = texture.isNormalMap ? true : false; Texture2D tex = texture.texture; Color32[] colors = new Color32[width * height]; int n = 18; if (imgType == 2) { for (int i = 0; i < width * height; i++) { colors[i].b = imageBuffer[n++]; colors[i].g = imageBuffer[n++]; colors[i].r = imageBuffer[n++]; if (alpha) { colors[i].a = imageBuffer[n++]; } else { colors[i].a = 255; } if (convertToNormalFormat) { colors[i].a = colors[i].r; colors[i].r = colors[i].g; colors[i].b = colors[i].g; } } } else if (imgType == 10) { int i = 0; int run = 0; while (i < width * height) { run = imageBuffer[n++]; if ((run & 0x80) != 0) { run = (run ^ 0x80) + 1; colors[i].b = imageBuffer[n++]; colors[i].g = imageBuffer[n++]; colors[i].r = imageBuffer[n++]; if (alpha) { colors[i].a = imageBuffer[n++]; } else { colors[i].a = 255; } if (convertToNormalFormat) { colors[i].a = colors[i].r; colors[i].r = colors[i].g; colors[i].b = colors[i].g; } i++; for (int c = 1; c < run; c++, i++) { colors[i] = colors[i - 1]; } } else { run += 1; for (int c = 0; c < run; c++, i++) { colors[i].b = imageBuffer[n++]; colors[i].g = imageBuffer[n++]; colors[i].r = imageBuffer[n++]; if (alpha) { colors[i].a = imageBuffer[n++]; } else { colors[i].a = 255; } if (convertToNormalFormat) { colors[i].a = colors[i].r; colors[i].r = colors[i].g; colors[i].b = colors[i].g; } } } } } else { ActiveTextureManagement.DBGLog("TGA format is not supported!"); } if (Texture.needsResize) { colors = TextureConverter.ResizePixels(colors, width, height, Texture.resizeWidth, Texture.resizeHeight); width = Texture.resizeWidth; height = Texture.resizeHeight; } tex.Resize((int)width, (int)height, texFormat, mipmaps); tex.SetPixels32(colors); tex.Apply(mipmaps, false); }
public static void MBMToTexture(TexInfo Texture, bool mipmaps) { GameDatabase.TextureInfo texture = Texture.texture; TextureConverter.InitImageBuffer(); FileStream mbmStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read); mbmStream.Position = 4; uint width = 0, height = 0; for (int b = 0; b < 4; b++) { width >>= 8; width |= (uint)(mbmStream.ReadByte() << 24); } for (int b = 0; b < 4; b++) { height >>= 8; height |= (uint)(mbmStream.ReadByte() << 24); } mbmStream.Position = 12; bool convertToNormalFormat = false; if (mbmStream.ReadByte() == 1) { texture.isNormalMap = true; } else { convertToNormalFormat = texture.isNormalMap ? true : false; } mbmStream.Position = 16; int format = mbmStream.ReadByte(); mbmStream.Position += 3; int imageSize = (int)(width * height * 3); TextureFormat texformat = TextureFormat.RGB24; bool alpha = false; if (format == 32) { imageSize += (int)(width * height); texformat = TextureFormat.ARGB32; alpha = true; } if (texture.isNormalMap) { texformat = TextureFormat.ARGB32; } mbmStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE); mbmStream.Close(); Texture2D tex = texture.texture; Color32[] colors = new Color32[width * height]; int n = 0; for (int i = 0; i < width * height; i++) { colors[i].r = imageBuffer[n++]; colors[i].g = imageBuffer[n++]; colors[i].b = imageBuffer[n++]; if (alpha) { colors[i].a = imageBuffer[n++]; } else { colors[i].a = 255; } if (convertToNormalFormat) { colors[i].a = colors[i].r; colors[i].r = colors[i].g; colors[i].b = colors[i].g; } } if (Texture.loadOriginalFirst) { Texture.Resize((int)width, (int)height); } if (Texture.needsResize) { colors = TextureConverter.ResizePixels(colors, (int)width, (int)height, Texture.resizeWidth, Texture.resizeHeight); width = (uint)Texture.resizeWidth; height = (uint)Texture.resizeHeight; } tex.Resize((int)width, (int)height, texformat, mipmaps); tex.SetPixels32(colors); tex.Apply(mipmaps, false); }