Reprinted from Tanmengmeng in CSDN,ar4ds in Sina etc.
1. Brief description of two properties 1.1 clear flags purge mark
Each camera stores color and depth information when it is rendered. The non-drawn portion of the screen is empty, and the Sky box is displayed by default. When you use multiple cameras, each stores its own color and depth information in a buffer and accumulates a large amount of rendering data for each camera. When any particular camera in the scene renders, you can set a clear flag to clear different sets of buffer information. This can be done with one of the following four options:
1.Skybox Sky Box
This is the default setting. The section above the screen will display the Sky box for the current camera. If the current camera does not have a sky box set, it will default to the Sky box selected in the Render settings (in Edit->render settings). Then it will be returned using the background color. In addition, the Sky box assembly can be added to the camera. If you want to create a new Sky box, you can use this guide.
2.Solid Color Solid Color
Any empty part of the screen displays the background color of the current camera.
3.Depth only Depth
If you want to draw a player's gun without letting it get cropped inside the environment, you need to set a camera with a depth of 0 to draw the environment, and another camera with a depth of 1 to draw the weapon separately. The weapon camera's purge mark should be set to depth only. This will keep the environment graphically displayed on the screen, but will discard all information about the location of each object in three-dimensional space. When the gun is drawn, the opaque part will completely cover anything that has been drawn, regardless of how close the gun is to the wall. (This part looks very laborious)
4.Don ' t clear does not erase
This mode does not clear any color or depth caches. The result is that each frame is drawn on top of the next frame, causing the smear effect. This is not a typical way to play a game, preferably with a custom shader.
1.2 Culling mask Culling mask
Culling masks use layers to selectively render a set of objects. Information about the usage layer can be found here.
It is usually good practice to put the user interface on a different layer and then render the UI layer separately with a separate camera.
In order for the UI to appear at the top of other camera views, you also need to set the purge mark to depth only, and make sure that the UI camera's depth is higher than the other camera.
2. Here are a few steps to demonstrate the two properties of the camera:
First create two cameras in the unity scene, named Camera1 and Camera2, respectively;
Then create a cube and sphere as the camera's object in the scene, and allow two cameras to capture two objects.
Then set the camera1 depth for 0,camera2 depth to 1;
You will find that the game window will only show the picture that Camera2 shot;
Visible Camera Depth properties set the camera drawing order , a camera with a larger value will be drawn on top of a camera with a smaller value, in other words, a camera with a depth of 1 will be drawn above a camera with a depth of 0.
Two:
Clear Falgs default Skybox;
Set the Camera2 clear Falgs to depth only;
At this point you can see that the game window will show the CAMERA1 and camera2 simultaneously shot ;
It can be seen that it is very simple to render the images taken by multiple cameras to the same screen, just set their clear falgs to depth only; one condition is that their depth need to be better than the clear The Falgs is a skybox or solid color camera with a high depth so that multiple cameras can be superimposed on the same screen separately.
Three:
Now the problem is that the two cameras shoot the same object, how can I let camera1 only shoot the scene background, and Camera2 only to shoot the set of objects;
Create a plane in the scene, and set the material, filled with camera1 screen, as the background of the scene.
Next, we want to set the layer relationship for the object. The concept of Unity's layer can be analogous to the layers inside PS.
First, add a layer, as shown, and name it object;
Next, set the layer for the object, set the layer of the Sphere object to the object layer, and the layer of the cube as the object layer.
Finally, you will set the culling mask property of two cameras so that the camera renders the specified Layer object.
the cullingmask of the camera defaults to everything, indicating that all layers of objects are rendered.
Camera1: culling mask inside the object layer is removed, culling mask will be shown as mixed ... You will find that the Camera1 window will only display the background picture and the object is not rendered.
Camera2: in the culling mask option, tick nothing, and then tick the object layer, indicating that the camera only renders objects belonging to object, and will not capture other layers.
Finally, the game window to see the two camera overlay screen;
Four:
At this point, we understand the powerful role of the camera's depth, Clear falgs, and culling mask.
Similarly, if you are making a game, you can create an interface camera, render the interface separately, and the other cameras will render the scene. All you need to do is add the UI layer and set the layer of all the interface objects to the culling mask of the Ui,ui camera and just tick the UI layer.
Cullingmask OP:
1. First introduce the concept of ' | ', ' & ', ' ~ '
With (intersection) &: 10000001 & 10000100 = = 10000000
or (and set) | : 10000001 | 10000100 = = 10000101
Non (reverse) ~: 10000001 = = ~01111110
Shift left << right >>
2.int mylayermask = 1 << curlayer;
The right curlayer of the equation represents a layer in the Inspector/layer
The left side of the equals sign holds information for multiple layers through a single number .
How did he do that?
It's actually saved with mylayermask = 00000000 Way .
i.e. mylayermask = 1 << 3---> mylayermask = = 00001000
the 3rd layer in the hierarchy is turned on and the remaining layers are turned off
Also, be aware that the sequence number of the layer is calculated starting from 0
Therefore, the above operation is equivalent to using a binary method for a one-to-many representation of the conversion process
3. Example explanation
with the top cushion, we can easily switch multiple layers.
1) Open 1, 2 layer
Camera.cullingmask = (1 << 1) | (1 << 2);
2) Open all layers except 2 layers
Camera.cullingmask = ~ (1 << 2);
3) Open all layers except 1, 2, and 4 layers
Camera.cullingmask = ~ ((1 << 1) | (1 << 2) | (1 << 4));
Refcamera.cullingmask = ~ (1 << layermask.nametolayer ("Water"));
This.camera.cullingMask = (1 << 0) + (1 << 8); So you can see the 0 and 8 layers, and remember to add parentheses.
Of course you can also open the corresponding layer through the layer name: This.camera.cullingMask = (1 << 0) + (1 << layermask.nametolayer ("Ground"));
Unity-camera&culling Mask ' s OP