SpriteOutline.shader 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. Shader "Hidden/2D-Animation-SpriteOutline"
  2. {
  3. Properties
  4. {
  5. _MainTex("Sprite Texture", 2D) = "white" {}
  6. _OutlineSize("Outline Size", Float) = 1
  7. _OutlineColor("Outline Color", Color) = (1,0,1,1)
  8. }
  9. SubShader
  10. {
  11. Tags
  12. {
  13. "Queue" = "Transparent"
  14. "IgnoreProjector" = "True"
  15. "RenderType" = "Transparent"
  16. }
  17. Cull Off
  18. Lighting Off
  19. ZWrite Off
  20. Blend SrcAlpha OneMinusSrcAlpha
  21. Pass
  22. {
  23. CGPROGRAM
  24. #pragma vertex vert
  25. #pragma fragment frag
  26. #include "UnityCG.cginc"
  27. struct appdata_t
  28. {
  29. float4 vertex : POSITION;
  30. float4 color : COLOR;
  31. float2 texcoord : TEXCOORD0;
  32. };
  33. struct v2f
  34. {
  35. float4 vertex : SV_POSITION;
  36. fixed4 color : COLOR;
  37. float2 texcoord : TEXCOORD0;
  38. float2 clipUV : TEXCOORD1;
  39. };
  40. float _Outline;
  41. fixed4 _OutlineColor;
  42. uniform float4 _MainTex_ST;
  43. uniform float4x4 unity_GUIClipTextureMatrix;
  44. uniform bool _AdjustLinearForGamma;
  45. sampler2D _GUIClipTexture;
  46. v2f vert(appdata_t IN)
  47. {
  48. v2f OUT;
  49. OUT.vertex = UnityObjectToClipPos(IN.vertex);
  50. OUT.texcoord = TRANSFORM_TEX(IN.texcoord, _MainTex);
  51. OUT.color = IN.color;
  52. float3 eyePos = UnityObjectToViewPos(IN.vertex);
  53. OUT.clipUV = mul(unity_GUIClipTextureMatrix, float4(eyePos.xy, 0, 1.0));
  54. return OUT;
  55. }
  56. sampler2D _MainTex;
  57. sampler2D _AlphaTex;
  58. float4 _MainTex_TexelSize;
  59. fixed _OutlineSize;
  60. fixed4 _ObjectSize;
  61. fixed4 frag(v2f i) : SV_Target
  62. {
  63. if (tex2D(_GUIClipTexture, i.clipUV).a == 0)
  64. discard;
  65. float width = _OutlineSize*_MainTex_TexelSize.x;
  66. float height = _OutlineSize*_MainTex_TexelSize.y;
  67. float2 texPos = i.texcoord + float2(-width, -height);
  68. half a1 = texPos.x >= 0 && texPos.y >= 0 && texPos.x <= 1 && texPos.y <= 1 ? tex2D(_MainTex, texPos).a : 0;
  69. texPos = i.texcoord + float2(0, -height);
  70. half a2 = texPos.x >= 0 && texPos.y >= 0 && texPos.x <= 1 && texPos.y <= 1 ? tex2D(_MainTex, texPos).a : 0;
  71. texPos = i.texcoord + float2(+width, -height);
  72. half a3 = texPos.x >= 0 && texPos.y >= 0 && texPos.x <= 1 && texPos.y <= 1 ? tex2D(_MainTex, texPos).a : 0;
  73. texPos = i.texcoord + float2(-width, 0);
  74. half a4 =texPos.x >= 0 && texPos.y >= 0 && texPos.x <= 1 && texPos.y <= 1 ? tex2D(_MainTex, texPos).a : 0;
  75. texPos = i.texcoord + float2(+width, 0);
  76. half a6 = texPos.x >= 0 && texPos.y >= 0 && texPos.x <= 1 && texPos.y <= 1 ? tex2D(_MainTex, texPos).a : 0;
  77. texPos = i.texcoord + float2(-width, +height);
  78. half a7 = texPos.x >= 0 && texPos.y >= 0 && texPos.x <= 1 && texPos.y <= 1 ? tex2D(_MainTex, texPos).a : 0;
  79. texPos = i.texcoord + float2(0, +height);
  80. half a8 = texPos.x >= 0 && texPos.y >= 0 && texPos.x <= 1 && texPos.y <= 1 ? tex2D(_MainTex, texPos).a : 0;
  81. texPos = i.texcoord + float2(+width, +height);
  82. half a9 = texPos.x >= 0 && texPos.y >= 0 && texPos.x <= 1 && texPos.y <= 1 ? tex2D(_MainTex, texPos).a : 0;
  83. half gx = -a1 - a2 - a3 + a7 + a8 + a9;
  84. half gy = -a1 - a4 - a7 + a3 + a6 + a9;
  85. half w = sqrt(gx * gx + gy * gy) * 1.25;
  86. float4 c = _OutlineColor;
  87. if (w >= 1)
  88. {
  89. if (_AdjustLinearForGamma)
  90. c.rgb = LinearToGammaSpace(c.rgb);
  91. }
  92. else
  93. discard;
  94. return c;
  95. }
  96. ENDCG
  97. }
  98. }
  99. }