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

5 Answers

5

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!

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?

cullingMask 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

...And what if I would like to change the camera's Culling Mask to 'Everything'?

0xffffffff - which is 32 bits set all set to 1.

Thank you! So would the syntax be Camera.cullingMask = 1 << 0xffffffff, or Camera.cullingMask = 1 << "0xffffffff" ?

Simple visual way:

var playerMask : LayerMask;    // select desired layer in inspector
var everythingMask : LayerMask; // select "Everything" in inspector
..
camera.cullingMask = playerMask;
..
camera.cullingMask = everythingMask;

This works great, thanks for sharing!

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).

Camera.main.cullingMask = LayerMask.GetMask(“UI”, “Other”);

Hi loliconest, Using PhotonNetwork.otherPlayers you can identify players by ID, you can call RPC method passing ID as parameter and check if ID is equal to the current object. Example: [Marco Polo Tutorial][1] Documentation: [RPCs And RaiseEvent][2] [1]: http://doc.photonengine.com/en-us/pun/current/tutorials/tutorial-marco-polo#_Toc317517606 [2]: http://doc.photonengine.com/en-us/pun/current/tutorials/rpcsandraiseevent