2020-02-03 10:52:39 -08:00
|
|
|
#version 330
|
|
|
|
|
|
|
|
in vec2 uv_coords;
|
|
|
|
|
|
|
|
in float uv_stroke_width;
|
|
|
|
in float uv_anti_alias_width;
|
2023-01-09 16:46:38 -08:00
|
|
|
in vec4 color;
|
2020-02-03 10:52:39 -08:00
|
|
|
|
2023-01-10 08:54:02 -08:00
|
|
|
in float is_linear;
|
2020-02-03 10:52:39 -08:00
|
|
|
|
|
|
|
out vec4 frag_color;
|
|
|
|
|
2023-01-20 13:22:18 -08:00
|
|
|
const float QUICK_DIST_WIDTH = 0.2;
|
2023-01-09 16:46:38 -08:00
|
|
|
|
2023-01-18 13:07:18 -08:00
|
|
|
float dist_to_curve(){
|
2023-01-10 08:54:02 -08:00
|
|
|
// In the linear case, the curve will have
|
|
|
|
// been set to equal the x axis
|
2023-01-19 20:24:59 -08:00
|
|
|
if(bool(is_linear)) return abs(uv_coords.y);
|
2023-01-10 08:54:02 -08:00
|
|
|
|
2023-01-20 13:22:18 -08:00
|
|
|
// Otherwise, find the distance from uv_coords to the curve y = x^2
|
2023-01-19 20:24:59 -08:00
|
|
|
float x0 = uv_coords.x;
|
|
|
|
float y0 = uv_coords.y;
|
2023-01-20 13:22:18 -08:00
|
|
|
|
2023-01-20 10:09:58 -08:00
|
|
|
// This is a quick approximation for computing
|
|
|
|
// the distance to the curve.
|
|
|
|
// Evaluate F(x, y) = y - x^2
|
|
|
|
// divide by its gradient's magnitude
|
|
|
|
float Fxy = y0 - x0 * x0;
|
2023-01-28 15:01:32 -08:00
|
|
|
float approx_dist = abs(Fxy) * inversesqrt(1.0 + 4 * x0 * x0);
|
2023-01-20 10:09:58 -08:00
|
|
|
if(approx_dist < QUICK_DIST_WIDTH) return approx_dist;
|
|
|
|
|
2023-01-09 18:51:41 -08:00
|
|
|
// Otherwise, solve for the minimal distance.
|
2023-01-09 16:46:38 -08:00
|
|
|
// The distance squared between (x0, y0) and a point (x, x^2) looks like
|
|
|
|
//
|
|
|
|
// (x0 - x)^2 + (y0 - x^2)^2 = x^4 + (1 - 2y0)x^2 - 2x0 * x + (x0^2 + y0^2)
|
|
|
|
//
|
|
|
|
// Setting the derivative equal to zero (and rescaling) looks like
|
|
|
|
//
|
|
|
|
// x^3 + (0.5 - y0) * x - 0.5 * x0 = 0
|
|
|
|
//
|
2023-01-20 13:22:18 -08:00
|
|
|
// Adapted from https://www.shadertoy.com/view/ws3GD7
|
|
|
|
x0 = abs(x0);
|
|
|
|
float p = (0.5 - y0) / 3.0; // p / 3 in usual Cardano's formula notation
|
|
|
|
float q = 0.25 * x0; // -q / 2 in usual Cardano's formula notation
|
|
|
|
float disc = q*q + p*p*p;
|
|
|
|
float r = sqrt(abs(disc));
|
|
|
|
|
|
|
|
float x = (disc > 0.0) ?
|
|
|
|
// 1 root
|
|
|
|
pow(q + r, 1.0 / 3.0) + pow(abs(q - r), 1.0 / 3.0) * sign(-p) :
|
|
|
|
// 3 roots
|
|
|
|
2.0 * cos(atan(r, q) / 3.0) * sqrt(-p);
|
|
|
|
|
|
|
|
return length(vec2(x0 - x, y0 - x * x));
|
2023-01-08 23:33:39 -05:00
|
|
|
}
|
2020-02-03 10:52:39 -08:00
|
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
if (uv_stroke_width == 0) discard;
|
2023-01-20 13:22:18 -08:00
|
|
|
frag_color = color;
|
2023-01-08 23:33:39 -05:00
|
|
|
|
2023-01-18 13:07:18 -08:00
|
|
|
// sdf for the region around the curve we wish to color.
|
|
|
|
float signed_dist = dist_to_curve() - 0.5 * uv_stroke_width;
|
2020-06-06 09:26:18 -07:00
|
|
|
|
2020-02-03 10:52:39 -08:00
|
|
|
frag_color.a *= smoothstep(0.5, -0.5, signed_dist / uv_anti_alias_width);
|
|
|
|
}
|