Shader画像をグレーにする

5554 ワード

参考:NGUI Sprite効果がグレーになる(ボタンが無効になっている)ソリューション
非ScrollViewでの使用
shaderは、フラグメント段階でfloat grey=dot(col.rgb,float 3(0.299,0.587,0.114))を通過する.この文は、頂点の画素を1つの値に点乗して、各頂点の画素に影響を与えて全体的な灰化の効果を示すことを意味します.NGUIのグレーコード
Shader "Unlit/Transparent Colored Gray"
{
    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 = UnityObjectToClipPos(v.vertex);
                o.texcoord = v.texcoord;
                o.color = v.color;
                return o;
            }
                
            fixed4 frag (v2f IN) : COLOR
            {
                fixed4 col = tex2D(_MainTex, IN.texcoord);  
                fixed grey = dot(col.rgb, fixed3(0.222, 0.707, 0.071));  //0.299, 0.587, 0.114
                col.rgb = fixed3(grey, grey, grey);
                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
            }
        }
    }
}

このshaderの中のコアコードはcol.rgb=dot(col.rgb,fixed 3(.222,.707,.071))しかありません.
個別のUIspriteにマテリアルを交換するには、通図セット内の他のピクチャには影響しません.
新しいXLSpriteクラスはUIspriteから継承されます
using UnityEngine;
using System.Collections;
using System;
public class XLSprite : UISprite
{
    protected UIPanel panelObj = null;
    protected Material GrayMaterial;
    /// 
    /// ngui Sprite        
    /// 
    /// The material.
    public override Material material
    {
        get
        {
            Material mat = base.material;

            if (mat == null)
            {
                mat = (atlas != null) ? atlas.spriteMaterial : null;
            }

            if (GrayMaterial != null)
            {
                return GrayMaterial;
            }
            else
            {
                return mat;
            }
        }
    }

    /// 
    ///        Sprite  
    /// 
    /// The material.
    public void SetGray()
    {
        Material mat = new Material(Shader.Find("Unlit/Transparent Colored Gray"));
        mat.mainTexture = material.mainTexture;
        GrayMaterial = mat;

        RefreshPanel(gameObject);
    }

    /// 
    ///     ,setActive       ,    。
    /// 
    /// The material.
    public void SetVisible(bool isVisible)
    {
        if (isVisible)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
        else
        {
            transform.localScale = new Vector3(0, 0, 0);
        }

    }

    /// 
    ///            ,false     
    /// 
    /// The material.
    public void SetEnabled(bool isEnabled)
    {
        if (isEnabled)
        {
            BoxCollider lisener = gameObject.GetComponent();
            if (lisener)
            {
                lisener.enabled = true;
            }

            SetNormal();
        }
        else
        {
            BoxCollider lisener = gameObject.GetComponent();
            if (lisener)
            {

                lisener.enabled = false;
            }

            SetGray();
        }
    }

    /// 
    ///  GrayMaterial  null,         ,  panel    Sprite
    /// 
    /// The material.
    public void SetNormal()
    {
        GrayMaterial = null;
        RefreshPanel(gameObject);

    }
    ///  panel,  Sprite 
    void RefreshPanel(GameObject go)
    {
        if (panelObj == null)
        {
            panelObj = NGUITools.FindInParents(go);
        }

        if (panelObj != null)
        {
            panelObj.enabled = false;
            panelObj.enabled = true;
        }
    }
}

このXLSpriteで元のSpriteを置き換えるとコードに注釈がつくのは簡単で、対応するメソッドを呼び出すことでspriteを操作できます
もう一つの記事:NGUI spriteグレーシェーダー