WaveWithSoftMask.shader 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. Shader "UI/Soft Mask/Custom Shader with Soft Mask support"
  2. {
  3. // This is an example of UI shader with Soft Mask support added. All places where
  4. // something related to Soft Mask support was added marked with comment
  5. // 'Soft Mask Support'.
  6. Properties
  7. {
  8. [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
  9. _Color("Tint", Color) = (1,1,1,1)
  10. _Amplitude("Amplitude", Float) = 0.25
  11. _Waves("Waves", Float) = 2.0
  12. _StencilComp("Stencil Comparison", Float) = 8
  13. _Stencil("Stencil ID", Float) = 0
  14. _StencilOp("Stencil Operation", Float) = 0
  15. _StencilWriteMask("Stencil Write Mask", Float) = 255
  16. _StencilReadMask("Stencil Read Mask", Float) = 255
  17. _ColorMask("Color Mask", Float) = 15
  18. [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
  19. // Soft Mask support
  20. // Soft Mask determines that shader supports soft masking by presence of this property.
  21. [PerRendererData] _SoftMask("Mask", 2D) = "white" {}
  22. }
  23. SubShader
  24. {
  25. Tags
  26. {
  27. "Queue" = "Transparent"
  28. "IgnoreProjector" = "True"
  29. "RenderType" = "Transparent"
  30. "PreviewType" = "Plane"
  31. "CanUseSpriteAtlas" = "True"
  32. }
  33. Stencil
  34. {
  35. Ref[_Stencil]
  36. Comp[_StencilComp]
  37. Pass[_StencilOp]
  38. ReadMask[_StencilReadMask]
  39. WriteMask[_StencilWriteMask]
  40. }
  41. Cull Off
  42. Lighting Off
  43. ZWrite Off
  44. ZTest[unity_GUIZTestMode]
  45. Blend SrcAlpha OneMinusSrcAlpha
  46. ColorMask[_ColorMask]
  47. Pass
  48. {
  49. CGPROGRAM
  50. #pragma vertex vert
  51. #pragma fragment frag
  52. #pragma target 3.0
  53. #include "UnityCG.cginc"
  54. #include "UnityUI.cginc"
  55. // Soft Mask Support
  56. // You also can use full path (Assets/...)
  57. #include "../../Shaders/SoftMask.cginc"
  58. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  59. // Soft Mask Support
  60. #pragma multi_compile __ SOFTMASK_SIMPLE SOFTMASK_SLICED SOFTMASK_TILED
  61. struct appdata_t
  62. {
  63. float4 vertex : POSITION;
  64. float4 color : COLOR;
  65. float2 texcoord : TEXCOORD0;
  66. };
  67. struct v2f
  68. {
  69. float4 vertex : SV_POSITION;
  70. fixed4 color : COLOR;
  71. half2 texcoord : TEXCOORD0;
  72. float4 worldPosition : TEXCOORD1;
  73. // Soft Mask Support
  74. // The number in braces determines what TEXCOORDn Soft Mask may use
  75. // (it required only one TEXCOORD).
  76. SOFTMASK_COORDS(2)
  77. };
  78. fixed4 _Color;
  79. fixed4 _TextureSampleAdd;
  80. float4 _ClipRect;
  81. float _Amplitude;
  82. float _Waves;
  83. inline float2 Wave(float2 uv)
  84. {
  85. return float2(uv.x, uv.y + sin(uv.x * _Waves + _Time.w) * _Amplitude);
  86. }
  87. inline float SmoothedClip(float2 position, float4 clipRect)
  88. {
  89. float2 inside = saturate((position.xy - clipRect.xy) * 500) * saturate((clipRect.zw - position.xy) * 200);
  90. return inside.x * inside.y;
  91. }
  92. v2f vert(appdata_t IN)
  93. {
  94. v2f OUT;
  95. OUT.worldPosition = IN.vertex;
  96. #if UNITY_VERSION >= 540
  97. OUT.vertex = UnityObjectToClipPos(IN.vertex);
  98. #else
  99. OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
  100. #endif
  101. OUT.texcoord = IN.texcoord;
  102. #ifdef UNITY_HALF_TEXEL_OFFSET
  103. OUT.vertex.xy += (_ScreenParams.zw - 1.0) * float2(-1, 1);
  104. #endif
  105. OUT.color = IN.color * _Color;
  106. SOFTMASK_CALCULATE_COORDS(OUT, IN.vertex) // Soft Mask Support
  107. return OUT;
  108. }
  109. sampler2D _MainTex;
  110. fixed4 frag(v2f IN) : SV_Target
  111. {
  112. float2 uv = Wave(IN.texcoord);
  113. half4 tex = tex2D(_MainTex, uv);
  114. tex.a *= SmoothedClip(uv, float4(0, 0, 1, 1));
  115. half4 color = (tex + _TextureSampleAdd) * IN.color;
  116. color.a *= SOFTMASK_GET_MASK(IN); // Soft Mask Support
  117. color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
  118. #ifdef UNITY_UI_ALPHACLIP
  119. clip(color.a - 0.001);
  120. #endif
  121. return color;
  122. }
  123. ENDCG
  124. }
  125. }
  126. }
  127. // UNITY_SHADER_NO_UPGRADE