Ejemplo n.º 1
0
        public static bool ExportTexture2D(AssetPreloadData asset, string exportPathName, bool flip)
        {
            var m_Texture2D = new Texture2D(asset, true);

            if (m_Texture2D.image_data == null || m_Texture2D.image_data.Length == 0)
            {
                return(false);
            }
            var converter      = new Texture2DConverter(m_Texture2D);
            var convertTexture = (bool)Properties.Settings.Default["convertTexture"];

            if (convertTexture)
            {
                var bitmap = converter.ConvertToBitmap(flip);
                if (bitmap == null)
                {
                    return(false);
                }
                ImageFormat format = null;
                var         ext    = (string)Properties.Settings.Default["convertType"];
                switch (ext)
                {
                case "BMP":
                    format = ImageFormat.Bmp;
                    break;

                case "PNG":
                    format = ImageFormat.Png;
                    break;

                case "JPEG":
                    format = ImageFormat.Jpeg;
                    break;
                }
                var exportFullName = exportPathName + asset.Text + "." + ext.ToLower();
                if (ExportFileExists(exportFullName))
                {
                    return(false);
                }
                bitmap.Save(exportFullName, format);
                bitmap.Dispose();
                return(true);
            }
            else
            {
                var exportFullName = exportPathName + asset.Text + converter.GetExtensionName();
                if (ExportFileExists(exportFullName))
                {
                    return(false);
                }
                File.WriteAllBytes(exportFullName, converter.ConvertToContainer());
                return(true);
            }
        }
Ejemplo n.º 2
0
        public static Image ConvertToImage(this Texture2D m_Texture2D, bool flip)
        {
            var converter = new Texture2DConverter(m_Texture2D);
            var bytes     = converter.DecodeTexture2D();

            if (bytes != null && bytes.Length > 0)
            {
                var image = Image.LoadPixelData <Bgra32>(bytes, m_Texture2D.m_Width, m_Texture2D.m_Height);
                if (flip)
                {
                    image.Mutate(x => x.Flip(FlipMode.Vertical));
                }
                return(image);
            }
            return(null);
        }
Ejemplo n.º 3
0
        private static Bitmap CutImage(AssetPreloadData texture2DAsset, RectangleF textureRect)
        {
            var texture2D = new Texture2DConverter(new Texture2D(texture2DAsset, true));

            using (var originalImage = texture2D.ConvertToBitmap(false))
            {
                if (originalImage != null)
                {
                    var spriteImage = originalImage.Clone(textureRect, PixelFormat.Format32bppArgb);
                    spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    return(spriteImage);
                }
            }

            return(null);
        }
Ejemplo n.º 4
0
        private static Bitmap CutImage(AssetPreloadData asset, AssetPreloadData texture2DAsset, RectangleF textureRect, Sprite sprite)
        {
            var texture2D = new Texture2DConverter(new Texture2D(texture2DAsset, true));

            using (var originalImage = texture2D.ConvertToBitmap(false))
            {
                if (originalImage != null)
                {
                    var info  = texture2DAsset.InfoText;
                    var start = info.IndexOf("Format");
                    info           = info.Substring(start, info.Length - start);
                    asset.InfoText = $"Width: {textureRect.Width}\nHeight: {textureRect.Height}\n" + info;
                    var spriteImage = originalImage.Clone(textureRect, PixelFormat.Format32bppArgb);
                    using (var brush = new TextureBrush(spriteImage))
                    {
                        using (var path = new GraphicsPath())
                        {
                            foreach (var p in sprite.m_PhysicsShape)
                            {
                                path.AddPolygon(p);
                            }
                            using (var matr = new Matrix())
                            {
                                matr.Translate(sprite.m_Rect.Width * sprite.m_Pivot.X, sprite.m_Rect.Height * sprite.m_Pivot.Y);
                                matr.Scale(sprite.m_PixelsToUnits, sprite.m_PixelsToUnits);
                                path.Transform(matr);
                                var bitmap = new Bitmap((int)textureRect.Width, (int)textureRect.Height);
                                using (var graphic = Graphics.FromImage(bitmap))
                                {
                                    graphic.FillPath(brush, path);
                                    bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                                    spriteCache.Add(asset, bitmap);
                                    return((Bitmap)bitmap.Clone());
                                }
                            }
                        }
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 5
0
        private static Bitmap CutImage(AssetPreloadData asset, AssetPreloadData texture2DAsset, RectangleF textureRect)
        {
            var texture2D = new Texture2DConverter(new Texture2D(texture2DAsset, true));

            using (var originalImage = texture2D.ConvertToBitmap(false))
            {
                if (originalImage != null)
                {
                    var info  = texture2DAsset.InfoText;
                    var start = info.IndexOf("Format");
                    info           = info.Substring(start, info.Length - start);
                    asset.InfoText = $"Width: {textureRect.Width}\nHeight: {textureRect.Height}\n" + info;
                    var spriteImage = originalImage.Clone(textureRect, PixelFormat.Format32bppArgb);
                    spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    return(spriteImage);
                }
            }

            return(null);
        }
Ejemplo n.º 6
0
        private void ConvertTexture2D(Texture2D tex2D, string name)
        {
            var iTex = ImportedHelpers.FindTexture(name, TextureList);

            if (iTex != null)
            {
                return;
            }

            using (var memStream = new MemoryStream())
            {
                var bitmap = new Texture2DConverter(tex2D).ConvertToBitmap(true);
                if (bitmap != null)
                {
                    bitmap.Save(memStream, ImageFormat.Png);
                    memStream.Position = 0;
                    iTex = new ImportedTexture(memStream, name);
                    TextureList.Add(iTex);
                }
            }
        }
Ejemplo n.º 7
0
        public static Image <Bgra32> ConvertToImage(this Texture2D m_Texture2D, bool flip)
        {
            var converter = new Texture2DConverter(m_Texture2D);
            var buff      = BigArrayPool <byte> .Shared.Rent(m_Texture2D.m_Width *m_Texture2D.m_Height * 4);

            try
            {
                if (converter.DecodeTexture2D(buff))
                {
                    var image = Image.LoadPixelData <Bgra32>(buff, m_Texture2D.m_Width, m_Texture2D.m_Height);
                    if (flip)
                    {
                        image.Mutate(x => x.Flip(FlipMode.Vertical));
                    }
                    return(image);
                }
                return(null);
            }
            finally
            {
                BigArrayPool <byte> .Shared.Return(buff);
            }
        }
Ejemplo n.º 8
0
        public static Bitmap ConvertToBitmap(this Texture2D m_Texture2D, bool flip)
        {
            var converter = new Texture2DConverter(m_Texture2D);

            return(converter.ConvertToBitmap(flip));
        }
Ejemplo n.º 9
0
        public static Bitmap ConvertToBitmap(this Texture2DArray m_Texture2DArray, bool flip, int layer)
        {
            var converter = new Texture2DConverter(m_Texture2DArray, layer);

            return(converter.ConvertToBitmap(flip));
        }
Ejemplo n.º 10
0
        private static Bitmap CutImage(Texture2D m_Texture2D, Sprite m_Sprite, RectangleF textureRect, Vector2 textureRectOffset, SpriteSettings settingsRaw)
        {
            var texture2D     = new Texture2DConverter(m_Texture2D);
            var originalImage = texture2D.ConvertToBitmap(false);

            if (originalImage != null)
            {
                using (originalImage)
                {
                    //var spriteImage = originalImage.Clone(textureRect, PixelFormat.Format32bppArgb);
                    var spriteImage = new Bitmap((int)textureRect.Width, (int)textureRect.Height, PixelFormat.Format32bppArgb);
                    var destRect    = new Rectangle(0, 0, (int)textureRect.Width, (int)textureRect.Height);
                    using (var graphic = Graphics.FromImage(spriteImage))
                    {
                        graphic.DrawImage(originalImage, destRect, textureRect, GraphicsUnit.Pixel);
                    }
                    if (settingsRaw.packed == 1)
                    {
                        //RotateAndFlip
                        switch (settingsRaw.packingRotation)
                        {
                        case SpritePackingRotation.kSPRFlipHorizontal:
                            spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
                            break;

                        case SpritePackingRotation.kSPRFlipVertical:
                            spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
                            break;

                        case SpritePackingRotation.kSPRRotate180:
                            spriteImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
                            break;

                        case SpritePackingRotation.kSPRRotate90:
                            spriteImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                            break;
                        }

                        //Tight
                        if (settingsRaw.packingMode == SpritePackingMode.kSPMTight)
                        {
                            try
                            {
                                var triangles = GetTriangles(m_Sprite.m_RD);
                                var points    = triangles.Select(x => x.Select(y => new PointF(y.X, y.Y)).ToArray());
                                using (var path = new GraphicsPath())
                                {
                                    foreach (var p in points)
                                    {
                                        path.AddPolygon(p);
                                    }
                                    using (var matr = new Matrix())
                                    {
                                        if (m_Sprite.m_Pivot == Vector2.Zero) //5.4.2 down
                                        {
                                            matr.Translate(m_Sprite.m_Rect.Width * 0.5f - textureRectOffset.X, m_Sprite.m_Rect.Height * 0.5f - textureRectOffset.Y);
                                        }
                                        else
                                        {
                                            matr.Translate(m_Sprite.m_Rect.Width * m_Sprite.m_Pivot.X - textureRectOffset.X, m_Sprite.m_Rect.Height * m_Sprite.m_Pivot.Y - textureRectOffset.Y);
                                        }
                                        matr.Scale(m_Sprite.m_PixelsToUnits, m_Sprite.m_PixelsToUnits);
                                        path.Transform(matr);
                                        var bitmap = new Bitmap((int)textureRect.Width, (int)textureRect.Height);
                                        using (var graphic = Graphics.FromImage(bitmap))
                                        {
                                            using (var brush = new TextureBrush(spriteImage))
                                            {
                                                graphic.FillPath(brush, path);
                                                bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                                                return(bitmap);
                                            }
                                        }
                                    }
                                }
                            }
                            catch
                            {
                                // ignored
                            }
                        }
                    }

                    //Rectangle
                    spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    return(spriteImage);
                }
            }

            return(null);
        }
Ejemplo n.º 11
0
        private static Bitmap CutImage(ObjectReader texture2DAsset, RectangleF textureRect, Sprite m_Sprite, SpriteSettings settingsRaw)
        {
            var texture2D     = new Texture2DConverter(new Texture2D(texture2DAsset, true));
            var originalImage = texture2D.ConvertToBitmap(false);

            if (originalImage != null)
            {
                using (originalImage)
                {
                    var spriteImage = originalImage.Clone(textureRect, PixelFormat.Format32bppArgb);

                    //RotateAndFlip
                    switch (settingsRaw.packingRotation)
                    {
                    case SpritePackingRotation.kSPRFlipHorizontal:
                        spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
                        break;

                    case SpritePackingRotation.kSPRFlipVertical:
                        spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
                        break;

                    case SpritePackingRotation.kSPRRotate180:
                        spriteImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
                        break;

                    case SpritePackingRotation.kSPRRotate90:
                        spriteImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                        break;
                    }

                    /* TODO Tight
                     * 2017之前没有PhysicsShape
                     * 5.6之前使用vertices
                     * 5.6需要使用VertexData
                     */
                    if (settingsRaw.packingMode == SpritePackingMode.kSPMTight && m_Sprite.m_PhysicsShape?.Length > 0) //Tight
                    {
                        try
                        {
                            using (var brush = new TextureBrush(spriteImage))
                            {
                                using (var path = new GraphicsPath())
                                {
                                    foreach (var p in m_Sprite.m_PhysicsShape)
                                    {
                                        path.AddPolygon(p);
                                    }
                                    using (var matr = new Matrix())
                                    {
                                        matr.Translate(m_Sprite.m_Rect.Width * m_Sprite.m_Pivot.X, m_Sprite.m_Rect.Height * m_Sprite.m_Pivot.Y);
                                        matr.Scale(m_Sprite.m_PixelsToUnits, m_Sprite.m_PixelsToUnits);
                                        path.Transform(matr);
                                        var bitmap = new Bitmap((int)textureRect.Width, (int)textureRect.Height);
                                        using (var graphic = Graphics.FromImage(bitmap))
                                        {
                                            graphic.FillPath(brush, path);
                                            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                                            return(bitmap);
                                        }
                                    }
                                }
                            }
                        }
                        catch
                        {
                            spriteImage = originalImage.Clone(textureRect, PixelFormat.Format32bppArgb);
                            spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
                            return(spriteImage);
                        }
                    }

                    //Rectangle
                    spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    return(spriteImage);
                }
            }

            return(null);
        }
Ejemplo n.º 12
0
        private static Bitmap CutImage(Texture2D m_Texture2D, RectangleF textureRect, Sprite m_Sprite, SpriteSettings settingsRaw)
        {
            var texture2D     = new Texture2DConverter(m_Texture2D);
            var originalImage = texture2D.ConvertToBitmap(false);

            if (originalImage != null)
            {
                using (originalImage)
                {
                    var spriteImage = originalImage.Clone(textureRect, PixelFormat.Format32bppArgb);

                    //RotateAndFlip
                    switch (settingsRaw.packingRotation)
                    {
                    case SpritePackingRotation.kSPRFlipHorizontal:
                        spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
                        break;

                    case SpritePackingRotation.kSPRFlipVertical:
                        spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
                        break;

                    case SpritePackingRotation.kSPRRotate180:
                        spriteImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
                        break;

                    case SpritePackingRotation.kSPRRotate90:
                        spriteImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                        break;
                    }

                    //Tight
                    //TODO 2017 and up use m_PhysicsShape should be better
                    if (settingsRaw.packingMode == SpritePackingMode.kSPMTight)
                    {
                        try
                        {
                            var polygon = GetPolygon(m_Sprite.m_RD);
                            var points  = polygon.Select(x => x.Select(y => new PointF(y.X, y.Y)).ToArray());
                            using (var path = new GraphicsPath())
                            {
                                foreach (var p in points)
                                {
                                    path.AddPolygon(p);
                                }
                                using (var matr = new System.Drawing.Drawing2D.Matrix())
                                {
                                    matr.Translate(m_Sprite.m_Rect.Width * m_Sprite.m_Pivot.X, m_Sprite.m_Rect.Height * m_Sprite.m_Pivot.Y);
                                    matr.Scale(m_Sprite.m_PixelsToUnits, m_Sprite.m_PixelsToUnits);
                                    path.Transform(matr);
                                    var bitmap = new Bitmap((int)textureRect.Width, (int)textureRect.Height);
                                    using (var graphic = Graphics.FromImage(bitmap))
                                    {
                                        using (var brush = new TextureBrush(spriteImage))
                                        {
                                            graphic.FillPath(brush, path);
                                            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                                            return(bitmap);
                                        }
                                    }
                                }
                            }
                        }
                        catch
                        {
                            spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
                            return(spriteImage);
                        }
                    }

                    //Rectangle
                    spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    return(spriteImage);
                }
            }

            return(null);
        }