Shader "UI/OutlightForImage" { Properties { _MainTex("Texture", 2D) = "white" {} _LightColor("Light Color", Color) = (1,1,1,1) _Size("Glow Size", Float) = 1.0 } SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" } LOD 100 // UI-friendly settings Cull Off ZWrite Off ZTest Always Blend SrcAlpha OneMinusSrcAlpha Pass { CGPROGRAM #include "UnityCG.cginc" #pragma vertex vert #pragma fragment frag #pragma target 2.0 struct appdata_t { float4 vertex : POSITION; float2 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; sampler2D _MainTex; float4 _MainTex_TexelSize; float4 _LightColor; float _Size; v2f vert(appdata_t v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.texcoord; return o; } fixed4 frag(v2f i) : SV_Target { fixed4 color = tex2D(_MainTex, i.uv); fixed4 glow = _LightColor; float alphaSum = color.a; for (float j = 1.0; j <= _Size; j += 1.0) { alphaSum += tex2D(_MainTex, i.uv + _MainTex_TexelSize.xy * float2(j, 0)).a; alphaSum += tex2D(_MainTex, i.uv + _MainTex_TexelSize.xy * float2(-j, 0)).a; alphaSum += tex2D(_MainTex, i.uv + _MainTex_TexelSize.xy * float2(0, j)).a; alphaSum += tex2D(_MainTex, i.uv + _MainTex_TexelSize.xy * float2(0, -j)).a; } glow.a = alphaSum / (4.0 * _Size + 1.0); // 如果原图有像素就显示原图,否则显示 glow return step(0.1, color.a) * color + step(0.1, 1.0 - color.a) * glow; } ENDCG } } }