tile_font.frag 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #version 330 core
  2. #define FONT_WIDTH 128
  3. #define FONT_HEIGHT 64
  4. #define FONT_COLS 18
  5. #define FONT_ROWS 7
  6. #define FONT_CH_W (FONT_WIDTH / FONT_COLS)
  7. #define FONT_CH_H (FONT_HEIGHT / FONT_ROWS)
  8. #define FONT_CH_W_UV (float(FONT_CH_W) / float(FONT_WIDTH))
  9. #define FONT_CH_H_UV (float(FONT_CH_H) / float(FONT_HEIGHT))
  10. uniform sampler2D font;
  11. uniform float time;
  12. uniform vec2 resolution;
  13. flat in int glyph_c;
  14. in vec4 glyph_fg_color;
  15. in vec4 glyph_bg_color;
  16. in vec2 uv;
  17. float map01(float x)
  18. {
  19. return (x + 1) / 2;
  20. }
  21. vec3 hsl2rgb( in vec3 c )
  22. {
  23. vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0,
  24. 0.0, 1.0 );
  25. return c.z + c.y * (rgb-0.5)*(1.0-abs(2.0*c.z-1.0));
  26. }
  27. void main() {
  28. int c = glyph_c;
  29. if ( ! (c >= 32 && c <= 126) ) {
  30. c = 63;
  31. }
  32. int index = c - 32;
  33. float x = float(index % FONT_COLS) * FONT_CH_W_UV;
  34. float y = float(index / FONT_COLS) * FONT_CH_H_UV;
  35. vec2 ch_size = vec2(FONT_CH_W_UV, -FONT_CH_H_UV);
  36. vec2 pos = vec2(x, y + FONT_CH_H_UV);
  37. vec2 uv_ = vec2(uv.x, uv.y);
  38. vec4 t = texture(font, pos + ch_size * uv_);
  39. vec2 frag_uv = gl_FragCoord.xy / resolution;
  40. vec4 rainbow = vec4(hsl2rgb(vec3((time + frag_uv.x + frag_uv.y) * 0.5,
  41. 0.5,
  42. 0.5)),
  43. 1);
  44. gl_FragColor = glyph_bg_color * (1.0 - t.x) + t.x * glyph_fg_color * rainbow;
  45. }