Fix zero stroke width issue

This commit is contained in:
Grant Sanderson 2024-08-16 14:59:18 -05:00
parent 108db87087
commit d8edccdab4
2 changed files with 13 additions and 11 deletions

View file

@ -1,17 +1,18 @@
#version 330 #version 330
// Value between -1 and 1 // Value between -1 and 1
in float scaled_signed_dist_to_curve; in float dist_to_curve;
in float anti_alias_prop; in float half_stroke_width;
in float half_anti_alias_width;
in vec4 color; in vec4 color;
out vec4 frag_color; out vec4 frag_color;
void main() { void main() {
if(anti_alias_prop < 0) discard; if(half_stroke_width == 0) discard;
frag_color = color; frag_color = color;
// sdf for the region around the curve we wish to 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; float signed_dist_to_region = abs(dist_to_curve) - half_stroke_width;
frag_color.a *= smoothstep(0, -anti_alias_prop, signed_dist_to_region); frag_color.a *= smoothstep(half_anti_alias_width, -half_anti_alias_width, signed_dist_to_region);
} }

View file

@ -16,8 +16,9 @@ in float v_stroke_width[3];
in vec4 v_color[3]; in vec4 v_color[3];
out vec4 color; out vec4 color;
out float anti_alias_prop; out float dist_to_curve;
out float scaled_signed_dist_to_curve; out float half_stroke_width;
out float half_anti_alias_width;
// Codes for joint types // Codes for joint types
const int NO_JOINT = 0; const int NO_JOINT = 0;
@ -167,8 +168,8 @@ void emit_point_with_width(
// Set styling // Set styling
color = finalize_color(joint_color, point, unit_normal); color = finalize_color(joint_color, point, unit_normal);
float aaw = anti_alias_width * pixel_size; half_anti_alias_width = 0.5 * anti_alias_width * pixel_size;
anti_alias_prop = (width == 0) ? -1.0 : 2 * aaw / (width + 2 * aaw); half_stroke_width = 0.5 * width;
// Figure out the step from the point to the corners of the // Figure out the step from the point to the corners of the
// triangle strip around the polyline // 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, // The frag shader will receive a value from -1 to 1,
// reflecting where in the stroke that point is // reflecting where in the stroke that point is
for (int sign = -1; sign <= 1; sign += 2){ for (int sign = -1; sign <= 1; sign += 2){
scaled_signed_dist_to_curve = sign; dist_to_curve = sign * (half_stroke_width + half_anti_alias_width);
emit_gl_Position(point + 0.5 * (width + aaw) * sign * step); emit_gl_Position(point + dist_to_curve * step);
EmitVertex(); EmitVertex();
} }
} }