text.frag.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. export const text_frag = `
  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. uniform sampler2D tFont;
  14. uniform vec3 uBorderColor;
  15. uniform float uBorderWidth;
  16. uniform vec3 uBackgroundColor;
  17. uniform float uBackgroundOpacity;
  18. varying vec2 vTexCoord;
  19. const float smoothness = 32.0;
  20. const float gamma = 2.2;
  21. void main2(){
  22. gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  23. }
  24. void main(){
  25. #include clip_pixel
  26. float fragmentDepth = gl_FragCoord.z;
  27. #include assign_material_color
  28. if (vTexCoord.x > 1.0) {
  29. #if defined(dRenderVariant_color)
  30. material = vec4(uBackgroundColor, uBackgroundOpacity * material.a);
  31. #endif
  32. } else {
  33. // retrieve signed distance
  34. float sdf = texture2D(tFont, vTexCoord).a + uBorderWidth;
  35. // perform adaptive anti-aliasing of the edges
  36. float w = clamp(smoothness * (abs(dFdx(vTexCoord.x)) + abs(dFdy(vTexCoord.y))), 0.0, 0.5);
  37. float a = smoothstep(0.5 - w, 0.5 + w, sdf);
  38. // gamma correction for linear attenuation
  39. a = pow(a, 1.0 / gamma);
  40. if (a < 0.5) discard;
  41. #if defined(dRenderVariant_color)
  42. material.a *= a;
  43. // add border
  44. float t = 0.5 + uBorderWidth;
  45. if (uBorderWidth > 0.0 && sdf < t) {
  46. material.xyz = mix(uBorderColor, material.xyz, smoothstep(t - w, t, sdf));
  47. }
  48. #endif
  49. }
  50. #if defined(dRenderVariant_pick)
  51. #include check_picking_alpha
  52. #ifdef requiredDrawBuffers
  53. gl_FragColor = vObject;
  54. gl_FragData[1] = vInstance;
  55. gl_FragData[2] = vGroup;
  56. gl_FragData[3] = packDepthToRGBA(fragmentDepth);
  57. #else
  58. gl_FragColor = vColor;
  59. #endif
  60. #elif defined(dRenderVariant_depth)
  61. gl_FragColor = material;
  62. #elif defined(dRenderVariant_marking)
  63. gl_FragColor = material;
  64. #elif defined(dRenderVariant_color)
  65. gl_FragColor = material;
  66. #include apply_marker_color
  67. #include apply_fog
  68. #include wboit_write
  69. #include dpoit_write
  70. #endif
  71. }
  72. `;