[URP] Fix Depth Clipping via Camera Stacking
Solution
Unity 2019.3.x - Unity 6.3.x
Published 1 days ago
Objects intended to appear in front of others are incorrectly rendered behind them, leading to visual clipping and depth inaccuracies. This is particularly noticeable with large-scale or distant objects where standard depth testing fails to maintain hierarchy. Traditional Camera priorities or secondary setups often fail to resolve these artifacts within the Universal Render Pipeline, especially when dealing with a complex Camera Stack.
A robust method for controlling object render order involves implementing a Camera Stack within the Universal Render Pipeline. This technique allows for explicit control over which objects are drawn and in what sequence, overcoming issues like depth testing interference. Distinct rendering cameras are assigned to different logical groups of objects based on the desired visual hierarchy.
- Create a
Camerato act as the base layer for the Camera Stack. - Create additional
Cameracomponents for each layer group, such as your ship script or distant planets. - Set the
Render Typeof the baseCameratoBasevia theInspector. - Set the
Render Typeof all additional cameras toOverlay. - In the
BaseCameracomponent, locate the Stack section and add theOverlaycameras in the order they should be rendered. - Configure the
Culling Maskon eachCamerato isolate specific layers. - Ensure the final
OverlayCamerain the Camera Stack is configured to clear depth to ignore previous depth values if necessary.
Additional Tips
- The order in the Camera Stack list determines the draw order; the bottom-most item in the list is rendered last and appears on top.
- For performance optimization, minimize the number of cameras in a Camera Stack to reduce the total number of draw calls.
- Use
Overlaycameras for cockpit or UI elements to prevent them from clipping into world geometry.
TL;DR
Implement a Camera Stack in the Universal Render Pipeline. Assign distinct Camera components to specific layers and order them in the Stack to ensure foreground elements render last, typically with the final Overlay camera using Clear Flags: Depth Only.
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
Content inspired by a Unity discussion post.