关于ngui图片变灰的解决方案(包括scrollView中灰图的处理)

发表于2015-08-06
评论0 3.4k浏览
游戏中,策划大大要求,某种状态图片变灰。但是如果增加资源,内存又会加大。
上网搜了些资料,用shader变灰来处理的话,在ngui的scrollview插件中又会出界,于是细读了ngui源码。解决方案如下:
1.增加第一个Unlit/Transparent Grayed:处理灰色的shader
2.再创建一个shader,来处理scrollview出界的问题。(Unlit/Transparent Grayed1)
3.修改ngui 源码:UIDrawCall类,大概307行:
//        if(shader == nullshader = Shader.Find("Unlit/Transparent Colored");//chenjy
修改成:
        if (shader == null){
            if(shaderName == ""){
                shader = Shader.Find("Unlit/Transparent Grayed");
            }else{
                shader = Shader.Find("Unlit/Transparent Colored");
            }
        }
附shader代码1:
  Unlit/Transparent Grayed
  //---如下:
Shader "Unlit/Geometry Grayed"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)"2D) = "black" {}
    }
    
    SubShader
    {
        LOD 200

        Tags
        {
            "Queue" = "Geometry"
            "IgnoreProjector" = "True"
            "RenderType" = "Geometry"
        }
        
        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_MVPv.vertex);
                o.texcoord = v.texcoord;
                o.color = v.color;
                return o;
            }
                
            fixed4 frag (v2f IN) : COLOR
            {
                fixed4 col = tex2D(_MainTexIN.texcoord);  
                float grey = dot(col.rgbfloat3(0.2990.5870.114));  
                col.rgb = float3(greygreygrey);
                return col;
            }
            ENDCG
        }
    }

    SubShader
    {
        LOD 100

        Tags
        {
            "Queue" = "Geometry"
            "IgnoreProjector" = "True"
            "RenderType" = "Geometry"
        }
        
        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
            }
        }
    }
}


附shader代码2:
  Unlit/Transparent Grayed1
Shader "Unlit/Geometry Grayed 1"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)"2D) = "black" {}
    }
    
    SubShader
    {
        LOD 200

        Tags
        {
            "Queue" = "Geometry"
            "IgnoreProjector" = "True"
            "RenderType" = "Geometry"
        }
        
        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;
            float4 _ClipRange0 = float4(0.00.01.01.0);
            float2 _ClipArgs0 = float2(1000.01000.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_MVPv.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.01.0) - abs(IN.worldPos)) * _ClipArgs0;
            
                // Sample the texture
                half4 col = tex2D(_MainTexIN.texcoord) * IN.color;
                col.a *= clampmin(factor.xfactor.y), 0.01.0);
                float grey = dot(col.rgbfloat3(0.2990.5870.114));  
                col.rgb = float3(greygreygrey);
                return col;
            }
            
//            fixed4 frag (v2f IN) : COLOR
//            {
//                fixed4 col = tex2D(_MainTexIN.texcoord);  
//                float grey = dot(col.rgbfloat3(0.2990.5870.114));  
//                col.rgb = float3(greygreygrey);
//                return col;
//            }
            ENDCG
        }
    }

    SubShader
    {
        LOD 100

        Tags
        {
            "Queue" = "Geometry"
            "IgnoreProjector" = "True"
            "RenderType" = "Geometry"
        }
        
        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
            }
        }
    }
}

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引