TMP_Bitmap.shader 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. Shader "TextMeshPro/Bitmap" {
  2. Properties {
  3. _MainTex ("Font Atlas", 2D) = "white" {}
  4. _FaceTex ("Font Texture", 2D) = "white" {}
  5. [HDR]_FaceColor ("Text Color", Color) = (1,1,1,1)
  6. _VertexOffsetX ("Vertex OffsetX", float) = 0
  7. _VertexOffsetY ("Vertex OffsetY", float) = 0
  8. _MaskSoftnessX ("Mask SoftnessX", float) = 0
  9. _MaskSoftnessY ("Mask SoftnessY", float) = 0
  10. _ClipRect("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
  11. _StencilComp("Stencil Comparison", Float) = 8
  12. _Stencil("Stencil ID", Float) = 0
  13. _StencilOp("Stencil Operation", Float) = 0
  14. _StencilWriteMask("Stencil Write Mask", Float) = 255
  15. _StencilReadMask("Stencil Read Mask", Float) = 255
  16. _CullMode("Cull Mode", Float) = 0
  17. _ColorMask("Color Mask", Float) = 15
  18. }
  19. SubShader{
  20. Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
  21. Stencil
  22. {
  23. Ref[_Stencil]
  24. Comp[_StencilComp]
  25. Pass[_StencilOp]
  26. ReadMask[_StencilReadMask]
  27. WriteMask[_StencilWriteMask]
  28. }
  29. Lighting Off
  30. Cull [_CullMode]
  31. ZTest [unity_GUIZTestMode]
  32. ZWrite Off
  33. Fog { Mode Off }
  34. Blend SrcAlpha OneMinusSrcAlpha
  35. ColorMask[_ColorMask]
  36. Pass {
  37. CGPROGRAM
  38. #pragma vertex vert
  39. #pragma fragment frag
  40. #pragma multi_compile __ UNITY_UI_CLIP_RECT
  41. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  42. #include "UnityCG.cginc"
  43. struct appdata_t {
  44. float4 vertex : POSITION;
  45. fixed4 color : COLOR;
  46. float2 texcoord0 : TEXCOORD0;
  47. float2 texcoord1 : TEXCOORD1;
  48. };
  49. struct v2f {
  50. float4 vertex : SV_POSITION;
  51. fixed4 color : COLOR;
  52. float2 texcoord0 : TEXCOORD0;
  53. float2 texcoord1 : TEXCOORD1;
  54. float4 mask : TEXCOORD2;
  55. };
  56. uniform sampler2D _MainTex;
  57. uniform sampler2D _FaceTex;
  58. uniform float4 _FaceTex_ST;
  59. uniform fixed4 _FaceColor;
  60. uniform float _VertexOffsetX;
  61. uniform float _VertexOffsetY;
  62. uniform float4 _ClipRect;
  63. uniform float _MaskSoftnessX;
  64. uniform float _MaskSoftnessY;
  65. float2 UnpackUV(float uv)
  66. {
  67. float2 output;
  68. output.x = floor(uv / 4096);
  69. output.y = uv - 4096 * output.x;
  70. return output * 0.001953125;
  71. }
  72. v2f vert (appdata_t v)
  73. {
  74. float4 vert = v.vertex;
  75. vert.x += _VertexOffsetX;
  76. vert.y += _VertexOffsetY;
  77. vert.xy += (vert.w * 0.5) / _ScreenParams.xy;
  78. float4 vPosition = UnityPixelSnap(UnityObjectToClipPos(vert));
  79. fixed4 faceColor = v.color;
  80. faceColor *= _FaceColor;
  81. v2f OUT;
  82. OUT.vertex = vPosition;
  83. OUT.color = faceColor;
  84. OUT.texcoord0 = v.texcoord0;
  85. OUT.texcoord1 = TRANSFORM_TEX(UnpackUV(v.texcoord1), _FaceTex);
  86. float2 pixelSize = vPosition.w;
  87. pixelSize /= abs(float2(_ScreenParams.x * UNITY_MATRIX_P[0][0], _ScreenParams.y * UNITY_MATRIX_P[1][1]));
  88. // Clamp _ClipRect to 16bit.
  89. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  90. OUT.mask = float4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy));
  91. return OUT;
  92. }
  93. fixed4 frag (v2f IN) : SV_Target
  94. {
  95. fixed4 color = tex2D(_MainTex, IN.texcoord0);
  96. color = fixed4 (tex2D(_FaceTex, IN.texcoord1).rgb * IN.color.rgb, IN.color.a * color.a);
  97. // Alternative implementation to UnityGet2DClipping with support for softness.
  98. #if UNITY_UI_CLIP_RECT
  99. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(IN.mask.xy)) * IN.mask.zw);
  100. color *= m.x * m.y;
  101. #endif
  102. #if UNITY_UI_ALPHACLIP
  103. clip(color.a - 0.001);
  104. #endif
  105. return color;
  106. }
  107. ENDCG
  108. }
  109. }
  110. CustomEditor "TMPro.EditorUtilities.TMP_BitmapShaderGUI"
  111. }