|
1 | 1 | # Set up a camera stack
|
2 | 2 |
|
3 |
| -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. |
| 3 | +This page describes how to use a camera stack to layer outputs from multiple cameras to the same render target. For more information on camera stacking, refer to [Understand camera stacking](cameras/camera-stacking-concepts.md). |
4 | 4 |
|
5 |
| - |
| 5 | +<br/>*An example of a scene that uses camera stacking to render a red capsule with a post-processing effect, and a blue capsule with no post-processing.* |
6 | 6 |
|
7 |
| -A Camera Stack consists of a [Base Camera](camera-types-and-render-type.md#base-camera) and one or more [Overlay Cameras](camera-types-and-render-type.md#overlay-camera). 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. |
| 7 | +Follow these steps to set up a camera stack: |
8 | 8 |
|
9 |
| - 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, refer to [Advanced information](cameras-advanced.md). |
| 9 | +1. [Create a camera stack](#create-a-camera-stack). |
| 10 | +2. [Set up layers and culling masks](#set-up-layers-and-culling-masks). |
10 | 11 |
|
11 |
| -For examples of how to use Camera Stacking, refer to the [Camera Stacking samples in URP Package Samples](package-sample-urp-package-samples.md#camera-stacking). |
| 12 | +## Create a camera stack |
12 | 13 |
|
13 |
| -<a name="adding-a-camera-to-a-camera-stack"></a> |
| 14 | +Create a camera stack with a Base Camera and one or more Overlay Cameras. |
14 | 15 |
|
15 |
| -## Adding a Camera to a Camera Stack |
| 16 | +For more information on how to do this, refer to [Add a camera to a camera stack](cameras/add-and-remove-cameras-in-a-stack.md#add-a-camera-to-a-camera-stack). |
16 | 17 |
|
17 |
| - |
| 18 | +## Set up layers and culling masks |
18 | 19 |
|
19 |
| -1. Create a Camera in your scene. Its **Render Type** defaults to **Base**, making it a Base Camera. |
20 |
| -2. Create another Camera in your scene, and select it. |
21 |
| -3. In the Camera Inspector, change the Camera’s **Render Type** to **Overlay**. |
22 |
| -4. 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. |
| 20 | +Once you create your camera stack, you must assign any GameObjects the Overlay Cameras need to render to a [layer](xref:Layers), then set the **Culling Mask** of each camera to match the layer. |
23 | 21 |
|
24 |
| -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. |
| 22 | +To do this use the following steps: |
25 | 23 |
|
26 |
| -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](xref:UnityEngine.Rendering.Universal.UniversalAdditionalCameraData) component, like this: |
| 24 | +1. Add as many layers as your project requires. For information on how to do this, refer to [Add a new layer](xref:create-layers). |
| 25 | +2. For each GameObject you want an Overlay Camera to render, assign the GameObject to the appropriate layer. |
| 26 | +3. Select the Base Camera of your camera stack and navigate to **Rendering** > **Culling Mask** in the Inspector Window. |
| 27 | +4. Remove any layers you don't want the Base Camera to render, such as layers that contain objects only an Overlay Camera should render. |
| 28 | +5. Select the first Overlay Camera in the camera stack and navigate to **Rendering** > **Culling Mask** in the Inspector window. |
| 29 | +6. Remove all layers except for the layers that contain GameObjects you want this camera to render. |
| 30 | +7. Repeat Step 5 and Step 6 for each Overlay Camera in the camera stack. |
27 | 31 |
|
28 |
| -```c# |
29 |
| -var cameraData = camera.GetUniversalAdditionalCameraData(); |
30 |
| -cameraData.cameraStack.Add(myOverlayCamera); |
31 |
| -``` |
32 |
| - |
33 |
| -## Removing a Camera from a Camera Stack |
34 |
| - |
35 |
| - |
36 |
| - |
37 |
| -1. Create a Camera Stack that contains at least one Overlay Camera. For instructions, refer to [Adding a Camera to a Camera Stack](#adding-a-camera-to-a-camera-stack). |
38 |
| -2. Select the Camera Stack's Base Camera. |
39 |
| -3. 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. |
40 |
| - |
41 |
| -The Overlay Camera remains in the scene, but is no longer part of the Camera Stack. |
42 |
| - |
43 |
| -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](xref:UnityEngine.Rendering.Universal.UniversalAdditionalCameraData) component, like this: |
44 |
| - |
45 |
| -```c# |
46 |
| -var cameraData = camera.GetUniversalAdditionalCameraData(); |
47 |
| -cameraData.cameraStack.Remove(myOverlayCamera); |
48 |
| -``` |
49 |
| - |
50 |
| -## Changing the order of Cameras in a Camera Stack |
51 |
| - |
52 |
| - |
53 |
| - |
54 |
| -1. Create a Camera Stack that contains more than one Overlay Camera. For instructions, refer to [Adding a Camera to a Camera Stack](#adding-a-camera-to-a-camera-stack). |
55 |
| -2. Select the Base Camera in the Camera Stack. |
56 |
| -3. In the Camera Inspector, scroll to the Stack section. |
57 |
| -4. Use the handles next to the names of the Overlay Cameras to reorder the list of Overlay Cameras. |
58 |
| - |
59 |
| -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. |
60 |
| - |
61 |
| -You can reorder a Camera Stack in a script by directly manipulating the `cameraStack` property of the Base Camera's [Universal Additional Camera Data](xref:UnityEngine.Rendering.Universal.UniversalAdditionalCameraData) component. |
62 |
| - |
63 |
| -## Adding the same Overlay Camera to multiple stacks |
64 |
| - |
65 |
| -To add an Overlay Camera to multiple Camera Stacks: |
66 |
| - |
67 |
| -1. Create a Camera Stack that contains at least one Overlay Camera. For instructions, refer to [Adding a Camera to a Camera Stack](#adding-a-camera-to-a-camera-stack). |
68 |
| -2. Create a Camera in your scene. Its **Render Type** defaults to **Base**, making it a Base Camera. |
69 |
| -3. Select the new Base Camera. |
70 |
| -4. 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. |
71 |
| - |
72 |
| -The Overlay Camera is now rendering in both Camera Stacks. |
73 |
| - |
74 |
| -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](xref:UnityEngine.Rendering.Universal.UniversalAdditionalCameraData) component, like this: |
75 |
| - |
76 |
| -```c# |
77 |
| -var cameraData = camera.GetUniversalAdditionalCameraData(); |
78 |
| -cameraData.cameraStack.Add(myOverlayCamera); |
79 |
| -``` |
| 32 | +> [!NOTE] |
| 33 | +> You don't need to configure the **Culling Mask** property of the cameras. However, cameras in URP render all layers by default, so rendering is faster if you remove layers that contain unneeded GameObjects. |
0 commit comments