I have a shader managing script from a github project, and I'm trying to understand what it does line-by-line. At one point it takes the camera and does this:
m_transparentCamera.cullingMask = ~(1 << LayerMask.NameToLayer("Transparent"));
And then it renders the camera. A bit later in the text it changes the target texture of the camera, and then does this:
m_transparentCamera.cullingMask = 1 << LayerMask.NameToLayer("Transparent");
And then renders again using a different shader.
What does the notation here mean? I know that ~ and << are bitwise operators, but what is it actually doing to the culling mask here?
Why does it do it this way?
And why invert the bits of the layer mask at first, and then not later on?
Github cred: https://github.com/candycat1992/OIT_Lab
The camera's culling mask tells it which layers to render.
The first mask, ~(1 << LayerMask.NameToLayer("Transparent"))
, renders everything except the transparent layer.
The second mask, 1 << LayerMask.NameToLayer("Transparent")
, renders only the transparent layer.
It's pretty common to render opaque geometry in one pass, then render transparent geometry in a second pass. This makes sure that you render any opaque objects that are behind transparent objects.
If none of that makes sense, you may want to read up on bit masks.
Bit masking tends to make heavy use of:
<<
) to pick a bit|
) to enable certain bits of a mask&
) to disable certain bits of a mask~
) to negate a mask