Anyone have experience implementing texture packer atlas support with trim and rotation?

I’ve been working on implementing texture packer support for our game. We have setup the loaders no problem, and sprites are mostly rendering correctly. But I’m having real trouble understanding how to handle a frame that is both rotated and trimmed.

These assets are from a power bar, so the idea is the artist has set them up with trim such that when they are all placed in the same position in game they should line up neatly as segments of the bar. However as can be seen in this screenshot, some segments appear almost rotated 180 upside down, and all these sprites are ones rotated by texture packer. The red square is the origin they should all share.

(just to explain texture packer has a rotation and trim feature that optimises the sprite sheet size, so this is done before loading into monogame, so unrelated to the sprites rotation in game).

And the render sprite code. Trim will either have some value or be zero in case of untrimmed.

Rectangle frame = sprite.TextureRegion.Frame;
Rectangle sourceRect = sprite.TextureRegion.SourceRectangle;
Rectangle trim = sprite.TextureRegion.Trim;
Vector2 size = sprite.TextureRegion.Size;

Vector2 origin = new Vector2(size.X * sprite.Anchor.X - trim.X, size.Y * sprite.Anchor.Y - trim.Y);

sprite.WorldMatrix.Decompose(out Vector2 scale, out float rotation, out Vector2 translation);

if (sprite.TextureRegion.IsRotated)
{
frame = new Rectangle(frame.X, frame.Y, frame.Height, frame.Width);
rotation -= ClockwiseNinetyDegreeRotation;
origin = new Vector2(size.X * (1 - sprite.Anchor.Y) - trim.Y, size.Y * sprite.Anchor.X - trim.X);
}

batcher.DrawSprite(
sprite.TextureRegion.BaseTexture,
translation,
frame,
sprite.Tint * sprite.WorldAlpha,
rotation,
origin,
scale,
sprite.Effect,
0
);

Does anyone have any knowledge here? Googling is not proving very fruitful, other people have implemented it but good info on how seems none existent. Currently I am just digging in other frameworks source code and attempting to understand their methods.

Do you really need to trim and rotate textures in your atlas? You won’t gain much from it and only will blow out your packer/reader/renderer’s complexity. I made a simple packer without any rotation\trimming and it works flawlessly.

For now yeah that’s what I’ve done, I just disabled rotation in TP, my trimming implementation works anyway.

I would dispute that it won’t make any difference though, most of our games use quite a lot of textures and we often target low end mobile devices. Its already running at ~45fps on the lowest device, which is perfectly fine but there’s still quite a bit to add yet. Add to that conditions from clients that the app size is below a certain size and it becomes pretty important.

Also we often use the trim to position stuff easily such as the power bar in that image, be fiddly to have to exactly position each of those segments and it’d get a lot worse in complex scenes and menus. So at least in our case it is a required feature.