Jump to content

Let's talk about sprite batches


Recommended Posts

I've scoured the internet and cannot find examples of how to use sprite batches. 

When would you use a sprite batch instead of a group? Is there any difference to using a sprite batch and using a group? If you create a display object with a group, then add it to a sprite batch, does it gain the performance benefits of being in a sprite batch?

What does this mean (in the docs):


 It's worth mentioning that by default sprite batches are used through-out the renderer, so you only really need to use a SpriteBatch if you have over 1000 sprites that all share the same texture (or texture atlas). It's also useful if running in Canvas mode and you have a lot of un-rotated or un-scaled Sprites as it skips all of the Canvas setTransform calls, which helps performance, especially on mobile devices.

Does this mean there is no need to use game.add.spriteBatch instead of game.add.group, because it is automatically used by the renderer?

What the hell are sprite batches and why do i need them? 

Link to comment
Share on other sites

from Pixi:

The ParticleContainer class is a really fast version of the Container built solely for speed,
so use when you need a lot of sprites or particles. The tradeoff of the ParticleContainer is that advanced
functionality will not work. ParticleContainer implements only the basic object transform (position, scale, rotation).
Any other functionality like tinting, masking, etc will not work on sprites in this batch.

Regular displayObjectContainers also batch automatically. For most uses case they are the best choice as they offer speed and flexibility. Having a SpriteBatch class implies that a displayObjectContainer does not do any batching and is magically going to be faster.

The reason a spriteBatch class is sometimes faster is that it uploads transforms to be calculated on the GPU. The canvas spriteBatcher also optimizes the draw calls by not using the translate() function if there is no rotation on a sprite.

Its performance gains will only be apparent if there are no nested children in the sprites added. A perfect use case for it would be particles and also a tilemap :)

It definitely has its place but the title SpriteBatch is really misleading. Really it should be called something like GPUTransformContainer - but thats a terrible name :/

Definitely worth noting again that 99% of the time a regular displayObjectContainer will be the most efficient way to render content.

Link to comment
Share on other sites

I'm not up to date on Phaser (ping @rich), but a Spritebatch will only position, scale, and rotate. Tinting, masking, etc won't work. A Group is a different thing altogether. It's more of a logical structure, that happens to extend a DisplayObjectContainer (will be optimized/batched also, just not as much as a Spritebatch). I would say, only use Spritebatch when you have a lot (1000+) of 'unGrouped/unNested' Sprites with which you're not doing anything advanced.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...