Depth only ?

Hello, I was wondering if anyone can give me a much clearer explanation of the depth only parameter int he clear flags property.

I keep getting confused with the language the unity manual keeps using.

Think of the depth as a hierarchy. The bigger the number, the more important it is. For example, let’s say you want to make sure an object is always visible, even behind walls. What you would do is something like this:

Camera 1: Set the Clear Flags to Depth Only. Set the depth to 1, or any number higher than Camera 2.

Camera 2: Set the depth to 0, or any number lower than Camera 1.

Essentially, the depth is the draw order. When you run a unity scene, the cameras are drawn in a specific order. Camera 2 is drawn first in this example because it has a lower depth. Everything is drawn for Camera 2, just like a normal camera. However, after Camera 2 is drawn, Camera 1 is drawn on top of it, which causes it to overlay. Because Camera 1’s Clear Flags is set to Depth Only, it will only draw whichever objects have the same Layer as the Culling Mask.

I hope this helped you to understand. Leave a comment if you are still confused or have any more questions.

It doesn´t work for me. I set two cameras with different Clear flags and depth, but the animation (rendered with the depth only) leaves a trail and doesn´t get any alpha information (black matte background). Honestly I cannot understed yet how could I resolve the depth only. I follow the

Firedan1176 suggestion but it didn´t work for me. help :slight_smile:

A camera in Unity holds info about the displayed object’s colors and depths.
In this case, depth info means the game world distance from the displayed object to the camera.

When a camera renders an image it can get rid of all or just some of the old image information and then displays the new image on top of what’s left.
Depending on what clear flags you select, the camera gets rid of different info.

  • The skybox clear flag means that when the camera renders a new frame, it clears everything from the old frame and it displays the new image on top of a skybox.
  • The solid color clear flag means that when the camera renders a new frame, it clears everything from the old frame and it displays the new image on top of a solid color.
  • The depth only clear flag means that when the camera renders a new frame, it clears only the depth information from the old frame and it keeps the color information on top of which displays the new frame. This means that the old frame can still show but it doesn’t have depth information, meaning the new objects to be displayed can’t be shown as intersected or obstructed by the old objects, because there’s no information about how far the old objects are(depth info was cleared). So the new objects will be on top of the old ones, mandatory.

This has useful implications for example in FPS games, where you want to avoid, when getting very close to a wall, the gun going through the wall. This can be avoided by having two cameras , one displaying on top of the other(camera order selected by using Depth parameter; the bigger Depth camera displays on top of the other). The first camera should display only the environment(and use skybox or solid color clear flag) and the second camera should display only the gun(and use depth only clear flag) (obs. selective display of game objects is done by using the culling mask ). Because the gun camera clears the depth info of the image shown by the environment camera, the gun image will be shown in full, on top of the wall image, regardless if those two objects intersect in the game world.