Table of Contents

Struct SpriteTransform

Namespace
Egami.Drawing
Assembly
Egami.Drawing.dll

A sprite's placement in DrawAtlas(Raster, ReadOnlySpan<SpriteTransform>, ReadOnlySpan<RectF>, ReadOnlySpan<RgbaF32>, BlendMode, SamplingOptions?, RectF?, Paint?), as Skia's RSXform: a rotation with a uniform scale, then a move. A point (x, y) of the sprite, measured from its source rectangle's top-left corner, goes to (ScaleCos·x − ScaleSin·y + Tx, ScaleSin·x + ScaleCos·y + Ty).

public readonly record struct SpriteTransform : IEquatable<SpriteTransform>
Implements
Inherited Members

Examples

// Pack the sprites once, then draw thousands of them in one call: each with its own place, turn, scale and tint.
using SpriteSheet sheet = SpriteSheet.Pack([spark, star], new SpritePackOptions { Padding = 1 });
Random random = new(7);
SpriteTransform[] transforms = new SpriteTransform[2000];
RectF[] sources = new RectF[transforms.Length];
RgbaF32[] tints = new RgbaF32[transforms.Length];
for (int i = 0; i < transforms.Length; i++)
{
    Rect frame = sheet.Frames[i % 2];
    sources[i] = frame;
    transforms[i] = SpriteTransform.FromRotationScale(
        scale: 0.3f + random.NextSingle(),
        degrees: random.NextSingle() * 360,
        position: new Vector2(random.NextSingle() * 320, random.NextSingle() * 200),
        anchor: new Vector2(frame.Width / 2f, frame.Height / 2f));
    tints[i] = new RgbaF32(1, 0.5f + (random.NextSingle() / 2), random.NextSingle(), 0.8f);
}

using (RasterCanvas<Rgba8> canvas = raster.CreateCanvas())
{
    // Plus adds the sprites up, as light does.
    canvas.DrawAtlas(sheet.Image, transforms, sources, tints, BlendMode.Modulate, paint: new SolidPaint(Rgba8.White) { BlendMode = BlendMode.Plus });
}

Constructors

SpriteTransform(float, float, float, float)

A sprite's placement in DrawAtlas(Raster, ReadOnlySpan<SpriteTransform>, ReadOnlySpan<RectF>, ReadOnlySpan<RgbaF32>, BlendMode, SamplingOptions?, RectF?, Paint?), as Skia's RSXform: a rotation with a uniform scale, then a move. A point (x, y) of the sprite, measured from its source rectangle's top-left corner, goes to (ScaleCos·x − ScaleSin·y + Tx, ScaleSin·x + ScaleCos·y + Ty).

public SpriteTransform(float ScaleCos, float ScaleSin, float Tx, float Ty)

Parameters

ScaleCos float

The scale times the cosine of the angle.

ScaleSin float

The scale times the sine of the angle.

Tx float

The horizontal move.

Ty float

The vertical move.

Examples

// Pack the sprites once, then draw thousands of them in one call: each with its own place, turn, scale and tint.
using SpriteSheet sheet = SpriteSheet.Pack([spark, star], new SpritePackOptions { Padding = 1 });
Random random = new(7);
SpriteTransform[] transforms = new SpriteTransform[2000];
RectF[] sources = new RectF[transforms.Length];
RgbaF32[] tints = new RgbaF32[transforms.Length];
for (int i = 0; i < transforms.Length; i++)
{
    Rect frame = sheet.Frames[i % 2];
    sources[i] = frame;
    transforms[i] = SpriteTransform.FromRotationScale(
        scale: 0.3f + random.NextSingle(),
        degrees: random.NextSingle() * 360,
        position: new Vector2(random.NextSingle() * 320, random.NextSingle() * 200),
        anchor: new Vector2(frame.Width / 2f, frame.Height / 2f));
    tints[i] = new RgbaF32(1, 0.5f + (random.NextSingle() / 2), random.NextSingle(), 0.8f);
}

using (RasterCanvas<Rgba8> canvas = raster.CreateCanvas())
{
    // Plus adds the sprites up, as light does.
    canvas.DrawAtlas(sheet.Image, transforms, sources, tints, BlendMode.Modulate, paint: new SolidPaint(Rgba8.White) { BlendMode = BlendMode.Plus });
}

Properties

ScaleCos

The scale times the cosine of the angle.

public float ScaleCos { get; init; }

Property Value

float

ScaleSin

The scale times the sine of the angle.

public float ScaleSin { get; init; }

Property Value

float

Tx

The horizontal move.

public float Tx { get; init; }

Property Value

float

Ty

The vertical move.

public float Ty { get; init; }

Property Value

float

Methods

FromRotationScale(float, float, Vector2, Vector2)

Creates a placement that scales and turns the sprite about an anchor and puts the anchor at a position.

public static SpriteTransform FromRotationScale(float scale, float degrees, Vector2 position, Vector2 anchor = default)

Parameters

scale float

The scale.

degrees float

The clockwise turn on a y-down screen, as Rotate(float).

position Vector2

Where the anchor goes, in local coordinates.

anchor Vector2

The point of the sprite that stays put, from its source rectangle's top-left corner.

Returns

SpriteTransform

The placement.

ToMatrix()

Returns the placement as a matrix from sprite coordinates to local coordinates.

public Matrix3x2 ToMatrix()

Returns

Matrix3x2

The matrix.