SpriteOutline.shader 4.1 KB

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