边缘发光Shader

Shader "Sprites/SpriteOuterGlow"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        _GlowScale ("Glow Scale", Range(0,1)) = 1
        _GlowColor ("Glow Color", Color) = (1,1,1,1)
        [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    }
 
    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }
 
        Cull Off
        Lighting Off
        ZWrite Off
        Blend One OneMinusSrcAlpha
 
        Pass
        {
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0
            #pragma multi_compile _ PIXELSNAP_ON
            #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
            #include "UnityCG.cginc"
           
            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
            };
 
            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                float2 texcoord  : TEXCOORD0;
            };
           
            fixed4 _Color;
 
            v2f vert(appdata_t IN)
            {
                v2f OUT;
                OUT.vertex = UnityObjectToClipPos(IN.vertex);
                OUT.texcoord = IN.texcoord;
                OUT.color = IN.color * _Color;
                #ifdef PIXELSNAP_ON
                OUT.vertex = UnityPixelSnap (OUT.vertex);
                #endif
 
                return OUT;
            }
 
            sampler2D _MainTex;
            sampler2D _AlphaTex;
 
            fixed _GlowScale;
            fixed4 _GlowColor;
 
            fixed4 SampleSpriteTexture (float2 uv)
            {
                fixed4 color = tex2D (_MainTex, uv);
 
#if ETC1_EXTERNAL_ALPHA
                // get the color from an external texture (usecase: Alpha support for ETC1 on android)
                color.a = tex2D (_AlphaTex, uv).r;
#endif //ETC1_EXTERNAL_ALPHA                
                return color;
            }
 
            fixed4 frag(v2f IN) : SV_Target
            {
                fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
 
                // Use alpha assuming glow is a gradient from 0.0 to ~0.75% alpha, and the rest is the sprite.
                fixed spriteAlpha = saturate(c.a * 4.0 - 3.0);
                fixed glowAlpha = saturate(1.0 - (1.0 - c.a / 0.75) / max(_GlowScale, 0.01)) * saturate(_GlowScale * 32.0);
                c.a = max(spriteAlpha, glowAlpha * _GlowColor.a);
                c.rgb = lerp(_GlowColor.rgb, c.rgb, 1.0 - (1.0 - saturate(spriteAlpha / max(_GlowColor.a, 0.01))) * saturate(_GlowScale * 32.0));
 
                c.rgb *= c.a;
                return c;
            }
        ENDCG
        }
    }
}
 

Unity中实现边缘发光的一种方式是使用Shader。以下是一个基础的边缘发光Shader示例: ``` Shader "Custom/EdgeGlow" { Properties { _MainTex ("Texture", 2D) = "white" {} _Threshold ("Threshold", Range(0, 1)) = 0.5 _Color ("Glow Color", Color) = (1,1,1,1) _Glow ("Glow Amount", Range(0, 1)) = 0.5 } SubShader { Tags {"Queue"="Transparent" "RenderType"="Transparent"} Pass { ZWrite off ColorMask RGB 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; float _Threshold; float4 _Color; float _Glow; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); float4 sum = 0; sum += tex2D(_MainTex, i.uv + float2(-0.01, -0.01)) - col; sum += tex2D(_MainTex, i.uv + float2( 0.00, -0.01)) - col; sum += tex2D(_MainTex, i.uv + float2( 0.01, -0.01)) - col; sum += tex2D(_MainTex, i.uv + float2(-0.01, 0.00)) - col; sum += tex2D(_MainTex, i.uv + float2( 0.01, 0.00)) - col; sum += tex2D(_MainTex, i.uv + float2(-0.01, 0.01)) - col; sum += tex2D(_MainTex, i.uv + float2( 0.00, 0.01)) - col; sum += tex2D(_MainTex, i.uv + float2( 0.01, 0.01)) - col; if (length(sum) > _Threshold) { fixed4 glow = _Color; glow.a = _Glow; return lerp(col, glow, _Glow); } return col; } ENDCG } } } ``` 在此示例中,我们使用了一个名为“_Threshold”的阈值属性,该属性定义了什么颜色被视为边缘。我们还使用了一个名为“_Color”的颜色属性,该属性定义了边缘发光的颜色。最后,我们使用了一个名为“_Glow”的属性,该属性定义了发光的强度。 在使用此Shader时,将其应用于一个具有透明度的材质上,并将其放置在需要发光的对象上即可。例如,你可以在场景中放置一个球体,并将具有此Shader的材质应用于该球体,以使其周围发出发光效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值