Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wrote the following shader which purpose is to force draw a mesh over the rest, regardless z-depth :

Shader "Custom/AlwaysOnTop"
{
    SubShader 
    {
        Tags { "Queue"="Overlay" "RenderType"="Overlay" }
        ZTest Always

        CGPROGRAM
        #pragma surface surf Lambert

        struct Input 
        {
            float4 color : COLOR;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            o.Albedo = 1;
        }
        ENDCG
    }
    Fallback "Diffuse"
}

It works great, however transparent objects still get draw over it. Setting the queue as Overlay should have solved the problem.

enter image description here

  • White = AlwaysOnTop shader
  • Green = Transparent diffuse
  • Blue = Diffuse

The left face of the white cube should be white, no tinted in green.

share|improve this question
    
Which version of Unity are you using? – Stephane Hockenhull Aug 20 '16 at 18:41
    
I use Unity 5.3.1 – tigrou Aug 20 '16 at 19:08
    
I pulled this shader into the version of Unity I have installed (5.4) and it works just fine. – Draco18s Sep 20 '16 at 4:05
    
There was a bug in a recent version of Unity where switching to a custom shader would sometimes override which queue the material was using. This answer describes the issue, and how to use the debug inspector to check for it and fix it, if that's the cause of the problem you're seeing. – DMGregory Sep 20 '16 at 12:18

If you want something overlaying everything the easiest way is to create a second camera with identical settings and position to the first except for:

  • A higher depth. (it's the camera render order, to render after the first camera.)
  • Clear flags set to depth only, this will render everything on top.
  • Culling mask set only to the object layer you want to render.
  • Remove the same layer from the 1st camera's culling mask.
  • Remove the audio listeners, UI rendering, and other extra components from the 2nd camera.

see: https://docs.unity3d.com/Manual/Layers.html

Easiest way to move them together is to make the 2nd camera a child of the 1st. If you change the camera's FOV or other effects you'll need to add a small script to copy the values over.

This is a method that works with Unity 4 as well (and more likely to work with future versions too).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.