Global Game Jam 2023
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.

143 lines
3.4 KiB

  1. Shader "TextMeshPro/Bitmap Custom Atlas" {
  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. _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. struct appdata_t {
  45. float4 vertex : POSITION;
  46. fixed4 color : COLOR;
  47. float2 texcoord0 : TEXCOORD0;
  48. float2 texcoord1 : TEXCOORD1;
  49. };
  50. struct v2f {
  51. float4 vertex : SV_POSITION;
  52. fixed4 color : COLOR;
  53. float2 texcoord0 : TEXCOORD0;
  54. float2 texcoord1 : TEXCOORD1;
  55. float4 mask : TEXCOORD2;
  56. };
  57. uniform sampler2D _MainTex;
  58. uniform sampler2D _FaceTex;
  59. uniform float4 _FaceTex_ST;
  60. uniform fixed4 _FaceColor;
  61. uniform float _VertexOffsetX;
  62. uniform float _VertexOffsetY;
  63. uniform float4 _ClipRect;
  64. uniform float _MaskSoftnessX;
  65. uniform float _MaskSoftnessY;
  66. float2 UnpackUV(float uv)
  67. {
  68. float2 output;
  69. output.x = floor(uv / 4096);
  70. output.y = uv - 4096 * output.x;
  71. return output * 0.001953125;
  72. }
  73. v2f vert (appdata_t v)
  74. {
  75. float4 vert = v.vertex;
  76. vert.x += _VertexOffsetX;
  77. vert.y += _VertexOffsetY;
  78. vert.xy += (vert.w * 0.5) / _ScreenParams.xy;
  79. float4 vPosition = UnityPixelSnap(UnityObjectToClipPos(vert));
  80. fixed4 faceColor = v.color;
  81. faceColor *= _FaceColor;
  82. v2f OUT;
  83. OUT.vertex = vPosition;
  84. OUT.color = faceColor;
  85. OUT.texcoord0 = v.texcoord0;
  86. OUT.texcoord1 = TRANSFORM_TEX(UnpackUV(v.texcoord1), _FaceTex);
  87. float2 pixelSize = vPosition.w;
  88. pixelSize /= abs(float2(_ScreenParams.x * UNITY_MATRIX_P[0][0], _ScreenParams.y * UNITY_MATRIX_P[1][1]));
  89. // Clamp _ClipRect to 16bit.
  90. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  91. OUT.mask = float4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy));
  92. return OUT;
  93. }
  94. fixed4 frag (v2f IN) : SV_Target
  95. {
  96. fixed4 color = tex2D(_MainTex, IN.texcoord0) * tex2D(_FaceTex, IN.texcoord1) * IN.color;
  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. }