Use cubic formula in stroke frag for large stroke width case

This commit is contained in:
Grant Sanderson 2023-01-20 13:22:18 -08:00
parent 6c262f63b1
commit 23b4e3e03b

View file

@ -10,23 +10,23 @@ in float is_linear;
out vec4 frag_color;
const float QUICK_DIST_WIDTH = 0.4;
const float QUICK_DIST_WIDTH = 0.2;
float dist_to_curve(){
// In the linear case, the curve will have
// been set to equal the x axis
if(bool(is_linear)) return abs(uv_coords.y);
// Returns distance from uv_coords to the curve v = u^2
// Otherwise, find the distance from uv_coords to the curve y = x^2
float x0 = uv_coords.x;
float y0 = uv_coords.y;
// 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;
float grad_sq = 1 + 4 * x0 * x0;
float approx_dist = abs(Fxy) / sqrt(grad_sq);
float approx_dist = abs(Fxy) / sqrt(1.0 + 4 * x0 * x0);
if(approx_dist < QUICK_DIST_WIDTH) return approx_dist;
// Otherwise, solve for the minimal distance.
@ -38,25 +38,29 @@ float dist_to_curve(){
//
// x^3 + (0.5 - y0) * x - 0.5 * x0 = 0
//
// Use two rounds of Newton's method
float x = x0 + 2 * x0 * Fxy / grad_sq; // Seed with a step along the gradient vector
float p = (0.5 - y0);
float q = -0.5 * x0;
for(int i = 0; i < 2; i++){
float fx = x * x * x + p * x + q;
float dfx = 3 * x * x + p;
x = x - fx / dfx;
}
return distance(uv_coords, vec2(x, x * x));
// 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));
}
void main() {
if (uv_stroke_width == 0) discard;
frag_color = color;
// sdf for the region around the curve we wish to color.
float signed_dist = dist_to_curve() - 0.5 * uv_stroke_width;
frag_color = color;
frag_color.a *= smoothstep(0.5, -0.5, signed_dist / uv_anti_alias_width);
}