UIEdgeFade.shader 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. Shader "RO/UI/UIEdgeFade"
  2. {
  3. Properties
  4. {
  5. [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
  6. _Color ("Tint", Color) = (1,1,1,1)
  7. _StencilComp ("Stencil Comparison", Float) = 8
  8. _Stencil ("Stencil ID", Float) = 0
  9. _StencilOp ("Stencil Operation", Float) = 0
  10. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  11. _StencilReadMask ("Stencil Read Mask", Float) = 255
  12. _ColorMask ("Color Mask", Float) = 15
  13. [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
  14. _EdgeValue ("Edge Value", float) = 0
  15. }
  16. SubShader
  17. {
  18. Tags
  19. {
  20. "Queue"="Transparent"
  21. "IgnoreProjector"="True"
  22. "RenderType"="Transparent"
  23. "PreviewType"="Plane"
  24. "CanUseSpriteAtlas"="True"
  25. }
  26. Stencil
  27. {
  28. Ref [_Stencil]
  29. Comp [_StencilComp]
  30. Pass [_StencilOp]
  31. ReadMask [_StencilReadMask]
  32. WriteMask [_StencilWriteMask]
  33. }
  34. Cull Off
  35. Lighting Off
  36. ZWrite Off
  37. ZTest [unity_GUIZTestMode]
  38. Blend SrcAlpha OneMinusSrcAlpha
  39. ColorMask [_ColorMask]
  40. Pass
  41. {
  42. Name "Default"
  43. CGPROGRAM
  44. #pragma vertex vert
  45. #pragma fragment frag
  46. #pragma target 2.0
  47. #include "UnityCG.cginc"
  48. #include "UnityUI.cginc"
  49. #pragma multi_compile __ UNITY_UI_CLIP_RECT
  50. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  51. struct appdata_t
  52. {
  53. float4 vertex : POSITION;
  54. float4 color : COLOR;
  55. float2 texcoord : TEXCOORD0;
  56. UNITY_VERTEX_INPUT_INSTANCE_ID
  57. };
  58. struct v2f
  59. {
  60. float4 vertex : SV_POSITION;
  61. fixed4 color : COLOR;
  62. float2 texcoord : TEXCOORD0;
  63. float4 worldPosition : TEXCOORD1;
  64. UNITY_VERTEX_OUTPUT_STEREO
  65. };
  66. sampler2D _MainTex;
  67. fixed4 _Color;
  68. fixed4 _TextureSampleAdd;
  69. float4 _ClipRect;
  70. float4 _MainTex_ST;
  71. fixed _EdgeValue;
  72. v2f vert(appdata_t v)
  73. {
  74. v2f OUT;
  75. UNITY_SETUP_INSTANCE_ID(v);
  76. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
  77. OUT.worldPosition = v.vertex;
  78. OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
  79. OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  80. OUT.color = v.color * _Color;
  81. return OUT;
  82. }
  83. fixed4 frag(v2f IN) : SV_Target
  84. {
  85. half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
  86. half a = smoothstep(0, _EdgeValue, IN.texcoord.x);
  87. half b = smoothstep(1, 1 - _EdgeValue, IN.texcoord.x);
  88. half c = smoothstep(0, _EdgeValue, IN.texcoord.y);
  89. half d = smoothstep(1, 1 - _EdgeValue, IN.texcoord.y);
  90. color.a = color.a * a * b * c * d;
  91. #ifdef UNITY_UI_CLIP_RECT
  92. color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
  93. #endif
  94. #ifdef UNITY_UI_ALPHACLIP
  95. clip (color.a - 0.001);
  96. #endif
  97. return color;
  98. }
  99. ENDCG
  100. }
  101. }
  102. }