TMP_Bitmap-Custom-Atlas.shader 3.7 KB

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