Skip to content

Understanding Rendering Order in Unity with Multiple Scenes and Cameras

When working with Unity and handling multiple scenes loaded additively, understanding the rendering order becomes crucial, especially when each scene contains multiple cameras and game objects on different layers. Let's break down how Unity handles rendering in this scenario.

Scene Loading and Hierarchy

In Unity, when you load multiple scenes additively, the game objects from all loaded scenes coexist in the hierarchy. However, the scenes themselves are logically separated. This means that all active cameras, lights, and game objects in these scenes can influence the final rendered output unless specifically controlled.

csharp
// Example of loading scenes additively
SceneManager.LoadScene("Scene1", LoadSceneMode.Additive);
SceneManager.LoadScene("Scene2", LoadSceneMode.Additive);

Cameras and Rendering Order

The rendering order of cameras in Unity is determined by their Depth property. Cameras are rendered from the lowest depth to the highest. If multiple cameras have the same depth, their rendering order is not guaranteed and may depend on the order they were loaded or their hierarchy in the scene.

Camera Depth

  • Lower Depth Value: Renders first.
  • Higher Depth Value: Renders on top of cameras with lower depth values.
csharp
// Setting camera depth
Camera cameraA = ...;
cameraA.depth = 0; // Renders first

Camera cameraB = ...;
cameraB.depth = 1; // Renders after cameraA

Culling Mask and Layers

Cameras use the Culling Mask to determine which layers they render. Game objects are assigned to layers, and cameras only render the layers included in their culling mask.

csharp
// Setting up layers
public enum Layers {
    Background = 8,
    Characters = 9,
    UI = 10
}

// Assigning a game object to a layer
gameObject.layer = (int)Layers.Characters;

// Setting camera culling mask to render only specific layers
cameraA.cullingMask = 1 << Layers.Background; // Only renders Background layer
cameraB.cullingMask = 1 << Layers.Characters; // Only renders Characters layer

Rendering Order with Multiple Scenes and Cameras

Given two scenes, each with two cameras, and game objects on different layers, here's how Unity determines the rendering order:

  1. All Active Cameras Are Considered: Unless a camera is disabled, it will be part of the rendering process regardless of the scene it belongs to.

  2. Cameras Are Ordered by Depth:

    • Unity collects all active cameras from all loaded scenes.
    • It sorts them based on their Depth property.
  3. Rendering Occurs Based on Camera Depth:

    • Cameras with lower depth values render first.
    • Subsequent cameras render on top of previous ones, potentially overwriting pixels.
  4. Culling Masks Define What Each Camera Sees:

    • Each camera renders only the game objects on the layers included in their culling mask.
    • Overlapping culling masks can cause multiple cameras to render the same game objects.

Example Scenario

  • Scene 1:

    • Camera A1: depth = 0, cullingMask = Background
    • Camera A2: depth = 2, cullingMask = Characters
  • Scene 2:

    • Camera B1: depth = 1, cullingMask = Background
    • Camera B2: depth = 3, cullingMask = UI
  • Game Objects:

    • Background Objects: Assigned to Background layer.
    • Character Objects: Assigned to Characters layer.
    • UI Objects: Assigned to UI layer.

Rendering Steps

  1. Camera A1 (depth = 0):

    • Renders Background layer objects from Scene 1.
    • Since it has the lowest depth, it renders first.
  2. Camera B1 (depth = 1):

    • Renders Background layer objects from Scene 2.
    • Renders on top of Camera A1's output.
    • If there are overlapping objects, Camera B1's view will overwrite Camera A1's.
  3. Camera A2 (depth = 2):

    • Renders Characters layer objects from Scene 1.
    • Renders on top of previous cameras.
  4. Camera B2 (depth = 3):

    • Renders UI layer objects from Scene 2.
    • Renders last, ensuring UI elements are on top.

Visual Result

The final rendered frame will display:

  • Background objects from Scene 2 (since Camera B1 overwrote Camera A1).
  • Character objects from Scene 1.
  • UI elements from Scene 2 on top of everything else.

Considerations

  • Active State: Ensure cameras and game objects are active. Inactive cameras won’t render anything.
  • Clear Flags: Cameras have Clear Flags settings that determine how they clear the screen before rendering:
    • Skybox or Solid Color: Clears the screen with the skybox or a solid color.
    • Depth Only: Does not clear color buffer, useful for overlay cameras.
    • Don't Clear: Doesn't clear the screen at all, potentially causing artifacts if not managed carefully.
csharp
// Setting clear flags
cameraA.clearFlags = CameraClearFlags.Skybox;
cameraB.clearFlags = CameraClearFlags.Depth;
  • Camera Stacking: In newer Unity versions (especially with URP), you can stack cameras for more controlled rendering. This is helpful for overlays like UI.

Role of activeScene

In multi-scene setups, the active scene has special significance:

  • Newly Created GameObjects: By default, new game objects are placed into the active scene.
  • Lightmapping and Baked Data: Some lighting data relies on the active scene.
  • Scene Management: Certain operations default to the active scene if no scene is specified.
csharp
// Setting the active scene
SceneManager.SetActiveScene(SceneManager.GetSceneByName("Scene1"));

However, the active scene does not directly impact the rendering order when dealing with cameras and game objects across multiple scenes.

Best Practices

  • Organize Layers and Culling Masks: Clearly define which cameras render which layers to avoid unintended overlaps.
  • Manage Camera Depths: Assign depth values thoughtfully to control the rendering order.
  • Use Post-Processing Carefully: If using post-processing effects, consider how they apply across cameras from different scenes.
  • Optimize Performance: Limit the number of active cameras when possible, as each camera incurs additional rendering overhead.

Conclusion

In a scenario with multiple scenes, each containing cameras and game objects on various layers, Unity determines the rendering order based on camera depth and culling masks, regardless of which scene the cameras and game objects belong to. By carefully managing camera properties and understanding how Unity composites the final image, you can control the rendering output effectively in complex multi-scene setups.