翻译一遍官方的stencil模版测试文档,且阅且珍惜,

像我这么优秀的人,他妈的不理解stencil,然后就傻逼一样翻译一遍,人生第一次翻译官方文档,处女作。让我感觉到又回到了考试的年代英汉互译!今晚会不会做噩梦呢哈哈哈~ 英文水平有限,先试试火~
我听说官方的stencil很敷衍,但不影响我翻译它的热情~~

官方链接请点这里

正文:

ShaderLab: Stencil

shaderlab 模版测试

The stencil buffer can be used as a general purpose per pixel mask for saving or discarding pixels
模版测试是一个像素遮罩,用来检测是否把当前像素存储到缓冲区或者是遗弃掉。
The stencil buffer is usually an 8 bit integer per pixel. The value can be written to, increment or decremented. Subsequent draw calls can test against the value, to decide if a pixel should be discarded before running the pixel shader
模板缓冲通常是每个像素占用一个8位整数。这个值可以被写入代替,增加或者减少。之后drawcall会对比测试这个值,来确定在运行这个shader前这个值是否被dis掉。、

Syntax 语法

Ref

Ref referenceValue

The value to be compared against (if Comp is anything else than always) and/or the value to be written to the buffer (if either Pass, Fail or ZFail is set to replace). 0–255 integer.
这个值是为了跟缓冲区里面的值(如果comp是除always状态以外的其他状态)进行比较写入(如果pass,fail或者zfail那么就替换掉)。从0到255的整数。

ReadMask 读遮罩

ReadMask readMask

An 8 bit mask as an 0–255 integer, used when comparing the reference value with the contents of the buffer (referenceValue & readMask) comparisonFunction (stencilBufferValue & readMask). Default: 255.
一个8位遮罩是一个从0到255的整数,当与缓冲区的数值对比时用得到(参考值&读取遮罩)对比函数(缓冲区值&读取遮罩)。默认255.

WriteMask 写遮罩

WriteMask writeMask

An 8 bit mask as an 0–255 integer, used when writing to the buffer. Note that, like other write masks, it specifies which bits of stencil buffer will be affected by write (i.e. WriteMask 0 means that no bits are affected and not that 0 will be written). Default: 255.
一个8位遮罩作为一个从0到255的整数,用来当写入缓冲区的时候用。请注意,跟其他写遮罩一样,它规定任何位缓冲区都会受到写入的影响(也就是写遮罩0意思就是没受到影响然后0不会被写入)。默认值255.

Comp 比较函数

 Comp comparisonFunction

The function used to compare the reference value to the current contents of the buffer. Default: always.
用于将引用值与缓冲区的当前内容进行比较的函数。默认值:始终。
Pass

Pass stencilOperation

What to do with the contents of the buffer if the stencil test (and the depth test) passes. Default: keep.
如果模板测试(和深度测试)通过,如何处理缓冲区的内容。默认值:保留。
Fail

Fail stencilOperation

What to do with the contents of the buffer if the stencil test fails. Default: keep.
如果模版测试失败,缓冲区的内容应该怎么处理。默认值:保留
ZFail

 ZFail stencilOperation

What to do with the contents of the buffer if the stencil test passes, but the depth test fails. Default: keep.
如果模板测试通过,但深度测试失败,如何处理缓冲区的内容?默认值:保留。

Comp, Pass, Fail and ZFail will be applied to the front-facing geometry, unless Cull Front is specified, in which case it’s back-facing geometry. You can also explicitly specify the two-sided stencil state by defining CompFront, PassFront, FailFront, ZFailFront (for front-facing geometry), and CompBack, PassBack, FailBack, ZFailBack (for back-facing geometry).

comp、pass、fail和zfail将应用于前向几何图形,除非指定了cull front,在这种情况下,它是后向几何图形。还可以通过定义compfront、passfront、failfront、zfailfront(用于前向几何图形)和compback、passback、failback、zfailback(用于后向几何图形)来显式指定双面模板状态。

Comparison Function

对比函数
Comparison function is one of the following: 对比函数可以是下面的任意一项

Greater Only render pixels whose reference value is greater than the value in the buffer.
大于 当渲染的像素的引用值大于缓冲区的值
GEqual Only render pixels whose reference value is greater than or equal to the value in the buffer.
大于等于 当渲染的像素的引用值大于等于缓冲区的值
Less Only render pixels whose reference value is less than the value in the buffer.
小于
LEqual Only render pixels whose reference value is less than or equal to the value in the buffer.
小于等于
Equal Only render pixels whose reference value equals the value in the buffer.
等于
NotEqual Only render pixels whose reference value differs from the value in the buffer.
不等于
Always Make the stencil test always pass.
总是通过 总是让缓冲区测试通过
Never Make the stencil test always fail.
总是不通过 模板测试总是不通过测试

Stencil Operation模板操作
Stencil operation is one of the following:
模板操作是以下操作之一:

Keep Keep the current contents of the buffer.
保留缓冲区的当前内容。
Zero Write 0 into the buffer.
将0写入缓冲区。
Replace Write the reference value into the buffer.
将引用值写入缓冲区。
IncrSat Increment the current value in the buffer. If the value is 255 already, it stays at 255.
增加缓冲区中的当前值。如果值已经是255,则它将保持在255。
DecrSat Decrement the current value in the buffer. If the value is 0 already, it stays at 0.
减小缓冲区中的当前值。如果该值已为0,则它将保持为0。
Invert Negate all the bits.
把所有的位都求反。
IncrWrap Increment the current value in the buffer. If the value is 255 already, it becomes 0.
增加缓冲区中的当前值。如果值已经是255,则它将变为0。
DecrWrap Decrement the current value in the buffer. If the value is 0 already, it becomes 255.
减小缓冲区中的当前值。如果该值已为0,则变为255。

Deferred rendering path 延迟渲染路径

Stencil functionality for GameObjects that Unity renders in the deferred rendering path is limited. This is because Unity uses the stencil buffer for other purposes during the G-buffer pass and lighting pass, and the stencil state that Unity defines in the Shader is ignored.
Unity在延迟渲染路径中渲染的游戏对象的模板功能受到限制。这是因为Unity在g-buffer过程和照明过程中将模板缓冲区用于其他目的,并且忽略了Unity在shader中定义的模板状态。
As such, you cannot mask out the GameObjects that Unity renders in the deferred rendering path based on a stencil test, but they can still modify the buffer contents that GameObjects rendered later in the frame use. GameObjects that Unity renders in the forward renderingpath after the deferred path, such as transparent GameObjects or GameObjects without a surface shader
, set their stencil state as expected.

因此,您不能基于模板测试屏蔽Unity在延迟渲染路径中渲染的游戏对象,但它们仍然可以修改稍后在帧使用中渲染的游戏对象的缓冲区内容。Unity在延迟路径之后的前向渲染路径中渲染的游戏对象,例如透明的游戏对象或不带表面着色器的游戏对象
,按预期设置其模板状态。

Unity uses these bits for the stencil buffer in the deferred rendering
Unity将这些位用于延迟渲染的模板缓冲区
path:

Bit #7 (value=128) indicates any non-background object.
Bit #6 (value=64) indicates non-lightmapped objects.
Bit #5 (value=32) is not used by Unity.
Bit #4 (value=16) is used for light shape culling during the lighting pass, so that Unity executes only the lighting shader on pixels that the light touches, and not on pixels where the surface geometry is behind the light volume.
Unity uses the Lowest four bits (values 1,2,4,8) for light layer culling masks

.
You can operate within the range of the unused bits by using the stencil read and write masks, or you can force the camera
to clean the stencil buffer after the lighting pass with Camera.clearStencilAfterLightingPass.

您可以使用模具读写掩码在未使用位的范围内操作,也可以强制相机使用camera.clearStencilAfterLightingPass在照明通过后清除模板缓冲区。

Example

The first example shader will write the value ‘2’ wherever the depth test passes. The stencil test is set to always pass.
第一个示例shader将在深度测试通过的任何位置写入值“2”。模板测试设置为始终通过。

Shader "Red" {
    SubShader {
        Tags { "RenderType"="Opaque" "Queue"="Geometry"}
        Pass {
            Stencil {
                Ref 2
                Comp always
                Pass replace
            }
        
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            struct appdata {
                float4 vertex : POSITION;
            };
            struct v2f {
                float4 pos : SV_POSITION;
            };
            v2f vert(appdata v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                return o;
            }
            half4 frag(v2f i) : SV_Target {
                return half4(1,0,0,1);
            }
            ENDCG
        }
    } 
}

The second shader will pass only for the pixels which the first (red) shader passed, because it is checking for equality with the value ‘2’. It will also decrement the value in the buffer wherever it fails the Z test.
第二个shader将只传递给第一个(红色)shader传递的像素,因为它正在检查值“2”是否相等。在Z测试失败的地方,它还将减小缓冲区中的值。

Shader "Green" {
    SubShader {
        Tags { "RenderType"="Opaque" "Queue"="Geometry+1"}
        Pass {
            Stencil {
                Ref 2
                Comp equal
                Pass keep 
                ZFail decrWrap
            }
        
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            struct appdata {
                float4 vertex : POSITION;
            };
            struct v2f {
                float4 pos : SV_POSITION;
            };
            v2f vert(appdata v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                return o;
            }
            half4 frag(v2f i) : SV_Target {
                return half4(0,1,0,1);
            }
            ENDCG
        }
    } 
}

The third shader will only pass wherever the stencil value is ‘1’, so only pixels at the intersection of both red and green spheres - that is, where the stencil is set to ‘2’ by the red shader and decremented to ‘1’ by the green shader.
第三个shader将仅通过模板值为“1”的任何位置,因此只有红色和绿色球体相交处的像素-也就是说,红色shader将模板设置为“2”,绿色shader将其递减为“1”。

Shader "Blue" {
    SubShader {
        Tags { "RenderType"="Opaque" "Queue"="Geometry+2"}
        Pass {
            Stencil {
                Ref 1
                Comp equal
            }
        
            CGPROGRAM
            #include "UnityCG.cginc"
            #pragma vertex vert
            #pragma fragment frag
            struct appdata {
                float4 vertex : POSITION;
            };
            struct v2f {
                float4 pos : SV_POSITION;
            };
            v2f vert(appdata v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                return o;
            }
            half4 frag(v2f i) : SV_Target {
                return half4(0,0,1,1);
            }
            ENDCG
        }
    }
}

The result:
在这里插入图片描述
Another example of a more directed effect. The sphere is first rendered with this shader to mark-up the proper regions in the stencil buffer:
另一个更直接效果的例子。首先使用此着色器渲染球体,以标记模板缓冲区中的正确区域:

Shader "HolePrepare" {
    SubShader {
        Tags { "RenderType"="Opaque" "Queue"="Geometry+1"}
        ColorMask 0
        ZWrite off
        Stencil {
            Ref 1
            Comp always
            Pass replace
        }

        CGINCLUDE
            struct appdata {
                float4 vertex : POSITION;
            };
            struct v2f {
                float4 pos : SV_POSITION;
            };
            v2f vert(appdata v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                return o;
            }
            half4 frag(v2f i) : SV_Target {
                return half4(1,1,0,1);
            }
        ENDCG

        Pass {
            Cull Front
            ZTest Less
        
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            ENDCG
        }
        Pass {
            Cull Back
            ZTest Greater
        
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            ENDCG
        }
    } 
}

And then rendered once more as a fairly standard surface shader, with the exception of front face culling, disabled depth test and stencil test discarding previously marked pixels:
然后再次渲染为相当标准的曲面着色器,除了正面剔除、禁用深度测试和模具测试,丢弃以前标记的像素:

Shader "Hole" {
    Properties {
        _Color ("Main Color", Color) = (1,1,1,0)
    }
    SubShader {
        Tags { "RenderType"="Opaque" "Queue"="Geometry+2"}

        ColorMask RGB
        Cull Front
        ZTest Always
        Stencil {
            Ref 1
            Comp notequal 
        }

        CGPROGRAM
        #pragma surface surf Lambert
        float4 _Color;
        struct Input {
            float4 color : COLOR;
        };
        void surf (Input IN, inout SurfaceOutput o) {
            o.Albedo = _Color.rgb;
            o.Normal = half3(0,0,-1);
            o.Alpha = 1;
        }
        ENDCG
    } 
}

在这里插入图片描述
.
.

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值