Exemplo n.º 1
0
        private static IEnumerable <VertexPositionTexture> CreateVertices(Texture2D texture, Vector2 position, Rectangle sourceRectangle, TiledMapTileFlipFlags flags = TiledMapTileFlipFlags.None)
        {
            var reciprocalWidth  = 1f / texture.Width;
            var reciprocalHeight = 1f / texture.Height;
            var texelLeft        = (sourceRectangle.X + 0.5f) * reciprocalWidth;
            var texelTop         = (sourceRectangle.Y + 0.5f) * reciprocalHeight;
            var texelRight       = (sourceRectangle.X + sourceRectangle.Width) * reciprocalWidth;
            var texelBottom      = (sourceRectangle.Y + sourceRectangle.Height) * reciprocalHeight;

            VertexPositionTexture vertexTopLeft, vertexTopRight, vertexBottomLeft, vertexBottomRight;

            vertexTopLeft.Position     = new Vector3(position, 0);
            vertexTopRight.Position    = new Vector3(position + new Vector2(sourceRectangle.Width, 0), 0);
            vertexBottomLeft.Position  = new Vector3(position + new Vector2(0, sourceRectangle.Height), 0);
            vertexBottomRight.Position = new Vector3(position + new Vector2(sourceRectangle.Width, sourceRectangle.Height), 0);

            vertexTopLeft.TextureCoordinate.Y = texelTop;
            vertexTopLeft.TextureCoordinate.X = texelLeft;

            vertexTopRight.TextureCoordinate.Y = texelTop;
            vertexTopRight.TextureCoordinate.X = texelRight;

            vertexBottomLeft.TextureCoordinate.Y = texelBottom;
            vertexBottomLeft.TextureCoordinate.X = texelLeft;

            vertexBottomRight.TextureCoordinate.Y = texelBottom;
            vertexBottomRight.TextureCoordinate.X = texelRight;

            var flipDiagonally   = (flags & TiledMapTileFlipFlags.FlipDiagonally) != 0;
            var flipHorizontally = (flags & TiledMapTileFlipFlags.FlipHorizontally) != 0;
            var flipVertically   = (flags & TiledMapTileFlipFlags.FlipVertically) != 0;

            if (flipDiagonally)
            {
                FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.X, ref vertexBottomLeft.TextureCoordinate.X);
                FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.Y, ref vertexBottomLeft.TextureCoordinate.Y);
            }

            if (flipHorizontally)
            {
                if (flipDiagonally)
                {
                    FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.Y, ref vertexTopRight.TextureCoordinate.Y);
                    FloatHelper.Swap(ref vertexBottomLeft.TextureCoordinate.Y, ref vertexBottomRight.TextureCoordinate.Y);
                }
                else
                {
                    FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.X, ref vertexTopRight.TextureCoordinate.X);
                    FloatHelper.Swap(ref vertexBottomLeft.TextureCoordinate.X, ref vertexBottomRight.TextureCoordinate.X);
                }
            }

            if (flipVertically)
            {
                if (flipDiagonally)
                {
                    FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.X, ref vertexBottomLeft.TextureCoordinate.X);
                    FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.X, ref vertexBottomRight.TextureCoordinate.X);
                }
                else
                {
                    FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.Y, ref vertexBottomLeft.TextureCoordinate.Y);
                    FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.Y, ref vertexBottomRight.TextureCoordinate.Y);
                }
            }

            yield return(vertexTopLeft);

            yield return(vertexTopRight);

            yield return(vertexBottomLeft);

            yield return(vertexBottomRight);
        }
Exemplo n.º 2
0
        public void AddTileVertices(Point2 position, Rectangle?sourceRectangle = null, TiledMapTileFlipFlags flags = TiledMapTileFlipFlags.None)
        {
            float texelLeft, texelTop, texelRight, texelBottom;
            var   sourceRectangle1 = sourceRectangle ?? new Rectangle(0, 0, (int)ImageSize.Width, (int)ImageSize.Height);

            if (sourceRectangle.HasValue)
            {
                var reciprocalWidth  = 1f / ImageSize.Width;
                var reciprocalHeight = 1f / ImageSize.Height;
                texelLeft   = (sourceRectangle1.X + 0.5f) * reciprocalWidth;
                texelTop    = (sourceRectangle1.Y + 0.5f) * reciprocalHeight;
                texelRight  = (sourceRectangle1.X + sourceRectangle1.Width) * reciprocalWidth;
                texelBottom = (sourceRectangle1.Y + sourceRectangle1.Height) * reciprocalHeight;
            }
            else
            {
                texelLeft   = 0;
                texelTop    = 0;
                texelBottom = 1;
                texelRight  = 1;
            }

            VertexPositionTexture vertexTopLeft, vertexTopRight, vertexBottomLeft, vertexBottomRight;

            vertexTopLeft.Position     = new Vector3(position, 0);
            vertexTopRight.Position    = new Vector3(position + new Vector2(sourceRectangle1.Width, 0), 0);
            vertexBottomLeft.Position  = new Vector3(position + new Vector2(0, sourceRectangle1.Height), 0);
            vertexBottomRight.Position =
                new Vector3(position + new Vector2(sourceRectangle1.Width, sourceRectangle1.Height), 0);

            vertexTopLeft.TextureCoordinate.Y = texelTop;
            vertexTopLeft.TextureCoordinate.X = texelLeft;

            vertexTopRight.TextureCoordinate.Y = texelTop;
            vertexTopRight.TextureCoordinate.X = texelRight;

            vertexBottomLeft.TextureCoordinate.Y = texelBottom;
            vertexBottomLeft.TextureCoordinate.X = texelLeft;

            vertexBottomRight.TextureCoordinate.Y = texelBottom;
            vertexBottomRight.TextureCoordinate.X = texelRight;

            var flipDiagonally   = (flags & TiledMapTileFlipFlags.FlipDiagonally) != 0;
            var flipHorizontally = (flags & TiledMapTileFlipFlags.FlipHorizontally) != 0;
            var flipVertically   = (flags & TiledMapTileFlipFlags.FlipVertically) != 0;

            if (flipDiagonally)
            {
                FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.X, ref vertexBottomLeft.TextureCoordinate.X);
                FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.Y, ref vertexBottomLeft.TextureCoordinate.Y);
            }

            if (flipHorizontally)
            {
                if (flipDiagonally)
                {
                    FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.Y, ref vertexTopRight.TextureCoordinate.Y);
                    FloatHelper.Swap(ref vertexBottomLeft.TextureCoordinate.Y, ref vertexBottomRight.TextureCoordinate.Y);
                }
                else
                {
                    FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.X, ref vertexTopRight.TextureCoordinate.X);
                    FloatHelper.Swap(ref vertexBottomLeft.TextureCoordinate.X, ref vertexBottomRight.TextureCoordinate.X);
                }
            }

            if (flipVertically)
            {
                if (flipDiagonally)
                {
                    FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.X, ref vertexBottomLeft.TextureCoordinate.X);
                    FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.X, ref vertexBottomRight.TextureCoordinate.X);
                }
                else
                {
                    FloatHelper.Swap(ref vertexTopLeft.TextureCoordinate.Y, ref vertexBottomLeft.TextureCoordinate.Y);
                    FloatHelper.Swap(ref vertexTopRight.TextureCoordinate.Y, ref vertexBottomRight.TextureCoordinate.Y);
                }
            }

            _vertices.Add(vertexTopLeft);
            _vertices.Add(vertexTopRight);
            _vertices.Add(vertexBottomLeft);
            _vertices.Add(vertexBottomRight);

            Debug.Assert(Vertices.Count <= TiledMapHelper.MaximumVerticesPerModel);
        }
Exemplo n.º 3
0
        public void AddSprite(Texture2D texture, Point2 position, Rectangle sourceRectangle, TiledMapTileFlipFlags flipFlags)
        {
            Indices.AddRange(CreateTileIndices(Vertices.Count));
            Debug.Assert(Indices.Count <= TiledMapHelper.MaximumIndicesPerModel);

            Vertices.AddRange(CreateVertices(texture, position, sourceRectangle, flipFlags));
            Debug.Assert(Vertices.Count <= TiledMapHelper.MaximumVerticesPerModel);
        }