1// Copyright (c) 2019 Sergio Gonzalez. All rights reserved.
2// License: https://github.com/serge-rgb/milton#license
3
4in vec3 v_pointa;
5in vec3 v_pointb;
6
7void
8main()
9{
10    vec2 screen_point = vec2(gl_FragCoord.x, u_screen_size.y - gl_FragCoord.y);
11
12    vec2 canvas_point = raster_to_canvas_gl(screen_point);
13    vec2 a = v_pointa.xy;
14    vec2 b = v_pointb.xy;
15
16    vec2 ab = b - a;
17    float len_ab = length(ab);
18
19    float t = clamp(dot((canvas_point - a)/len_ab, ab / len_ab), 0.0, 1.0);
20
21    vec2 stroke_point = mix(a, b, t);
22
23    float pressure = mix(v_pointa.z, v_pointb.z, t);
24
25    // Distance between fragment and stroke
26    float dist = distance(stroke_point, canvas_point);
27
28    float rad = u_radius * pressure;
29    out_color.r = dist / rad;
30    out_color.a = 0.0f;
31    if (dist < rad) {
32        out_color.a = pressure;
33    }
34}