예제 #1
0
        public static bool RefreshTxPic(string fileName)
        {
            var xnd = EngineNS.IO.XndHolder.SyncLoadXND(fileName);

            if (xnd == null)
            {
                return(false);
            }

            CTxPicDesc txDesc = new CTxPicDesc();

            txDesc.SetDefault();

            #region Desc
            var attr = xnd.Node.FindAttrib("Desc");
            attr.BeginRead();
            unsafe
            {
                if (attr.Version == 3)
                {
                    attr.Read((IntPtr)(&txDesc), sizeof(CTxPicDesc));
                }
                else if (attr.Version == 2)
                {
                    attr.Read(out txDesc.sRGB);
                }
            }
            attr.EndRead();
            #endregion

            var pngBitmap = LoadPngBitmapFromTxPic(xnd);

            txDesc.Width  = pngBitmap.Width;
            txDesc.Height = pngBitmap.Height;

            var saveXnd = EngineNS.IO.XndHolder.NewXNDHolder();
            SaveTxPic(saveXnd, ref txDesc, pngBitmap, txDesc.EtcFormat, txDesc.MipLevel);

            EngineNS.IO.XndHolder.SaveXND(fileName, saveXnd);
            return(true);
        }
예제 #2
0
        private static unsafe bool CookTxPicImpl(RName rn, string targetPath, bool copyRInfo, bool isandroid)
        {
            var xnd = EngineNS.IO.XndHolder.SyncLoadXND(rn.Address);

            if (xnd == null)
            {
                return(false);
            }

            var targetXnd = EngineNS.IO.XndHolder.NewXNDHolder();

            var txDesc = new EngineNS.CTxPicDesc();

            {
                var attr = xnd.Node.FindAttrib("Desc");
                if (attr.Version != 3)
                {
                    return(false);
                }
                attr.BeginRead();
                attr.Read((IntPtr)(&txDesc), sizeof(EngineNS.CTxPicDesc));
                attr.EndRead();

                var sattr = targetXnd.Node.AddAttrib("Desc");
                sattr.Version = 3;
                sattr.BeginWrite();
                sattr.Write((IntPtr)(&txDesc), sizeof(EngineNS.CTxPicDesc));
                sattr.EndWrite();
            }

            byte[] pngData = null;
            if (copyRInfo)
            {
                var attr = xnd.Node.FindAttrib("PNG");
                if (attr == null)
                {
                    return(false);
                }
                attr.BeginRead();
                attr.Read(out pngData, (int)attr.Length);
                attr.EndRead();

                if (copyRInfo)
                {
                    var sattr = xnd.Node.AddAttrib("PNG");
                    sattr.BeginWrite();
                    sattr.Write(pngData, pngData.Length);
                    sattr.EndWrite();
                }
            }

            bool hasCompressData = false;

            if ((TexCompressFlags & ETexCompressMode.ETC2) != 0 && txDesc.EtcFormat != ETCFormat.UNKNOWN)
            {
                hasCompressData = true;
                using (var etcBlob = EngineNS.Support.CBlobProxy2.CreateBlobProxy())
                {
                    fixed(byte *dataPtr = &pngData[0])
                    {
                        var texCompressor = new EngineNS.Bricks.TexCompressor.CTexCompressor();

                        texCompressor.EncodePng2ETC((IntPtr)dataPtr, (uint)pngData.Length, txDesc.EtcFormat, txDesc.MipLevel, etcBlob);
                        etcBlob.BeginRead();
                    }

                    if (etcBlob.DataLength >= 0)
                    {
                        var etcNode  = targetXnd.Node.AddNode("EtcMips", 0, 0);
                        int fmt      = 0;
                        int MipLevel = 0;
                        etcBlob.Read(out fmt);
                        etcBlob.Read(out MipLevel);
                        var layer = new EngineNS.Bricks.TexCompressor.ETCLayer();
                        for (int i = 0; i < MipLevel; i++)
                        {
                            etcBlob.Read(out layer);
                            byte[] etcMipData;
                            etcBlob.Read(out etcMipData, (int)layer.Size);

                            var mipAttr = etcNode.AddAttrib($"Mip_{i}");
                            mipAttr.BeginWrite();
                            mipAttr.Write(layer);
                            mipAttr.Write(etcMipData, etcMipData.Length);
                            mipAttr.EndWrite();
                        }
                    }
                }
            }

            if ((TexCompressFlags & ETexCompressMode.ASTC) != 0)
            {
                //"AstcMips"
                hasCompressData = true;
                //EngineNS.Editor.Runner.ProcessExecuter.RunProcess("astcenc -cl example.png example.astc 6x6 -medium",.....)
            }

            if ((TexCompressFlags & ETexCompressMode.PNG) != 0 || hasCompressData == false)
            {
                var mipsNode = xnd.Node.FindNode("PngMips");
                if (mipsNode == null)
                {
                    return(false);
                }
                var smipsNode = targetXnd.Node.AddNode("PngMips", 0, 0);

                int curMip = 0;
                while (curMip < txDesc.MipLevel)
                {
                    var mipAttr  = mipsNode.FindAttrib($"Mip_{curMip}");
                    var smipAttr = smipsNode.AddAttrib($"Mip_{curMip}");
                    curMip++;

                    mipAttr.BeginRead();
                    byte[] pngMipData;
                    mipAttr.Read(out pngMipData, (int)mipAttr.Length);
                    mipAttr.EndRead();

                    smipAttr.BeginWrite();
                    smipAttr.Write(pngMipData, pngMipData.Length);
                    smipAttr.EndWrite();
                }
            }

            EngineNS.IO.XndHolder.SaveXND(targetPath, targetXnd);
            CMDEngine.CMDEngineInstance.AddAssetInfos(targetPath);
            return(true);
        }
예제 #3
0
        public override void DoImport(string file, string target, bool overwrite = false)
        {
            var rawData = EditorCommon.ImageInit.ReadRawData(file);
            //var pngData = EditorCommon.ImageInit.ConverImage2PNG(file);
            //if (pngData == null)
            //{
            //    EngineNS.Profiler.Log.WriteLine(EngineNS.Profiler.ELogTag.Error, "Resource", $"Texture {file} is not found");
            //    return;
            //}

            var xnd = EngineNS.IO.XndHolder.NewXNDHolder();
            var attr = xnd.Node.AddAttrib("Desc");

            attr.Version = 1;
            attr.BeginWrite();
            attr.Write(file);
            //这里最好导出的时候有参数,确定是否sRGB
            var txDesc = new EngineNS.CTxPicDesc();
            txDesc.SetDefault();
            unsafe
            {
                attr.Write((IntPtr)(&txDesc), sizeof(EngineNS.CTxPicDesc));
            }
            attr.EndWrite();

            attr = xnd.Node.AddAttrib("RawImage");
            attr.BeginWrite();
            attr.Write(rawData);
            attr.EndWrite();

            attr = xnd.Node.AddAttrib("PNG");
            attr.BeginWrite();
            var pngBlob = new EngineNS.Support.CBlobObject();
            EngineNS.CShaderResourceView.ConvertImage(EngineNS.CEngine.Instance.RenderContext, rawData, EIMAGE_FILE_FORMAT.PNG, pngBlob);
            var pngData = pngBlob.ToBytes();
            attr.Write(pngData);
            attr.EndWrite();

            attr = xnd.Node.AddAttrib("JPG");
            attr.BeginWrite();
            var jpgBlob = new EngineNS.Support.CBlobObject();
            EngineNS.CShaderResourceView.ConvertImage(EngineNS.CEngine.Instance.RenderContext, rawData, EIMAGE_FILE_FORMAT.JPG, jpgBlob);
            attr.Write(jpgBlob.ToBytes());
            attr.EndWrite();

            attr = xnd.Node.AddAttrib("DDS");
            attr.BeginWrite();
            var ddsBlob = new EngineNS.Support.CBlobObject();
            EngineNS.CShaderResourceView.ConvertImage(EngineNS.CEngine.Instance.RenderContext, rawData, EIMAGE_FILE_FORMAT.DDS, ddsBlob);
            attr.Write(ddsBlob.ToBytes());
            attr.EndWrite();

            attr = xnd.Node.AddAttrib("ETC2");
            attr.BeginWrite();
            //EngineNS.CShaderResourceView.SaveETC2(file, attr, 0, true);
            EngineNS.CShaderResourceView.SaveETC2_Png(pngData, attr, 0, true);
            attr.EndWrite();

            var pos = target.LastIndexOf('.');
            target = target.Substring(0, pos);
            target += EngineNS.CEngineDesc.TextureExtension;
            EngineNS.IO.XndHolder.SaveXND(target, xnd);

            pos = this.ResourceName.Name.LastIndexOf('.');
            var newRName = this.ResourceName.Name.Substring(0, pos);
            newRName += EngineNS.CEngineDesc.TextureExtension;
            this.ResourceName = RName.GetRName(newRName);
        }
예제 #4
0
        public static bool SaveTxPic(IO.XndHolder xnd, ref CTxPicDesc txDesc, string filename,
                                     ETCFormat etcFormat = ETCFormat.RGBA8,
                                     int mipMapLevel     = 0)
        {
            #region Read Pixels
            var imageimport = new EngineNS.Bricks.ImageImport.ImageImport();
            EngineNS.Support.CBlobObject blob = new EngineNS.Support.CBlobObject();
            imageimport.LoadTexture(filename, blob);
            int w        = imageimport.GetWidth();
            int h        = imageimport.GetHeight();
            int channels = imageimport.GetChannels();

            byte[] data = blob.ToBytes();//new byte[w * h * channels];
            System.Drawing.Imaging.PixelFormat format = System.Drawing.Imaging.PixelFormat.Undefined;
            if (channels == 4)
            {
                format = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
            }
            else if (channels == 3)
            {
                format = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
            }
            else if (channels == 1)
            {
                format = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
            }

            var bitmap = new System.Drawing.Bitmap(w, h, format);
            int offset = 0;
            for (int j = 0; j < h; j++)
            {
                for (int i = 0; i < w; i++)
                {
                    if (channels == 4)
                    {
                        System.Drawing.Color color = System.Drawing.Color.FromArgb(data[offset + 3], data[offset], data[offset + 1], data[offset + 2]);
                        offset += 4;
                        bitmap.SetPixel(i, j, color);
                    }
                    else if (channels == 3)
                    {
                        System.Drawing.Color color = System.Drawing.Color.FromArgb(data[offset], data[offset + 1], data[offset + 2]);
                        offset += 3;
                        bitmap.SetPixel(i, j, color);
                    }
                    else if (channels == 1)
                    {
                        System.Drawing.Color color = System.Drawing.Color.FromArgb(data[offset++]);
                        bitmap.SetPixel(i, j, color);
                    }
                }
            }
            var tagBitmap = bitmap.Clone(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            #endregion

            if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
            {
                txDesc.EtcFormat = ETCFormat.UNKNOWN;
            }
            txDesc.Width  = w;
            txDesc.Height = h;

            SaveTxPic(xnd, ref txDesc, tagBitmap, etcFormat, mipMapLevel);
            return(true);
        }
예제 #5
0
        public static void SaveTxPic(IO.XndHolder xnd, ref CTxPicDesc txDesc, Bitmap tagBitmap,
                                     ETCFormat etcFormat = ETCFormat.RGBA8,
                                     int mipMapLevel     = 0)
        {
            #region Png
            var attr = xnd.Node.AddAttrib("PNG");
            attr.BeginWrite();
            var tagStream = new MemoryStream();
            tagBitmap.Save(tagStream, System.Drawing.Imaging.ImageFormat.Png);
            var pngData = tagStream.ToArray();
            attr.Write(pngData, pngData.Length);
            attr.EndWrite();
            #endregion

            #region PngMips
            var mipsNode = xnd.Node.AddNode("PngMips", 0, 0);
            int curMip   = 0;
            while (true)
            {
                var mipAttr = mipsNode.AddAttrib($"Mip_{curMip}");
                curMip++;
                mipAttr.BeginWrite();
                var mipStream = new MemoryStream();
                tagBitmap.Save(mipStream, System.Drawing.Imaging.ImageFormat.Png);
                var pngMipData = mipStream.ToArray();
                mipAttr.Write(pngMipData, pngMipData.Length);
                mipAttr.EndWrite();

                if (txDesc.MipLevel == curMip)
                {
                    break;
                }
                if (tagBitmap.Width == 1 && tagBitmap.Height == 1)
                {
                    break;
                }
                var mipWidth  = tagBitmap.Width / 2;
                var mipHeight = tagBitmap.Height / 2;
                if (mipWidth == 0)
                {
                    mipWidth = 1;
                }
                if (mipHeight == 0)
                {
                    mipHeight = 1;
                }
                //tagBitmap = EngineNS.BitmapProc.ScaleBitmap(tagBitmap, mipWidth, mipHeight);
                tagBitmap = EngineNS.BitmapProc.GenerateMip(tagBitmap, mipWidth, mipHeight);
            }
            #endregion

            if (CEngine.IsWriteEtc)
            {
                if (etcFormat != ETCFormat.UNKNOWN)
                {
                    using (var etcBlob = EngineNS.Support.CBlobProxy2.CreateBlobProxy())
                    {
                        unsafe
                        {
                            fixed(byte *dataPtr = &pngData[0])
                            {
                                var texCompressor = new EngineNS.Bricks.TexCompressor.CTexCompressor();

                                texCompressor.EncodePng2ETC((IntPtr)dataPtr, (uint)pngData.Length, etcFormat, mipMapLevel, etcBlob);
                                etcBlob.BeginRead();
                            }
                        }
                        if (etcBlob.DataLength >= 0)
                        {
                            var etcNode  = xnd.Node.AddNode("EtcMips", 0, 0);
                            int fmt      = 0;
                            int MipLevel = 0;
                            etcBlob.Read(out fmt);
                            etcBlob.Read(out MipLevel);
                            var layer = new EngineNS.Bricks.TexCompressor.ETCLayer();
                            for (int i = 0; i < MipLevel; i++)
                            {
                                etcBlob.Read(out layer);
                                byte[] etcMipData;
                                etcBlob.Read(out etcMipData, (int)layer.Size);

                                var mipAttr = etcNode.AddAttrib($"Mip_{i}");
                                mipAttr.BeginWrite();
                                mipAttr.Write(layer);
                                mipAttr.Write(etcMipData, etcMipData.Length);
                                mipAttr.EndWrite();
                            }
                        }
                    }
                }
            }

            attr             = xnd.Node.AddAttrib("Desc");
            txDesc.MipLevel  = curMip;
            txDesc.EtcFormat = etcFormat;
            #region Desc
            attr.Version = 3;
            attr.BeginWrite();
            unsafe
            {
                fixed(EngineNS.CTxPicDesc *descPin = &txDesc)
                {
                    attr.Write((IntPtr)(descPin), sizeof(EngineNS.CTxPicDesc));
                }
            }
            attr.EndWrite();
            #endregion
        }