Color Mask解釋,見unity文檔:html
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 }
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 }
只因此出現黑色,這裏有兩層混合,一層是攝像機中此粒子與攝像機默認顏色,由於此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,因此黑色在混合時消失了,其它顏色保留下來了,且能與背景很好地融合。
資源以下: