/HDRP-UI-Camera-Stacking

Optimized implementation of camera stacking for UI only in HDRP.

Primary LanguageShaderLabMIT LicenseMIT

HDRP UI Camera Stacking

The HDRP UI Camera Stacking package allows you to stack multiple camera rendering UI only at a fraction of the cost of a standard camera.

This is achieved by taking advantage of the customRender feature to render only the GUI elements and nothing else, with the only downside to not be able to render Lit objects which are generally not found for UI.

UI.mp4

This implementation also provides all the benefits of standard camera stacking:

  • No post process bleeding on UI (motion vectors, ect.)
  • No clipping in the geometry

Installation

The UI Camera Stacking package supports the following versions:

Unity Version HDRP Version Compatible
2019.4.x 7.x
2020.1.x 8.x
2020.2.x and 2020.3.x 10.x ✔️
2021.1.x 11.x ✔️
2021.2.x 12.x ✔️
2022.1.x 13.x ✔️
2022.2.x 14.x ✔️
2022.3.x 14.x ✔️
2023.1.x 15.x ✔️
2023.2.x 16.x ✔️
6000.0.x 17.x ✔️
Instructions

HDRP UI Camera stacking is available on the OpenUPM package registry, to install it in your project, follow the instructions below.

  1. Open the Project Settings and go to the Package Manager tab.
  2. In the Scoped Registry section, click on the small + icon to add a new scoped registry and fill the following information:
Name:     Open UPM
URL:      https://package.openupm.com
Scope(s): com.alelievr
  1. Next, open the Package Manager window, select My Registries in the top left corner and you should be able to see the HDRP UI Camera Stacking package.
  2. Click the Install button and you can start using the package :)

PackageManager

⚠️ If you don't see My Registries in the dropdown for some reason, click on the + icon in the top left corner of the package manager window and select Add package from Git URL, then paste com.alelievr.hdrp-ui-camera-stacking and click Add.

Note that sometimes, the package manager can be slow to update the list of available packages. In that case, you can force it by clicking the circular arrow button at the bottom of the package list.

Tutorial

First, create a UI Camera gameobject with the menu UI > Camera (HDRP)

image

In this new UI Camera there is a component called HD Camera UI, this is the component that will do the rendering of the UI. It's important to correctly configure this component because the camera depth and culling mask are ignored when this component is added.

In the UI Camera gameobject a canvas was also created and correctly configured with the "Screen Space - Camera" mode. You can add your UI in this canvas.

image

Property Description
Ui Layer Mask Layer mask of your UI objects, by default it's set to UI.
Priority Used to define a draw order between the UI cameras, a high priority means rendered in front of the other cameras.
Compositing Mode Select how you want the compositing to happen. By default the Automatic will blend the UI render texture with the camera color after the camera rendering. The Custom mode allows you to replace the compositing material by your own fullscreen shader. The Manual mode disables the compositing and you have to do your own one with the renderTexture field in HDCameraUI.
Target Camera Specifies which camera will receive the UI. By default the Main value only adds the UI to the main camera. The All value outputs the UI to every camera in the scene. The Layer value allows you to filter cameras by layer. The Specific mode allows you to only output the UI to a specific camera.
Graphics Format Format of the color buffer used to render the UI, the default is 16 bit RGBA to avoid banding and keep the alpha channel.
Render In Camera Buffer If true, the UI will also be rendered in the attached camera color buffer.

Performances

In HDRP using more than one camera have a very high performance cost. While you can avoid most of the performance issue on the GPU side with correct culling settings and disabling almost every in the frame settings, you won't be able to escape the CPU cost.

The scenes used for the performance test are available in the Benchmark folder of the project. For HDRP, the Graphics Compositor was used to perform the UI camera stacking. The UI camera had custom frame settings optimized to render GUI (transparent unlit objects).

All performance metrics were captured at a resolution of 1920x1080 on Windows 10 in the Unity Editor.

Setup CPU Time (ms) GPU Time (ms)
HDRP camera stacking ~1.35 0.45
Custom UI camera stacking ~0.10 0.18

Without much surprise, we can see a big difference on CPU side, mostly because we're skipping all the work of a standard HDRP camera. On the GPU side things are pretty even though the camera is still a bit slower because of the overhead of compute shader and fullscreen passes that can't be disabled in the frame settings.

System Information
  • System: Windows 10 Pro, DirectX 11
  • CPU: i9-10980k 3.00GHz 18 cores
  • GPU: RTX 3090

Limitations

Rendering Lit objects is not supported. Currently the UI rendering happen before the rendering of the main camera, thus before any lighting structure is built so it's not possible to access the lighting data when rendering the UI for camera stacking.

Future Improvements

  • Fullscreen effect applied after rendering the UI
  • Add an injection point before post processes
  • Support post processing volumes for UI Cameras.