Shader学习19——2DUI描边

这个shader也是好多年前摸索着写的了,主要针对一些2D透明图片做描边效果,现在用起来居然感觉还不错,这里也发出来一下

Shader "Custom/OutterGlow"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        _Color("Color", Color) = (1,1,1,1)
        _Factor("Factor", Range(0, 10)) = 1
        _TexSize("Texture Size", vector) = (256,256,0,0)
        
        _radius("_radius", Range(0, 20)) = 5
    }

    SubShader
    {
        LOD 200

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }

        Pass
        {
            Cull back
            Lighting Off
            ZWrite Off
            Offset -1, -1
            Fog { Mode Off }

            Blend SrcAlpha OneMinusSrcAlpha
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #pragma target 3.0

            sampler2D _MainTex;
            float4 _Color;
            float _Factor;
            float2 _TexSize;
            float _radius;

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
                fixed4 color : COLOR;
            };
    
            struct v2f
            {
                float4 vertex : SV_POSITION;
                half2 uv : TEXCOORD0;
                fixed4 color : COLOR;
            };

            v2f vert(appdata_t v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP,v.vertex);
                o.uv = v.texcoord;
                o.color = v.color;
                return o;
            }

            half4 frag(v2f i) : COLOR
            {
                float radiusX = _radius / _TexSize.x;
                float radiusY = _radius / _TexSize.y;
                float inner = 0;
                float outter = 0;
                int count = 0;
                float4 m;
                for (int k = -5; k <= 5; ++k)
                {
                    for (int j = -5; j <= 5; ++j)
                    {
                        m = tex2D(_MainTex, float2(i.uv.x + k*radiusX , i.uv.y + j*radiusY));
                        outter += 1 - m.a;
                        inner += m.a;
                        count += 1;
                    }
                }
                inner /= count;
                outter /= count;
                
                float4 col = tex2D(_MainTex, i.uv) * i.color;
                float out_alpha = max(col.a, inner);


                   col.rgb = col.rgb + (1-col.a) * _Factor * _Color.a * _Color.rgb;

                col.a = out_alpha;
                return col;
            }
            ENDCG
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值