From 86e54bd373974dfc775fca51435248045a7cc6ed Mon Sep 17 00:00:00 2001 From: hellerve Date: Mon, 5 Jun 2017 21:17:55 -0400 Subject: [PATCH] basic: added ryb example --- basic/ryb_and_beyond.glsl | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 basic/ryb_and_beyond.glsl diff --git a/basic/ryb_and_beyond.glsl b/basic/ryb_and_beyond.glsl new file mode 100644 index 0000000..44c6ecf --- /dev/null +++ b/basic/ryb_and_beyond.glsl @@ -0,0 +1,49 @@ +#ifdef GL_ES +precision mediump float; +#endif + +#define TAU 6.28318530718 + +uniform vec2 u_resolution; + +vec3 hsb2rgb( in 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 ); + rgb = rgb*rgb*(3.0-2.0*rgb); + return c.z * mix( vec3(1.0), rgb, c.y); +} + +float scale_with(float hue, vec2 begin, vec2 end) { + return (((end.y-begin.y)/(end.x-begin.x))*(hue-begin.x)+begin.y); +} + +// if you find the right values for bbend and ebend, you can scale parts of the color spectrum +float scale(float hue) { + vec2 bbend = vec2(0.250,0.330); + vec2 ebend = vec2(0.340,0.390); + + if (hue < bbend.x) { + return scale_with(hue, vec2(0,0), bbend); + } else if (hue < ebend.x) { + return scale_with(hue, bbend, ebend); + } + return scale_with(hue, ebend, vec2(1.0, 1.0)); +} + +void main(){ + vec2 st = gl_FragCoord.xy/u_resolution; + vec3 color = vec3(0.0); + + // Use polar coordinates instead of cartesian + vec2 toCenter = vec2(0.5)-st; + float angle = atan(toCenter.y, toCenter.x)/TAU; + float radius = length(toCenter)*2.0; + + // Map the angle (-PI to PI) to the Hue (from 0 to 1) + // and the Saturation to the radius + color = hsb2rgb(vec3(scale(angle+0.5)+0.5,radius,1.0)); + + gl_FragColor = vec4(color,1.0); +}