How to render one line on top of another without flickering?

Avatar
  • updated
  • Answered

This is probably a really simple question, but I'm trying to understand what's the best way to render one line on top of another (e.g. for a health bar) that are on the same z-plane (in a 3D game).  I have two GameObjects, one called Gauge, which should be rendered on top of GaugeBackground.

I would have expected that by setting the depth offset factor to -1 for Gauge, it would always render on top of the GaugeBackground.  However, instead, it flickers.  This is true even if I set the depth offset factor to 0.  I have tried setting the Depth Test to Always, and that does make Gauge render on top of GaugeBackground without flickering, but this seems a bit hacky?  I'm trying to understand what's the right way to do this.  See below for how Gauge and GaugeBackground are set up.

Reporting a bug? please specify Unity version:
Reporting a bug? please specify Shapes version:
Reporting a bug? please specify Render Pipeline:
URP
Pinned replies
Avatar
Freya Holmér
  • Answer
  • Answered

if you're using an orthographic camera, setting depth units to -1 for the one on top should work.
if you're using a perspective camera you should set depth factor to -1 to handle different perspectives. It seems to work on my end, so I'm not sure what could be wrong on yours I'm afraid!

Avatar
Freya Holmér
  • Answer
  • Answered

if you're using an orthographic camera, setting depth units to -1 for the one on top should work.
if you're using a perspective camera you should set depth factor to -1 to handle different perspectives. It seems to work on my end, so I'm not sure what could be wrong on yours I'm afraid!

Avatar
airoll

I have a perspective camera but am running into this flickering when I use the depth factor = -1.  Any idea what might be causing this?

Video of Flickering: https://kapwi.ng/c/GAkZ-k2a3O

These are my camera settings.  I am using URP.

Avatar
Freya Holmér

try setting depth factor even closer than -1, see if -2 or -4 or -8 helps

you can also tweak depth offset, set them to at least -1 or -2, along with the other

the values needed for these depend on how the depth buffer is handled, and the specific ways the camera deals with it

Avatar
airoll

Thanks, it turned out for whatever reason setting Depth Offset Units = -3 worked, changing Depth Offset Factor didn't appear to do anything.  Strange.  In any case, if multiple objects share the same Depth Offset Unit, are they instanced at all?

Avatar
Freya Holmér

in immediate mode yes, assuming they are consecutive draws, but not if they are component shapes