遇到的问题
1) Camera直接输出画面时,混合效果和混合公式对不上
底图Quad_Bg是蓝色(0, 0, 1, 1),混合图Quad_Src是红色(1, 0, 0, 0.5),混合模式为Blend DstColor Zero, One Zero
a) 只有将Camera的画面输出到RenderTexture时,混合效果才正常
红蓝相交的地方混合后:DstColor(0, 0, 1) * SrcColor(1, 0, 0) + 0 * DstColor = (0, 0, 0),rgb为黑色;1 * SrcAlpha(0.5) + 0 * DstAlpha = 0.5;最终为50%黑色
红色和相机背景白色混合后:DstColor(1, 1, 1) * SrcColor(1, 0, 0) = (1, 0, 0),rgb为红色;1 * SrcAlpha(0.5) + 0 * DstAlpha = 0.5;最终为50%红色
b) Camera直接输出画面,他的alpha总是会被设置为1
红蓝相交的地方混合后:DstColor(0, 0, 1) * SrcColor(1, 0, 0) + 0 * DstColor = (0, 0, 0),rgb为黑色;alpha总是为1,所以为黑色
红色和相机背景白色混合后:DstColor(1, 1, 1) * SrcColor(1, 0, 0) = (1, 0, 0),rgb为红色;alpha总是为1,所以为红色
常见的一些混合效果(使用RenderTexture)
1) 用到的shader
注意:这边的alpha值使用额外的混合参数控制:Blend SrcFactor DstFactor, SrcFactorA DstFactorA
Shader "My/Blend2" { Properties { _MainTex("Texture", 2D) = "white" {} [Enum(UnityEngine.Rendering.BlendMode)]_SrcFactor("SrcFactor", int) = 5 //BlendMode.SrcAlpha [Enum(UnityEngine.Rendering.BlendMode)]_DstFactor("DstFactor", int) = 10 //BlendMode.OneMinusSrcAlpha [Enum(UnityEngine.Rendering.BlendMode)]_SrcFactorA("SrcFactorA", int) = 5 //BlendMode.SrcAlpha [Enum(UnityEngine.Rendering.BlendMode)]_DstFactorA("DstFactorA", int) = 10 //BlendMode.OneMinusSrcAlpha } SubShader { Tags { "Queue" = "Transparent" "RenderType" = "Transparent" } ZWrite Off Blend[_SrcFactor] [_DstFactor], [_SrcFactorA] [_DstFactorA] Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o; } fixed4 frag(v2f i) : SV_Target { fixed4 c = tex2D(_MainTex, i.uv); return c; } ENDCG } } }
2) 正常模式 Blend SrcAlpha OneMinusSrcAlpha, SrcAlpha OneMinusSrcAlpha
3) 线性减淡 Blend One One, One One
4) 正片叠底 Blend DstColor Zero, DstColor Zero
参考
【UnityShader】Blend混合模式 与 20余种颜色混合模式代码实现(多图预警) - 哔哩哔哩 (bilibili.com)
Unity Shader 混合(Blend) - 知乎 (zhihu.com)
标签:SrcAlpha,MainTex,模式,混合,BlendMode,DstColor,Blend From: https://www.cnblogs.com/sailJs/p/16576641.html