text.frag 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. precision highp float;
  7. precision highp int;
  8. #pragma glslify: import('./chunks/common-frag-params.glsl')
  9. #pragma glslify: import('./chunks/color-frag-params.glsl')
  10. uniform sampler2D tFont;
  11. uniform vec3 uBorderColor;
  12. uniform float uBorderWidth;
  13. uniform vec3 uBackgroundColor;
  14. uniform float uBackgroundOpacity;
  15. varying vec2 vTexCoord;
  16. const float smoothness = 32.0;
  17. const float gamma = 2.2;
  18. void main2(){
  19. gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  20. }
  21. void main(){
  22. #pragma glslify: import('./chunks/assign-material-color.glsl')
  23. if (vTexCoord.x > 1.0) {
  24. gl_FragColor = vec4(uBackgroundColor, uBackgroundOpacity);
  25. } else {
  26. // retrieve signed distance
  27. float sdf = texture2D(tFont, vTexCoord).a + uBorderWidth;
  28. // perform adaptive anti-aliasing of the edges
  29. float w = clamp(smoothness * (abs(dFdx(vTexCoord.x)) + abs(dFdy(vTexCoord.y))), 0.0, 0.5);
  30. float a = smoothstep(0.5 - w, 0.5 + w, sdf);
  31. // gamma correction for linear attenuation
  32. a = pow(a, 1.0 / gamma);
  33. if (a < 0.5) discard;
  34. material.a *= a;
  35. // add border
  36. float t = 0.5 + uBorderWidth;
  37. if (uBorderWidth > 0.0 && sdf < t) {
  38. material.xyz = mix(uBorderColor, material.xyz, smoothstep(t - w, t, sdf));
  39. }
  40. gl_FragColor = material;
  41. }
  42. #if defined(dColorType_objectPicking) || defined(dColorType_instancePicking) || defined(dColorType_groupPicking)
  43. if (uAlpha < uPickingAlphaThreshold)
  44. discard; // ignore so the element below can be picked
  45. #else
  46. #pragma glslify: import('./chunks/apply-marker-color.glsl')
  47. #pragma glslify: import('./chunks/apply-fog.glsl')
  48. #endif
  49. }