shader clip function

clip function can discard some pixel when the value does not satisfy the value.
take the example, if the alpha value is lower than 0.3, we will discard it.
the function can be :
clip(xx.a - 0.3);

or the function can be :
if((xx.a < 0.3)
discard;

the source shader is:

Shader "Course/ClipFunction"
{
    Properties
    {
        _CutOff("CutOff",Range(0,1))=0
        _MainTex("MainTex",2D)="white"{}
    }

    SubShader
    {
        Pass
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            float _CutOff;
            sampler2D _MainTex;

            struct a2v
            {
                float4 localPosition:POSITION;
                float2 uv:TEXCOORD0;
            };

            struct v2f
            {
                float4 position:SV_POSITION;
                float2 uvXX:TEXCOORD0;
            };

            v2f vert(a2v a)
            {
                v2f v;
                v.position = UnityObjectToClipPos(a.localPosition);
                v.uvXX = a.uv;
                return v;
            }

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

                clip(color.a-_CutOff);
                // is equal to the following code
                //if(color.a <_CutOff)
                //{
                //  discard;
                //}
                return color;
            }

            ENDCG
        }
    }
}

then we can tune the value of _CutOff in the inspector panel.
shader clip function_第1张图片

here, we should choose a pic which has alpha channel. because the cull the pixel according to the alpha value of the texture we sampled from.
you can clip other channels such as r or g or b. and even the position of the vertex. so you can clip whatever you want.

你可能感兴趣的:(Unity,unity,shader,clip,discard,key)