Docs / Building a world · v0.120.0

Camera stacks: one camera drawn over another

Camera stacks: one camera drawn over another

A camera's stack is a list of other cameras drawn on top of its picture, bottom first. Each stacked camera draws only the layers it renders, and it draws them over an empty depth buffer, so nothing the camera beneath drew can hide them.

The usual reason for a stack is first-person arms. Arms that are part of the world push into every wall the player stands against. Arms filmed by a stacked camera never touch the wall, because that camera never draws it.

First-person arms, step by step

  1. Put the arms on a layer of their own. Add an Arms layer in Settings ▸ Layers and set it on every node of the arm rig that draws something. Each node has its own layer, so a model child does not take its parent's.
  2. Take that layer out of the main camera. On the player's camera, open renders: and untick Arms, so the main camera no longer draws them.
  3. Add a second camera for the arms. Parent it to the main camera at zero offset, so it moves with the player's view. Under renders:, tick only Arms. It does not need to be the active camera.
  4. Stack it. On the main camera, under stack, pick the arms camera from + add camera, or drag it in from the Hierarchy.

Step 4 works from a script too, once the layers are set:

lua
function start(node)
  find('ArmsCam'):setCamera{ fovY = 0.9 }
  find('PlayerCam'):setCamera{ stack = { 'ArmsCam' } }
end

The stacked camera has its own field of view. Shooters usually give the arms a narrower one than the world, so the arms don't stretch at the edges of a wide view.

Depth of field and a stacked camera

By default a stacked camera's picture goes in with the rest, so depth of field blurs it by its own distance. A cockpit 0.6 m away, with the focus on a ship 25 m off, is blurred the way anything that near would be. First-person arms, a gun or a cockpit usually want to stay sharp. Set post = "after" on the stacked camera, or tick stacked: over depth of field in its Inspector, and it is laid over after depth of field and motion blur:

lua
find('SeatCam'):setCamera{ post = 'after' }

The rest of post still applies to it: the grade, tonemapping, film grain, your stage post shaders. Bloom and ambient occlusion run before depth of field, so a camera laid over after it gets neither. post = "with" goes back to the default.

Order

Rows draw bottom of the list last, so the bottom row is on top. Drag a row by its ≣ handle to reorder. In a script, stack = { 'Arms', 'Scope' } draws the scope over the arms. Setting stack replaces the whole list, and stack = {} clears it.

What a stacked camera draws

  • Its own layers, lit by the scene. The arms get the same sun, lamps and fog as the world around them, and cast sun shadows onto themselves. The terrain shades them too: standing in the shadow of a hill or in a cave, the arms are in shadow like everything else there. The sun's shadow map in a stacked camera holds what that camera draws and the terrain, so a wall or a building that the stacked camera does not draw shades the arms only through shadow proxies.
  • No sky, terrain, particles or script shapes. The camera beneath has already drawn all of those, and drawing them again would cover the world.
  • The base camera's framing. The stacked camera renders at the same size and aspect ratio as the picture it is laid over. Its position, rotation and field of view are its own.
  • Before post-processing. Bloom, tonemapping, grading and your stage post shaders apply to the whole picture at once. Where the stacked camera drew something solid, its depth goes into the picture's depth too, measured from the base camera. So depth of field and ambient occlusion see a cockpit 0.6 m away as 0.6 m away, and keep it sharp while the hills blur.

Wherever the stacked camera drew nothing, the picture underneath shows through unchanged.

Where it applies

Every view of the game uses the stack: the Game tab (docked or fullscreen), an exported build, floptle shot and camera.capture. A camera with a render target carries its own stack into its texture, so a scope's feed can have a reticle camera on top.

Things to know

  • One level deep. If a stacked camera has a stack of its own, that stack is not drawn, and the Inspector says so. This also means two cameras that stack each other cannot loop.
  • A name that matches no camera draws nothing. The Inspector shows a warning on the row, and the Console reports it once.
  • Switching a stacked camera off hides what it films. Disable the arms camera during a cutscene and the arms are gone, with no warning, because that's a normal thing to do.
  • Every stacked camera is another render of its layers. A pair of arms costs little. A stacked camera that renders every layer redraws the whole scene.