bezier.frag (846B)
1 #ifdef GL_ES 2 precision mediump float; 3 #endif 4 5 #define PI 3.14159265359 6 7 uniform vec2 u_resolution; 8 uniform vec2 u_mouse; 9 uniform float u_time; 10 11 float quadraticBezier(float x, vec2 ab) { 12 float epsilon = 0.00001; 13 float a = max(0.0, min(1.0, ab.x)); 14 float b = max(0.0, min(1.0, ab.y)); 15 16 if (a == 0.5) { 17 a += epsilon; 18 } 19 20 // solve t from x (an inverse operation) 21 float om2a = 1.0 - 2.0 * a; 22 float t = (sqrt(a * a + om2a * x) - a) / om2a; 23 return (1.0 - 2.0 * b) * (t * t) + (2.0 * b) * t; 24 } 25 26 void main() { 27 vec2 st = gl_FragCoord.xy / u_resolution; 28 29 float y = quadraticBezier(st.x, u_mouse / u_resolution); 30 vec3 color = vec3(y); 31 32 float plot = smoothstep(y - 0.02, y, st.y) - smoothstep(y, y + 0.02, st.y); 33 34 color = (1.0 - plot) * color + plot * vec3(1.0, 0.0, 1.0); 35 36 gl_FragColor = vec4(color, 1.0); 37 } 38