- Home /
Two quads, same position, which one is seen ahead?
Hi all, lets say I have two quads at the same position with a sprite in them as texture. Is there any way you can identify which one will be seen on top of the other from an orthographic camera?
I don't know whether this question would be better in another thread but, what ways do people use for 2d games with objects getting on top of other? In starcraft 1 you could make units go on top of each other though they were doing it reluctantly lol. Any idea what they use there?
Q&A sites like UnityAnswers don't have "threads". We have questions and answers to that questions and for stuff that's not an answer we have comments. There is only one question which might have multiple answers. Here on the Qato design it's not that well seperated since the question looks almost like the answers below.
I've converted your answer into a comment.
Well, starcraft is actually a 2d game. Essentially in (fake) isometric 2D views you usually don't use a depth buffer (those has been invented mainly for 3D). In an isometric view you can simply say everything with a greater y coordinate is below things with lower y coordinate. So if you draw things from top to bottom they should appear correctly (using shaders without depth testing).
Such games usually have multiple passes, so static geometry is drawn first, so all units will appear on top of it.
Back the old days you also don't redraw the whole screen all the time. You usually just update the areas where something has changed. This was done because we don't had great graphics hardware ;)
If you create such a game in Unity you would simply create the game as a 2.5D game (so using 3D objects / sprites and view them at a 45° angle). Objects that are literally behind other objects will be displayed correctly when using the depth buffer. You would only use the x-z plane for the gameplay and keep most stuff at y == 0.
Of course if you like you can still doing everything in pure 2D, but that's usually much more work
Great information, you rock! So I think I am just going to write a script that arranges objects that intersect, changing their z position a little, putting them front or behind.
Are there any posts you can suggest about doing everything in pure 2D with unity?
Answer by Bunny83 · Nov 08, 2013 at 12:50 PM
No, because if they have the same position you will experience something called z-fighting. This happens because the GPU uses a depth-buffer in which it stores a depth value for each pixel. Due to floating point inaccuracy two objects that are at the same point (or almost at the same) will be "fighting" for each pixel. Some will belong to the one some to the other.
**edit*
If you use a shader that doesn't write to the depth buffer (like most transparent shaders) the order is given simply by the render order. The Render order might depend on the shader you use on each object and by the internal render order which can't be changed directly. Shaders can define a (Render)Queue tag value to partially control the order in which they are drawn.
If the two quads are part of the same mesh, it only depends on the order of the triangles within the Mesh itself. Again, keep in mind that's only true for shaders that don't use the depth buffer. All other shaders are based on the depth buffer.
...and for the last para, tri-order, Optimize may swap those around.
Thanks for the answer. Could you elaborate on the order of triangles and why it affects z-fighting?
Z-fighting only happens when the depth buffer is used. For each rendered fragment the GPU performs a depth test. If two primitives are at the same depth level it's kind of random who will win the fight.
The order of triangles are irrelevant when the depth buffer is used, only shaders that don't perform a depth test are affected by the render order. It's like painting, what you draw last will be on top.
However if the shader does use the depth buffer some pixels might belong to the thing drawn first and some to the thing drawn last.
$$anonymous$$eep in $$anonymous$$d there are shaders that do perform the depth test, but don't write to the depth buffer (most transparent shaders do this). That way the engine will render all opaque geometry which will be written to the depth buffer. When the transparent geometry is drawn it performs a depth test but the shader won't update the depth value. So transparent shaders can have z-fighting with opaque geometry, but not with other transparent geometry. In this case it depends solely on the render order.
That's why transparent geometry is sorted from far to near, however it's only sorted per object. Transparency sorting is a great problem on it's own which doesn't have a clear solution. So Unity only sorts per object so if triangles of the same mesh are overlapping, they are drawn in the order they appear in the triangle array.
See this thread on the forum for more information.