text.frag.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. export default `
  7. precision highp float;
  8. precision highp int;
  9. #include common
  10. #include common_frag_params
  11. #include color_frag_params
  12. #include common_clip
  13. #include wboit_params
  14. uniform sampler2D tFont;
  15. uniform vec3 uBorderColor;
  16. uniform float uBorderWidth;
  17. uniform vec3 uBackgroundColor;
  18. uniform float uBackgroundOpacity;
  19. varying vec2 vTexCoord;
  20. const float smoothness = 32.0;
  21. const float gamma = 2.2;
  22. void main2(){
  23. gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  24. }
  25. void main(){
  26. #include clip_pixel
  27. #include assign_material_color
  28. if (vTexCoord.x > 1.0) {
  29. gl_FragColor = vec4(uBackgroundColor, uBackgroundOpacity * material.a);
  30. } else {
  31. // retrieve signed distance
  32. float sdf = texture2D(tFont, vTexCoord).a + uBorderWidth;
  33. // perform adaptive anti-aliasing of the edges
  34. float w = clamp(smoothness * (abs(dFdx(vTexCoord.x)) + abs(dFdy(vTexCoord.y))), 0.0, 0.5);
  35. float a = smoothstep(0.5 - w, 0.5 + w, sdf);
  36. // gamma correction for linear attenuation
  37. a = pow(a, 1.0 / gamma);
  38. if (a < 0.5) discard;
  39. material.a *= a;
  40. // add border
  41. float t = 0.5 + uBorderWidth;
  42. if (uBorderWidth > 0.0 && sdf < t) {
  43. material.xyz = mix(uBorderColor, material.xyz, smoothstep(t - w, t, sdf));
  44. }
  45. gl_FragColor = material;
  46. }
  47. #if defined(dRenderVariant_pick)
  48. #include check_picking_alpha
  49. #elif defined(dRenderVariant_depth)
  50. gl_FragColor = material;
  51. #elif defined(dRenderVariant_color)
  52. #include apply_marker_color
  53. float fragmentDepth = gl_FragCoord.z;
  54. bool interior = false;
  55. #include apply_fog
  56. #include wboit_write
  57. #endif
  58. }
  59. `;