5
\$\begingroup\$

The problem: I can't fit all my 2d animation sprites for all units and environment in 1 spritesheet.

Everything batches fine until I use two spritesheets... I noticed the following : all units from spritesheet A batch fine until something needs to be rendered fom spritesheet B. so lets say from back to camera : 50units - tree -50 units - camera will result in 3 drawcalls... Not that bad but if you have: unit- tree - unit -tree -unit -tree ... like it's probably most of the time... this wil result in 100+ drawcalls even though you have only two spritesheets...

How can you solve this? Or better, how can i use two spritesheets and have only two drawcalls even though the sprites are at different distances to the camera

Hope this image helps to understand the problem:

1 : the trees are on spritesheet B and batch fine 2: the batching is broken as the unitsprite sits on spritesheet A and is put in between the trees... enter image description here

\$\endgroup\$
4
\$\begingroup\$

Sort the rendering order by the used sprite sheet and not by distance from camera. This works fine if you use z-buffer and don't render transparent objects. I.e. first draw all the trees (and other objects in the same sprite sheet) into the scene and test and write the depth in z-buffer. Then render all units and do the same. This will result in 2 draw calls regardless how many units and trees you have or how they are placed.

Another option is to use array textures if it's supported on your target platform. Here you can store all the atlas textures into a single texture (it's just a stack of 2D textures) and you can render the whole scene with single draw call in any order you want.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ can you explain this in a bit more detail because I don't really understand it \$\endgroup\$ – Ruben O Sep 23 '14 at 9:53
  • \$\begingroup\$ Ok, I added a bit more explanation. \$\endgroup\$ – JarkkoL Sep 23 '14 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.