OutlightForImage.shader 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. Shader "UI/OutlightForImage"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. _LightColor("Light Color", Color) = (1,1,1,1)
  7. _Size("Glow Size", Float) = 1.0
  8. }
  9. SubShader
  10. {
  11. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
  12. LOD 100
  13. // UI-friendly settings
  14. Cull Off
  15. ZWrite Off
  16. ZTest Always
  17. Blend SrcAlpha OneMinusSrcAlpha
  18. Pass
  19. {
  20. CGPROGRAM
  21. #include "UnityCG.cginc"
  22. #pragma vertex vert
  23. #pragma fragment frag
  24. #pragma target 2.0
  25. struct appdata_t {
  26. float4 vertex : POSITION;
  27. float2 texcoord : TEXCOORD0;
  28. };
  29. struct v2f {
  30. float4 pos : SV_POSITION;
  31. float2 uv : TEXCOORD0;
  32. };
  33. sampler2D _MainTex;
  34. float4 _MainTex_TexelSize;
  35. float4 _LightColor;
  36. float _Size;
  37. v2f vert(appdata_t v)
  38. {
  39. v2f o;
  40. o.pos = UnityObjectToClipPos(v.vertex);
  41. o.uv = v.texcoord;
  42. return o;
  43. }
  44. fixed4 frag(v2f i) : SV_Target
  45. {
  46. fixed4 color = tex2D(_MainTex, i.uv);
  47. fixed4 glow = _LightColor;
  48. float alphaSum = color.a;
  49. for (float j = 1.0; j <= _Size; j += 1.0)
  50. {
  51. alphaSum += tex2D(_MainTex, i.uv + _MainTex_TexelSize.xy * float2(j, 0)).a;
  52. alphaSum += tex2D(_MainTex, i.uv + _MainTex_TexelSize.xy * float2(-j, 0)).a;
  53. alphaSum += tex2D(_MainTex, i.uv + _MainTex_TexelSize.xy * float2(0, j)).a;
  54. alphaSum += tex2D(_MainTex, i.uv + _MainTex_TexelSize.xy * float2(0, -j)).a;
  55. }
  56. glow.a = alphaSum / (4.0 * _Size + 1.0);
  57. // 如果原图有像素就显示原图,否则显示 glow
  58. return step(0.1, color.a) * color + step(0.1, 1.0 - color.a) * glow;
  59. }
  60. ENDCG
  61. }
  62. }
  63. }