NGUI配合shader,把精灵图片置灰

17 篇文章 79 订阅
3 篇文章 0 订阅

方法一

图集的材质球的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.cs

Color32 drawingColor
{
    get
    {
        ...

        if(m_grayState)
            colF.a =0;
            
        return colF;
    }
}

然后再改一下UIWidgetInspetor

//UIBasicSprite.cs

static 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
            }
        }
    }
}

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林新发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值