Hello Guest

Author Topic: Dynamic Depth Layer?  (Read 10206 times)

zeteginara

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Dynamic Depth Layer?
« on: April 20, 2014, 12:21:32 am »
I'm making a top down RPG kind of a game.  In it, I have the NPC's and player's Z Coordinate calculated according to their Y coordinate, so that characters lower appear in front and characters behind appear behind, and this can change between frames.  Is there way to make the tiles in a layer behave in this manner using 2dtoolkit?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamic Depth Layer?
« Reply #1 on: April 20, 2014, 10:24:30 am »
The tile map tiles are offset in z - you CAN order stuff in there, but only if its set up using a solid or alpha tested (cutout) shader. There is no way to sort if using a normal blended shader without making the whole thing very very inefficient.

zeteginara

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Dynamic Depth Layer?
« Reply #2 on: April 20, 2014, 10:07:16 pm »
Hmm, I don't know much about shaders or how they operate.  I'm making a low res game with point filtering for graphics so that the graphics appear blocky, and don't really need to be affected by lights or shadows.  Do I don't have to worry much about blending or anything.  I set the atlas material on Chunk 0 0 of the layer in question to "cutout", and didn't see any adverse effects.  What shader would you recommend?

Also, what would the best way to make this calculation?  I can't see a concieveable way to access the sprite map tiles.  I can't find the Y coordinates of the tiles, so I can't perform the calculation based off where they are on the screen.
« Last Edit: April 20, 2014, 10:22:49 pm by zeteginara »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamic Depth Layer?
« Reply #3 on: April 22, 2014, 02:54:00 pm »
I would recommend the tk2d/CutoutVertexCoor shader as it works with vertex colours, but any cutout shader will work. You should change this on the sprite collection material.

Next thing to do is to add some z offset to each tile -
tk2dTileMapBuilder.cs, line 56
               Vector3 currentPos = new Vector3(tileSize.x * (x + xOffset), tileSize.y * y, 0);

the z offset is currently 0, but you can set that to (y * (x1 - x0) + x) * 0.01f
That will space each tile by 0.01 unit, and you can position your sprites between these tile values to get them to sort properly. This requires some fiddling to work properly, but once it works should give you good results.

Let me know if this works - you will still need to do a couple more things for this to work properly across the board

zeteginara

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Dynamic Depth Layer?
« Reply #4 on: April 26, 2014, 01:18:01 am »
So, I tried a couple methods.  The way I calculate my character zCoordinate is like this:

z = (position.y * .001f)

So, that works with my characters.  They appear above/below each other depending on where they are at on the screen.
So, with that working , I went with your first suggestion, using this formula to alter the position of the Z coordinate based on the position of each tile, and then covered my map with tiles on the layer I want to perform this logic.

(y * (x1 - x0) + x) * 0.001f)

So, this produces weird results.  Most tiles my characters are visible over.  However, as I'm moving up and down, there is a group of 4 tiles, every 10 tiles or so, that my characters will disappear behind.

So, I tried setting calculating the z Coordinate of the tiles based on how the y Coordinate is calculated, kinda like I do with my characters:

(tileSize.y * y) * .001f)

This works perfectly, on the bottom chunk of my tilemap only.  On "chunk 0 1"- "chunk 0 5", it works great, but once I get to "chunk 1 0" and above, my characters completely disappear under the tiles.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamic Depth Layer?
« Reply #5 on: April 26, 2014, 09:29:51 am »
Ok now that you've got that working - you will need to consider the position of the chunks. They will need to be offset in z so they are in the correct position.

tk2dTileMapBUilderUtil.cs,

Code: [Select]
if (chunk.gameObject != null)
{
Vector3 tilePosition = GetTilePosition(tileMap, x * tileMap.partitionSizeX, y * tileMap.partitionSizeY);
tilePosition.z += z;

z is hardcoded to 0. It should be whatever value based on your offset x number of tiles.

zeteginara

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Dynamic Depth Layer?
« Reply #6 on: April 30, 2014, 04:28:59 am »
Changed the line to:

tilePosition.z = (tilePosition.y * 0.001f);

And that works for all the chunks.  Thanks!

I think this should work out of the box, a "dynamic depth" layer.  There were a few old SNES games that did something like this, and it was really neat.  I mean, it wasn't easy to notice if you didn't look for it but it was there.

EDIT:  There does seem to be an occasional 'magic' row of tiles that my characters always appear in front of, and the point in which the tiles disappear behind my characters seems to be slightly different each row.  It's alright, I can workaround it, but if you have any ideas, let me know.
« Last Edit: April 30, 2014, 04:33:25 am by zeteginara »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamic Depth Layer?
« Reply #7 on: April 30, 2014, 05:15:26 pm »
If theres any crazy things that happen try adding a bit more separation. floating point numbers are "fun" to work with reliably :)

zeteginara

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Dynamic Depth Layer?
« Reply #8 on: April 30, 2014, 05:49:49 pm »
Yeah, I made my standard multiplier a bit higher number and that seemed to fix it.  :)

zeteginara

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Dynamic Depth Layer?
« Reply #9 on: August 15, 2014, 03:49:24 pm »
I honestly have no idea how shaders work or where to begin creating one.  I believe in the thread about the dynamic depth layer you made mention of using a "solid" shader or a "cutout" shader.  Would a "Solid" shader work?

I've been reading this: http://docs.unity3d.com/Manual/ShadersOverview.html

And it makes no mention of 'cutout' shaders.

My needs aren't high, I'm making a 'retro-style' 16 bit style game with point filtering so everything looks blocky.  Any recommendations?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamic Depth Layer?
« Reply #10 on: August 16, 2014, 12:12:37 pm »
This is unsupported, but heres a cutout shader with a color parameter....
Code: [Select]
// unlit, vertex colour, alpha blended
// cull off

Shader "tk2d/CutoutVertexColor"
{
Properties
{
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_Color ("Main Color", Color) = (1,1,1,1)
}

SubShader
{
Tags {"IgnoreProjector"="True" "RenderType"="TransparentCutout"}
Lighting Off Cull Off Fog { Mode Off } AlphaTest Greater 0
LOD 110

Pass
{
CGPROGRAM
#pragma vertex vert_vct
#pragma fragment frag_mult
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Color;

struct vin_vct
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};

struct v2f_vct
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
};

v2f_vct vert_vct(vin_vct v)
{
v2f_vct o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
o.texcoord = v.texcoord;
return o;
}

fixed4 frag_mult(v2f_vct i) : COLOR
{
fixed4 col = tex2D(_MainTex, i.texcoord) * i.color * _Color;
return col;
}

ENDCG
}
}
}


Delete  * i.color  if you dont want the vertex color to affect it at all. Experiment with this to see what you can get.

teomaragakis

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Dynamic Depth Layer?
« Reply #11 on: September 02, 2014, 06:37:04 pm »
I've had the same issue, I'll test the answer and see if it works for me. You really should consider implementing a top-down tilemap mode to make things easier.

teomaragakis

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Dynamic Depth Layer?
« Reply #12 on: September 02, 2014, 08:46:47 pm »
Next thing to do is to add some z offset to each tile -
tk2dTileMapBuilder.cs, line 56
               Vector3 currentPos = new Vector3(tileSize.x * (x + xOffset), tileSize.y * y, 0);

the z offset is currently 0, but you can set that to (y * (x1 - x0) + x) * 0.01f
That will space each tile by 0.01 unit, and you can position your sprites between these tile values to get them to sort properly. This requires some fiddling to work properly, but once it works should give you good results.

If I'm not mistaken, This doesn't work any more.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamic Depth Layer?
« Reply #13 on: September 03, 2014, 12:22:35 pm »
Quote
If I'm not mistaken, This doesn't work any more.
Does it visibly add the offset to the tiles?

Quote
You really should consider implementing a top-down tilemap mode to make things easier.
The issue isn't top down mode, its the overlapping tiles that need a fixed sort order with your sprites. I'd rather not have that as a official feature than have a really complicated implementation - eg. if you must use a particular shader, cutout in this case, and position your player sprites at fixed positions.... If your world isn't too big it might be considerably easier to just create one prefab per tile and use unity sorting order based on X / Y. Its orders of magnitude more inefficient, but it will give you results really quickly and doesn't have any restrictions.