Unity HDRP – Render Object On Top Of Everything
When we decided to use Unity HDRP for our game The Strange Story Of Brian Fisher: Chapter 2, we needed to make a lot of changes and take different approaches for some things that we made in Chapter 1 (in the first chapter we are not using HDRP). One of these things was to have 3D objects in inventory and examine mode that we can rotate, and at the same time to avoid clipping with other objects in the scene.
In Chapter 1 we took the easiest way (not smartest, not with the best performance): we added an additional Camera in the scene that will render only objects in the Inventory and Examine mode. And this approach is a common approach when some First Person Shooter games want to solve the problem by clipping weapons with walls and other 3D objects in the scene.
But with HDRP that was not possible, and we needed to find a way how to solve this problem. We started to search on the net, and we end up in a dead zone – no proper way to solve this problem in HDRP. So, we decided to try to find a solution by ourselves – and we found it. Again, maybe this is not the best solution, but we found a workable way that at least for now meets our expectations.
Our solution was to use Custom Passes. We will try to explain the approach below with 3 steps:
- Step one – Add a proper layer to the objects that are in Inventory or Examine mode: We added new layers named “Inventory” and “Examine”. After that, every time when we pick the object in inventory, or we are examining the object, we are changing his layer (and layers for children recursively) to “Inventory”, or “Examine”, depending on which action we are performing, with code.
- Step two – Remove “Inventory” and “Examine” layers from the “Culling Mask” in Camera: Since Custom Pass will “extend” the rendering by the Camera, we want to be sure that only the Custom Pass that we will create below will render objects that are in “Inventory” and “Examine” layers. For that reason, we removed these two layers from the Camera rendering.
- Step three – Create needed Custom Passes: The last step that we performed was creating two Custom Passes in the empty prefab in the scene (the prefab doesn’t have any impact in the solution, you can put these Custom Passes anywhere. We just wanted to have an empty prefab where we will keep these Custom Passes). Both Custom Passes are “DrawRenderersCustomPass”.The first Custom Pass overrides Depth Test to “Always”. The whole configuration looks like this:The second Custom Pass overrides Depth Test to “Less Equals”. The whole configuration looks like this:Both Custom Passes in “Layer Mask” have ONLY “Inventory” and “Examine” layers.The first Custom Pass (with Depth Test to “Always”) will make sure that everything that has layer “Inventory” or “Examine” will be rendered on top of everything. But this custom pass will not care about Z-index and your object can have strange look (some parts of the objects that should be behind, they will be also rendered at the same level as the other parts of your object). For fixing that part, we are using the second Custom Pass (with Depth Test to “Less Equal”) that will be responsible for everything to take his order of rendering only for that object that is in “Inventory” or “Examine” layer (order of executions of Custom Passes is important).
Here is the full configuration of our Custom Passes for this purpose:
Also, depending on your needs, maybe you should adjust the other properties of the Custom Passes, but for our needs, this setting is meeting our expectations.
And EUREKAAA 🙂 – no more clipping, the object is rendered on top of everything, without using a second camera, in HDRP.
If you have any questions or have advice on how to improve this solution, please contact us at email@example.com.