Text.hlsl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Transform.hlsl"
  4. #ifndef D3D11
  5. // D3D9 uniforms
  6. uniform float2 cShadowOffset;
  7. uniform float4 cShadowColor;
  8. uniform float4 cStrokeColor;
  9. #else
  10. #ifdef COMPILEPS
  11. // D3D11 constant buffers
  12. cbuffer CustomPS : register(b6)
  13. {
  14. float2 cShadowOffset;
  15. float4 cShadowColor;
  16. float4 cStrokeColor;
  17. }
  18. #endif
  19. #endif
  20. void VS(float4 iPos : POSITION,
  21. float2 iTexCoord : TEXCOORD0,
  22. out float2 oTexCoord : TEXCOORD0,
  23. float4 iColor : COLOR0,
  24. out float4 oColor : COLOR0,
  25. out float4 oPos : OUTPOSITION)
  26. {
  27. float4x3 modelMatrix = iModelMatrix;
  28. float3 worldPos = GetWorldPos(modelMatrix);
  29. oPos = GetClipPos(worldPos);
  30. oColor = iColor;
  31. oTexCoord = iTexCoord;
  32. }
  33. // See notes in GLSL shader
  34. #if defined(COMPILEPS) && defined(SIGNED_DISTANCE_FIELD)
  35. float GetAlpha(float distance, float width)
  36. {
  37. return smoothstep(0.5 - width, 0.5 + width, distance);
  38. }
  39. // Comment this define to turn off supersampling
  40. #define SUPERSAMPLING
  41. #endif
  42. void PS(float2 iTexCoord : TEXCOORD0,
  43. float4 iColor : COLOR0,
  44. out float4 oColor : OUTCOLOR0)
  45. {
  46. #ifdef SIGNED_DISTANCE_FIELD
  47. oColor.rgb = iColor.rgb;
  48. float distance = Sample2D(DiffMap, iTexCoord).a;
  49. #ifdef TEXT_EFFECT_STROKE
  50. #ifdef SUPERSAMPLING
  51. float outlineFactor = smoothstep(0.5, 0.525, distance); // Border of glyph
  52. oColor.rgb = lerp(cStrokeColor.rgb, iColor.rgb, outlineFactor);
  53. #else
  54. if (distance < 0.525)
  55. oColor.rgb = cStrokeColor.rgb;
  56. #endif
  57. #endif
  58. #ifdef TEXT_EFFECT_SHADOW
  59. if (Sample2D(DiffMap, iTexCoord - cShadowOffset).a > 0.5 && distance <= 0.5)
  60. oColor = cShadowColor;
  61. #ifndef SUPERSAMPLING
  62. else if (distance <= 0.5)
  63. oColor.a = 0.0;
  64. #endif
  65. else
  66. #endif
  67. {
  68. float width = fwidth(distance);
  69. float alpha = GetAlpha(distance, width);
  70. #ifdef SUPERSAMPLING
  71. float2 deltaUV = 0.354 * fwidth(iTexCoord); // (1.0 / sqrt(2.0)) / 2.0 = 0.354
  72. float4 square = float4(iTexCoord - deltaUV, iTexCoord + deltaUV);
  73. float distance2 = Sample2D(DiffMap, square.xy).a;
  74. float distance3 = Sample2D(DiffMap, square.zw).a;
  75. float distance4 = Sample2D(DiffMap, square.xw).a;
  76. float distance5 = Sample2D(DiffMap, square.zy).a;
  77. alpha += GetAlpha(distance2, width)
  78. + GetAlpha(distance3, width)
  79. + GetAlpha(distance4, width)
  80. + GetAlpha(distance5, width);
  81. // For calculating of average correct would be dividing by 5.
  82. // But when text is blurred, its brightness is lost. Therefore divide by 4.
  83. alpha = alpha * 0.25;
  84. #endif
  85. oColor.a = alpha;
  86. }
  87. #else
  88. #ifdef ALPHAMAP
  89. oColor.rgb = iColor.rgb;
  90. oColor.a = iColor.a * Sample2D(DiffMap, iTexCoord).a;
  91. #else
  92. oColor = iColor* Sample2D(DiffMap, iTexCoord);
  93. #endif
  94. #endif
  95. }