Guest User

SpriteOutline.shader

a guest
Aug 21st, 2018
6,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. Shader "Sprites/Outline"
  2. {
  3. Properties
  4. {
  5. [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
  6. _Color ("Tint", Color) = (1,1,1,1)
  7. _Brightness("Outline Brightness", Range(0,8)) = 3.2
  8. _Width("Outline Width", Range(0,0.05)) = 0.003
  9. _OutlineTex ("Outline Texture", 2D) = "white" {}
  10. _SpeedX("Scroll Speed X", Range(-20,20)) = 0.003
  11. _SpeedY("Scroll Speed Y", Range(-20,20)) = 0.003
  12. _OutlineColor("OutlineColor", Color) = (1,1,1,1)
  13. [MaterialToggle] JustOutline ("JustOutline", Float) = 0
  14. [MaterialToggle] TexturedOutline ("TexturedOutline", Float) = 0
  15. // stencil for (UI) Masking
  16. _StencilComp ("Stencil Comparison", Float) = 8
  17. _Stencil ("Stencil ID", Float) = 0
  18. _StencilOp ("Stencil Operation", Float) = 0
  19. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  20. _StencilReadMask ("Stencil Read Mask", Float) = 255
  21. _ColorMask ("Color Mask", Float) = 15
  22. }
  23.  
  24. SubShader
  25. {
  26. Tags
  27. {
  28. "Queue"="Transparent"
  29. "IgnoreProjector"="True"
  30. "RenderType"="Transparent"
  31. "PreviewType"="Plane"
  32. "CanUseSpriteAtlas"="True"
  33. }
  34. // stencil for (UI) Masking
  35. Stencil
  36. {
  37. Ref [_Stencil]
  38. Comp [_StencilComp]
  39. Pass [_StencilOp]
  40. ReadMask [_StencilReadMask]
  41. WriteMask [_StencilWriteMask]
  42. }
  43. Cull Off
  44. Lighting Off
  45. ZWrite Off
  46. Blend One OneMinusSrcAlpha
  47.  
  48.  
  49. Pass
  50. {
  51. CGPROGRAM
  52. #pragma vertex vert
  53. #pragma fragment frag
  54. #pragma multi_compile _ JUSTOUTLINE_ON
  55. #pragma multi_compile _ TEXTUREDOUTLINE_ON
  56. #include "UnityCG.cginc"
  57.  
  58. struct appdata_t
  59. {
  60. float4 vertex : POSITION;
  61. float4 color : COLOR;
  62. float2 texcoord : TEXCOORD0;
  63. };
  64.  
  65. struct v2f
  66. {
  67. float4 vertex : SV_POSITION;
  68. fixed4 color : COLOR;
  69. float2 texcoord : TEXCOORD0;
  70. };
  71.  
  72.  
  73. v2f vert(appdata_t IN)
  74. {
  75. v2f OUT;
  76. UNITY_INITIALIZE_OUTPUT(v2f,OUT)
  77. OUT.vertex = UnityObjectToClipPos(IN.vertex);
  78. OUT.texcoord = IN.texcoord;
  79. return OUT;
  80. }
  81.  
  82. fixed4 _Color;
  83. fixed4 _OutlineColor;
  84. sampler2D _MainTex;
  85. sampler2D _OutlineTex;
  86. float _Brightness;
  87. float _Width;
  88. float _SpeedX, _SpeedY;
  89.  
  90. fixed4 frag(v2f IN) : SV_Target
  91. {
  92. fixed4 c = tex2D(_MainTex, IN.texcoord) * _Color;
  93. fixed4 t = tex2D(_OutlineTex, float2(IN.texcoord.x + (_Time.x * _SpeedX), IN.texcoord.y + (_Time.x * _SpeedY))) * _Color;
  94. c.rgb *= c.a;
  95.  
  96. // Move sprite in 4 directions according to width, we only care about the alpha
  97. float spriteLeft = tex2D(_MainTex, IN.texcoord + float2(_Width, 0)).a;
  98. float spriteRight = tex2D(_MainTex, IN.texcoord - float2(_Width, 0)).a;
  99. float spriteBottom = tex2D(_MainTex, IN.texcoord + float2( 0 ,_Width)).a;
  100. float spriteTop = tex2D(_MainTex, IN.texcoord - float2( 0 , _Width)).a;
  101.  
  102. // then combine
  103. float result = (spriteRight + spriteLeft + spriteTop+ spriteBottom);
  104. // delete original alpha to only leave outline
  105. result *= (1-c.a);
  106. // add color and brightness
  107. float4 outlines = result * _OutlineColor* _Brightness;
  108.  
  109. #ifdef TEXTUREDOUTLINE_ON
  110. outlines *= t;
  111. #endif
  112. #ifdef JUSTOUTLINE_ON
  113. // only show outlines
  114. c = outlines;
  115. #else
  116. // show outlines +sprite
  117. c.rgb = c.rgb + outlines;
  118. #endif
  119.  
  120. return c ;
  121. }
  122. ENDCG
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment