ShaderLab: Blending
Blending是用来实现透明物体的。
当图像被渲染,在所有shader运行后和所有纹理提供后,像素会被写到屏幕上。它们如何与已经存在的内容结合在一起,是由混合命令控制的。
Syntax
Blend Off
: 关闭blending (this is the default)
Blend SrcFactor DstFactor
: 启动 blending. 生成的颜色乘以 SrcFactor. 屏幕上已经存在的颜色乘以 DstFactor 然后将两者相加。
Blend SrcFactor DstFactor, SrcFactorA DstFactorA
: 同上,但使用不同的因素来混合alpha通道.
BlendOp Op
: 不要将混合色相加,对它们进行不同的操作。
BlendOp OpColor, OpAlpha
: 同上,但使用不同的混合操作来混合颜色(RGB)和alpha(A)通道。
此外你可以设置upper-rendertarget blending modes. 当使用多目标渲染时(MRT), 上面的语法设置了相同的混合模式给所有目标. 下面的语法可以设置不同的混合模式给不同的渲染目标,N
所在位置是渲染目标的索引(0..7)。这个特征在大多现代APIs/GPUs上有效(DX11/12, GLCore, Metal, PS4):
Blend N SrcFactor DstFactor
Blend N SrcFactor DstFactor, SrcFactorA DstFactorA
BlendOp N Op
BlendOp N OpColor, OpAlpha
AlphaToMask On
:Turns on alpha-to-coverage. 当 MSAA 被使用, alpha-to-coverage多重采样范围遮罩按比例修改像素Shader的alpha值. 这通常用于得到比常规alpha test少的轮廓走样; 对于植被等其他alpha-tested Shaders有效。
Blend operations
以下blend操作可用:
operations | description |
---|---|
Add | 添加源和目标。 |
Sub | 目标加上源。 |
RevSub | 从目标减去源。 |
Min | 使用目标和源种较小的。 |
Max | 使用目标和源种较大的。 |
LogicalClear | Logical operation: Clear (0) DX11.1 only. |
LogicalSet | Logical operation: Set (1) DX11.1 only. |
LogicalCopy | Logical operation: Copy (s) DX11.1 only. |
LogicalCopyInverted | Logical operation: Copy inverted (!s) DX11.1 only. |
LogicalNoop | Logical operation: Noop (d) DX11.1 only. |
LogicalInvert | Logical operation: Invert (!d) DX11.1 only. |
LogicalAnd | Logical operation: And (s & d) DX11.1 only. |
LogicalNand | Logical operation: Nand !(s & d) DX11.1 only. |
LogicalOr | Logical operation: Or (s |
LogicalNor | Logical operation: Nor !(s |
LogicalXor | Logical operation: Xor (s ^ d) DX11.1 only. |
LogicalEquiv | Logical operation: Equivalence !(s ^ d) DX11.1 only. |
LogicalAndReverse | Logical operation: Reverse And (s & !d) DX11.1 only. |
LogicalAndInverted | Logical operation: Inverted And (!s & d) DX11.1 only. |
LogicalOrReverse | Logical operation: Reverse Or (s |
LogicalOrInverted | Logical operation: Inverted Or (!s |
Blend factors
以下的所有属性对于在Blend command的SrcFactor & DstFactor都有效。 Source 是指计算的颜色, Destination 是已经在屏幕上的颜色. 如果BlendOp被使用作为逻辑操作,混合系数会被忽略。
factors | description |
---|---|
One | The value of one - 使用此项可以使源或目标颜色完全通过。 |
Zero | The value zero - 使用此方法可删除源或目标值。 |
SrcColor | 此阶段的值乘以源颜色值. |
SrcAlpha | 此阶段的值乘以源alpha值. |
DstColor | 此阶段的值乘以帧缓冲区源色值. |
DstAlpha | 此阶段的值乘以帧缓冲区源alpha值. |
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). |
Details
以下是最常见的混合类型:
Blend SrcAlpha OneMinusSrcAlpha // Traditional transparency
Blend One OneMinusSrcAlpha // Premultiplied transparency
Blend One One // Additive
Blend OneMinusDstColor One // Soft Additive
Blend DstColor Zero // Multiplicative
Blend DstColor SrcColor // 2x Multiplicative
Alpha blending, alpha testing, alpha-to-coverage
主要用于绘制完全不透明或完全透明的物体, 透明度是由纹理的alpha通道定义的 (e.g. leaves, grass, chain fences etc.), 常用的几种方法:
Alpha blending
常规alpha混合
这通常意味着对象必须被视为“半透明”,因此不能使用一些渲染特性 (例如: 延迟阴影,不能接收阴影)。 凹形或重叠alpha-blended对象,经常也有绘制顺序问题。
通常, alpha-blended Shaders 也设置 transparent render queue, 并关闭深度写入(depth writes). So the Shader code looks like:
// inside SubShader
Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" }
// inside Pass
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Alpha testing/cutout
clip() in pixel Shader
通过在像素Shader中使用 clip()
HLSL 指令, 一个像素可以被“丢弃”或不基于某些标准。这意味着对象仍然可以被视为完全不透明的, 并且没有绘制顺序的问题. 然而, 这意味着所有的像素都是不透明或全透明的 导致混叠(“锯齿”)。
经常, alpha-tested Shaders 也设置 cutout render queue, so the Shader code looks like this:
// inside SubShader
Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" "IgnoreProjector"="True" }
// inside CGPROGRAM in the fragment Shader:
clip(textureColor.a - alphaCutoffValue);
Alpha-to-coverage
AlphaToMask On, at 4xMSAA
当使用多重采样抗锯齿(MSAA, see QualitySettings), 它是有可能提高alpha testing通过alpha-to-coverage GPU 功能。这提高了边缘的外观,这取决于所使用的 MSAA 等级.
这个功能在大部分是全透明或不透明并且有很薄的“部分透明”区域的纹理上效果很好(grass, leaves and similar).
Often, alpha-to-coverage Shaders also set cutout render queue. So the Shader code looks like:
// inside SubShader
Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" "IgnoreProjector"="True" }
// inside Pass
AlphaToMask On
Example
这是一个小的例子Shader用来添加纹理到任何已经存在屏幕上的东西:
Shader "Simple Additive" {
Properties {
_MainTex ("Texture to blend", 2D) = "black" {}
}
SubShader {
Tags { "Queue" = "Transparent" }
Pass {
Blend One One
SetTexture [_MainTex] { combine texture }
}
}
}