Camera Stacking
In the Universal Render Pipeline (URP), you use Camera Stacking to layer the output of multiple Cameras and create a single combined output. Camera Stacking allows you to create effects such as a 3D model in a 2D UI, or the cockpit of a vehicle.
A Camera Stack consists of a Base Camera and one or more Overlay Cameras. A Camera Stack overrides the output of the Base Camera with the combined output of all the Cameras in the Camera Stack. As such, anything that you can do with the output of a Base Camera, you can do with the output of a Camera Stack. For example, you can render a Camera Stack to a given render target, apply post-process effects, and so on.
URP performs several optimizations within a Camera, including rendering order optimizations to reduce overdraw. However, when you use a Camera Stack, you effectively define the order in which those Cameras are rendered. You must therefore be careful not to order the Cameras in a way that causes excessive overdraw. For more information on overdraw in URP, see Advanced information.
Adding a Camera to a Camera Stack
- Create a Camera in your Scene. Its Render Type defaults to Base, making it a Base Camera.
- Create another Camera in your Scene, and select it.
- In the Camera Inspector, change the Camera’s Render Type to Overlay.
- Select the Base Camera again. In the Camera Inspector, scroll to the Stack section, click the plus (+) button, and click the name of the Overlay Camera.
The Overlay Camera is now part of the Base Camera's Camera Stack. Unity renders the Overlay Camera's output on top of the Base Camera's output.
You can add a Camera to a Camera Stack in a script by directly manipulating the cameraStack
property of the Base Camera's Universal Additional Camera Data component, like this:
var cameraData = camera.GetUniversalAdditionalCameraData();
cameraData.cameraStack.Add(myOverlayCamera);
Removing a Camera from a Camera Stack
- Create a Camera Stack that contains at least one Overlay Camera. For instructions, see Adding a Camera to a Camera Stack.
- Select the Camera Stack's Base Camera.
- In the Camera Inspector, scroll to the Stack section, click the name of the Overlay Camera you want to remove, and then click the minus (-) button.
The Overlay Camera remains in the Scene, but is no longer part of the Camera Stack.
You can remove a Camera from a Camera Stack in a script by directly manipulating the cameraStack
property of the Base Camera's Universal Additional Camera Data component, like this:
var cameraData = camera.GetUniversalAdditionalCameraData();
cameraData.cameraStack.Remove(myOverlayCamera);
Changing the order of Cameras in a Camera Stack
- Create a Camera Stack that contains more than one Overlay Camera. For instructions, see Adding a Camera to a Camera Stack.
- Select the Base Camera in the Camera Stack.
- In the Camera Inspector, scroll to the Stack section.
- Use the handles next to the names of the Overlay Cameras to reorder the list of Overlay Cameras.
The Base Camera renders the base layer of the Camera Stack, and the Overlay Cameras in the stack render on top of this in the order that they are listed, from top to bottom.
You can reorder a Camera Stack in a script by directly manipulating the cameraStack
property of the Base Camera's Universal Additional Camera Data component.
Adding the same Overlay Camera to multiple stacks
To add an Overlay Camera to multiple Camera Stacks:
- Create a Camera Stack that contains at least one Overlay Camera. For instructions, see Adding a Camera to a Camera Stack.
- Create a Camera in your Scene. Its Render Type defaults to Base, making it a Base Camera.
- Select the new Base Camera.
- In the Camera Inspector, scroll to the Stack section, click the plus (+) button, and click the name of the Overlay Camera that you want to use in both Camera Stacks.
The Overlay Camera is now rendering in both Camera Stacks.
You can also add a Camera to a Camera Stack in a script by directly manipulating the cameraStack
property of the Base Camera's Universal Additional Camera Data component, like this:
var cameraData = camera.GetUniversalAdditionalCameraData();
cameraData.cameraStack.Add(myOverlayCamera);