UIOuterGlowV2.shader 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. Shader "UI/OuterGlowV2"
  2. {
  3. Properties
  4. {
  5. [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
  6. _Color ("Tint", Color) = (1,1,1,1)
  7. _GlowColor ("Glow Color", Color) = (1,0.82,0.2,1)
  8. _GlowRadius ("Glow Radius (Source Pixels)", Range(0,64)) = 32
  9. _GlowIntensity ("Glow Intensity", Range(0,8)) = 3
  10. _GlowSoftness ("Glow Softness", Range(0,1)) = 0.75
  11. _StencilComp ("Stencil Comparison", Float) = 8
  12. _Stencil ("Stencil ID", Float) = 0
  13. _StencilOp ("Stencil Operation", Float) = 0
  14. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  15. _StencilReadMask ("Stencil Read Mask", Float) = 255
  16. _ColorMask ("Color Mask", Float) = 15
  17. [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
  18. }
  19. SubShader
  20. {
  21. Tags
  22. {
  23. "Queue" = "Transparent"
  24. "IgnoreProjector" = "True"
  25. "RenderType" = "Transparent"
  26. "PreviewType" = "Plane"
  27. "CanUseSpriteAtlas" = "True"
  28. }
  29. Stencil
  30. {
  31. Ref [_Stencil]
  32. Comp [_StencilComp]
  33. Pass [_StencilOp]
  34. ReadMask [_StencilReadMask]
  35. WriteMask [_StencilWriteMask]
  36. }
  37. Cull Off
  38. Lighting Off
  39. ZWrite Off
  40. ZTest [unity_GUIZTestMode]
  41. Blend SrcAlpha OneMinusSrcAlpha
  42. ColorMask [_ColorMask]
  43. Pass
  44. {
  45. Name "UIOuterGlowV2"
  46. CGPROGRAM
  47. #pragma vertex vert
  48. #pragma fragment frag
  49. #pragma target 3.0
  50. #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
  51. #pragma multi_compile_local _ UNITY_UI_ALPHACLIP
  52. #include "UnityCG.cginc"
  53. #include "UnityUI.cginc"
  54. struct appdata
  55. {
  56. float4 vertex : POSITION;
  57. float4 uv0 : TEXCOORD0;
  58. float4 uv1 : TEXCOORD1; // Sprite UV bounds, provided by UIOuterGlowMeshPadding
  59. fixed4 color : COLOR;
  60. };
  61. struct v2f
  62. {
  63. float4 pos : SV_POSITION;
  64. float2 uv : TEXCOORD0;
  65. float4 uvBounds : TEXCOORD1;
  66. fixed4 tint : COLOR;
  67. float4 localPos : TEXCOORD2;
  68. };
  69. sampler2D _MainTex;
  70. float4 _MainTex_TexelSize;
  71. fixed4 _TextureSampleAdd;
  72. fixed4 _Color;
  73. fixed4 _GlowColor;
  74. float _GlowRadius;
  75. float _GlowIntensity;
  76. float _GlowSoftness;
  77. float4 _ClipRect;
  78. v2f vert(appdata v)
  79. {
  80. v2f o;
  81. o.localPos = v.vertex;
  82. o.pos = UnityObjectToClipPos(v.vertex);
  83. o.uv = v.uv0.xy;
  84. o.uvBounds = v.uv1;
  85. o.tint = v.color * _Color;
  86. return o;
  87. }
  88. float IsInsideSprite(float2 uv, float4 bounds)
  89. {
  90. float2 a = step(bounds.xy, uv);
  91. float2 b = step(uv, bounds.zw);
  92. return a.x * a.y * b.x * b.y;
  93. }
  94. fixed4 ReadSprite(float2 uv, float4 bounds)
  95. {
  96. fixed4 s = tex2D(_MainTex, uv) + _TextureSampleAdd;
  97. return s * IsInsideSprite(uv, bounds);
  98. }
  99. float SampleRing(float2 uv, float2 uvRadius, float4 bounds)
  100. {
  101. // 12 points arranged on a circle (30 degree spacing).
  102. const float2 a = float2(0.8660254, 0.5);
  103. const float2 b = float2(0.5, 0.8660254);
  104. float sum = 0;
  105. sum += ReadSprite(uv + float2( uvRadius.x, 0), bounds).a;
  106. sum += ReadSprite(uv + float2(-uvRadius.x, 0), bounds).a;
  107. sum += ReadSprite(uv + float2(0, uvRadius.y), bounds).a;
  108. sum += ReadSprite(uv + float2(0, -uvRadius.y), bounds).a;
  109. sum += ReadSprite(uv + a * uvRadius, bounds).a;
  110. sum += ReadSprite(uv - a * uvRadius, bounds).a;
  111. sum += ReadSprite(uv + float2(-a.x, a.y) * uvRadius, bounds).a;
  112. sum += ReadSprite(uv + float2( a.x,-a.y) * uvRadius, bounds).a;
  113. sum += ReadSprite(uv + b * uvRadius, bounds).a;
  114. sum += ReadSprite(uv - b * uvRadius, bounds).a;
  115. sum += ReadSprite(uv + float2(-b.x, b.y) * uvRadius, bounds).a;
  116. sum += ReadSprite(uv + float2( b.x,-b.y) * uvRadius, bounds).a;
  117. return sum * (1.0 / 12.0);
  118. }
  119. fixed4 frag(v2f i) : SV_Target
  120. {
  121. fixed4 original = ReadSprite(i.uv, i.uvBounds) * i.tint;
  122. float sourceAlpha = saturate(original.a);
  123. float2 radiusUV = _MainTex_TexelSize.xy * _GlowRadius;
  124. // Five radial bands create a broad, gradual halo.
  125. float h1 = SampleRing(i.uv, radiusUV * 0.16, i.uvBounds);
  126. float h2 = SampleRing(i.uv, radiusUV * 0.36, i.uvBounds);
  127. float h3 = SampleRing(i.uv, radiusUV * 0.58, i.uvBounds);
  128. float h4 = SampleRing(i.uv, radiusUV * 0.80, i.uvBounds);
  129. float h5 = SampleRing(i.uv, radiusUV, i.uvBounds);
  130. // Softer settings preserve more of the distant halo.
  131. float halo = h1 * 0.26 + h2 * 0.24 + h3 * 0.21 + h4 * 0.17 + h5 * 0.12;
  132. halo = pow(saturate(halo), lerp(1.6, 0.65, _GlowSoftness));
  133. float glowAlpha = saturate(halo * _GlowIntensity * _GlowColor.a * i.tint.a);
  134. glowAlpha *= (1.0 - sourceAlpha);
  135. // Keep source sprite color intact. Glow appears behind its alpha.
  136. float outputAlpha = sourceAlpha + glowAlpha;
  137. float3 outputRGB = (original.rgb * sourceAlpha + _GlowColor.rgb * glowAlpha)
  138. / max(outputAlpha, 0.00001);
  139. #ifdef UNITY_UI_CLIP_RECT
  140. outputAlpha *= UnityGet2DClipping(i.localPos.xy, _ClipRect);
  141. #endif
  142. #ifdef UNITY_UI_ALPHACLIP
  143. clip(outputAlpha - 0.001);
  144. #endif
  145. return fixed4(outputRGB, outputAlpha);
  146. }
  147. ENDCG
  148. }
  149. }
  150. }