Esempio n. 1
0
        private static Bitmap CutImage(Texture2D m_Texture2D, Sprite m_Sprite, RectangleF textureRect, 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);

                    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
                        //TODO 2017 and up use m_PhysicsShape should be better
                        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, m_Sprite.m_Rect.Height * 0.5f);
                                        }
                                        else
                                        {
                                            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
                            {
                                // ignored
                            }
                        }
                    }

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

            return(null);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        private static Bitmap CutImage(Texture2D m_Texture2D, Sprite m_Sprite, Rectf textureRect, Vector2 textureRectOffset, SpriteSettings settingsRaw)
        {
            var originalImage = m_Texture2D.ConvertToBitmap(false);

            if (originalImage != null)
            {
                using (originalImage)
                {
                    //var spriteImage = originalImage.Clone(textureRect, PixelFormat.Format32bppArgb);
                    var rectf = new RectangleF(textureRect.x, textureRect.y, textureRect.width, textureRect.height);
                    var rect  = Rectangle.Round(rectf);
                    if (rect.Width == 0)
                    {
                        rect.Width = 1;
                    }
                    if (rect.Height == 0)
                    {
                        rect.Height = 1;
                    }
                    var spriteImage = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
                    var destRect    = new Rectangle(0, 0, rect.Width, rect.Height);
                    using (var graphic = Graphics.FromImage(spriteImage))
                    {
                        graphic.DrawImage(originalImage, destRect, rect, 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())
                                {
                                    var version = m_Sprite.version;
                                    if (version[0] < 5 ||
                                        (version[0] == 5 && version[1] < 4) ||
                                        (version[0] == 5 && version[1] == 4 && version[2] <= 1))   //5.4.1p3 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(rect.Width, rect.Height);
                                    using (var graphic = Graphics.FromImage(bitmap))
                                    {
                                        using (var brush = new TextureBrush(spriteImage))
                                        {
                                            graphic.FillPath(brush, path);
                                            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                                            spriteImage.Dispose();
                                            return(bitmap);
                                        }
                                    }
                                }
                            }
                        }
                        catch
                        {
                            // ignored
                        }
                    }

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

            return(null);
        }
Esempio n. 4
0
        private static Image <Bgra32> CutImage(Texture2D m_Texture2D, Sprite m_Sprite, Rectf textureRect, Vector2 textureRectOffset, SpriteSettings settingsRaw)
        {
            var originalImage = m_Texture2D.ConvertToImage(false);

            if (originalImage != null)
            {
                using (originalImage)
                {
                    var rectX      = (int)Math.Floor(textureRect.x);
                    var rectY      = (int)Math.Floor(textureRect.y);
                    var rectRight  = (int)Math.Ceiling(textureRect.x + textureRect.width);
                    var rectBottom = (int)Math.Ceiling(textureRect.y + textureRect.height);
                    rectRight  = Math.Min(rectRight, m_Texture2D.m_Width);
                    rectBottom = Math.Min(rectBottom, m_Texture2D.m_Height);
                    var rect        = new Rectangle(rectX, rectY, rectRight - rectX, rectBottom - rectY);
                    var spriteImage = originalImage.Clone(x => x.Crop(rect));
                    if (settingsRaw.packed == 1)
                    {
                        //RotateAndFlip
                        switch (settingsRaw.packingRotation)
                        {
                        case SpritePackingRotation.kSPRFlipHorizontal:
                            spriteImage.Mutate(x => x.Flip(FlipMode.Horizontal));
                            break;

                        case SpritePackingRotation.kSPRFlipVertical:
                            spriteImage.Mutate(x => x.Flip(FlipMode.Vertical));
                            break;

                        case SpritePackingRotation.kSPRRotate180:
                            spriteImage.Mutate(x => x.Rotate(180));
                            break;

                        case SpritePackingRotation.kSPRRotate90:
                            spriteImage.Mutate(x => x.Rotate(270));
                            break;
                        }
                    }

                    //Tight
                    if (settingsRaw.packingMode == SpritePackingMode.kSPMTight)
                    {
                        try
                        {
                            var             triangles = GetTriangles(m_Sprite.m_RD);
                            var             polygons  = triangles.Select(x => new Polygon(new LinearLineSegment(x.Select(y => new PointF(y.X, y.Y)).ToArray()))).ToArray();
                            IPathCollection path      = new PathCollection(polygons);
                            var             matrix    = Matrix3x2.CreateScale(m_Sprite.m_PixelsToUnits);
                            matrix *= Matrix3x2.CreateTranslation(m_Sprite.m_Rect.width * m_Sprite.m_Pivot.X - textureRectOffset.X, m_Sprite.m_Rect.height * m_Sprite.m_Pivot.Y - textureRectOffset.Y);
                            path    = path.Transform(matrix);
                            var graphicsOptions = new GraphicsOptions
                            {
                                Antialias            = false,
                                AlphaCompositionMode = PixelAlphaCompositionMode.DestOut
                            };
                            var options = new DrawingOptions
                            {
                                GraphicsOptions = graphicsOptions
                            };
                            using (var mask = new Image <Bgra32>(rect.Width, rect.Height, SixLabors.ImageSharp.Color.Black))
                            {
                                mask.Mutate(x => x.Fill(options, SixLabors.ImageSharp.Color.Red, path));
                                var bursh = new ImageBrush(mask);
                                spriteImage.Mutate(x => x.Fill(graphicsOptions, bursh));
                                spriteImage.Mutate(x => x.Flip(FlipMode.Vertical));
                                return(spriteImage);
                            }
                        }
                        catch
                        {
                            // ignored
                        }
                    }

                    //Rectangle
                    spriteImage.Mutate(x => x.Flip(FlipMode.Vertical));
                    return(spriteImage);
                }
            }

            return(null);
        }
Esempio n. 5
0
        public SpriteRenderData(ObjectReader reader)
        {
            var version = reader.version;

            texture = new PPtr <Texture2D>(reader);
            if (version[0] > 5 || (version[0] == 5 && version[1] >= 2)) //5.2 and up
            {
                alphaTexture = new PPtr <Texture2D>(reader);
            }

            if (version[0] >= 2019) //2019 and up
            {
                var secondaryTexturesSize = reader.ReadInt32();
                secondaryTextures = new SecondarySpriteTexture[secondaryTexturesSize];
                for (int i = 0; i < secondaryTexturesSize; i++)
                {
                    secondaryTextures[i] = new SecondarySpriteTexture(reader);
                }
            }

            if (version[0] > 5 || (version[0] == 5 && version[1] >= 6)) //5.6 and up
            {
                var m_SubMeshesSize = reader.ReadInt32();
                m_SubMeshes = new SubMesh[m_SubMeshesSize];
                for (int i = 0; i < m_SubMeshesSize; i++)
                {
                    m_SubMeshes[i] = new SubMesh(reader);
                }

                m_IndexBuffer = reader.ReadBytes(reader.ReadInt32());
                reader.AlignStream();

                m_VertexData = new VertexData(reader);
            }
            else
            {
                var verticesSize = reader.ReadInt32();
                vertices = new SpriteVertex[verticesSize];
                for (int i = 0; i < verticesSize; i++)
                {
                    vertices[i] = new SpriteVertex(reader);
                }

                indices = reader.ReadUInt16Array();
                reader.AlignStream();
            }

            if (version[0] >= 2018) //2018 and up
            {
                m_Bindpose = reader.ReadMatrixArray();

                if (version[0] == 2018 && version[1] < 2) //2018.2 down
                {
                    var m_SourceSkinSize = reader.ReadInt32();
                    for (int i = 0; i < m_SourceSkinSize; i++)
                    {
                        m_SourceSkin[i] = new BoneWeights4(reader);
                    }
                }
            }

            textureRect       = reader.ReadRectangleF();
            textureRectOffset = reader.ReadVector2();
            if (version[0] > 5 || (version[0] == 5 && version[1] >= 6)) //5.6 and up
            {
                atlasRectOffset = reader.ReadVector2();
            }

            settingsRaw = new SpriteSettings(reader);
            if (version[0] > 4 || (version[0] == 4 && version[1] >= 5)) //4.5 and up
            {
                uvTransform = reader.ReadVector4();
            }

            if (version[0] >= 2017) //2017 and up
            {
                downscaleMultiplier = reader.ReadSingle();
            }
        }
Esempio n. 6
0
        public PointF[][] m_PhysicsShape; //Vector2[][]

        public Sprite(ObjectReader reader) : base(reader)
        {
            //Rectf m_Rect
            m_Rect = reader.ReadRectangleF();
            //Vector2f m_Offset
            reader.Position += 8;
            if (version[0] > 4 || (version[0] == 4 && version[1] >= 5)) //4.5 and up
            {
                //Vector4f m_Border
                reader.Position += 16;
            }

            m_PixelsToUnits = reader.ReadSingle();
            if (version[0] > 5 ||
                (version[0] == 5 && version[1] > 4) ||
                (version[0] == 5 && version[1] == 4 && version[2] >= 2))    //5.4.2 and up
            {
                //Vector2f m_Pivot
                m_Pivot = reader.ReadVector2();
            }

            var m_Extrude = reader.ReadUInt32();

            if (version[0] > 5 || (version[0] == 5 && version[1] >= 3)) //5.3 and up
            {
                var m_IsPolygon = reader.ReadBoolean();
                reader.AlignStream(4);
            }

            if (version[0] >= 2017) //2017 and up
            {
                //pair m_RenderDataKey
                var first  = new Guid(reader.ReadBytes(16));
                var second = reader.ReadInt64();
                m_RenderDataKey = new Tuple <Guid, long>(first, second);
                //vector m_AtlasTags
                var size = reader.ReadInt32();
                for (int i = 0; i < size; i++)
                {
                    var data = reader.ReadAlignedString();
                }

                //PPtr<SpriteAtlas> m_SpriteAtlas
                m_SpriteAtlas = reader.ReadPPtr();
            }

            //SpriteRenderData m_RD
            //  PPtr<Texture2D> texture
            texture = reader.ReadPPtr();
            //  PPtr<Texture2D> alphaTexture
            if (version[0] > 5 || (version[0] == 5 && version[1] >= 2)) //5.2 and up
            {
                var alphaTexture = reader.ReadPPtr();
            }

            if (version[0] > 5 || (version[0] == 5 && version[1] >= 6)) //5.6 and up
            {
                //  vector m_SubMeshes
                var size = reader.ReadInt32();
                //      SubMesh data
                if (version[0] > 2017 || (version[0] == 2017 && version[1] >= 3)) //2017.3 and up
                {
                    reader.Position += 48 * size;
                }
                else
                {
                    reader.Position += 44 * size;
                }

                //  vector m_IndexBuffer
                size             = reader.ReadInt32();
                reader.Position += size; //UInt8 data
                reader.AlignStream(4);
                //  VertexData m_VertexData
                if (version[0] < 2018)//2018 down
                {
                    var m_CurrentChannels = reader.ReadInt32();
                }
                var m_VertexCount = reader.ReadUInt32();
                //      vector m_Channels
                size             = reader.ReadInt32();
                reader.Position += size * 4; //ChannelInfo data
                                             //      TypelessData m_DataSize
                size             = reader.ReadInt32();
                reader.Position += size;     //UInt8 data
                reader.AlignStream(4);

                if (version[0] >= 2018)//2018 and up
                {
                    //	vector m_Bindpose
                    //			Matrix4x4f data
                    size             = reader.ReadInt32();
                    reader.Position += size * 64;
                    if (version[0] == 2018 && version[1] < 2) //2018.2 down
                    {
                        //	vector m_SourceSkin
                        //			BoneWeights4 data
                        size             = reader.ReadInt32();
                        reader.Position += size * 32;
                    }
                }
            }
            else
            {
                //  vector vertices
                var size = reader.ReadInt32();
                for (int i = 0; i < size; i++)
                {
                    //SpriteVertex data
                    reader.Position += 12;                                      //Vector3f pos
                    if (version[0] < 4 || (version[0] == 4 && version[1] <= 3)) //4.3 and down
                    {
                        reader.Position += 8;                                   //Vector2f uv
                    }
                }

                //  vector indices
                size             = reader.ReadInt32();
                reader.Position += 2 * size; //UInt16 data
                reader.AlignStream(4);
            }

            //  Rectf textureRect
            textureRect = reader.ReadRectangleF();
            //  Vector2f textureRectOffset
            reader.Position += 8;
            //  Vector2f atlasRectOffset - 5.6 and up
            if (version[0] > 5 || (version[0] == 5 && version[1] >= 6)) //5.6 and up
            {
                reader.Position += 8;
            }
            //  unsigned int settingsRaw
            settingsRaw = new SpriteSettings(reader);
            //  Vector4f uvTransform - 4.5 and up
            if (version[0] > 4 || (version[0] == 4 && version[1] >= 5)) //4.5 and up
            {
                reader.Position += 16;
            }
            if (version[0] >= 2017) //2017 and up
            {
                //  float downscaleMultiplier - 2017 and up
                reader.Position += 4;
                //vector m_PhysicsShape - 2017 and up
                var m_PhysicsShape_size = reader.ReadInt32();
                m_PhysicsShape = new PointF[m_PhysicsShape_size][];
                for (int i = 0; i < m_PhysicsShape_size; i++)
                {
                    var data_size = reader.ReadInt32();
                    //Vector2f
                    m_PhysicsShape[i] = new PointF[data_size];
                    for (int j = 0; j < data_size; j++)
                    {
                        m_PhysicsShape[i][j] = new PointF(reader.ReadSingle(), reader.ReadSingle());
                    }
                }
            }
            //vector m_Bones 2018 and up
        }