Ejemplo n.º 1
0
        private static void PNG2TEX(string input, string output, string resSPath = "./")
        {
            byte[]    dstTex2D = File.ReadAllBytes(output);
            Texture2D texture;

            try
            {
                texture = new Texture2D(dstTex2D);
                if (!texture.isTexture2D)
                {
                    return;
                }


                if (texture.format == TextureFormat.Alpha8 ||
                    texture.format == TextureFormat.ARGB4444 ||
                    texture.format == TextureFormat.RGBA4444 ||
                    texture.format == TextureFormat.RGBA32 ||
                    texture.format == TextureFormat.ARGB32 ||
                    texture.format == TextureFormat.RGB24 ||
                    texture.format == TextureFormat.RGB565 ||
                    texture.format == TextureFormat.ETC_RGB4 ||
                    texture.format == TextureFormat.ETC2_RGB ||
                    texture.format == TextureFormat.ETC2_RGBA8 ||
                    texture.format == TextureFormat.DXT5 ||
                    texture.format == TextureFormat.DXT1 ||
                    texture.format == TextureFormat.ASTC_RGBA_4x4 ||
                    texture.format == TextureFormat.ASTC_RGB_4x4)
                {
                    ImageMagick.MagickImage im = new MagickImage(input);

                    if ((im.Width != texture.width) || im.Height != texture.height)
                    {
                        //display error to user
                        Console.WriteLine("Error: texture is {0} x {1} , but png bitmap is {2} x {3}.Exit.",
                                          texture.width,
                                          texture.height,
                                          im.Width,
                                          im.Height);
                        return;
                    }

                    im.Flip();

                    byte[] sourceData = im.GetPixels().ToByteArray(0, 0, im.Width, im.Height, "RGBA");
                    byte[] outputData;
                    Console.WriteLine("Reading:{0}\n Width:{1}\n Height:{2}\n Format:{3}\n",
                                      input,
                                      im.Width,
                                      im.Height,
                                      texture.format.ToString());

                    Console.WriteLine("Converting...");

                    TextureConverter.CompressTexture(texture.format, im.Width, im.Height, sourceData, out outputData);

                    if (texture.bMipmap && texture.mipmapCount >= 3)
                    {
                        Console.WriteLine("Generating Mipmap...");
                        for (var m = 0; m <= 3; m++)
                        {
                            im.AdaptiveResize(im.Width / 2, im.Height / 2);
                            Console.WriteLine("Generating ...{0}x{1}",
                                              im.Width,
                                              im.Height);

                            sourceData = im.GetPixels().ToByteArray(0, 0, im.Width, im.Height, "RGBA");

                            byte[] dst;

                            TextureConverter.CompressTexture(texture.format, im.Width, im.Height, sourceData, out dst);

                            outputData = outputData.Concat(dst).ToArray();
                        }
                    }
                    if (outputData != null)
                    {
                        if ((outputData.Length > texture.textureSize))
                        {
                            Console.WriteLine("Error: generated data size {0}> original texture size {1}. Exit.", outputData.Length, texture.textureSize);
                        }
                        if (texture.bHasResSData == true)
                        {
                            output = string.Format("{0}/{1}", resSPath, texture.resSName);
                        }
                        if (File.Exists(output))
                        {
                            Console.WriteLine("Writing...{0}", output);
                            using (FileStream fs = File.Open(output, FileMode.Open, FileAccess.ReadWrite))
                            {
                                fs.Seek(texture.dataPos, SeekOrigin.Begin);
                                fs.Write(outputData, 0, outputData.Length);
                                fs.Flush();
                            }
                            Console.WriteLine("File Created...");
                        }

                        else
                        {
                            Console.WriteLine("Error: file {0} not found", output);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error: generated data size {0}> original texture size {1}. Exit.", outputData.Length, texture.textureSize);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error:Not A valid Texture: {0}", ex.ToString());
            }
        }