Unity shader之ColorMask

Color Mask解释,见unity文档:html

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.app

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.ide

 

 

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

但愿去掉黑色区域而彻底展示背景颜色,可经过ColorMask解决。code

摄像机颜色为:htm

粒子shader为:blog

 1 Shader "effect/distortadd Lv3"
 2 {
 3     Properties
 4     {
 5         _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
 6         _NoiseTex ("Distort Texture (RG)", 2D) = "white" {}
 7         _MainTex ("Alpha (A)", 2D) = "white" {}
 8         _HeatTime  ("Heat Time", range (-1,1)) = 0
 9         _ForceX  ("Strength X", range (0,1)) = 0.1
10         _ForceY  ("Strength Y", range (0,1)) = 0.1
11     }
12 
13     Category
14     {
15         Tags { "Queue"="Transparent+400" "RenderType"="Transparent" }
16         Blend SrcAlpha One
17         Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
18         BindChannels
19         {
20             Bind "Color", color
21             Bind "Vertex", vertex
22             Bind "TexCoord", texcoord
23         }
24 
25         SubShader
26         {
27             Pass
28             {
29                 CGPROGRAM
30                 #pragma vertex vert
31                 #pragma fragment frag
32                 #pragma fragmentoption ARB_precision_hint_fastest
33                 #pragma multi_compile_particles
34                 #include "UnityCG.cginc"
35 
36                 struct appdata_t
37                 {
38                     float4 vertex : POSITION;
39                     fixed4 color : COLOR;
40                     float2 texcoord: TEXCOORD0;
41                 };
42 
43                 struct v2f
44                 {
45                     float4 vertex : POSITION;
46                     fixed4 color : COLOR;
47                     float2 uvmain : TEXCOORD1;
48                 };
49 
50                 fixed4 _TintColor;
51                 fixed _ForceX;
52                 fixed _ForceY;
53                 fixed _HeatTime;
54                 float4 _MainTex_ST;
55                 float4 _NoiseTex_ST;
56                 sampler2D _NoiseTex;
57                 sampler2D _MainTex;
58 
59                 v2f vert (appdata_t v)
60                 {
61                     v2f o;
62                     o.vertex = UnityObjectToClipPos(v.vertex);
63                     o.color = v.color;
64                     o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
65                     return o;
66                 }
67 
68                 fixed4 frag( v2f i ) : COLOR
69                 {
70                     //noise effect
71                     fixed4 offsetColor1 = tex2D(_NoiseTex, i.uvmain + _Time.xz*_HeatTime);
72                     fixed4 offsetColor2 = tex2D(_NoiseTex, i.uvmain + _Time.yx*_HeatTime);
73                     i.uvmain.x += ((offsetColor1.r + offsetColor2.r) - 1) * _ForceX;
74                     i.uvmain.y += ((offsetColor1.r + offsetColor2.r) - 1) * _ForceY;
75                     return 2.0f * i.color * _TintColor * tex2D( _MainTex, i.uvmain);
76                 }
77                 ENDCG
78             }
79         }
80         // ------------------------------------------------------------------
81         // Fallback for older cards and Unity non-Pro
82         
83         SubShader
84         {
85             Blend DstColor Zero
86             Pass
87             {
88                 Name "BASE"
89                 SetTexture [_MainTex] {    combine texture }
90             }
91         }
92     }
93 }
View Code

shader使用图片为:图片

 

 

 

NGUI中 UITexture使用的shader为:ip

 1 Shader "Unlit/Premultiplied Colored"
 2 {
 3     Properties
 4     {
 5         _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
 6     }
 7 
 8     SubShader
 9     {
10         LOD 200
11 
12         Tags
13         {
14             "Queue" = "Transparent"
15             "IgnoreProjector" = "True"
16             "RenderType" = "Transparent"
17         }
18 
19         Pass
20         {
21             Cull Off
22             Lighting Off
23             ZWrite Off
24             AlphaTest Off
25             Fog { Mode Off }
26             Offset -1, -1
27             ColorMask RGB
28             Blend One OneMinusSrcAlpha
29         
30             CGPROGRAM
31             #pragma vertex vert
32             #pragma fragment frag
33             #include "UnityCG.cginc"
34 
35             sampler2D _MainTex;
36             float4 _MainTex_ST;
37 
38             struct appdata_t
39             {
40                 float4 vertex : POSITION;
41                 float2 texcoord : TEXCOORD0;
42                 half4 color : COLOR;
43             };
44 
45             struct v2f
46             {
47                 float4 vertex : POSITION;
48                 float2 texcoord : TEXCOORD0;
49                 half4 color : COLOR;
50             };
51 
52             v2f vert (appdata_t v)
53             {
54                 v2f o;
55                 o.vertex = UnityObjectToClipPos(v.vertex);
56                 o.texcoord = v.texcoord;
57                 o.color = v.color;
58                 return o;
59             }
60 
61             half4 frag (v2f IN) : COLOR
62             {
63                 half4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
64                 return col;
65             }
66             ENDCG
67         }
68     }
69     
70     SubShader
71     {
72         LOD 100
73 
74         Tags
75         {
76             "Queue" = "Transparent"
77             "IgnoreProjector" = "True"
78             "RenderType" = "Transparent"
79         }
80         
81         Pass
82         {
83             Cull Off
84             Lighting Off
85             ZWrite Off
86             AlphaTest Off
87             Fog { Mode Off }
88             Offset -1, -1
89             ColorMask RGB
90             Blend One OneMinusSrcAlpha 
91             ColorMaterial AmbientAndDiffuse
92             
93             SetTexture [_MainTex]
94             {
95                 Combine Texture * Primary
96             }
97         }
98     }
99 }
View Code

 

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

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

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

此时效果以下:

转载请注明出处:http://www.javashuo.com/article/p-dgbiqzjg-gw.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

相关文章
相关标签/搜索