UGUI 图片将其他颜色去除,显示灰色图片
本例适用于 UGUI
一般项目中会有一些图标,当你拥有该图标显示为彩色图标,一般项目中最占资源的就是图片,不允许添加一张彩色图片,一张灰色图片,必须通过一些方法将彩色图标的颜色去掉,获取图标的灰色值。
UGUI 自带了一个Shader, Sprites/Default, 默认给了一个添加该Shader 的材质球
1 Shader "UISprites/DefaultGray" 2 { 3 Properties 4 { 5 [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} 6 _Color ("Tint", Color) = (1,1,1,1) 7 [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 8 9 //---Add--- 10 // Change the brightness of the Sprite 11 _GrayScale ("GrayScale", Float) = 1 12 //---Add--- 13 } 14 15 SubShader 16 { 17 Tags 18 { 19 "Queue"="Transparent" 20 "IgnoreProjector"="True" 21 "RenderType"="Transparent" 22 "PreviewType"="Plane" 23 "CanUseSpriteAtlas"="True" 24 } 25 26 Cull Off 27 Lighting Off 28 ZWrite Off 29 Blend One OneMinusSrcAlpha 30 31 Pass 32 { 33 CGPROGRAM 34 #pragma vertex vert 35 #pragma fragment frag 36 #pragma multi_compile _ PIXELSNAP_ON 37 #include "UnityCG.cginc" 38 39 struct appdata_t 40 { 41 float4 vertex : POSITION; 42 float4 color : COLOR; 43 float2 texcoord : TEXCOORD0; 44 }; 45 46 struct v2f 47 { 48 float4 vertex : SV_POSITION; 49 fixed4 color : COLOR; 50 half2 texcoord : TEXCOORD0; 51 }; 52 53 fixed4 _Color; 54 55 v2f vert(appdata_t IN) 56 { 57 v2f OUT; 58 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex); 59 OUT.texcoord = IN.texcoord; 60 OUT.color = IN.color * _Color; 61 #ifdef PIXELSNAP_ON 62 OUT.vertex = UnityPixelSnap (OUT.vertex); 63 #endif 64 65 return OUT; 66 } 67 68 sampler2D _MainTex; 69 float _GrayScale; 70 71 fixed4 frag(v2f IN) : SV_Target 72 { 73 fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color; 74 75 //---Add-- 76 float cc = (c.r * 0.299 + c.g * 0.518 + c.b * 0.184); 77 cc *= _GrayScale; 78 c.r = c.g = c.b = cc; 79 //---Add-- 80 81 c.rgb *= c.a; 82 return c; 83 } 84 ENDCG 85 } 86 } 87 }
此博客出自TaoHu,转载请注明原文地址 |
博客地址:http://www.cnblogs.com/htwzl/ |
-- Unity3D 学习交流 --