Shader "UI/OuterGlowV2" { Properties { [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} _Color ("Tint", Color) = (1,1,1,1) _GlowColor ("Glow Color", Color) = (1,0.82,0.2,1) _GlowRadius ("Glow Radius (Source Pixels)", Range(0,64)) = 32 _GlowIntensity ("Glow Intensity", Range(0,8)) = 3 _GlowSoftness ("Glow Softness", Range(0,1)) = 0.75 _StencilComp ("Stencil Comparison", Float) = 8 _Stencil ("Stencil ID", Float) = 0 _StencilOp ("Stencil Operation", Float) = 0 _StencilWriteMask ("Stencil Write Mask", Float) = 255 _StencilReadMask ("Stencil Read Mask", Float) = 255 _ColorMask ("Color Mask", Float) = 15 [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0 } SubShader { Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "PreviewType" = "Plane" "CanUseSpriteAtlas" = "True" } Stencil { Ref [_Stencil] Comp [_StencilComp] Pass [_StencilOp] ReadMask [_StencilReadMask] WriteMask [_StencilWriteMask] } Cull Off Lighting Off ZWrite Off ZTest [unity_GUIZTestMode] Blend SrcAlpha OneMinusSrcAlpha ColorMask [_ColorMask] Pass { Name "UIOuterGlowV2" CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma target 3.0 #pragma multi_compile_local _ UNITY_UI_CLIP_RECT #pragma multi_compile_local _ UNITY_UI_ALPHACLIP #include "UnityCG.cginc" #include "UnityUI.cginc" struct appdata { float4 vertex : POSITION; float4 uv0 : TEXCOORD0; float4 uv1 : TEXCOORD1; // Sprite UV bounds, provided by UIOuterGlowMeshPadding fixed4 color : COLOR; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; float4 uvBounds : TEXCOORD1; fixed4 tint : COLOR; float4 localPos : TEXCOORD2; }; sampler2D _MainTex; float4 _MainTex_TexelSize; fixed4 _TextureSampleAdd; fixed4 _Color; fixed4 _GlowColor; float _GlowRadius; float _GlowIntensity; float _GlowSoftness; float4 _ClipRect; v2f vert(appdata v) { v2f o; o.localPos = v.vertex; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv0.xy; o.uvBounds = v.uv1; o.tint = v.color * _Color; return o; } float IsInsideSprite(float2 uv, float4 bounds) { float2 a = step(bounds.xy, uv); float2 b = step(uv, bounds.zw); return a.x * a.y * b.x * b.y; } fixed4 ReadSprite(float2 uv, float4 bounds) { fixed4 s = tex2D(_MainTex, uv) + _TextureSampleAdd; return s * IsInsideSprite(uv, bounds); } float SampleRing(float2 uv, float2 uvRadius, float4 bounds) { // 12 points arranged on a circle (30 degree spacing). const float2 a = float2(0.8660254, 0.5); const float2 b = float2(0.5, 0.8660254); float sum = 0; sum += ReadSprite(uv + float2( uvRadius.x, 0), bounds).a; sum += ReadSprite(uv + float2(-uvRadius.x, 0), bounds).a; sum += ReadSprite(uv + float2(0, uvRadius.y), bounds).a; sum += ReadSprite(uv + float2(0, -uvRadius.y), bounds).a; sum += ReadSprite(uv + a * uvRadius, bounds).a; sum += ReadSprite(uv - a * uvRadius, bounds).a; sum += ReadSprite(uv + float2(-a.x, a.y) * uvRadius, bounds).a; sum += ReadSprite(uv + float2( a.x,-a.y) * uvRadius, bounds).a; sum += ReadSprite(uv + b * uvRadius, bounds).a; sum += ReadSprite(uv - b * uvRadius, bounds).a; sum += ReadSprite(uv + float2(-b.x, b.y) * uvRadius, bounds).a; sum += ReadSprite(uv + float2( b.x,-b.y) * uvRadius, bounds).a; return sum * (1.0 / 12.0); } fixed4 frag(v2f i) : SV_Target { fixed4 original = ReadSprite(i.uv, i.uvBounds) * i.tint; float sourceAlpha = saturate(original.a); float2 radiusUV = _MainTex_TexelSize.xy * _GlowRadius; // Five radial bands create a broad, gradual halo. float h1 = SampleRing(i.uv, radiusUV * 0.16, i.uvBounds); float h2 = SampleRing(i.uv, radiusUV * 0.36, i.uvBounds); float h3 = SampleRing(i.uv, radiusUV * 0.58, i.uvBounds); float h4 = SampleRing(i.uv, radiusUV * 0.80, i.uvBounds); float h5 = SampleRing(i.uv, radiusUV, i.uvBounds); // Softer settings preserve more of the distant halo. float halo = h1 * 0.26 + h2 * 0.24 + h3 * 0.21 + h4 * 0.17 + h5 * 0.12; halo = pow(saturate(halo), lerp(1.6, 0.65, _GlowSoftness)); float glowAlpha = saturate(halo * _GlowIntensity * _GlowColor.a * i.tint.a); glowAlpha *= (1.0 - sourceAlpha); // Keep source sprite color intact. Glow appears behind its alpha. float outputAlpha = sourceAlpha + glowAlpha; float3 outputRGB = (original.rgb * sourceAlpha + _GlowColor.rgb * glowAlpha) / max(outputAlpha, 0.00001); #ifdef UNITY_UI_CLIP_RECT outputAlpha *= UnityGet2DClipping(i.localPos.xy, _ClipRect); #endif #ifdef UNITY_UI_ALPHACLIP clip(outputAlpha - 0.001); #endif return fixed4(outputRGB, outputAlpha); } ENDCG } } }