RectMask2D对UI的支持
Shader "chicai/UIRectClip"
{
    Properties
    {
        [PerRendererData]_MainTex ("MainTex", 2D) = "white" {}
        _Ref("Ref",int) = 0
        [Enum(UnityEngine.Rendering.CompareFunction)]_StencilComp("Stencil Comp",int) = 0
        [Enum(UnityEngine.Rendering.StencilOp)]_StencilOp("Stencil OP",int) = 0
    }

    SubShader
    {
        Tags { "Queue"="Transparent" "RenderType"="Transparent" }
        LOD 100
        Blend SrcAlpha OneMinusSrcAlpha
        ColorMask RGBA

        Stencil
        {
            Ref [_Ref]
            ReadMask 3
            Comp [_StencilComp]
            Pass [_StencilOp]
        }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #pragma multi_compile _ UNITY_UI_CLIP_RECT//内置的宏定义,有RectMask2D会自动对它修改

            #include "UnityCG.cginc"
            #include "UnityUI.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                fixed4 color : COLOR;//UI的颜色 传过来的是顶点颜色 
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                fixed4 color : COLOR;
                fixed4 worldPos : TEXCOORD1;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _ClipRect;//内置变量 RectMask2D组件

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.color = v.color;
                o.worldPos = v.vertex;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                col *= i.color;

                #if UNITY_UI_CLIP_RECT
                    fixed2 rect = clip(_ClipRect.xy, i.worldPos.xy) * clip(i.worldPos.xy, _ClipRect.zw);
                    col.a *= rect.x * rect.y;
                    //col.a *= UnityGet2DClipping(i.worldPos, _ClipRect);
                #endif

                return col;
            }
            ENDCG
        }
    }
}



首页 我的博客
粤ICP备17103704号