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.

142 lines
3.4 KiB

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