From d8edccdab4a542781477adc1d1dd6cea59d0b94a Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Fri, 16 Aug 2024 14:59:18 -0500 Subject: [PATCH] Fix zero stroke width issue --- manimlib/shaders/quadratic_bezier_stroke/frag.glsl | 11 ++++++----- manimlib/shaders/quadratic_bezier_stroke/geom.glsl | 13 +++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/manimlib/shaders/quadratic_bezier_stroke/frag.glsl b/manimlib/shaders/quadratic_bezier_stroke/frag.glsl index e1013b3a..7fb668aa 100644 --- a/manimlib/shaders/quadratic_bezier_stroke/frag.glsl +++ b/manimlib/shaders/quadratic_bezier_stroke/frag.glsl @@ -1,17 +1,18 @@ #version 330 // Value between -1 and 1 -in float scaled_signed_dist_to_curve; -in float anti_alias_prop; +in float dist_to_curve; +in float half_stroke_width; +in float half_anti_alias_width; in vec4 color; out vec4 frag_color; void main() { - if(anti_alias_prop < 0) discard; + if(half_stroke_width == 0) discard; frag_color = color; // sdf for the region around the curve we wish to color. - float signed_dist_to_region = abs(scaled_signed_dist_to_curve) - 1.0; - frag_color.a *= smoothstep(0, -anti_alias_prop, signed_dist_to_region); + float signed_dist_to_region = abs(dist_to_curve) - half_stroke_width; + frag_color.a *= smoothstep(half_anti_alias_width, -half_anti_alias_width, signed_dist_to_region); } \ No newline at end of file diff --git a/manimlib/shaders/quadratic_bezier_stroke/geom.glsl b/manimlib/shaders/quadratic_bezier_stroke/geom.glsl index f8075b8c..ede46580 100644 --- a/manimlib/shaders/quadratic_bezier_stroke/geom.glsl +++ b/manimlib/shaders/quadratic_bezier_stroke/geom.glsl @@ -16,8 +16,9 @@ in float v_stroke_width[3]; in vec4 v_color[3]; out vec4 color; -out float anti_alias_prop; -out float scaled_signed_dist_to_curve; +out float dist_to_curve; +out float half_stroke_width; +out float half_anti_alias_width; // Codes for joint types const int NO_JOINT = 0; @@ -167,8 +168,8 @@ void emit_point_with_width( // Set styling color = finalize_color(joint_color, point, unit_normal); - float aaw = anti_alias_width * pixel_size; - anti_alias_prop = (width == 0) ? -1.0 : 2 * aaw / (width + 2 * aaw); + half_anti_alias_width = 0.5 * anti_alias_width * pixel_size; + half_stroke_width = 0.5 * width; // Figure out the step from the point to the corners of the // triangle strip around the polyline @@ -178,8 +179,8 @@ void emit_point_with_width( // The frag shader will receive a value from -1 to 1, // reflecting where in the stroke that point is for (int sign = -1; sign <= 1; sign += 2){ - scaled_signed_dist_to_curve = sign; - emit_gl_Position(point + 0.5 * (width + aaw) * sign * step); + dist_to_curve = sign * (half_stroke_width + half_anti_alias_width); + emit_gl_Position(point + dist_to_curve * step); EmitVertex(); } }