How to use multiple cameras in URP?

Hello, I am the great wisdom of the metaverse, let you learn Unity a few steps faster.
This article was first published on my official account: Hongliu School

In the Universal Rendering Pipeline (URP), the camera works in the following ways:

  • Stack Cameras can combine the outputs of multiple cameras into a single output. With the camera stack, you can add 3D models to a 2D UI, or create the effect of a vehicle cockpit.
  • Render multiple base cameras or camera stacks to the same render target . This allows you to create effects such as split screen rendering.
  • Renders the base camera or camera stack to a RenderTexture . By rendering to a RenderTexture, effects such as CCTV monitors can be created.

Camera stack in URP

You can combine these working methods to achieve more complex effects. For example, you can define two camera stacks, and then set each camera stack to render to a different area of ​​the same render target.

camera stack

A camera stack consists of a base camera and one or more overlay cameras. A camera stack superimposes the output of the base camera with the combined output of all cameras in the stack. This way, you can do whatever processing you want with the output of the base camera, or with the output of the camera stack. For example, you can render the camera stack to a given render target, apply post-processing effects, and more.

URP performs multiple optimizations in Camera, including rendering order optimization to reduce overdraw. However, when using a camera stack, you are manually defining the order in which those cameras are rendered. Therefore, you have to be careful to sequence the cameras so as not to cause excessive overdraw. For information about overdraw in URP, a section will be devoted to it later.

Add camera to camera stack

Add camera to camera stack

  1. Create a camera in the scene. Its Render Type defaults to Base , making it a base camera.
  2. Create another camera in the scene and select it.
  3. In the Camera Inspector, change the camera's RenderType to Overlay .
  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 stack camera.

Overlay cameras are now part of the base camera's camera stack. Unity renders the output of the overlay camera on top of the output of the base camera.

You can add cameras from scripts to the camera stack by directly manipulating the properties of the cameraStackunderlying camera's component, like so:UniversalAdditionalCameraData

var cameraData = camera.GetUniversalAdditionalCameraData();
cameraData.cameraStack.Add(myOverlayCamera);

remove camera from camera stack

remove camera from camera stack

  1. Create a camera stack containing at least one superimposed camera.
  2. Select the base camera for the camera stack.
  3. In the Camera Inspector, scroll to the Stack section, click the name of the stacked camera you want to delete, and click the **minus (-)** button.

Overlay cameras remain in the scene, but are no longer part of the camera stack.

You can remove a camera from the camera stack in script by directly manipulating the properties of cameraStackthe underlying camera's component, like so:UniversalAdditionalCameraData

var cameraData = camera.GetUniversalAdditionalCameraData();
cameraData.cameraStack.Remove(myOverlayCamera);

Change the order of cameras in the camera stack

remove camera from camera stack

  1. Creates a camera stack containing multiple superimposed cameras.
  2. Select the base camera in the camera stack.
  3. In the Camera Inspector, scroll to the "Stacks" section.
  4. Use the handle next to the overlay camera name to rearrange the overlay camera list.

The base camera renders the base layer of the camera stack, and the overlay cameras in the stack are rendered on top of it in the order they are listed from top to bottom.

You can reorder the camera stack in script by directly manipulating the properties of the cameraStackunderlying camera's components.UniversalAdditionalCameraData

Add the same stacked camera to multiple stacks

To add an overlay camera to multiple camera stacks:

  1. Create a camera stack containing at least one superimposed camera.
  2. Create a camera in the scene. Its render type defaults to Base , making it the base camera.
  3. Select a new base camera.
  4. In the Camera Inspector, scroll to the Stacks section, click the *plus (+)* button, and click the name of the overlay camera you want to use in the two camera stacks.

Overlay cameras will now render in both camera stacks at the same time.

You can also add cameras from script to the camera stack by directly manipulating the properties of the cameraStackunderlying camera's component, like this:UniversalAdditionalCameraData

var cameraData = camera.GetUniversalAdditionalCameraData();
cameraData.cameraStack.Add(myOverlayCamera);

Render multiple cameras to one target

In a Universal Render Pipeline (URP), multiple underlying cameras or camera stacks can render to the same render target. This allows you to create effects such as split screen rendering.

If there are multiple underlying cameras or camera stacks rendering to the same area of ​​the render target, Unity will draw each pixel in the overlapping area multiple times. The highest priority base camera or camera stack is drawn last.

You can use the Base Camera's Output Target property to define a render target, and the Viewport Rect property to define the area of ​​the render target to render into.

Set up split screen rendering

Set up split screen rendering in URP

  1. Create a camera in the scene. Its rendering mode defaults to Base , making it a Base Camera.
  2. Choose a camera. In the Inspector, scroll to the Output section. Change the value of Viewport to the following:
    • X:0
    • Y:0
    • Width:0.5
    • Height:1
  3. Create another camera in the scene. Its rendering mode defaults to Base , making it a Base Camera.
  4. Choose a camera. In the Inspector, scroll to the Output section. Change the value of Viewport Rect to the following:
    • X:0.5
    • Y:0
    • Width:0.5
    • Height:1

Unity renders the first Camera to the left of the screen and the second Camera to the right of the screen.

You can rectchange the camera's viewport rectangle in script by setting its properties like so:

myUniversalAdditionalCameraData.rect = new Rect(0.5f, 0f, 0.5f, 0f);

Render to RenderTexture

In the Universal Rendering Pipeline (URP), the camera can render to the screen or to a RenderTexture (render texture). Rendering to the screen is the default and the most common use case, but rendering to a RenderTexture lets you create effects like CCTV camera monitors.

If you have a camera that you render to a RenderTexture, you must have a second camera before rendering that RenderTexture to the screen. In URP, all cameras that render to the RenderTexture execute their render loop before all cameras that render to the screen. This ensures that the RenderTexture is ready to be rendered to the screen.

Render to a RenderTexture, then render that RenderTexture to the screen

Render to RenderTexture in URP

  1. Create a Render Texture asset in your project using Assets > Create > Render Texture .
  2. Create a Quad in the scene.
  3. Create a material in your project, then select it. In the Inspector, drag the RenderTexture to the BaseMap field of the material.
  4. In the Scene View, drag the Material onto the Quad.
  5. Creates a camera in the scene, whose rendering mode defaults to Base , making it a Base Camera.
  6. Choose a base camera. In the Inspector, scroll to the Output section and drag Render Texture to the Output Texture field.
  7. Create another camera in the scene. Its render mode defaults to Base , making it a Base Camera, placing the Quad inside the view of the new Base Camera.

The first Camera renders its view to a RenderTexture. The second camera renders the scene including the RenderTexture to the screen.

You can set the camera's Output in the script by setting the properties of cameraOutputthe camera's UniversalAdditionalCameraDatacomponent, as follows:

myUniversalAdditionalCameraData.cameraOutput = CameraOutput.Texture;
myCamera.targetTexture = myRenderTexture;

[Extended learning]URP You can read all articles in this series by replying to the official account of Hongliu Academy , and there are even more video tutorials waiting for you!


I'm Dazhi (vx: zhz11235), your technology pathfinder, see you next time!

don't go! Like it , collect it!

OK, you can go.

Guess you like

Origin blog.csdn.net/zhenghongzhi6/article/details/111839634
Recommended