static void BuildTransform(Vector3 position, Vector2 size, SpriteFlags flags, out Matrix4x4 transform)
        {
            var offset = (flags & SpriteFlags.AlignmentMask) switch
            {
                0 => Vector3.Zero,
                SpriteFlags.MidAligned => new Vector3(0, -0.5f, 0),
                SpriteFlags.BottomAligned => new Vector3(0, -1.0f, 0),
                SpriteFlags.LeftAligned => new Vector3(0.5f, 0, 0),
                SpriteFlags.LeftAligned | SpriteFlags.MidAligned => new Vector3(0.5f, -0.5f, 0),
                SpriteFlags.LeftAligned | SpriteFlags.BottomAligned => new Vector3(0.5f, -1.0f, 0),
                _ => Vector3.Zero
            };

            transform = Matrix4x4.CreateTranslation(offset);

            if (flags.HasFlag(SpriteFlags.Floor))
            {
                transform *= new Matrix4x4(
                    1, 0, 0, 0,
                    0, 0, -1, 0,
                    0, 1, 0, 0,
                    0, 0, 0, 1);
            }

            transform *= Matrix4x4.CreateScale(new Vector3(size.X, size.Y, size.X));
            transform *= Matrix4x4.CreateTranslation(position);
        }
Example #2
0
        SpriteInstanceData(Vector3 position, Vector2 size, Vector2 texPosition, Vector2 texSize, uint texLayer, SpriteFlags flags)
        {
            var offset = (flags & SpriteFlags.AlignmentMask) switch
            {
                0 => Vector3.Zero,
                SpriteFlags.MidAligned => new Vector3(0, -0.5f, 0),
                SpriteFlags.BottomAligned => new Vector3(0, -1.0f, 0),
                SpriteFlags.LeftAligned => new Vector3(0.5f, 0, 0),
                SpriteFlags.LeftAligned | SpriteFlags.MidAligned => new Vector3(0.5f, -0.5f, 0),
                SpriteFlags.LeftAligned | SpriteFlags.BottomAligned => new Vector3(0.5f, -1.0f, 0),
                _ => Vector3.Zero
            };

            var transform = Matrix4x4.CreateTranslation(offset);

            if (flags.HasFlag(SpriteFlags.Floor))
            {
                transform *= new Matrix4x4(
                    1, 0, 0, 0,
                    0, 0, -1, 0,
                    0, 1, 0, 0,
                    0, 0, 0, 1);
            }

            transform *= Matrix4x4.CreateScale(new Vector3(size.X, size.Y, size.X));
            transform *= Matrix4x4.CreateTranslation(position);

            Transform1 = new Vector3(transform.M11, transform.M12, transform.M13);
            Transform2 = new Vector3(transform.M21, transform.M22, transform.M23);
            Transform3 = new Vector3(transform.M31, transform.M32, transform.M33);
            Transform4 = new Vector3(transform.M41, transform.M42, transform.M43);
            // Assume right column is always 0,0,0,1

            TexPosition = texPosition;
            TexSize     = texSize;
            TexLayer    = texLayer;
            Flags       = flags;
        }