TMPro_Mobile.cginc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. struct vertex_t
  2. {
  3. UNITY_VERTEX_INPUT_INSTANCE_ID
  4. float4 position : POSITION;
  5. float3 normal : NORMAL;
  6. float4 color : COLOR;
  7. float4 texcoord0 : TEXCOORD0;
  8. float2 texcoord1 : TEXCOORD1;
  9. };
  10. struct pixel_t
  11. {
  12. UNITY_VERTEX_INPUT_INSTANCE_ID
  13. UNITY_VERTEX_OUTPUT_STEREO
  14. float4 position : SV_POSITION;
  15. float4 faceColor : COLOR;
  16. float4 outlineColor : COLOR1;
  17. float4 texcoord0 : TEXCOORD0;
  18. float4 param : TEXCOORD1; // x = weight, y = no longer used
  19. float2 mask : TEXCOORD2;
  20. #if (UNDERLAY_ON || UNDERLAY_INNER)
  21. float4 texcoord2 : TEXCOORD3;
  22. float4 underlayColor : COLOR2;
  23. #endif
  24. };
  25. float4 SRGBToLinear(float4 rgba)
  26. {
  27. return float4(lerp(rgba.rgb / 12.92f, pow((rgba.rgb + 0.055f) / 1.055f, 2.4f), step(0.04045f, rgba.rgb)), rgba.a);
  28. }
  29. float _UIMaskSoftnessX;
  30. float _UIMaskSoftnessY;
  31. pixel_t VertShader(vertex_t input)
  32. {
  33. pixel_t output;
  34. UNITY_INITIALIZE_OUTPUT(pixel_t, output);
  35. UNITY_SETUP_INSTANCE_ID(input);
  36. UNITY_TRANSFER_INSTANCE_ID(input, output);
  37. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  38. float bold = step(input.texcoord0.w, 0);
  39. float4 vert = input.position;
  40. vert.x += _VertexOffsetX;
  41. vert.y += _VertexOffsetY;
  42. float4 vPosition = UnityObjectToClipPos(vert);
  43. float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  44. weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  45. // Generate UV for the Masking Texture
  46. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  47. float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  48. float4 color = input.color;
  49. #if (FORCE_LINEAR && !UNITY_COLORSPACE_GAMMA)
  50. color = SRGBToLinear(input.color);
  51. #endif
  52. float opacity = color.a;
  53. #if (UNDERLAY_ON | UNDERLAY_INNER)
  54. opacity = 1.0;
  55. #endif
  56. float4 faceColor = float4(color.rgb, opacity) * _FaceColor;
  57. faceColor.rgb *= faceColor.a;
  58. float4 outlineColor = _OutlineColor;
  59. outlineColor.a *= opacity;
  60. outlineColor.rgb *= outlineColor.a;
  61. output.position = vPosition;
  62. output.faceColor = faceColor;
  63. output.outlineColor = outlineColor;
  64. output.texcoord0 = float4(input.texcoord0.xy, maskUV.xy);
  65. output.param = float4(0.5 - weight, 0, _OutlineWidth * _ScaleRatioA * 0.5, 0);
  66. float2 mask = float2(0, 0);
  67. #if UNITY_UI_CLIP_RECT
  68. mask = vert.xy * 2 - clampedRect.xy - clampedRect.zw;
  69. #endif
  70. output.mask = mask;
  71. #if (UNDERLAY_ON || UNDERLAY_INNER)
  72. float4 underlayColor = _UnderlayColor;
  73. underlayColor.rgb *= underlayColor.a;
  74. float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
  75. float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
  76. output.texcoord2 = float4(input.texcoord0 + float2(x, y), input.color.a, 0);
  77. output.underlayColor = underlayColor;
  78. #endif
  79. return output;
  80. }
  81. float4 PixShader(pixel_t input) : SV_Target
  82. {
  83. UNITY_SETUP_INSTANCE_ID(input);
  84. float d = tex2D(_MainTex, input.texcoord0.xy).a;
  85. float pixelSize = abs(ddx(input.texcoord0.y)) + abs(ddy(input.texcoord0.y));
  86. pixelSize *= _TextureHeight * 0.75;
  87. float scale = 1 / pixelSize * _GradientScale * (_Sharpness + 1);
  88. #if (UNDERLAY_ON | UNDERLAY_INNER)
  89. float layerScale = scale;
  90. layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
  91. float layerBias = input.param.x * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
  92. #endif
  93. scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
  94. float4 faceColor = input.faceColor * saturate((d - input.param.x) * scale + 0.5);
  95. #if OUTLINE_ON
  96. float4 outlineColor = lerp(input.faceColor, input.outlineColor, sqrt(min(1.0, input.param.z * scale * 2)));
  97. faceColor = lerp(outlineColor, input.faceColor, saturate((d - input.param.x - input.param.z) * scale + 0.5));
  98. faceColor *= saturate((d - input.param.x + input.param.z) * scale + 0.5);
  99. #endif
  100. #if UNDERLAY_ON
  101. d = tex2D(_MainTex, input.texcoord2.xy).a * layerScale;
  102. faceColor += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - layerBias) * (1 - faceColor.a);
  103. #endif
  104. #if UNDERLAY_INNER
  105. float bias = input.param.x * scale - 0.5;
  106. float sd = saturate(d * scale - bias - input.param.z);
  107. d = tex2D(_MainTex, input.texcoord2.xy).a * layerScale;
  108. faceColor += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - layerBias)) * sd * (1 - faceColor.a);
  109. #endif
  110. #if MASKING
  111. float a = abs(_MaskInverse - tex2D(_MaskTex, input.texcoord0.zw).a);
  112. float t = a + (1 - _MaskWipeControl) * _MaskEdgeSoftness - _MaskWipeControl;
  113. a = saturate(t / _MaskEdgeSoftness);
  114. faceColor.rgb = lerp(_MaskEdgeColor.rgb * faceColor.a, faceColor.rgb, a);
  115. faceColor *= a;
  116. #endif
  117. // Alternative implementation to UnityGet2DClipping with support for softness
  118. #if UNITY_UI_CLIP_RECT
  119. half2 maskSoftness = half2(max(_UIMaskSoftnessX, _MaskSoftnessX), max(_UIMaskSoftnessY, _MaskSoftnessY));
  120. float2 maskZW = 0.25 / (0.25 * maskSoftness + 1 / scale);
  121. float2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * maskZW);
  122. faceColor *= m.x * m.y;
  123. #endif
  124. #if (UNDERLAY_ON | UNDERLAY_INNER)
  125. faceColor *= input.texcoord2.z;
  126. #endif
  127. #if UNITY_UI_ALPHACLIP
  128. clip(faceColor.a - 0.001);
  129. #endif
  130. return faceColor;
  131. }