TMP_SDF Internal SSD.shader 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "Hidden/TextMeshPro/Internal/Distance Field SSD" {
  6. Properties {
  7. _FaceColor ("Face Color", Color) = (1,1,1,1)
  8. _FaceDilate ("Face Dilate", Range(-1,1)) = 0
  9. _OutlineSoftness ("Outline Softness", Range(0,1)) = 0.02
  10. _WeightNormal ("Weight Normal", float) = 0
  11. _WeightBold ("Weight Bold", float) = .5
  12. _MainTex ("Font Atlas", 2D) = "white" {}
  13. _TextureWidth ("Texture Width", float) = 512
  14. _TextureHeight ("Texture Height", float) = 512
  15. _GradientScale ("Gradient Scale", float) = 5
  16. _ScaleX ("Scale X", float) = 1
  17. _ScaleY ("Scale Y", float) = 1
  18. _Sharpness ("Sharpness", Range(-1,1)) = 0
  19. _VertexOffsetX ("Vertex OffsetX", float) = 0
  20. _VertexOffsetY ("Vertex OffsetY", float) = 0
  21. _ColorMask ("Color Mask", Float) = 15
  22. }
  23. SubShader {
  24. Tags
  25. {
  26. "ForceSupported" = "True"
  27. }
  28. Lighting Off
  29. Blend One OneMinusSrcAlpha
  30. Cull Off
  31. ZWrite Off
  32. ZTest Always
  33. Pass {
  34. CGPROGRAM
  35. #pragma vertex VertShader
  36. #pragma fragment PixShader
  37. #include "UnityCG.cginc"
  38. #include "TMP_Properties.cginc"
  39. sampler2D _GUIClipTexture;
  40. uniform float4x4 unity_GUIClipTextureMatrix;
  41. struct vertex_t {
  42. float4 vertex : POSITION;
  43. float3 normal : NORMAL;
  44. fixed4 color : COLOR;
  45. float2 texcoord0 : TEXCOORD0;
  46. float2 texcoord1 : TEXCOORD1;
  47. };
  48. struct pixel_t {
  49. float4 vertex : SV_POSITION;
  50. fixed4 faceColor : COLOR;
  51. float2 texcoord0 : TEXCOORD0;
  52. float2 clipUV : TEXCOORD1;
  53. };
  54. pixel_t VertShader(vertex_t input)
  55. {
  56. // Does not handle simulated bold correctly.
  57. float4 vert = input.vertex;
  58. vert.x += _VertexOffsetX;
  59. vert.y += _VertexOffsetY;
  60. float4 vPosition = UnityObjectToClipPos(vert);
  61. float opacity = input.color.a;
  62. fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor;
  63. faceColor.rgb *= faceColor.a;
  64. // Generate UV for the Clip Texture
  65. float3 eyePos = UnityObjectToViewPos(input.vertex);
  66. float2 clipUV = mul(unity_GUIClipTextureMatrix, float4(eyePos.xy, 0, 1.0));
  67. // Structure for pixel shader
  68. pixel_t output = {
  69. vPosition,
  70. faceColor,
  71. float2(input.texcoord0.x, input.texcoord0.y),
  72. clipUV,
  73. };
  74. return output;
  75. }
  76. half transition(half2 range, half distance)
  77. {
  78. return smoothstep(range.x, range.y, distance);
  79. }
  80. // PIXEL SHADER
  81. fixed4 PixShader(pixel_t input) : SV_Target
  82. {
  83. half distanceSample = tex2D(_MainTex, input.texcoord0).a;
  84. half smoothing = fwidth(distanceSample) * (1 - _Sharpness) + _OutlineSoftness;
  85. half contour = 0.5 - _FaceDilate * 0.5;
  86. half2 edgeRange = half2(contour - smoothing, contour + smoothing);
  87. half4 c = input.faceColor;
  88. half edgeTransition = transition(edgeRange, distanceSample);
  89. c *= edgeTransition;
  90. c *= tex2D(_GUIClipTexture, input.clipUV).a;
  91. return c;
  92. }
  93. ENDCG
  94. }
  95. }
  96. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
  97. }