commit 8fcb2a80f889be068dbb45bc2ea17644e3c138b3
parent e23d49af72402351acb1e68a0d8a8d25e1d2601f
Author: rexim <reximkut@gmail.com>
Date: Tue, 6 Jul 2021 23:05:24 +0700
Add rainbow to the fragment shader
Diffstat:
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/shaders/font.frag b/shaders/font.frag
@@ -14,6 +14,7 @@
uniform sampler2D font;
uniform float time;
+uniform vec2 resolution;
in vec2 uv;
flat in int glyph_ch;
@@ -25,6 +26,11 @@ float map01(float x)
return (x + 1) / 2.0;
}
+vec3 hsl2rgb(vec3 c) {
+ vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0);
+ return c.z + c.y * (rgb-0.5)*(1.0-abs(2.0*c.z-1.0));
+}
+
void main() {
int ch = glyph_ch;
if (!(ASCII_DISPLAY_LOW <= ch && ch <= ASCII_DISPLAY_HIGH)) {
@@ -39,10 +45,7 @@ void main() {
vec2 t = pos + size * uv;
vec4 tc = texture(font, t);
- vec4 rainbow = vec4(
- map01(sin(time + uv.x)),
- map01(cos(time + uv.y)),
- map01(sin(time + uv.x + uv.y)),
- 1.0);
+ vec2 frag_uv = gl_FragCoord.xy / resolution;
+ vec4 rainbow = vec4(hsl2rgb(vec3((time + frag_uv.x + frag_uv.y), 0.5, 0.5)), 1.0);
gl_FragColor = glyph_bg_color * (1.0 - tc.x) + tc.x * glyph_fg_color * rainbow;
}