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.
这里写图片描述

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值