示例#1
0
        static void ExtractTex(Texture Tex, string FileName)
        {
            if (Tex.FormatType >= TextureFormatType.ASTC4x4 &&
                Tex.FormatType <= TextureFormatType.ASTC12x12)
            {
                Console.WriteLine("Extracting " + Tex.Name + " (ASTC)...");

                FileName = FileName.Replace(Path.GetExtension(FileName), ".astc");

                ASTC.Save(Tex, FileName);
            }
            else
            {
                Console.WriteLine("Extracting " + Tex.Name + "...");

                void PrintWarning()
                {
                    Console.ForegroundColor = ConsoleColor.Red;

                    Console.WriteLine("ERROR: Texture " + Tex.Name + " have a unsupported format!");

                    Console.ResetColor();
                }

                if (Tex.Type == TextureType.Cube)
                {
                    int Length = Tex.Data.Length / Tex.ArrayCount;

                    int Offset = 0;

                    string[] CubeFaces = new string[] { "x+", "x-", "y+", "y-", "z+", "z-" };

                    for (int Index = 0; Index < Tex.ArrayCount; Index++)
                    {
                        if (!PixelDecoder.TryDecode(Tex, out Bitmap Img, Offset))
                        {
                            PrintWarning();

                            break;
                        }

                        string Ext = Path.GetExtension(FileName);

                        Img.Save(FileName.Replace(Ext, "." + CubeFaces[Index] + Ext));

                        Offset += Length;
                    }
                }
                else
                {
                    if (Tex.MipmapCount == 1)
                    {
                        if (PixelDecoder.TryDecode(Tex, out Bitmap Img))
                        {
                            Img.Save(FileName);
                        }
                        else
                        {
                            PrintWarning();
                        }
                    }
                    else
                    {
                        for (int Index = 0; Index < Tex.MipmapCount; Index++)
                        {
                            if (!PixelDecoder.TryDecode(Tex, out Bitmap Img, (int)Tex.MipOffsets[Index]))
                            {
                                PrintWarning();

                                break;
                            }

                            string Ext = Path.GetExtension(FileName);

                            Img.Save(FileName.Replace(Ext, "." + Index + "." + Ext));

                            Tex.Width  = Math.Max(Tex.Width >> 1, 1);
                            Tex.Height = Math.Max(Tex.Height >> 1, 1);

                            while (Tex.GetBlockHeight() * 8 > Tex.GetPow2HeightInTexels() && Tex.BlockHeightLog2 > 0)
                            {
                                Tex.BlockHeightLog2--;
                            }
                        }
                    }
                }
            }
        }