TMP_SDF-Mobile Masking.shader 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Simplified SDF shader:
  2. // - No Shading Option (bevel / bump / env map)
  3. // - No Glow Option
  4. // - Softness is applied on both side of the outline
  5. Shader "TextMeshPro/Mobile/Distance Field - Masking" {
  6. Properties {
  7. _FaceColor ("Face Color", Color) = (1,1,1,1)
  8. _FaceDilate ("Face Dilate", Range(-1,1)) = 0
  9. _OutlineColor ("Outline Color", Color) = (0,0,0,1)
  10. _OutlineWidth ("Outline Thickness", Range(0,1)) = 0
  11. _OutlineSoftness ("Outline Softness", Range(0,1)) = 0
  12. _UnderlayColor ("Border Color", Color) = (0,0,0,.5)
  13. _UnderlayOffsetX ("Border OffsetX", Range(-1,1)) = 0
  14. _UnderlayOffsetY ("Border OffsetY", Range(-1,1)) = 0
  15. _UnderlayDilate ("Border Dilate", Range(-1,1)) = 0
  16. _UnderlaySoftness ("Border Softness", Range(0,1)) = 0
  17. _WeightNormal ("Weight Normal", float) = 0
  18. _WeightBold ("Weight Bold", float) = .5
  19. _ShaderFlags ("Flags", float) = 0
  20. _ScaleRatioA ("Scale RatioA", float) = 1
  21. _ScaleRatioB ("Scale RatioB", float) = 1
  22. _ScaleRatioC ("Scale RatioC", float) = 1
  23. _MainTex ("Font Atlas", 2D) = "white" {}
  24. _TextureWidth ("Texture Width", float) = 512
  25. _TextureHeight ("Texture Height", float) = 512
  26. _GradientScale ("Gradient Scale", float) = 5
  27. _ScaleX ("Scale X", float) = 1
  28. _ScaleY ("Scale Y", float) = 1
  29. _PerspectiveFilter ("Perspective Correction", Range(0, 1)) = 0.875
  30. _Sharpness ("Sharpness", Range(-1,1)) = 0
  31. _VertexOffsetX ("Vertex OffsetX", float) = 0
  32. _VertexOffsetY ("Vertex OffsetY", float) = 0
  33. _ClipRect ("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
  34. _MaskSoftnessX ("Mask SoftnessX", float) = 0
  35. _MaskSoftnessY ("Mask SoftnessY", float) = 0
  36. _MaskTex ("Mask Texture", 2D) = "white" {}
  37. _MaskInverse ("Inverse", float) = 0
  38. _MaskEdgeColor ("Edge Color", Color) = (1,1,1,1)
  39. _MaskEdgeSoftness ("Edge Softness", Range(0, 1)) = 0.01
  40. _MaskWipeControl ("Wipe Position", Range(0, 1)) = 0.5
  41. _StencilComp ("Stencil Comparison", Float) = 8
  42. _Stencil ("Stencil ID", Float) = 0
  43. _StencilOp ("Stencil Operation", Float) = 0
  44. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  45. _StencilReadMask ("Stencil Read Mask", Float) = 255
  46. _CullMode ("Cull Mode", Float) = 0
  47. _ColorMask ("Color Mask", Float) = 15
  48. }
  49. SubShader {
  50. Tags
  51. {
  52. "Queue"="Transparent"
  53. "IgnoreProjector"="True"
  54. "RenderType"="Transparent"
  55. }
  56. Stencil
  57. {
  58. Ref [_Stencil]
  59. Comp [_StencilComp]
  60. Pass [_StencilOp]
  61. ReadMask [_StencilReadMask]
  62. WriteMask [_StencilWriteMask]
  63. }
  64. Cull [_CullMode]
  65. ZWrite Off
  66. Lighting Off
  67. Fog { Mode Off }
  68. ZTest [unity_GUIZTestMode]
  69. Blend One OneMinusSrcAlpha
  70. ColorMask [_ColorMask]
  71. Pass {
  72. CGPROGRAM
  73. #pragma vertex VertShader
  74. #pragma fragment PixShader
  75. #pragma shader_feature __ OUTLINE_ON
  76. #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
  77. #pragma multi_compile __ UNITY_UI_CLIP_RECT
  78. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  79. #include "UnityCG.cginc"
  80. #include "UnityUI.cginc"
  81. #include "TMPro_Properties.cginc"
  82. struct vertex_t
  83. {
  84. float4 vertex : POSITION;
  85. float3 normal : NORMAL;
  86. fixed4 color : COLOR;
  87. float4 texcoord0 : TEXCOORD0;
  88. float2 texcoord1 : TEXCOORD1;
  89. };
  90. struct pixel_t
  91. {
  92. float4 vertex : SV_POSITION;
  93. fixed4 faceColor : COLOR;
  94. fixed4 outlineColor : COLOR1;
  95. float4 texcoord0 : TEXCOORD0; // Texture UV, Mask UV
  96. half4 param : TEXCOORD1; // Scale(x), BiasIn(y), BiasOut(z), Bias(w)
  97. half4 mask : TEXCOORD2; // Position in clip space(xy), Softness(zw)
  98. #if (UNDERLAY_ON | UNDERLAY_INNER)
  99. float4 texcoord1 : TEXCOORD3; // Texture UV, alpha, reserved
  100. half2 underlayParam : TEXCOORD4; // Scale(x), Bias(y)
  101. #endif
  102. };
  103. float _MaskWipeControl;
  104. float _MaskEdgeSoftness;
  105. fixed4 _MaskEdgeColor;
  106. bool _MaskInverse;
  107. float _UIMaskSoftnessX;
  108. float _UIMaskSoftnessY;
  109. int _UIVertexColorAlwaysGammaSpace;
  110. pixel_t VertShader(vertex_t input)
  111. {
  112. float bold = step(input.texcoord0.w, 0);
  113. float4 vert = input.vertex;
  114. vert.x += _VertexOffsetX;
  115. vert.y += _VertexOffsetY;
  116. float4 vPosition = UnityObjectToClipPos(vert);
  117. float2 pixelSize = vPosition.w;
  118. pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  119. float scale = rsqrt(dot(pixelSize, pixelSize));
  120. scale *= abs(input.texcoord0.w) * _GradientScale * (_Sharpness + 1);
  121. if(UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
  122. float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  123. weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  124. float layerScale = scale;
  125. scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
  126. float bias = (0.5 - weight) * scale - 0.5;
  127. float outline = _OutlineWidth * _ScaleRatioA * 0.5 * scale;
  128. if (_UIVertexColorAlwaysGammaSpace && !IsGammaSpace())
  129. {
  130. input.color.rgb = UIGammaToLinear(input.color.rgb);
  131. }
  132. float opacity = input.color.a;
  133. #if (UNDERLAY_ON | UNDERLAY_INNER)
  134. opacity = 1.0;
  135. #endif
  136. fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor;
  137. faceColor.rgb *= faceColor.a;
  138. fixed4 outlineColor = _OutlineColor;
  139. outlineColor.a *= opacity;
  140. outlineColor.rgb *= outlineColor.a;
  141. outlineColor = lerp(faceColor, outlineColor, sqrt(min(1.0, (outline * 2))));
  142. #if (UNDERLAY_ON | UNDERLAY_INNER)
  143. layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
  144. float layerBias = (.5 - weight) * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
  145. float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
  146. float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
  147. float2 layerOffset = float2(x, y);
  148. #endif
  149. // Generate UV for the Masking Texture
  150. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  151. float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  152. const half2 maskSoftness = half2(max(_UIMaskSoftnessX, _MaskSoftnessX), max(_UIMaskSoftnessY, _MaskSoftnessY));
  153. // Structure for pixel shader
  154. pixel_t output = {
  155. vPosition,
  156. faceColor,
  157. outlineColor,
  158. float4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y),
  159. half4(scale, bias - outline, bias + outline, bias),
  160. half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * maskSoftness + pixelSize.xy)),
  161. #if (UNDERLAY_ON | UNDERLAY_INNER)
  162. float4(input.texcoord0 + layerOffset, input.color.a, 0),
  163. half2(layerScale, layerBias),
  164. #endif
  165. };
  166. return output;
  167. }
  168. // PIXEL SHADER
  169. fixed4 PixShader(pixel_t input) : SV_Target
  170. {
  171. half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x;
  172. half4 c = input.faceColor * saturate(d - input.param.w);
  173. #ifdef OUTLINE_ON
  174. c = lerp(input.outlineColor, input.faceColor, saturate(d - input.param.z));
  175. c *= saturate(d - input.param.y);
  176. #endif
  177. #if UNDERLAY_ON
  178. d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  179. c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (1 - c.a);
  180. #endif
  181. #if UNDERLAY_INNER
  182. half sd = saturate(d - input.param.z);
  183. d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  184. c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - input.underlayParam.y)) * sd * (1 - c.a);
  185. #endif
  186. // Alternative implementation to UnityGet2DClipping with support for softness.
  187. //#if UNITY_UI_CLIP_RECT
  188. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
  189. c *= m.x * m.y;
  190. //#endif
  191. float a = abs(_MaskInverse - tex2D(_MaskTex, input.texcoord0.zw).a);
  192. float t = a + (1 - _MaskWipeControl) * _MaskEdgeSoftness - _MaskWipeControl;
  193. a = saturate(t / _MaskEdgeSoftness);
  194. c.rgb = lerp(_MaskEdgeColor.rgb*c.a, c.rgb, a);
  195. c *= a;
  196. #if (UNDERLAY_ON | UNDERLAY_INNER)
  197. c *= input.texcoord1.z;
  198. #endif
  199. #if UNITY_UI_ALPHACLIP
  200. clip(c.a - 0.001);
  201. #endif
  202. return c;
  203. }
  204. ENDCG
  205. }
  206. }
  207. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
  208. }