I would look to render certain objects in my world so that certain ones are always on top of others. This is so that I can solve some z-fighting issues (e.g. grass will always render on top of concrete, both of which are polygons at y=0) and so that some of my HUD-like icons that are rendered in 3D will always be on top of the game world.
I’m aware of GameObject.renderer.material.renderQueue, that I can set it in the shader with something like “Queue”=“Geometry+1” and that it can’t be changed at runtime.
What I don’t understand is its relationship with the shader’s ZWrite property.
From experimenting it seems that the use of ZWrite is all-or-nothing, meaning I have to set ZWrite Off for every single shader even if I only want to force one GameObject on top of the rest.
For example, lets say I have a world where every single object uses the default renderer queue value of 2000 (which is the “Geometry” value), but I have some HUD-like-icons that I want to always be on top. So I set the renderQueue of the HUD-like-icons to 2001 (Geometry+1) and turn the ZWrite to Off (or On, it doesn’t matter) but it won’t work at all until I go into the shaders being used by the world objects and set ZWrite to off in there as well.
Is this correct? It just seems a bit weird to me that I have to edit every shader to ZWrite Off just so that I can ensure one particular GameObject is on top of all the others. That being said, I’m a shader novice so keen to figure out the nuances of it.
Thanks.
(I’d actually posted this to answers.unity3d.com put it didn’t pass the moderators for being subjective or argumentative. That’s confusing to me because I’d have thought there is a right or wrong answer to the problem I’m trying to solve here)
There is another important parameter: “ZTest”. If you want an object to render always on top you have to set
“ZTest Always”.
This is most useful for HUD elements which should be rendered on top of any other game element. Additionally you want to set “Queue” = “Overlay”.
Then there is the possibility to use multiple cameras on top of each other. A typical use case for a second camera would be the player’s weapon in a first person shooter. It should be always rendered on top of other geometry but z-buffering might still be required.
I’ve played with ZTest but it feels like I’m fighting the render queue by using it. The HUD-like elements I mention aren’t really HUD items as such, they’re actually in 3D space, not in screen space. I’m trying to render things like path lines (like the paths a tank might follow), or coloured region overlays that cover the terrain but are under other game objects.
My confusion is that it seems strange to have to set ZWrite to Off for the ordinary world objects that are just using the default render geometry queue value of 2000. I would have thought that I’d just have to modify the queue order and ZWrite of the world items I’m trying to enforce above or below the default placement of other world objects.
I’m totally fine with it being the case that I have to do ZWrite Off for everything but I want to know that that is the actual solution to defining exact render queue/ordering.
It depends on what you want to do exactly. There is also another parameter “Offset” which is useful if you for example want to place decals like path markers on a terrain… Disabling the zbuffer as a whole by using ZWrite Off on every object probably isn’t your solution as you most likely will face other problems due to this. Ideally you wouldn’t have to mess with the render queue at all.
If you could provide a screenshot or mock-up of what you want to achieve exactly we might be able to help you better.
I have something that looks like the above picture.
I want to enforce the order so that it goes something like…
Terrain
flat polygons (they’re like water areas and parks)
3D buildings.
Those are essentially what make up the world
On top of that are the information items draw in 3D space such as
unit selection circles
arrows
then unit icons. The unit icons are actually billboards that always fact the camera.
The large 463m is a 3DText object.
I’ve tried using the Offset parameter to try and remedy the z-fighting between the terrain and flat polygons, but there are always artifacts to some degree and looks bad.