public static void MBMToTexture(TexInfo Texture, bool mipmaps)
{
TextureInfoWrapper 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);
}