You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.9 KiB

  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 Overlay" {
  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. _VertexOffsetX ("Vertex OffsetX", float) = 0
  31. _VertexOffsetY ("Vertex OffsetY", float) = 0
  32. _ClipRect ("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
  33. _MaskSoftnessX ("Mask SoftnessX", float) = 0
  34. _MaskSoftnessY ("Mask SoftnessY", float) = 0
  35. _StencilComp ("Stencil Comparison", Float) = 8
  36. _Stencil ("Stencil ID", Float) = 0
  37. _StencilOp ("Stencil Operation", Float) = 0
  38. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  39. _StencilReadMask ("Stencil Read Mask", Float) = 255
  40. _ColorMask ("Color Mask", Float) = 15
  41. }
  42. SubShader {
  43. Tags
  44. {
  45. "Queue"="Overlay"
  46. "IgnoreProjector"="True"
  47. "RenderType"="Transparent"
  48. }
  49. Stencil
  50. {
  51. Ref [_Stencil]
  52. Comp [_StencilComp]
  53. Pass [_StencilOp]
  54. ReadMask [_StencilReadMask]
  55. WriteMask [_StencilWriteMask]
  56. }
  57. Cull [_CullMode]
  58. ZWrite Off
  59. Lighting Off
  60. Fog { Mode Off }
  61. ZTest Always
  62. Blend One OneMinusSrcAlpha
  63. ColorMask [_ColorMask]
  64. Pass {
  65. CGPROGRAM
  66. #pragma vertex VertShader
  67. #pragma fragment PixShader
  68. #pragma shader_feature __ OUTLINE_ON
  69. #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
  70. #pragma multi_compile __ UNITY_UI_CLIP_RECT
  71. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  72. #include "UnityCG.cginc"
  73. #include "UnityUI.cginc"
  74. #include "TMPro_Properties.cginc"
  75. struct vertex_t {
  76. float4 vertex : POSITION;
  77. float3 normal : NORMAL;
  78. fixed4 color : COLOR;
  79. float2 texcoord0 : TEXCOORD0;
  80. float2 texcoord1 : TEXCOORD1;
  81. };
  82. struct pixel_t {
  83. float4 vertex : SV_POSITION;
  84. fixed4 faceColor : COLOR;
  85. fixed4 outlineColor : COLOR1;
  86. float4 texcoord0 : TEXCOORD0; // Texture UV, Mask UV
  87. half4 param : TEXCOORD1; // Scale(x), BiasIn(y), BiasOut(z), Bias(w)
  88. half4 mask : TEXCOORD2; // Position in clip space(xy), Softness(zw)
  89. #if (UNDERLAY_ON | UNDERLAY_INNER)
  90. float4 texcoord1 : TEXCOORD3; // Texture UV, alpha, reserved
  91. half2 underlayParam : TEXCOORD4; // Scale(x), Bias(y)
  92. #endif
  93. };
  94. pixel_t VertShader(vertex_t input)
  95. {
  96. float bold = step(input.texcoord1.y, 0);
  97. float4 vert = input.vertex;
  98. vert.x += _VertexOffsetX;
  99. vert.y += _VertexOffsetY;
  100. float4 vPosition = UnityObjectToClipPos(vert);
  101. float2 pixelSize = vPosition.w;
  102. pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  103. float scale = rsqrt(dot(pixelSize, pixelSize));
  104. scale *= abs(input.texcoord1.y) * _GradientScale * 1.5;
  105. if(UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
  106. float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  107. weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  108. float layerScale = scale;
  109. scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
  110. float bias = (0.5 - weight) * scale - 0.5;
  111. float outline = _OutlineWidth * _ScaleRatioA * 0.5 * scale;
  112. float opacity = input.color.a;
  113. #if (UNDERLAY_ON | UNDERLAY_INNER)
  114. opacity = 1.0;
  115. #endif
  116. fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor;
  117. faceColor.rgb *= faceColor.a;
  118. fixed4 outlineColor = _OutlineColor;
  119. outlineColor.a *= opacity;
  120. outlineColor.rgb *= outlineColor.a;
  121. outlineColor = lerp(faceColor, outlineColor, sqrt(min(1.0, (outline * 2))));
  122. #if (UNDERLAY_ON | UNDERLAY_INNER)
  123. layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
  124. float layerBias = (.5 - weight) * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
  125. float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
  126. float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
  127. float2 layerOffset = float2(x, y);
  128. #endif
  129. // Generate UV for the Masking Texture
  130. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  131. float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  132. // Structure for pixel shader
  133. pixel_t output = {
  134. vPosition,
  135. faceColor,
  136. outlineColor,
  137. float4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y),
  138. half4(scale, bias - outline, bias + outline, bias),
  139. half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy)),
  140. #if (UNDERLAY_ON | UNDERLAY_INNER)
  141. float4(input.texcoord0 + layerOffset, input.color.a, 0),
  142. half2(layerScale, layerBias),
  143. #endif
  144. };
  145. return output;
  146. }
  147. // PIXEL SHADER
  148. fixed4 PixShader(pixel_t input) : SV_Target
  149. {
  150. half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x;
  151. half4 c = input.faceColor * saturate(d - input.param.w);
  152. #ifdef OUTLINE_ON
  153. c = lerp(input.outlineColor, input.faceColor, saturate(d - input.param.z));
  154. c *= saturate(d - input.param.y);
  155. #endif
  156. #if UNDERLAY_ON
  157. d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  158. c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (1 - c.a);
  159. #endif
  160. #if UNDERLAY_INNER
  161. half sd = saturate(d - input.param.z);
  162. d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  163. c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - input.underlayParam.y)) * sd * (1 - c.a);
  164. #endif
  165. // Alternative implementation to UnityGet2DClipping with support for softness.
  166. #if UNITY_UI_CLIP_RECT
  167. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
  168. c *= m.x * m.y;
  169. #endif
  170. #if (UNDERLAY_ON | UNDERLAY_INNER)
  171. c *= input.texcoord1.z;
  172. #endif
  173. #if UNITY_UI_ALPHACLIP
  174. clip(c.a - 0.001);
  175. #endif
  176. return c;
  177. }
  178. ENDCG
  179. }
  180. }
  181. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
  182. }