概括:

看上圖,會(huì)發(fā)現(xiàn) Blending 發(fā)生在著色流程的最后。片段著色,
Alptha Test 完成后,如何和 ColorBuffer 中已有顏色混合,就是 Bland指令需要做的事情。
應(yīng)用:
Blend 指令可以在 Pass 內(nèi)部,用來(lái)決定這一個(gè) Pass 的渲染狀態(tài);也可以放在 SubShader ,用來(lái)決定其所有 Pass 的渲染狀態(tài)。
Blending 方程式為:
finalValue = sourceFactor * sourceValue operation destinationFactor * destinationValue
方程式中各變量的含義:
finalValueis the value that the GPU writes to the destination buffer.
GPU 寫(xiě)進(jìn)目標(biāo)緩存的值sourceFactoris defined in the Blend command.
由 Blend 指令定義sourceValueis the value output by the fragment shader.
片段著色器輸出的值-
operationis the blending operation.
Blend 操作,可用的指令有:值 含義 Add buffer_value+片段著色器輸出值Sub 片段著色器輸出值-buffer_valueRevSub buffer_value-片段著色器輸出值Min Math.Min( 片段著色器輸出值,buffer_value)Max Math.Max( 片段著色器輸出值,buffer_value) destinationFactoris defined in the Blend command.
由 Blend 指令定義destinationValueis the value already in the desination buffer.
目標(biāo)緩存中已經(jīng)存在的值
| 定義 | 樣例 | 描述 |
|---|---|---|
Blend <state> |
Blend Off |
關(guān)閉 Blend ,這是默認(rèn)設(shè)置 |
Blend <render target> <state> |
Blend 1 Off |
同上,但是給定了渲染目標(biāo) |
Blend <source factor> <destination factor> |
Blend One Zero |
為默認(rèn)渲染目標(biāo)開(kāi)啟Blend |
Blend <render target> <source factor><destination factor> |
Blend 1 One Zero |
同上,但是指定了渲染目標(biāo) |
Blend <source factor RGB> <destination factor RGB>, <source factor alpha> <destination factor alpha> |
Blend One Zero,Zero One |
為默認(rèn)渲染目標(biāo)開(kāi)啟Blend,為 RGB 和 alpha 值設(shè)置單獨(dú)的混合因子 |
Blend <render target> <source factor RGB> <destination factor RGB>, <source factor alpha> <destination factor alpha> |
Blend 1 One Zero,Zero One | 同上,但給定了渲染目標(biāo) |
參數(shù)可能的值:
| 名稱 | 值 | 含義 |
|---|---|---|
| render target | int[0-7] | 渲染目標(biāo)編號(hào),從0到7 |
| state | Off |
關(guān)閉 blending |
| factor | One |
輸入值1,相當(dāng)于直接用源值或者目標(biāo)值代入公式 |
Zero |
輸入值0,相當(dāng)于移除源值或者目標(biāo)值 | |
SrcColor |
GPU 用此輸入值乘源值 | |
DestColor |
GPU 將此輸入的值乘以目標(biāo)值(color buffer中的值) | |
OneMinusSrcColor |
GPU 將此輸入值乘以(1 - 源值) | |
OneMinusSrcAlpha |
GPU 將此輸入值乘以(1 - 源值的Alpha) | |
OneMinusDstColor |
GPU 將此輸入值乘以(1 - 目標(biāo)值) | |
OneMinusDstAlpha |
GPU 將此輸入值乘以(1 - 目標(biāo)的Alpha) |
注釋
- 源值:片段著色器輸出的值
- 目標(biāo)值:color buffer 中的當(dāng)前值
常見(jiàn)的混合類型:
-
Blend SrcAlpha OneMinusSrcAlpha// 傳統(tǒng)的透明度
傳統(tǒng)的透明度 -
Blend One OneMinusSrcAlpha// 預(yù)乘透明度
預(yù)乘透明度 -
Blend One One// 疊加
疊加 -
Blend OneMinusDstColor One// 柔性疊加
柔性疊加 -
Blend DstColor Zero// 乘
Blend DstColor Zero -
Blend DstColor SrcColor// 加倍乘
Blend DstColor SrcColor
測(cè)試用的Shader
Shader "Unlit/TestBlendShader"
{
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
[Header(Blending)]
// https://docs.unity3d.com/ScriptReference/Rendering.BlendMode.html
[Enum(UnityEngine.Rendering.BlendMode)]_DecalSrcBlend("_DecalSrcBlend (default = SrcAlpha)", Int) = 5 // 5 = SrcAlpha
[Enum(UnityEngine.Rendering.BlendMode)]_DecalDstBlend("_DecalDstBlend (default = OneMinusSrcAlpha)", Int) = 10 // 10 = OneMinusSrcAlpha
}
SubShader
{
Tags { "RenderType"="Opaque" }
Blend[_DecalSrcBlend][_DecalDstBlend]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 normal : NORMAL;
};
struct v2f
{
float2 wnormal : NORMAL;
float4 vertex : SV_POSITION;
};
fixed4 _Color;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.wnormal = UnityObjectToWorldNormal((v.normal,1));
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return _Color * (dot(normalize(_WorldSpaceLightPos0.xyz),i.wnormal) * 0.5 + 0.5);
}
ENDCG
}
}
}





