Hello Guest

Author Topic: Tilemap draw order by y-axis  (Read 10724 times)

andy

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Tilemap draw order by y-axis
« on: February 18, 2013, 03:34:33 pm »
Hi,

I'm writing a tiled-based top down game and I need to fix up the draw order of the tiles in the tilemap such that tiles with higher y values, up the top of the screen, are drawn first, effectively
z-ordering by the y-axis.  Would there be any easy way to do this with the Tilemap?
 
I was going to hack around in the code and set z values to the sprites based on y tile-position of the tile but then realised the sprites are grouped into chunks.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap draw order by y-axis
« Reply #1 on: February 18, 2013, 11:19:05 pm »
When you choose the TopLeft sort mode, it should sort the partitions so they are from top left, and then individual tiles by the same sort order. Does this not work properly for you?

andy

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Tilemap draw order by y-axis
« Reply #2 on: February 19, 2013, 01:35:00 pm »
Sorry I think I asked the question incorrectly, the top-left sort order is fine.

Here's the scenario (top-down view, x,y co-ordinate system is top-left) :
1) Fence tile is at 0,1 on tilemap and player sprite (not a tile on the tile map) is at the equivalent location of 0,0.5 on the tile map.   The player sprite is drawn behind the fence.
2) Fence is at tile 0,1 and player is at the equivalent location of  0,1.5 on the tile map. The  player is drawn in front of the fence.

If I weren't doing this through unity I'd sort all tiles and sprites by their y co-ordinate and layer, and then render them in that order.

The only way I could think about re-creating this process using the tilemap was by having each row of tiles in the tilemap have a specific z value, and this z value increases
as you go down the y-axis. The player sprite would have have it's z-value changed depending upon it's y-axis location.
However looks like the tile map only increments the z value of the chunks created( somewhere around line 306 of TileMapBuilderUtil). 

So...I tried adding calculated z values for tile vertices created in RenderMeshBuilder.BuildForChunk  i.e. float z = -((layer.numRows * layer.divY - 1) - (baseY + y)) * someZDepthFactor;
and got the ordering working somewhat. However there was some inconsistency in draw order between transparent objects at the same z value (e.g. player sprite and
fence tile). A search on the interwebs revealed that objects with transparency shaders sort by object pivots, and multiple transparent objects that have been
meshed together and have one pivot don't sort well. I changed the materials shaders to use SolidVertexColor and it did seem to fix the problem...albeit with no transparency.

I don't know if that made any sense, but is there an easier solution you can think of?


 

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap draw order by y-axis
« Reply #3 on: February 19, 2013, 05:40:41 pm »
Yeah I think I get what you're trying to do... this is the one scenario where the easiest solution isn't particularly great (ie. a y partition size of 1). If you can get away with alpha testing, then that might be the easiest solution. If not, a proper catch-all solution won't be pretty at all.

andy

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Tilemap draw order by y-axis
« Reply #4 on: February 20, 2013, 07:43:40 am »
Using an alpha test shader fixed the transparency sorting problem. Thanks :)
Also can I add a request for an auto tile system, something similar to the one in TiDE or Rotorz, think it'll be a big selling point for the tilemap editor.

dsuse15

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Tilemap draw order by y-axis
« Reply #5 on: June 07, 2013, 10:01:06 pm »
Hmm...I'm actually having the same issue.  I have a top-down view and I'm having a difficult time getting the character's z-value to make sense within the map of tiles.  I think it's essentially the same issue andy was having.  I'm going to try to implement his solution in RenderMeshBuilder.BuildForChunk, so we'll see how that goes. 

I'm just wondering if you think we'll see any type of solution to this issue in a future release?  I feel like all top-down tile-based games will require this functionality at some point.  Would it make sense to have an option to make the tilemap's z values based on the tile's y value regardless of what chunk it's in, or is it more complicated than that?  Thanks for any help you can offer.  I'm really enjoying using 2D Toolkit.  It's dramatically improved my workflow   :D

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap draw order by y-axis
« Reply #6 on: June 08, 2013, 12:18:57 pm »
If its a top down view, how have you set it up? Do you have the tilemap object rotated so it's lying flat on the ground?
What about the prefabs / players / etc? How have you got that set up?


dsuse15

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Tilemap draw order by y-axis
« Reply #7 on: June 10, 2013, 05:11:30 am »
I've got the tilemap setup so that x is to the right and y is up.  I have the map setup as a few different layers to help with z-order in relation to other tiles (base layer, overlay layer, objects layer, and an "always on-top" layer).  As the player is moving up on the y axis, he should be moving behind tiles that are lower on the y-axis.  There is also the issue with bridges: they are over the player even when the player's y-value is less; unless the player is on the bridge.  The player is an animated (2-tile high) sprite with a collider on his feet, by the way.

I changed the tilemap code to allow a y-partition size of 1, just to see what I could get going.  I also changed the z-"increase" per row from .000001 to something a bit more drastic so I could see the change.  This gave me a bit more control, but obviously at a huge slowdown.  I'm looking for a really clean way to do this and I know I'm really going in the wrong direction.  I was thinking of adding a field into the tilemap settings inspector for "tile row z-change".  In the code, regardless of what partition a tile is in I'll base that tile's z-position on it's row's y-position and change it by that inspector z-value (taking layer z into account also).  I'm not sure if this will give the results I'm looking for, but it's just an idea.

I'm making a 2D SNES-like rpg and I'm trying to find an all-encompassing solution that will work in a variety of situations.  For the most part, everything is perfect.  There are just a few instances where the player needs to be behind something.  I know this problem has been solved in the past hundreds of times.  I'm just trying to find a clean solution in the context of 2D Toolkit.  I really appreciate you consistent help on these forums...This has been money well-spent  ;)
 

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap draw order by y-axis
« Reply #8 on: June 10, 2013, 11:28:49 am »
@dsuse15 - Can you use the alpha test shader on the tilemap? If so that might make things an order of magnitude easier to deal with. If you did that, then you need to tweak it in such a way that the separation between tiles is larger. tk2dTileMapMeshBuilder.BuildForChunk is the function you want.

Interesting note: The tilemap system actually did do this in the past, but it was removed because it caused lots of potential confusion in more simple use cases.

Basically - currentPos.z need to change based on the tile and the sorting order. The sprites are already being added back to front, so it just needs to increment z for each tile, or each row in your case.

After that all you need to do is move the sprites back and forth in z - since its alpha tested, you won't need to sort individual tiles, the zbuffer will sort that out for you.


prokopst

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Tilemap draw order by y-axis
« Reply #9 on: October 03, 2014, 09:04:40 pm »
Sorry for reviving quite old thread, but what does "Can you use the alpha test shader on the tilemap?" exactly mean? It might be a stupid question, but I know only shader basics and I did not find anything called "AlphaTestShader"...

I hit exactly the same issue with zelda-like game.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap draw order by y-axis
« Reply #10 on: October 04, 2014, 09:02:02 pm »
tk2d/CutoutVertexColor is one that uses alpha test.