【Shader】Shader实现头像遮罩效果

原文地址:http://www.manew.com/thread-104374-1-1.html

在Unity中有个Mask遮罩逐渐,使用这个组件也可以实现遮罩效果
但是我在使用Unity自带的圆形图片时候效果如下:

Shader "LJL/Cut"
{
        Properties 
        {
                _Radius("_Radius",Range(0,1.2))=0.5
                _MainTex("_MainTex",2D)="white"{}
        }
        SubShader 
        {
                Tags{ "Queue"="Transparent" "IngnoreProjector"="True" "RenderType"="Transparent" }
 
                Pass 
                {
 
                        Tags{ "LightMode" = "ForwardBase" }
 
                        //必须加入才能实现透明效果
                        ZWrite Off
                        Blend SrcAlpha OneMinusSrcAlpha
 
                        CGPROGRAM
 
                        #include "UnityCG.cginc"
                        #include "Lighting.cginc"  
 
                        #pragma vertex vert
                        #pragma fragment frag
 
                        sampler2D _MainTex;
                        fixed4 _MainTex_ST;
                        float _Radius;
 
                        struct a2v
                        {
                                float4 position:POSITION ;
                                float4 texcoord:TEXCOORD0 ; 
                        };
 
                        struct v2f
                        {
                                float4 position:SV_POSITION ;
                                float2 texcoord:TEXCOORD0 ; 
                        };
 
                        v2f vert(a2v v)
                        {
                                v2f f;
                                f.position=mul(UNITY_MATRIX_MVP,v.position) ;
 
                                //获取该顶点下的纹理坐标
                                f.texcoord=v.texcoord.xy*_MainTex_ST.xy+_MainTex_ST.zw;
 
                                return f;
                        }
 
                        fixed4 frag(v2f f):SV_Target
                        {
                                //获取纹理坐标下的颜色值
                                fixed4 color = tex2D(_MainTex,f.texcoord);
 
                                //左上方区域
                                if(f.texcoord.x<0.5 && f.texcoord.y>0.5)
                                {
                                        float2 r;
                                        r.x=0.5-f.texcoord.x;
                                        r.y=f.texcoord.y-0.5;
                                        if(length(r)>_Radius)//以r.x、r.y为两直角边长度,计算斜边长度
                                        {
                                                return fixed4(1,1,1,0);
                                        }
                                        else
                                        {
                                                return color;
                                        }
                                }
                                //左下方区域
                                else if(f.texcoord.x<0.5 && f.texcoord.y<0.5)
                                {
                                        float2 r;
                                        r.x=0.5-f.texcoord.x;
                                        r.y=0.5-f.texcoord.y;
                                        if(length(r)>_Radius)
                                        {
                                                return fixed4(1,1,1,0);
                                        }
                                        else
                                        {
                                                return color;
                                        }
                                }
                                //右上方区域
                                else if(f.texcoord.x>0.5 && f.texcoord.y>0.5)
                                {
                                        float2 r;
                                        r.x=f.texcoord.x-0.5;
                                        r.y=f.texcoord.y-0.5;
                                        if(length(r)>_Radius)
                                        {
                                                return fixed4(1,1,1,0);
                                        }
                                        else
                                        {
                                                return color;
                                        }
                                }
                                //右下方区域
                                else if(f.texcoord.x>0.5 && f.texcoord.y<0.5)
                                {
                                        float2 r;
                                        r.x=f.texcoord.x-0.5;
                                        r.y=0.5-f.texcoord.y;
                                        if(length(r)>_Radius)
                                        {
                                                return fixed4(1,1,1,0);
                                        }
                                        else
                                        {
                                                return color;
                                        }
                                }
 
                                return color;
                        }
 
                        ENDCG
                }
        }
 
        Fallback "Diffuse"
}



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值