UGUI——图片边缘虚化

作者:追风剑情 发布于:2024-3-12 19:35 分类:Shader


Shader "Custom/EdgeBlurImage"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color("Color", Color) = (1,1,1,1)
        _Radius("Radius", Range(0, 1)) = 0.5
    }
    SubShader
    {
        Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
        LOD 100

        Pass
        {
            Blend SrcAlpha OneMinusSrcAlpha

            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;
            float _Radius;

            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
            {
                //中心点UV坐标
                fixed2 center = fixed2(0.5, 0.5);
                //方向向量
                fixed2 dir = i.uv - center;
                //向量长度[0,1]
                fixed len = length(dir) * 2;
                //圆环宽度
                fixed cw = 1.0 - _Radius;
                //当前圆环宽度
                fixed cr = len - _Radius;
                //当前圆环宽度百分比
                fixed cl = cr / cw;
                fixed dv = step(0, cr);
                fixed a = 1 - cl * dv;

                fixed4 col = tex2D(_MainTex, i.uv) * _Color;
                col.a = a;
                return col;
            }
            ENDCG
        }
    }
}


效果

11111.png

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号