NGUI配合shader,把精灵图片置灰
发布日期:2021-06-30 19:40:29 浏览次数:2 分类:技术文章

本文共 8688 字,大约阅读时间需要 28 分钟。

方法一

图集的材质球的shader使用下面的shader脚本

Shader "Unlit/Transparent Colored (AlphaClip_r) "{    Properties    {        _MainTex ("Base (RGB)", 2D) = "white" {}        _AlphaTex ("Trans (A)", 2D) = "white" {}    }    SubShader    {        LOD 100        Tags        {            "Queue" = "Transparent"            "IgnoreProjector" = "True"            "RenderType" = "Transparent"        }        Pass        {            Cull Off            Lighting Off            ZWrite Off            Offset -1, -1            Fog { Mode Off }            ColorMask RGB            Blend SrcAlpha OneMinusSrcAlpha            CGPROGRAM            #pragma vertex vert            #pragma fragment frag            #include "UnityCG.cginc"            sampler2D _MainTex,_AlphaTex;            float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0);            float2 _ClipArgs0 = float2(1000.0, 1000.0);            struct appdata_t            {                float4 vertex : POSITION;                half4 color : COLOR;                float2 texcoord : TEXCOORD0;            };            struct v2f            {                float4 vertex : POSITION;                half4 color : COLOR;                float2 texcoord : TEXCOORD0;                float2 worldPos : TEXCOORD1;            };            v2f o;            v2f vert (appdata_t v)            {                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);                o.color = v.color;                o.texcoord = v.texcoord;                o.worldPos = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy;                return o;            }            half4 frag (v2f IN) : COLOR            {                // Softness factor                float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos)) * _ClipArgs0;                            // Sample the texture                //half4 col = half4(tex2D(_MainTex, IN.texcoord).xyz,tex2D(_AlphaTex, IN.texcoord).r) * IN.color;                                half4 col = half4(tex2D(_MainTex, IN.texcoord).xyz, tex2D(_AlphaTex, IN.texcoord).r);                //half a = IN.color.a;                if (IN.color.a == 0)                {                    col.rgb = Luminance(col.rgb);                }                else                {                    col *= IN.color;                }                /*                //step(a, x) x < 0 = 0, x >= 0 1                 //用step 优化掉if                half a = IN.color.a;                half4 grayColor = Luminance(col.rgb);                fixed stepV = step(0.000001, a);                col = IN.color * col * stepV + half4(grayColor.rgb, col.a) * (1 - stepV);                */                col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0);                return col;            }            ENDCG        }    }    SubShader    {        LOD 50        Tags        {            "Queue" = "Transparent"            "IgnoreProjector" = "True"            "RenderType" = "Transparent"        }        Pass        {            Cull Off            Lighting Off            ZWrite Off            Offset -1, -1            Fog { Mode Off }            ColorMask RGB            Blend SrcAlpha OneMinusSrcAlpha            CGPROGRAM            #pragma vertex vert            #pragma fragment frag            #include "UnityCG.cginc"            sampler2D _MainTex,_AlphaTex;            float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0);            float2 _ClipArgs0 = float2(1000.0, 1000.0);            struct appdata_t            {                float4 vertex : POSITION;                half4 color : COLOR;                float2 texcoord : TEXCOORD0;            };            struct v2f            {                float4 vertex : POSITION;                fixed4 color : COLOR;                float2 texcoord : TEXCOORD0;                float2 worldPos : TEXCOORD1;            };            v2f o;            v2f vert (appdata_t v)            {                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);                o.color = v.color;                o.texcoord = v.texcoord;                o.worldPos = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy;                return o;            }            fixed4 frag (v2f IN) : COLOR            {                // Softness factor                float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos)) * _ClipArgs0;                            // Sample the texture                //fixed4 col = fixed4(tex2D(_MainTex, IN.texcoord).xyz,tex2D(_AlphaTex, IN.texcoord).r) * IN.color;                fixed4 col = fixed4(tex2D(_MainTex, IN.texcoord).xyz, tex2D(_AlphaTex, IN.texcoord).r);                if (IN.color.a == 0)                {                    col.rgb = Luminance(col.rgb);                }                else                {                    col *= IN.color;                }                /*                half a = IN.color.a;                half4 grayColor = Luminance(col.rgb);                fixed stepV = step(0.000001, a);                col = IN.color * col * stepV + half4(grayColor.rgb, col.a) * (1 - stepV);                */                col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0);                return col;            }            ENDCG        }    }    }

接着,在UIWidget类中,新增m_grayState变量和grayState属性

//UIWidget.cs[SerializeField]protected bool m_grayState = false;public bool grayState{    get { return m_grayState; }    set    {        if(m_grayState != value)        {            m_grayState = value;            MarkAsChanged();        }    }}

然后再UIBasicSprite类中,修改drawingColor属性

//UIBasicSprite.csColor32 drawingColor{    get    {        ...        if(m_grayState)            colF.a =0;                    return colF;    }}

然后再改一下UIWidgetInspetor

//UIBasicSprite.csstatic public void DrawInspectorProperties(SerializedObject so, UIWidget w, bool drawColor){    ...    if(NGUIEditorTools.DrawHeader("Widget"))    {        ...        SerializedProperty graySpPre = so.FindProperty("m_grayState");        bool preGrayValue = graySpPre.boolValue;        SerializedProperty graySp = NGUIEditorTools.DrawProperty("Gray", so, "m_grayState");        if(graySp.boolValue != preGrayValue)        {            w.MarkAsChanged();        }        NGUIEditorTools.SetLabelWidth(80f);                NGUIEditorTools.EndContents();    }}

效果如下:


方法二

图集的材质球的shader使用下面的shader脚本,当精灵的颜色设置成全黑的时候,就会自动变灰色

Shader "Unlit/Transparent Colored"{    Properties    {        _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}    }        SubShader    {        LOD 200        Tags        {            "Queue" = "Transparent"            "IgnoreProjector" = "True"            "RenderType" = "Transparent"        }                Pass        {            Cull Off            Lighting Off            ZWrite Off            Fog { Mode Off }            Offset -1, -1            Blend SrcAlpha 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;                fixed4 color : COLOR;            };                struct v2f            {                float4 vertex : SV_POSITION;                half2 texcoord : TEXCOORD0;                fixed4 color : COLOR;            };                v2f o;            v2f vert (appdata_t v)            {                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);                o.texcoord = v.texcoord;                o.color = v.color;                return o;            }                            fixed4 frag (v2f IN) : COLOR            {                fixed4 col;                if (IN.color.r < 0.001)                  {                      col = tex2D(_MainTex, IN.texcoord);                      float grey = dot(col.rgb, float3(0.299, 0.587, 0.114));                      col.rgb = float3(grey, grey, grey);                  }                  else                  {                      col = tex2D(_MainTex, IN.texcoord) * IN.color;                  }                  return col;             }            ENDCG        }    }    SubShader    {        LOD 100        Tags        {            "Queue" = "Transparent"            "IgnoreProjector" = "True"            "RenderType" = "Transparent"        }                Pass        {            Cull Off            Lighting Off            ZWrite Off            Fog { Mode Off }            Offset -1, -1            ColorMask RGB            Blend SrcAlpha OneMinusSrcAlpha            ColorMaterial AmbientAndDiffuse                        SetTexture [_MainTex]            {                Combine Texture * Primary            }        }    }}

 

转载地址:https://linxinfa.blog.csdn.net/article/details/86667423 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:NGUI的UIPanel双层裁剪
下一篇:NGUI的UIPanel的Depth改良:二级排序

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年05月03日 06时44分55秒