在pass中,我们可以设置一些渲染状态:
culling 与深度测试阶段:
culling与深度测试是为了减少没必要的overdraw的,大大减少像素着色器的计算量。
- Cull 控制哪一面将会被剔除:
- Back ,不渲染多边形背面(默认)
- Front ,不渲染正面
- Off ,都不剔除,两面都渲染
- ZWrite 控制是否需要写入深度缓存,默认开启,一般渲染半透明物体的时候关闭
- ZTest 控制深度测试怎么测试,默认为LEqual
ZTest Less | Greater | LEqual | GEqual | Equal | NotEqual | Always
- Offset 两个参数,一个缩放因子,一个偏移
Offset Factor, Units
Blending阶段:
当图形被渲染时,在所有着色器被执行和所有纹理被应用之后,像素被写到屏幕上。它们如何与已经存在的内容相混合是由Blend命令控制的。
Syntax | 描述 |
Blend Off | 关闭混合(默认) |
Blend SrcFactor DstFactor | 配置并且开启混合,SrcColor*SrcFacto + DstCoclor * DstFactor |
Blend SrcFactor DstFactor, SrcFactorA DstFactorA | 与上面相同,不过alpha使用的factor为 SrcFactorA DstFactorA |
BlendOp Op | 默认是相加操作,我们可以指定不一样的混合操作 |
BlendOp OpColor, OpAlpha | 与上面相同,不过alpha使用的操作可以指定不一样的 |
Add | 对混合后 SrcColor 与 DstCoclor 使用相加操作 |
Sub | 对混合后 SrcColor 与 DstCoclor 使用相减操作 |
RevSub | 对混合后 DstCoclor 与 SrcColor 使用相减操作 |
Min | 逐分量计算DstCoclor SrcColor中较小的 |
Max | 逐分量计算DstCoclor SrcColor中较大的 |
One | The value of one - use this to let either the source or the destination color come through fully. |
Zero | The value zero - use this to remove either the source or the destination values. |
SrcColor | The value of this stage is multiplied by the source color value. |
SrcAlpha | The value of this stage is multiplied by the source alpha value. |
DstColor | The value of this stage is multiplied by frame buffer source color value. |
DstAlpha | The value of this stage is multiplied by frame buffer source alpha value. |
OneMinusSrcColor | The value of this stage is multiplied by (1 - source color). |
OneMinusSrcAlpha | The value of this stage is multiplied by (1 - source alpha). |
OneMinusDstColor | The value of this stage is multiplied by (1 - destination color). |
OneMinusDstAlpha | The value of this stage is multiplied by (1 - destination alpha). |
示例:
Shader "Shaders/Blend" {
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_MainTex ("Main Tex", 2D) = "white" {}
_AlphaScale ("Alpha Scale", Range(0, 1)) = 1
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True"
"RenderType"="Transparent"}
Pass {
Tags { "LightMode"="ForwardBase" }
ZWrite Off
// // Normal
// Blend SrcAlpha OneMinusSrcAlpha
//
// // Soft Additive
// Blend OneMinusDstColor One
//
// // Multiply
Blend DstColor Zero
//
// // 2x Multiply
// Blend DstColor SrcColor
//
// // Darken
// BlendOp Min
// Blend One One // When using Min operation, these factors are ignored
//
// // Lighten
// BlendOp Max
// Blend One One // When using Max operation, these factors are ignored
//
// // Screen
// Blend OneMinusDstColor One
// Or
// Blend One OneMinusSrcColor
//
// // Linear Dodge
Blend One One
}
}
}
Stencil测试:
模板缓冲,用一个8位的整数标识每一个像素的模板值,模板的值可以被修改,之后的绘制调用可以针对该值进行测试,决定是丢弃该像素。
Syntax | 使用 | 描述 |
Ref | Ref referenceValue | 要与之比较的值 和/或 要写入缓冲区的值(如果Pass、Fail或ZFail被设置为replace)。0 - 255整数。 |
ReadMask | ReadMask readMask | 一个8位的掩码,作为0-255的整数,在比较参考值和缓冲区内容(referenceValue和readMask)比较函数(stencilBufferValue和readMask)时使用。默认值:255。 |
WriteMask | WriteMask writeMask | 一个8位掩码,作为0-255整数,在写入缓冲区时使用。请注意,与其他写掩码一样,它指定哪些模板缓冲区位将受到写的影响(例如,WriteMask 0表示没有位受到影响,也不会写入0)。默认值:255。 |
Comp | Comp comparisonFunction | 用于将引用值与缓冲区的当前内容进行比较的函数。Default:always。 |
Pass | Pass stencilOperation | 如果模板测试(和深度测试)通过,该如何处理缓冲区的内容,Default:keep。 |
Fail | Fail stencilOperation | 如果模具测试失败,该如何处理缓冲区的内容Default: keep. |
ZFail | ZFail stencilOperation | 如果模板测试通过,但是深度测试失败,该如何处理缓冲区的内容Default: keep. |
Comparison Function:
Greater | 仅渲染参考值大于缓冲区值的像素。 |
GEqual | 仅渲染参考值大于或等于缓冲区值的像素。 |
Less | 仅渲染参考值小于缓冲区值的像素。 |
LEqual | 仅渲染参考值小于或等于缓冲区值的像素。 |
Equal | 仅渲染参考值等于缓冲区值的像素。 |
NotEqual | 仅渲染参考值不同于缓冲区值的像素。 |
Always | 使模板测试始终通过。 |
Never | 使模板测试始终失败。 |
Stencil Operation
Keep | 保持缓冲区的当前内容。 |
Zero | 将 0 写入缓冲区。 |
Replace | 将参考值写入缓冲区。 |
IncrSat | 递增缓冲区中的当前值。如果该值已经是 255,则保持为 255。 |
DecrSat | 递减缓冲区中的当前值。如果该值已经是 0,则保持为 0。 |
Invert | 将所有位求反。 |
IncrWrap | 递增缓冲区中的当前值。如果该值已经是 255,则变为 0。 |
DecrWrap | 递减缓冲区中的当前值。如果该值已经是 0,则变为 255。 |
示例:
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
}
}
}
Name:
Name "PassName" 指定pass的name
还有一些旧版本的光照,纹理 ,雾效可以直接访问:
https://docs.unity3d.com/Manual/SL-SubShader.html