diff options
Diffstat (limited to 'res/shaders/glsl330/quadratic-bezier.fs')
| -rw-r--r-- | res/shaders/glsl330/quadratic-bezier.fs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/res/shaders/glsl330/quadratic-bezier.fs b/res/shaders/glsl330/quadratic-bezier.fs new file mode 100644 index 0000000..cac602f --- /dev/null +++ b/res/shaders/glsl330/quadratic-bezier.fs | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | #version 330 core | ||
| 2 | |||
| 3 | uniform float time; | ||
| 4 | uniform vec2 resolution; | ||
| 5 | uniform vec2 mouse; | ||
| 6 | |||
| 7 | out vec4 color; | ||
| 8 | |||
| 9 | float | ||
| 10 | quadratic_bezier (float x, vec2 a) | ||
| 11 | { | ||
| 12 | float epsilon = 0.00001; | ||
| 13 | a.x = clamp (a.x, 0, 1); | ||
| 14 | a.y = clamp (a.y, 0, 1); | ||
| 15 | if (a.x == .5) | ||
| 16 | a += epsilon; | ||
| 17 | |||
| 18 | // solve t from x (an inverse operation) | ||
| 19 | float om2a = 1 - 2*a.x; | ||
| 20 | float t = (sqrt (a.x*a.x + om2a*x) - a.x)/om2a; | ||
| 21 | float y = (1 - 2*a.y)*(t*t) + 2*a.y*t; | ||
| 22 | return y; | ||
| 23 | } | ||
| 24 | |||
| 25 | float | ||
| 26 | line_segment (vec2 p, vec2 a, vec2 b) | ||
| 27 | { | ||
| 28 | vec2 pa = p - a; | ||
| 29 | vec2 ba = b - a; | ||
| 30 | float h = clamp (dot (pa, ba)/dot (ba, ba), 0, 1); | ||
| 31 | return smoothstep (0, 1/resolution.x, length (pa - ba*h)); | ||
| 32 | } | ||
| 33 | |||
| 34 | void | ||
| 35 | main () | ||
| 36 | { | ||
| 37 | vec2 st = gl_FragCoord.xy/resolution; | ||
| 38 | float px = 1/resolution.y; | ||
| 39 | |||
| 40 | // control point | ||
| 41 | // vec2 cp = mouse/resolution; | ||
| 42 | vec2 cp = vec2 (cos (time), sin (time))*0.45 + 0.5; | ||
| 43 | float l = quadratic_bezier(st.x, cp); | ||
| 44 | vec3 c = vec3 (smoothstep (l, l + px, st.y)); | ||
| 45 | |||
| 46 | // draw control point | ||
| 47 | c = mix (vec3 (.5), c, line_segment (st, vec2 (0), cp)); | ||
| 48 | c = mix (vec3 (.5), c, line_segment (st, vec2 (1), cp)); | ||
| 49 | float d = distance (cp, st); | ||
| 50 | c = mix (vec3 (1, 0, 0), c, smoothstep (.01, .01 + px, d)); | ||
| 51 | |||
| 52 | color = vec4 (c, 1); | ||
| 53 | } | ||
