简体   繁体   English

Unity / C#相机剔除蒙版符号

[英]Unity/C# Camera Culling Mask Notation

I have a shader managing script from a github project, and I'm trying to understand what it does line-by-line. 我有一个来自github项目的着色器管理脚本,我试图逐行了解它的作用。 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 Github信誉: 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. 第一个蒙版~(1 << LayerMask.NameToLayer("Transparent"))呈现透明层之外的所有内容。

The second mask, 1 << LayerMask.NameToLayer("Transparent") , renders only the transparent layer. 第二个遮罩1 << LayerMask.NameToLayer("Transparent")仅渲染透明层。

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 mask

Bit masking tends to make heavy use of: 位屏蔽倾向于大量使用:

  • left shift ( << ) to pick a bit 左移( << )进行选择
  • bitwise OR ( | ) to enable certain bits of a mask 按位或( | )以启用掩码的某些位
  • bitwise AND ( & ) to disable certain bits of a mask 按位与( & )以禁用掩码的某些位
  • bitwise NOT ( ~ ) to negate a mask 按位NOT( ~ )否定掩码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM