LayerMasks - how to use 'em.

So i get the concept of using layers. And I understand the idea of a layerMask, but I don't quite get it in practice. Specifically, the following syntax is not clear to me:

layerMask = ~(1 << 8);

What does the "~" and the "<<" do? Is the layermask a number? Anyone willing to take a minute or two to explain? Or to point out a good reference?

Thanks!
d[/code]

The layermask is a bit mask where every bit tells whether the corresponding layer is on or off. (Ie. bit 0 indicates whether layer 0 is on or off, bit 1 for layer 1, etc)

The <<, >> and ~ operators are bitwise operators. << and >> shift a bitfield or a number a certain amount of bits left or right and the ~ operators inverses it, changing all ones to zeroes and vice versa.

The expression ~(1 << 8) creates a bit mask with all bits set to one except bit number 8. We can break it down a bit more:

  • Let's start with the number 1. This is in itself a bitmask with only bit number 0 turned on. (The bits are numbered from 0 and up from the rightmost bit to the left.)
  • Then, we shift it to the left by 8 places using the << operator, which gives us a mask with all bits set to zero, except bit number 8.
  • And at last we apply the ~ to the result in order to invert the bits in the mask, turning it to a mask with all bits set to 1 except bit number 8.
2 Likes

That was very helpful! I understand the concept now.

To take it a step further, how do you generate a layermask (or cullingmask) number if you have multiple layers you want to exclude? I assume this involves some binary math and converting that to an integer.

For instance if I wanted layers 8 and 9 to be invisible to the camera, I know this number would be 64767 because I output the camera's cullingMask property while playing the game with layers 8 and 9 de-selected. I'm assuming this number is based on all 16 of Unity's available layers. But how would I calculate these numbers from scratch when excluding multiple layers?

I hope this is clear. Let me know if i'm not making any sense and i'll try to clarify.

thanks much!
d

You have to or them:

// All layers except 8 and 9
layerMask = ~((1 << 8) | (1 << 9));
1 Like

... or and them after negation:

(~(1 << 8) ~(1 << 9))

... or since bits 8 and 9 are right next to each other:

~(3 << 8)

:-P

How about adding and subtracting from a layerMask? Like this?

layerMask = layerMask | (1 << 8) | ~(1 << 9);

(For the current layerMask with 8 enabled and 9 disabled.)

[quote]

layerMask = layerMask | (1 << 8) | ~(1 << 9);

[/quote]
You cactually have to AND not OR to subtract from a mask:

layerMask |= (1 << 8);
layerMask = ~((1 << 9);

Just for the sake of completeness, if anyone comes across this thread wondering how to exclude a layer from a layerMask, you'll have to do this :

layerMask ^= (1 << layerToExclude);
3 Likes

And if you want to add to a layermask, you do this.

layerMask |= (1 << layerToExclude);