[Unity]模块缓冲技术记录

2019/09 01 16:09
Stencil
{
	Ref ref
	ReadMask readMask
	WriteMask writeMask
	Comp comp
	Pass passOp
	Fail failOp
	ZFail zfailOp
}

注:模块缓冲在图形管道中,以AlphaTest之后,在DepthTest 之前
AlphaTest -> StencilTest -> DepthTest

一个像素当配置了Stencil后,执行以下执行:
if( ref&readMask comp stencil&readMask)
{
  DO: passOp
}
else
{
  DO: failOP
}

其中,初始化时stencil等于0
Ref/ReadMask/WriteMask 都为0~255的整数

Comp列表:
Greater>;
GEqual>=;
Less<;
LEqual<=;
Equal=;
NotEqual!=;
Always总是成功像素渲染;
Never总是失败像素抛弃

OP列表:
Keep	保留当前缓冲中的内值
Zero        将0写入当前缓冲的值
Replace	将参考值写入缓冲,即将Ref赋值给当前缓冲的值
IncrSat	当前缓冲的值加1,如当前缓冲的值超过255了,那么保留为255,即不大于255
DecrSat	当前缓冲的值减1,如果当前缓冲的值超过为0,那么保留为0,即不小于0
Invert	当前缓冲的值按位取反
IncrWrap 当前缓冲的值加1,如果缓冲值超过255了,那么变成0,然后继续自增
DecrWrap 当前缓冲的值减1,如果缓冲值已经为0,那么变成255,然后继续自减

示例:

只渲染一次
Stencil
{
  Ref 0
  Comp Equal
  Pass IncrSat
}

仅渲染第二次
Stencil
{
  Ref 1
  Comp Greater
  Pass IncrSat
}

UnityEngine.Rendering.CompareFunction
    public enum CompareFunction
    {
        //     Depth or stencil test is disabled.
        Disabled = 0,
        //     Never pass depth or stencil test.
        Never = 1,
        //     Pass depth or stencil test when new value is less than old one.
        Less = 2,
        //     Pass depth or stencil test when values are equal.
        Equal = 3,
        //     Pass depth or stencil test when new value is less or equal than old one.
        LessEqual = 4,
        //     Pass depth or stencil test when new value is greater than old one.
        Greater = 5,
        //     Pass depth or stencil test when values are different.
        NotEqual = 6,
        //     Pass depth or stencil test when new value is greater or equal than old one.
        GreaterEqual = 7,
        //     Always pass depth or stencil test.
        Always = 8
    }