Render object in front of everything, including transparant objects

I am making a worldspace UI (it needs to be worldspace because it moves slightly delayed and in different directions in 3D based on the VR headset). In this case, the UI covers the entire visible screen. In the future, I also want smaller black backgrounds in a further bright environment. I am using the Built-in render pipeline.

The current UI works as worldspace overlay using a script to set ZTest mode to Always for my UI materials.
materialCopy.SetInt(“unity_GUIZTestMode”, (int) UnityEngine.Rendering.CompareFunction.Always);

This works for UI rendered on top of most materials, but in the places it is rendered on top of materials with transparancy, it shows them bright as ever. They don’t write in depth I heard, so that explains it. Changing the transparent materials RenderQueue numbers, or the one of the UI does not fix this either.

What can I do to solve this, and make my UI hide all objects including transparant ones (plants, glass, water)?

It really should, since RenderQueue determines the order in which geometry is drawn. Simply setting your UI material’s render queue to Overlay should do it.

Transparent materials don’t write to the depth buffer, but that only means that stuff rendered after them will always appear on top of them. If you render UI after transparent objects, it should appear on top of transparents no matter what.

However merely setting the UI’s z test to “always” doesn’t guarantee this: sure, the depth buffer’s contents at the time of drawing the UI will be ignored and all fragments will pass the test, but if your UI is drawn before any of the transparent objects they will still appear on top (unless your UI is opaque and writes to the depth buffer)

Thanks so much for your answer. I was on holiday so only got back to it now.
My black image color overlay UI uses the default material, and I turn up and down the transparancy to fade it in/out, so it is in the transparant render queue as well (it does not even give me the Overlay render queue option clicking Transparant).
9620582--1365764--upload_2024-2-2_18-49-46.png
So I tried changing the queue value to 2999 and 3001. I checked the other objects, which are all rendered at 3000 in the Transparant render queue. But no matter these settings, it does not render in front of the other objects.

Do you have any idea why that is?

That doesn’t make much sense. Have you made sure the material is actually used by these UI objects? (it’s easy to create a material, change the settings, then forget to apply the material to the UI and they will still use Unity’s internal material with the default UI shader). Just tried it myself on an empty project, and changing the queue value did affect the order in which UI and transparent objects were drawn.

Assuming render queues are not working for you for some strange reason, the only remaining option is that the transform position of your UI is actually behind the pivot point of the leaves’ geometry. Unity will sort all transparent meshes in the same render queue by distance to camera when determining render order (painter’s algorithm: render stuff from further away to closer).

It calculates the distance not for every single vertex in a mesh, but for the transform position. So if your UI’s transform is further away from the camera than the other transparent object’s transform, Unity will render UI first, then the other object on top.