Struct SpriteTransform
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
ScaleCosfloatThe scale times the cosine of the angle.
ScaleSinfloatThe scale times the sine of the angle.
TxfloatThe horizontal move.
TyfloatThe 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
ScaleSin
The scale times the sine of the angle.
public float ScaleSin { get; init; }
Property Value
Tx
The horizontal move.
public float Tx { get; init; }
Property Value
Ty
The vertical move.
public float Ty { get; init; }
Property Value
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
scalefloatThe scale.
degreesfloatThe clockwise turn on a y-down screen, as Rotate(float).
positionVector2Where the anchor goes, in local coordinates.
anchorVector2The 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.