addTransform method

int addTransform({
  1. required Rect source,
  2. RSTransform? transform,
  3. bool flip = false,
  4. Color? color,
  5. double bleed = 0,
})

Add a new batch item using a RSTransform.

The source parameter is the source location on the atlas.

You can position, rotate, scale and flip it on the canvas using the transform and flip parameters.

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.

The add method may be a simpler way to add a batch item to the batch. However, if there is a way to factor out the computations of the sine and cosine of the rotation so that they can be reused over multiple calls to this constructor, it may be more efficient to directly use this method instead.

Implementation

int addTransform({
  required Rect source,
  RSTransform? transform,
  bool flip = false,
  Color? color,
  double bleed = 0,
}) {
  final handle = _allocateHandle();

  final batchItem = BatchItem(
    source: source,
    transform: transform ??= defaultTransform ?? RSTransform(1, 0, 0, 0),
    flip: flip,
    color: color ?? defaultColor,
    bleed: bleed,
  );

  if (flip && useAtlas && _flippedAtlasStatus.isNone) {
    _makeFlippedAtlas();
  }

  final slot = _batchItems.length;

  _handleToSlot[handle] = slot;
  _slotToHandle.add(handle);

  _batchItems.add(batchItem);
  _sources.add(_resolveSourceForAtlas(batchItem));

  // Apply bleed to the transform
  final bleedTransform = _computeBleedTransform(
    batchItem.transform,
    batchItem.source,
    batchItem.bleed,
  );
  _transforms.add(bleedTransform);

  // If color is not explicitly provided, store transparent.
  _colors.add(color ?? _defaultColor);

  return handle;
}