Hello Guest

Author Topic: Isometric game using 2dToolkit, overlapping buildings > too many draw calls  (Read 5023 times)

Velvety

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Velvety Couch Games
Hi,

I am running into the same issue explained here: http://2dtoolkit.com/forum/index.php/topic,1756.msg8564.html.  Basically I have an isometric game with 2dToolkit with lots of overlapping buildings and troops, each set with a different draw layer to ensure they draw in the correct order so it looks isometric.  Even with all the buildings in 1 sprite atlas and no troops showing up I can get around 70 draw calls because (I think) the various overlapping sprites are all set to different draw orders.  Is that correct?

Am I doing something wrong, or is this just the nature of how it works?

Are there any ways to help reduce the draw calls for something like this?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
If you need a particular draw order and have more than one atlas involved your draw calls will stack up. If you can find some way of defining draw calls explicitly in your sprites to reduce the draw calls, set up the order in layer value in such a way to define draw order on absolutely everything - that removes any ambiguity from material sorting and z sorting, leaving you fully in charge of sorting.


[  ATLAS0 ]   [ ATLAS1]   [ATLAS0]   [ATLAS1]
0 1 2 3 4 5    6 7 8         9               10

The general rule is, things that are next to each other in order in layer get merged.
If the order is specified as above, it will result in 4 draw calls.

Velvety

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Velvety Couch Games
Thanks for the reply.  That's what I suspected.  Things start to get out of whack as soon as I have a few sprites from other atlases (units, animated buildings that couldn't fit into the main building atlas) drawing in between other layers.  But knowing how it works I can at least try to minimize how often that happens, to an extent.

Is there any way to control what order things are drawn within the same draw layer?  This must be happening somewhere, but I suspect it is deep within Unity and unaccessible?  For instance, if I had all of ATLAS0 drawing at layer 10 and then had some way of determining what order within that layer each sprite was drawn,  it seems like I could make significant draw call improvements.  But the best I can tell is that Unity draws them in the order they were first instantiated (I think).  If that is the case, my buildings can all be dragged around and moved by the user so that won't help me.  Any idea how one might access that order and modify it?  Is it even possible? Thanks.

Velvety

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Velvety Couch Games
Also, it seems like mixed layers of atlases and draw orders trigger extra draw calls even if the sprites don't overlap.  Is that how it works?  So for instance, your example of:

[  ATLAS0 ]   [ ATLAS1]   [ATLAS0]   [ATLAS1]
0 1 2 3 4 5    6 7 8         9               10

would still be 4 draw calls regardless if the sprites overlapped or not.  Is this this true? If so, why is this the case when they aren't even overlapping?

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Unity sorts by z after sorting by Sorting Layer and subsequently Order In Layer. So.. if you use Order In Layer exclusively to order your sprites, it will trump the z order. By setting the 0, 1,2,3,4 values to Order In Layer as in the "diagram" above, you will be able to fully control what draws when. You can take advantage of your knowledge of the scene to sort things more efficiently.