Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

LayerMasks - how to use 'em.

Discussion in 'Scripting' started by madcalf, May 1, 2006.

  1. madcalf

    madcalf

    Joined:
    Jan 12, 2006
    Posts:
    56
    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:

    Code (csharp):
    1. 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]
     
  2. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    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.
     
  3. madcalf

    madcalf

    Joined:
    Jan 12, 2006
    Posts:
    56
    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
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You have to or them:

    Code (csharp):
    1.  
    2. // All layers except 8 and 9
    3. layerMask = ~((1 << 8) | (1 << 9));
    4.  
     
    ProbePLayer likes this.
  5. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    ... or and them after negation:

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

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

    ~(3 << 8)

    :p
     
  6. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    How about adding and subtracting from a layerMask? Like this?

    Code (csharp):
    1. layerMask = layerMask | (1 << 8) | ~(1 << 9);
    (For the current layerMask with 8 enabled and 9 disabled.)
     
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You cactually have to AND not OR to subtract from a mask:

    Code (csharp):
    1.  
    2. layerMask |= (1 << 8);
    3. layerMask = ~((1 << 9);
    4.  
     
  8. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    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 :
    Code (CSharp):
    1. layerMask ^= (1 << layerToExclude);
     
  9. Deleted User

    Deleted User

    Guest

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

    Code (CSharp):
    1. layerMask |= (1 << layerToExclude);