How to always render one object on top?

While using URP, I have 1 object that I want to always render on top of everything else. I’m using the default URP lit shader. Is there an option I can pass to the shader to set ztest to always?

1 Like

Although you set material/shader’s ZTest to Always, it just only on the top of all objects whose ZTest is not set to Always or Great etc.

First of all, UIs are excluded.

Then, there are some choices:

  1. Set material to a large render queue, maybe Overlay which value is 4000, and set ZTest to Always. Now, if you did not add any render feature, this material should be on the top of all scene objects.
    All RenderQueues you can choose:
public enum RenderQueue
{
    Background = 1000,
    Geometry = 2000,
    AlphaTest = 2450, // we want it to be in the end of geometry queue
    GeometryLast = 2500, // last queue that is considered "opaque" by Unity
    Transparent = 3000,
    Overlay = 4000,
}
  1. (More recommend) Add a render feature, with a event large enough, like Before RenderingPostProcessing (which means do rendering just before post-processing), and override its Depth option with Write Depth unchecked and Depth Test is Always.

Finally it will like
7453109--914489--upload_2021-8-27_17-56-41.png
(Don’t forget to select your appropriate Layer Mask)

All Events you can choose:

public enum RenderPassEvent
{
    BeforeRendering = 0,
    BeforeRenderingShadows = 50,
    AfterRenderingShadows = 100,
    BeforeRenderingPrepasses = 150,
    AfterRenderingPrePasses = 200,
    BeforeRenderingOpaques = 250,
    AfterRenderingOpaques = 300,
    BeforeRenderingSkybox = 350,
    AfterRenderingSkybox = 400,
    BeforeRenderingTransparents = 450,
    AfterRenderingTransparents = 500,
    BeforeRenderingPostProcessing = 550,
    AfterRenderingPostProcessing = 600,
    AfterRendering = 1000,
}
5 Likes

Setting the render queue of the material to 4000 via script seemed to work perfect for me. Thanks!

1 Like