UGUI—圆形边框效果

作者:追风剑情 发布于:2024-2-1 13:33 分类:Shader

Shader "Custom/UIRoundBorder"
{
    Properties
    {
        //主纹理
        _MainTex ("Texture", 2D) = "white" {}
        //边框颜色
        _Color("Color", Color) = (1, 0, 0, 1)
        //外边缘宽度 0~0.5
        _NormalMargin("NormalMargin", Range(0, 0.5)) = 0.1
        //要挖空的内圆半径范围 0~0.5
        _NormalRadius("NormalRadius", Range(0, 0.5)) = 0.4
    }
    SubShader
    {
        Tags { "Queue" = "Transparent" "IgnoreProjector" = "true" "RenderType" = "Transparent" }
        LOD 100
        //开启Alpha混合才能调节透明度
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;
            fixed _NormalMargin;
            fixed _NormalRadius;

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

            fixed4 frag(v2f i) : SV_Target
            {
                //纹理中心
                fixed2 center = fixed2(0.5, 0.5);
                //计算uv向量
                fixed2 p = i.uv - center;
                //向量长度
                fixed len = length(p);
                //裁剪边缘外的区域
                fixed marginRadius = _NormalRadius + _NormalMargin;
                //裁剪掉不在边缘内的区域
                clip(marginRadius - len);
                clip(len - _NormalRadius);
                //加个Alpha渐变效果
                _Color.a = (marginRadius - len) / _NormalMargin;
                return _Color;
            }
            ENDCG
        }
    }
}

22222.png

11111.png

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号