TMP_SDF-Mobile-2-Pass.shader 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 - 2 Pass" {
  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. _StencilComp ("Stencil Comparison", Float) = 8
  37. _Stencil ("Stencil ID", Float) = 0
  38. _StencilOp ("Stencil Operation", Float) = 0
  39. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  40. _StencilReadMask ("Stencil Read Mask", Float) = 255
  41. _CullMode ("Cull Mode", Float) = 0
  42. _ColorMask ("Color Mask", Float) = 15
  43. }
  44. SubShader {
  45. // Draw Outline and Underlay
  46. Name "Outline"
  47. Tags
  48. {
  49. "Queue"="Transparent"
  50. "IgnoreProjector"="True"
  51. "RenderType"="Transparent"
  52. }
  53. Stencil
  54. {
  55. Ref [_Stencil]
  56. Comp [_StencilComp]
  57. Pass [_StencilOp]
  58. ReadMask [_StencilReadMask]
  59. WriteMask [_StencilWriteMask]
  60. }
  61. Cull [_CullMode]
  62. ZWrite Off
  63. Lighting Off
  64. Fog { Mode Off }
  65. ZTest [unity_GUIZTestMode]
  66. Blend One OneMinusSrcAlpha
  67. ColorMask [_ColorMask]
  68. Pass {
  69. CGPROGRAM
  70. #pragma vertex VertShader
  71. #pragma fragment PixShader
  72. #pragma shader_feature __ OUTLINE_ON
  73. #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
  74. #pragma multi_compile __ UNITY_UI_CLIP_RECT
  75. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  76. #include "UnityCG.cginc"
  77. #include "UnityUI.cginc"
  78. #include "TMPro_Properties.cginc"
  79. struct vertex_t {
  80. UNITY_VERTEX_INPUT_INSTANCE_ID
  81. float4 vertex : POSITION;
  82. float3 normal : NORMAL;
  83. fixed4 color : COLOR;
  84. float4 texcoord0 : TEXCOORD0;
  85. float2 texcoord1 : TEXCOORD1;
  86. };
  87. struct pixel_t {
  88. UNITY_VERTEX_INPUT_INSTANCE_ID
  89. UNITY_VERTEX_OUTPUT_STEREO
  90. float4 vertex : SV_POSITION;
  91. fixed4 faceColor : COLOR;
  92. fixed4 outlineColor : COLOR1;
  93. float4 texcoord0 : TEXCOORD0; // Texture UV, Mask UV
  94. half4 param : TEXCOORD1; // Scale(x), BiasIn(y), BiasOut(z), Bias(w)
  95. half4 mask : TEXCOORD2; // Position in clip space(xy), Softness(zw)
  96. #if (UNDERLAY_ON | UNDERLAY_INNER)
  97. float4 texcoord1 : TEXCOORD3; // Texture UV, alpha, reserved
  98. half2 underlayParam : TEXCOORD4; // Scale(x), Bias(y)
  99. #endif
  100. };
  101. float _UIMaskSoftnessX;
  102. float _UIMaskSoftnessY;
  103. pixel_t VertShader(vertex_t input)
  104. {
  105. pixel_t output;
  106. UNITY_INITIALIZE_OUTPUT(pixel_t, output);
  107. UNITY_SETUP_INSTANCE_ID(input);
  108. UNITY_TRANSFER_INSTANCE_ID(input, output);
  109. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  110. const float bold = step(input.texcoord0.w, 0);
  111. float4 vert = input.vertex;
  112. vert.x += _VertexOffsetX;
  113. vert.y += _VertexOffsetY;
  114. float4 vPosition = UnityObjectToClipPos(vert);
  115. float2 pixelSize = vPosition.w;
  116. pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  117. float scale = rsqrt(dot(pixelSize, pixelSize));
  118. scale *= abs(input.texcoord0.w) * _GradientScale * (_Sharpness + 1);
  119. if(UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
  120. float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  121. weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  122. float layerScale = scale;
  123. scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
  124. float bias = (0.5 - weight) * scale - 0.5;
  125. const float outline = _OutlineWidth * _ScaleRatioA * 0.5 * scale;
  126. float opacity = input.color.a;
  127. #if (UNDERLAY_ON | UNDERLAY_INNER)
  128. opacity = 1.0;
  129. #endif
  130. fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor;
  131. faceColor.rgb *= faceColor.a;
  132. fixed4 outlineColor = _OutlineColor;
  133. outlineColor.a *= opacity;
  134. outlineColor.rgb *= outlineColor.a;
  135. //outlineColor = lerp(faceColor, outlineColor, sqrt(min(1.0, outline * 2)));
  136. #if (UNDERLAY_ON | UNDERLAY_INNER)
  137. layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
  138. float layerBias = (.5 - weight) * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
  139. float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
  140. float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
  141. float2 layerOffset = float2(x, y);
  142. #endif
  143. // Generate UV for the Masking Texture
  144. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  145. float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  146. // Populate structure for pixel shader
  147. output.vertex = vPosition;
  148. output.faceColor = faceColor;
  149. output.outlineColor = outlineColor;
  150. output.texcoord0 = float4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y);
  151. output.param = half4(scale, bias - outline, bias + outline, bias);
  152. const half2 maskSoftness = half2(max(_UIMaskSoftnessX, _MaskSoftnessX), max(_UIMaskSoftnessY, _MaskSoftnessY));
  153. output.mask = half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * maskSoftness + pixelSize.xy));
  154. #if (UNDERLAY_ON || UNDERLAY_INNER)
  155. output.texcoord1 = float4(input.texcoord0 + layerOffset, input.color.a, 0);
  156. output.underlayParam = half2(layerScale, layerBias);
  157. #endif
  158. return output;
  159. }
  160. // PIXEL SHADER
  161. fixed4 PixShader(pixel_t input) : SV_Target
  162. {
  163. UNITY_SETUP_INSTANCE_ID(input);
  164. half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x;
  165. half4 c = half4(0, 0, 0, 0);
  166. #if OUTLINE_ON
  167. c = input.outlineColor * saturate(d - input.param.y);
  168. #endif
  169. #if UNDERLAY_ON
  170. d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  171. c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (1 - c.a);
  172. #endif
  173. #if UNDERLAY_INNER
  174. half sd = saturate(d - input.param.z);
  175. d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  176. c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - input.underlayParam.y)) * sd * (1 - c.a);
  177. #endif
  178. // Alternative implementation to UnityGet2DClipping with support for softness.
  179. #if UNITY_UI_CLIP_RECT
  180. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
  181. c *= m.x * m.y;
  182. #endif
  183. #if (UNDERLAY_ON | UNDERLAY_INNER)
  184. c *= input.texcoord1.z;
  185. #endif
  186. #if UNITY_UI_ALPHACLIP
  187. clip(c.a - 0.001);
  188. #endif
  189. return c;
  190. }
  191. ENDCG
  192. }
  193. // Draw face
  194. Name "Face"
  195. Tags
  196. {
  197. "Queue"="Transparent"
  198. "IgnoreProjector"="True"
  199. "RenderType"="Transparent"
  200. }
  201. Stencil
  202. {
  203. Ref [_Stencil]
  204. Comp [_StencilComp]
  205. Pass [_StencilOp]
  206. ReadMask [_StencilReadMask]
  207. WriteMask [_StencilWriteMask]
  208. }
  209. Cull [_CullMode]
  210. ZWrite Off
  211. Lighting Off
  212. Fog { Mode Off }
  213. ZTest [unity_GUIZTestMode]
  214. Blend One OneMinusSrcAlpha
  215. ColorMask [_ColorMask]
  216. Pass {
  217. CGPROGRAM
  218. #pragma vertex VertShader
  219. #pragma fragment PixShader
  220. #pragma multi_compile __ UNITY_UI_CLIP_RECT
  221. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  222. #include "UnityCG.cginc"
  223. #include "UnityUI.cginc"
  224. #include "TMPro_Properties.cginc"
  225. struct vertex_t {
  226. UNITY_VERTEX_INPUT_INSTANCE_ID
  227. float4 vertex : POSITION;
  228. float3 normal : NORMAL;
  229. fixed4 color : COLOR;
  230. float4 texcoord0 : TEXCOORD0;
  231. float2 texcoord1 : TEXCOORD1;
  232. };
  233. struct pixel_t {
  234. UNITY_VERTEX_INPUT_INSTANCE_ID
  235. UNITY_VERTEX_OUTPUT_STEREO
  236. float4 vertex : SV_POSITION;
  237. fixed4 faceColor : COLOR;
  238. float4 texcoord0 : TEXCOORD0; // Texture UV, Mask UV
  239. half2 param : TEXCOORD1; // Scale(x), BiasIn(y), BiasOut(z), Bias(w)
  240. half4 mask : TEXCOORD2; // Position in clip space(xy), Softness(zw)
  241. };
  242. float _UIMaskSoftnessX;
  243. float _UIMaskSoftnessY;
  244. int _UIVertexColorAlwaysGammaSpace;
  245. pixel_t VertShader(vertex_t input)
  246. {
  247. pixel_t output;
  248. UNITY_INITIALIZE_OUTPUT(pixel_t, output);
  249. UNITY_SETUP_INSTANCE_ID(input);
  250. UNITY_TRANSFER_INSTANCE_ID(input, output);
  251. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  252. const float bold = step(input.texcoord0.w, 0);
  253. float4 vert = input.vertex;
  254. vert.x += _VertexOffsetX;
  255. vert.y += _VertexOffsetY;
  256. float4 vPosition = UnityObjectToClipPos(vert);
  257. float2 pixelSize = vPosition.w;
  258. pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  259. float scale = rsqrt(dot(pixelSize, pixelSize));
  260. scale *= abs(input.texcoord0.w) * _GradientScale * (_Sharpness + 1);
  261. if(UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
  262. float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  263. weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  264. scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
  265. float bias = (0.5 - weight) * scale - 0.5;
  266. if (_UIVertexColorAlwaysGammaSpace && !IsGammaSpace())
  267. {
  268. input.color.rgb = UIGammaToLinear(input.color.rgb);
  269. }
  270. float opacity = input.color.a;
  271. fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor;
  272. faceColor.rgb *= faceColor.a;
  273. // Generate UV for the Masking Texture
  274. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  275. float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  276. // Populate structure for pixel shader
  277. output.vertex = vPosition;
  278. output.faceColor = faceColor;
  279. output.texcoord0 = float4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y);
  280. output.param = half2(scale, bias);
  281. const half2 maskSoftness = half2(max(_UIMaskSoftnessX, _MaskSoftnessX), max(_UIMaskSoftnessY, _MaskSoftnessY));
  282. output.mask = half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * maskSoftness + pixelSize.xy));
  283. return output;
  284. }
  285. // PIXEL SHADER
  286. fixed4 PixShader(pixel_t input) : SV_Target
  287. {
  288. UNITY_SETUP_INSTANCE_ID(input);
  289. half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x;
  290. half4 c = input.faceColor * saturate(d - input.param.y);
  291. // Alternative implementation to UnityGet2DClipping with support for softness.
  292. #if UNITY_UI_CLIP_RECT
  293. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
  294. c *= m.x * m.y;
  295. #endif
  296. #if UNITY_UI_ALPHACLIP
  297. clip(c.a - 0.001);
  298. #endif
  299. return c;
  300. }
  301. ENDCG
  302. }
  303. }
  304. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
  305. }