Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Tilemap Sorting on Z-Axis with individual vs chunk modes

Discussion in '2D' started by GazingUp, Jun 30, 2020.

  1. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    135
    The tilemaps' chunk and individual modes are interesting because they have an impact on performance (at least based on 2 year old threads) but I wanted a bit more clarity as to whether this is a real concern for PC platforms.

    I intend on having multiple height levels in my game (3/4 perspective rpg) and I found the simplest way is to deal with the z-axis than sorting orders (Thank you GraphicsSettings.transparencySortAxis.)

    If the tilemap mode is set to Chunk this works in a sort of hacky way. The tilemap I want to sort would only work on the z-axis based on the number of tiles on the tilemap (at least that's what I think is happening...) And this does seem to be very performant as well albeit hacky since now I would need to calculate the number of tiles to calculate a z-index value and my player would need to "jump" to a number higher than this z-index value which is not possible if I want to implement jumping on a higher level.

    When I use the individual mode though, the z-axis works beautifully. Setting the z index of a tilemap to say 1, will render it ahead of everything that has a z-index of 0. But now the performance (well I haven't noticed it yet since I only have a small starter field with a handful of sprites and a couple tilemaps so far) has more batch calls.

    My question is this - do I need to worry about this? I really want to implement a height system using the indivudal-z-axis sorting. It would be amazing. And my only platform target is PC not mobile. Do I need to be concerned? If so, can I know of some optimization steps I can take? I have already done occlusion culling on the camera and sprite atlas packing, but any other tips ? Thank you.
     
    Last edited: Jun 30, 2020
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    175
    From my experience, the individual tiling seems to be fine so long as you don't have a huge tilemap. So depends on the size of your tilemap. If it is large, you probably want to add/remove the tiles as you go to keep things manageable. My own game it using individually, and so far, with add/remove handling, I'm getting multiple hundred FPS and that's doing a lot of pretty inefficient stuff each frame so the tilemap has been a minimal concern.
     
  3. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    135
    Thank you for the response.

    That makes sense. I think if I created smaller "chunks" of tilemap the batches are significantly less. Unity seems to be automatically doing batch calls of existing tilemap/sprites in the camera's view. So if the camera is viewing a tilemap, it will make draw calls of ALL the tile sprites of that tilemap.

    Because when I was removing sprites/tiles outside of the game view, it wasn't affecting performance and as my player runs around the batch calls decrease/increase depending on the number of sprites on the screen. I hope I am right because this would be a huge optimization technique! I love individual mode! Solves so many issues with tilemap sorting.
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    259
    Right now the batching for Individual mode isn't the best. See here:
    https://forum.unity.com/threads/how...mode-work-under-the-hood.904340/#post-5941853
    Basically it doesn't do per-sprite culling. From my own tests, GameObjects render faster due to far superior culling, but have heavier overhead in other ways.

    If you're using z-as-y, you probably want to be careful about overdraw, so custom outlines on your sprites will help. Only draw the outward facing part of the sprite so that a wall, say composed of three things stacked on top of one another, do not overdraw where they overlap. Instead, there's very little overlap. And you smartly figure out which wall-part is on top and only draw the top there as to avoid overdraw for the middle sections. But that may be tedious so reserve for a later optimization pass.
     
    Last edited: Jun 30, 2020
  5. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    135
    That's what I thought so as well. The batching is considering all the tiles of an entire tilemap regardless of whether it's in or out of the camera view. Promising that they are working on improving this! But I think for now a solution would be to create smaller tilemaps so the batch calls can be reduced in the camera's frustum. Going to try this out and see if it works!!!


    Also thanks for the tip about the outlines! I noticed that has an effect on the number of verts drawn!
     
    Last edited: Jun 30, 2020
  6. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    259
    If you are early in your development process, it will would probably be best to focus on getting Unity to make this optimization for you rather than splitting the tilemaps yourself, adding to complexity. In the time being, use a wrapper for accessing the Tilemap API in case they're too slow, so that there's a nice convenient place to make the fix yourself, if necessary. That way you hedge against spending development time doing something Unity may address themselves within your timeframe.
     
  7. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    135
    Thank you! Yes I am indeed early in the process. You're right about that. I just wanted to make sure I knew that the alternatives were possible to plan my workflow accordingly :)
     
unityunity