add method

int add({
  1. required Rect source,
  2. double scale = 1.0,
  3. Vector2? anchor,
  4. double rotation = 0,
  5. Vector2? offset,
  6. bool flip = false,
  7. Color? color,
  8. double bleed = 0,
})

Add a new batch item.

The source parameter is the source location on the atlas. You can position it on the canvas using the offset parameter.

You can transform the sprite from its offset using scale, rotation, anchor and flip.

The color parameter allows you to render a color behind the batch item, as a background color.

The bleed parameter expands the rendered sprite outward by this amount in all directions while keeping the source sampling region the same. This helps prevent edge artifacts (seams between tiles in a tilemap). For best results, the atlas should have padding between sprites.

Note: when useAtlas is true, a uniform scale is applied (see BatchItem.bleed). For non-square sources the shorter axis is scaled slightly more than the exact bleed value.

This method creates a new RSTransform based on the given transform arguments. If many RSTransform objects are being created and there is a way to factor out the computations of the sine and cosine of the rotation (which are computed each time this method is called) and reuse them over multiple RSTransform objects, it may be more efficient to directly use the more direct addTransform method instead.

Implementation

int add({
  required Rect source,
  double scale = 1.0,
  Vector2? anchor,
  double rotation = 0,
  Vector2? offset,
  bool flip = false,
  Color? color,
  double bleed = 0,
}) {
  anchor ??= Vector2.zero();
  offset ??= Vector2.zero();
  RSTransform? transform;

  // If any of the transform arguments is different from the defaults,
  // then we create one. This is to prevent unnecessary computations
  // of the sine and cosine of the rotation.
  if (scale != 1.0 ||
      anchor != Vector2.zero() ||
      rotation != 0 ||
      offset != Vector2.zero()) {
    transform = RSTransform.fromComponents(
      scale: scale,
      anchorX: anchor.x,
      anchorY: anchor.y,
      rotation: rotation,
      translateX: offset.x,
      translateY: offset.y,
    );
  }

  return addTransform(
    source: source,
    transform: transform,
    flip: flip,
    color: color,
    bleed: bleed,
  );
}