SoftMaskTemplate.cginc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Template for SoftMask.shader and SoftMaskETC1.shader.
  2. // The SOFTMASK_ETC1 macro defines whether alpha split texture should be supported.
  3. #include "UnityCG.cginc"
  4. #include "UnityUI.cginc"
  5. #include "SoftMask.cginc"
  6. struct appdata_t
  7. {
  8. float4 vertex : POSITION;
  9. float4 color : COLOR;
  10. float2 texcoord : TEXCOORD0;
  11. #if UNITY_VERSION >= 550
  12. UNITY_VERTEX_INPUT_INSTANCE_ID
  13. #endif
  14. };
  15. struct v2f
  16. {
  17. float4 vertex : SV_POSITION;
  18. fixed4 color : COLOR;
  19. float2 texcoord : TEXCOORD0;
  20. float4 worldPosition : TEXCOORD1;
  21. #if UNITY_VERSION >= 202000
  22. half4 mask : TEXCOORD2;
  23. SOFTMASK_COORDS(3)
  24. #endif
  25. #if UNITY_VERSION >= 550
  26. UNITY_VERTEX_OUTPUT_STEREO
  27. #endif
  28. #if UNITY_VERSION < 202000
  29. SOFTMASK_COORDS(2)
  30. #endif
  31. };
  32. fixed4 _Color;
  33. fixed4 _TextureSampleAdd;
  34. float4 _ClipRect;
  35. sampler2D _MainTex;
  36. #ifdef SOFTMASK_ETC1
  37. sampler2D _AlphaTex;
  38. #endif
  39. #if UNITY_VERSION >= 201800
  40. float4 _MainTex_ST;
  41. #endif
  42. #if UNITY_VERSION >= 202000
  43. float _MaskSoftnessX;
  44. float _MaskSoftnessY;
  45. #endif
  46. v2f vert(appdata_t IN)
  47. {
  48. v2f OUT;
  49. #if UNITY_VERSION >= 550
  50. UNITY_SETUP_INSTANCE_ID(IN);
  51. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
  52. #endif
  53. OUT.worldPosition = IN.vertex;
  54. #if UNITY_VERSION >= 202000
  55. float4 vPosition = UnityObjectToClipPos(IN.vertex);
  56. OUT.vertex = vPosition;
  57. float2 pixelSize = vPosition.w;
  58. pixelSize /= float2(1, 1) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  59. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  60. float2 maskUV = (IN.vertex.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  61. OUT.texcoord = float4(IN.texcoord.x, IN.texcoord.y, maskUV.x, maskUV.y);
  62. OUT.mask = half4(IN.vertex.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + abs(pixelSize.xy)));
  63. #else
  64. #if UNITY_VERSION >= 540
  65. OUT.vertex = UnityObjectToClipPos(IN.vertex);
  66. #else
  67. OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
  68. #endif
  69. #if UNITY_VERSION >= 201800
  70. OUT.texcoord = TRANSFORM_TEX(IN.texcoord, _MainTex);
  71. #else
  72. OUT.texcoord = IN.texcoord;
  73. #endif
  74. #if UNITY_VERSION < 550
  75. #ifdef UNITY_HALF_TEXEL_OFFSET
  76. OUT.vertex.xy += (_ScreenParams.zw - 1.0) * float2(-1, 1);
  77. #endif
  78. #endif
  79. #endif
  80. OUT.color = IN.color * _Color;
  81. SOFTMASK_CALCULATE_COORDS(OUT, IN.vertex)
  82. return OUT;
  83. }
  84. fixed4 frag(v2f IN) : SV_Target
  85. {
  86. #ifdef SOFTMASK_ETC1
  87. half4 color = UnityGetUIDiffuseColor(IN.texcoord, _MainTex, _AlphaTex, _TextureSampleAdd) * IN.color;
  88. #else
  89. half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
  90. #endif
  91. color.a *= SOFTMASK_GET_MASK(IN);
  92. #if defined(UNITY_UI_CLIP_RECT) || UNITY_VERSION < 201720
  93. #if UNITY_VERSION >= 202000
  94. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(IN.mask.xy)) * IN.mask.zw);
  95. color.a *= m.x * m.y;
  96. #else
  97. color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
  98. #endif
  99. #endif
  100. #if defined(UNITY_UI_ALPHACLIP)
  101. clip(color.a - 0.001);
  102. #endif
  103. return color;
  104. }
  105. // UNITY_SHADER_NO_UPGRADE