3b1b-manim/manimlib/shaders/quadratic_bezier_stroke/frag.glsl

59 lines
1.6 KiB
Text
Raw Normal View History

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;
in vec4 color;
2020-02-03 10:52:39 -08:00
in float is_linear;
2020-02-03 10:52:39 -08:00
out vec4 frag_color;
const float QUICK_DIST_WIDTH = 0.2;
// Distance from (x0, y0) to the curve y = x^2
float dist_to_curve(float x0, float y0){
// In the linear case, the curve will have
// been set to equal the x axis
if(bool(is_linear)) return y0;
if(uv_stroke_width < QUICK_DIST_WIDTH){
// 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
return (y0 - x0 * x0) / sqrt(1 + 4 * x0 * x0);
2023-01-08 23:33:39 -05:00
}
// Otherwise, solve for the minimal distance.
// 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
//
// Use two rounds of Newton's method
float x = x0;
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;
2023-01-08 23:33:39 -05:00
}
return distance(uv_coords, vec2(x, 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-08 23:33:39 -05:00
2023-01-09 19:54:21 -08:00
// Compute sdf for the region around the curve we wish to color.
2023-01-10 08:58:08 -08:00
float dist = dist_to_curve(uv_coords.x, uv_coords.y);
float signed_dist = abs(dist) - 0.5 * uv_stroke_width;
frag_color = color;
2020-02-03 10:52:39 -08:00
frag_color.a *= smoothstep(0.5, -0.5, signed_dist / uv_anti_alias_width);
}