Unity shader之ColorMask

时间:2023-03-09 06:31:09
Unity shader之ColorMask

Color Mask解释,见unity文档:

ColorMask

ColorMask RGB | A | 0 | any combination of R, G, B, A

Set color channel writing mask. Writing ColorMask 0 turns off rendering to all color channels. Default mode is writing to all channels (RGBA), but for some special effects you might want to leave certain channels unmodified, or disable color writes completely.

When using multiple render target (MRT) rendering, it is possible to set up different color masks for each render target, by adding index (0–7) at the end. For example, ColorMask RGB 3 would make render target #3 write only to RGB channels.

如下面问题,这是通过RenderTexture渲染了一个粒子,挂在NGUI的UITexture中。

希望去掉黑色区域而完全展现背景颜色,可通过ColorMask解决。

Unity shader之ColorMask

摄像机颜色为:

Unity shader之ColorMask

粒子shader为:

 Shader "effect/distortadd Lv3"
{
Properties
{
_TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
_NoiseTex ("Distort Texture (RG)", 2D) = "white" {}
_MainTex ("Alpha (A)", 2D) = "white" {}
_HeatTime ("Heat Time", range (-,)) =
_ForceX ("Strength X", range (,)) = 0.1
_ForceY ("Strength Y", range (,)) = 0.1
} Category
{
Tags { "Queue"="Transparent+400" "RenderType"="Transparent" }
Blend SrcAlpha One
Cull Off Lighting Off ZWrite Off Fog { Color (,,,) }
BindChannels
{
Bind "Color", color
Bind "Vertex", vertex
Bind "TexCoord", texcoord
} SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_particles
#include "UnityCG.cginc" struct appdata_t
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord: TEXCOORD0;
}; struct v2f
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 uvmain : TEXCOORD1;
}; fixed4 _TintColor;
fixed _ForceX;
fixed _ForceY;
fixed _HeatTime;
float4 _MainTex_ST;
float4 _NoiseTex_ST;
sampler2D _NoiseTex;
sampler2D _MainTex; v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = v.color;
o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
return o;
} fixed4 frag( v2f i ) : COLOR
{
//noise effect
fixed4 offsetColor1 = tex2D(_NoiseTex, i.uvmain + _Time.xz*_HeatTime);
fixed4 offsetColor2 = tex2D(_NoiseTex, i.uvmain + _Time.yx*_HeatTime);
i.uvmain.x += ((offsetColor1.r + offsetColor2.r) - ) * _ForceX;
i.uvmain.y += ((offsetColor1.r + offsetColor2.r) - ) * _ForceY;
return 2.0f * i.color * _TintColor * tex2D( _MainTex, i.uvmain);
}
ENDCG
}
}
// ------------------------------------------------------------------
// Fallback for older cards and Unity non-Pro SubShader
{
Blend DstColor Zero
Pass
{
Name "BASE"
SetTexture [_MainTex] { combine texture }
}
}
}
}

shader使用图片为:

Unity shader之ColorMask

Unity shader之ColorMaskUnity shader之ColorMask

NGUI中 UITexture使用的shader为:

 Shader "Unlit/Premultiplied Colored"
{
Properties
{
_MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
} SubShader
{
LOD Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
} Pass
{
Cull Off
Lighting Off
ZWrite Off
AlphaTest Off
Fog { Mode Off }
Offset -, -
ColorMask RGB
Blend One OneMinusSrcAlpha CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" sampler2D _MainTex;
float4 _MainTex_ST; struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
half4 color : COLOR;
}; struct v2f
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
half4 color : COLOR;
}; v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord;
o.color = v.color;
return o;
} half4 frag (v2f IN) : COLOR
{
half4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
return col;
}
ENDCG
}
} SubShader
{
LOD Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
} Pass
{
Cull Off
Lighting Off
ZWrite Off
AlphaTest Off
Fog { Mode Off }
Offset -, -
ColorMask RGB
Blend One OneMinusSrcAlpha
ColorMaterial AmbientAndDiffuse SetTexture [_MainTex]
{
Combine Texture * Primary
}
}
}
}

只所以出现黑色,这里有两层混合,一层是摄像机中此粒子与摄像机默认颜色,因为此shader渲染出来的颜色就是黑色(见图片颜色),alpha也全为1,再通过Blend SrcAlpha One命令混合,而目标颜色为黑色,alpha为0(见摄像机颜色),因此这一层混合后只剩下图片的颜色。

第二层混合在NGUI的UITexture中,此UITexture使用shader的混合命令为:Blend One OneMinusSrcAlpha,由于srcAlpha一直为1,所以UITexture后面的背景颜色在混合中消失了,渲染出来的结果只剩下图片的颜色。

要想去除黑色,可以通过 ColorMask RGB来屏蔽此通道输出的alpha,而完全取摄像机的alpha(即为0)。见shader代码:

Unity shader之ColorMask

此时效果如下:

Unity shader之ColorMask

转载请注明出处:https://www.cnblogs.com/jietian331/p/10675265.html

之所以ColorMask会解决这个问题,是因为ColorMask和Blend命令的执行先后顺序,先Blend,再ColorMask,这样Blend时使用frag shader中输出的alpha,保持了rgb颜色的正常,再color mask,屏蔽alpha通道,此时会去取摄像机的alpha,即为0。

而UITexture使用shader的混合命令为:Blend One OneMinusSrcAlpha,所以黑色在混合时消失了,其它颜色保留下来了,且能与背景很好地融合。

资源如下:

https://files.cnblogs.com/files/jietian331/ColorMask.zip