public SpriteRenderItem(VertexPosition3ColorTexture topLeft, VertexPosition3ColorTexture topRight, VertexPosition3ColorTexture bottomLeft, VertexPosition3ColorTexture bottomRight, Texture2D texture)
 {
     TopLeft     = topLeft;
     TopRight    = topRight;
     BottomLeft  = bottomLeft;
     BottomRight = bottomRight;
     Texture     = texture;
 }
Ejemplo n.º 2
0
        public void Draw(Texture2D texture, Vector2 position, IntRect?sourceRectangle, RgbaFloat color, float rotation, Vector2 origin, Vector2 scale, float depth)
        {
            var     actualOrigin = origin * scale;
            float   w;
            float   h;
            Vector2 texCoordsTL;
            Vector2 texCoordsBR;

            if (sourceRectangle.HasValue)
            {
                var sourceRect = sourceRectangle.GetValueOrDefault();
                w = sourceRect.Width * scale.X;
                h = sourceRect.Height * scale.Y;
                float wF = 1f / texture.Width;
                float hF = 1f / texture.Height;
                texCoordsTL = new Vector2(sourceRect.Left * wF, sourceRect.Top * hF);
                texCoordsBR = new Vector2(sourceRect.Right * wF, sourceRect.Bottom * hF);
            }
            else
            {
                w           = texture.Width * scale.X;
                h           = texture.Height * scale.Y;
                texCoordsTL = Vector2.Zero;
                texCoordsBR = Vector2.One;
            }

            Vector2 posTL;
            Vector2 posTR;
            Vector2 posBL;
            Vector2 posBR;

            if (rotation == 0f)
            {
                float x = position.X - actualOrigin.X;
                float y = position.Y - actualOrigin.Y;
                posTL = new Vector2(x, y);
                posTR = new Vector2(x + w, y);
                posBL = new Vector2(x, y + h);
                posBR = new Vector2(x + w, y + h);
            }
            else
            {
                float rotSin = (float)Math.Sin(rotation);
                float rotCos = (float)Math.Cos(rotation);
                float dx     = -actualOrigin.X;
                float dy     = -actualOrigin.Y;
                posTL = new Vector2(
                    position.X + (dx * rotCos) - (dy * rotSin),
                    position.Y + (dx * rotSin) - (dy * rotCos));
                posTR = new Vector2(
                    position.X + ((dx + w) * rotCos) - (dy * rotSin),
                    position.Y + ((dx + w) * rotSin) - (dy * rotCos));
                posBL = new Vector2(
                    position.X + (dx * rotCos) - ((dy + h) * rotSin),
                    position.Y + (dx * rotSin) - ((dy + h) * rotCos));
                posBR = new Vector2(
                    position.X + ((dx + w) * rotCos) - ((dy + h) * rotSin),
                    position.Y + ((dx + w) * rotSin) - ((dy + h) * rotCos));
            }

            var vertexTL = new VertexPosition3ColorTexture(new Vector3(posTL, depth), color, texCoordsTL);
            var vertexTR = new VertexPosition3ColorTexture(new Vector3(posTR, depth), color, new Vector2(texCoordsBR.X, texCoordsTL.Y));
            var vertexBL = new VertexPosition3ColorTexture(new Vector3(posBL, depth), color, new Vector2(texCoordsTL.X, texCoordsBR.Y));
            var vertexBR = new VertexPosition3ColorTexture(new Vector3(posBR, depth), color, texCoordsBR);

            var sprite = new SpriteRenderItem(vertexTL, vertexTR, vertexBL, vertexBR, texture);

            sprites.Add(sprite);
        }