Skip to content

Commit

Permalink
perf: Optimize TexturePackerSprite when sprites do not need to be r…
Browse files Browse the repository at this point in the history
…otated (#3236)

TexturePackerSprite creates unnecessary Transform2D and
Transform2DDecorator objects when region rotation is set to false. This
bloats the TexturePackerSprite object size and causes severe performance
issues when using thousands of TexturePackerSprites. This PR wraps the
creation of these objects in a region rotation check.

---------

Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com>
  • Loading branch information
gnarhard and spydon committed Jul 23, 2024
1 parent d881db0 commit e9512e9
Showing 1 changed file with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class TexturePackerSprite extends Sprite {
useOriginalSize ? region.originalHeight : region.height,
),
) {
_decorator = Transform2DDecorator(_transform);
if (region.rotate) {
_transform.angle = math.pi / 2;
final transform = Transform2D()..angle = math.pi / 2;
_decorator = Transform2DDecorator(transform);
}
}

Expand Down Expand Up @@ -132,8 +132,7 @@ class TexturePackerSprite extends Sprite {
src = (position ?? Vector2.zero()).toPositionedRect(_srcSize);
}

late final Decorator _decorator;
final Transform2D _transform = Transform2D();
Decorator? _decorator;

// Used to avoid the creation of new Vector2 objects in render.
static final _tmpRenderPosition = Vector2.zero();
Expand Down Expand Up @@ -206,7 +205,7 @@ class TexturePackerSprite extends Sprite {
-_tmpRenderOffset.x - _tmpRenderImageSize.x,
);

_decorator.applyChain(
_decorator?.applyChain(
(applyCanvas) => super.render(
applyCanvas,
position: _tmpRenderPosition,
Expand Down

0 comments on commit e9512e9

Please sign in to comment.