- Home /
Edit Camera Culling Mask
Hi, all! Is it possible to change my camera's culling mask via code? How so?
(I'd like to change it from rendering just one layer, to rendering everything, then back to just the player).
Thanks! -YA
Answer by Paulius-Liekis · Nov 16, 2012 at 10:56 AM
http://docs.unity3d.com/Documentation/ScriptReference/Camera-cullingMask.html
int oldMask = camera.cullingMask;
// change mask
camera.cullingMask = (1 << LayerMask.NameToLayer("TransparentFX")) | (1 << LayerMask.NameToLayer("OtherLayer"));
// do something
// ...
// restore mask
camera.cullingMask = oldMask;
Oh yes I understand the syntax for it, I just need someone to explain it more in depth for me. Like, why is there a pipe character and what is its functionality, why do you have two arguments, and which one does what?
culling$$anonymous$$ask is stored as bit mask. First bit (i.e. (1 << 0) or simply 1) represenst first layer. Sencond bit (i.e. (1 << 1) or simply 2) represent second layer. Third bit (i.e. (1 << 2) or simply 4) represent third layer. And so on. When you want to have two layers active you combine their appropriate bits with | operator. For example I need fist and fourth layer, so my mask would be ((1 << 0) | (1 << 3)) = (1 | 8) = 9
No. Just: Camera.culling$$anonymous$$ask = 0xffffffff; $$anonymous$$an you really need to lear bitwise logic and understand how binary math. Understand numbers with base 16 is useful too ;)
If someone don't know how to convert uint to int (Camera.culling$$anonymous$$ask = 0xffffffff;) just use unchecked keyword: http://stackoverflow.com/questions/1131843/how-do-i-convert-uint-to-int-in-c
Answer by roberto_sc · Nov 23, 2012 at 05:50 PM
This way you don't need backup values of old masks:
// Turn on the bit using an OR operation:
private void Show() {
camera.cullingMask |= 1 << LayerMask.NameToLayer("SomeLayer");
}
// Turn off the bit using an AND operation with the complement of the shifted int:
private void Hide() {
camera.cullingMask &= ~(1 << LayerMask.NameToLayer("SomeLayer"));
}
// Toggle the bit using a XOR operation:
private void Toggle() {
camera.cullingMask ^= 1 << LayerMask.NameToLayer("SomeLayer");
}
Roberto: Thanks for showing some "real" CS here! Good technique! -m
Listed for others convenience: ~ BIT-NOT (010111 becomes 101000) & BIT-AND (011 & 001 becomes 001) | BIT-OR (011 | 001 becomes 011) ^ EXCLUSIVE-OR (011 ^ 001 becomes 010)
Thanks dude! very thanks for encapsulating this code it's very good, God bless you!
Answer by Eric5h5 · Nov 24, 2012 at 09:33 PM
Simple visual way:
var playerMask : LayerMask; // select desired layer in inspector
var everythingMask : LayerMask; // select "Everything" in inspector
..
camera.cullingMask = playerMask;
..
camera.cullingMask = everythingMask;
Answer by asterix14 · Dec 22, 2015 at 05:23 PM
An easy way to set the culling mask to "Everything" is:
camera.cullingMask = -1; // -1 means "Everything"
-1 is converted to a bit mask having all bits set to 1 (which means all layers).
Answer by GLeBaTi · Oct 01, 2021 at 12:23 PM
Camera.main.cullingMask = LayerMask.GetMask("UI", "Other");
Your answer
Follow this Question
Related Questions
ratchet and clank style camera 0 Answers
GameObject disappear as I move/rotate the camera 1 Answer
Where To Learn Occlusion Culling Scripts 1 Answer
Camera focusing on an object specified in a script 1 Answer
Camera Control/ Rotation C# 0 Answers