Skip to content

Commit 268a3df

Browse files
authored
Fix AA with large linewidth differences (#2953)
* scale AA padding with line width difference * update NEWS [skip ci] * optimize expression
1 parent 51fc7ac commit 268a3df

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

GLMakie/assets/shader/lines.geom

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ void emit_vertex(vec3 position, vec2 uv, int index)
5555
f_color = g_color[index];
5656
gl_Position = vec4((position.xy / resolution), position.z, 1.0);
5757
f_id = g_id[index];
58-
f_thickness = g_thickness[index];
58+
// linewidth scaling may shrink the effective linewidth
59+
f_thickness = abs(uv.y) - AA_THICKNESS;
5960
EmitVertex();
6061
}
6162

@@ -77,7 +78,7 @@ void emit_vertex(vec3 position, vec2 uv, int index, vec4 color)
7778
f_color = color;
7879
gl_Position = vec4((position.xy / resolution), position.z, 1.0);
7980
f_id = g_id[index];
80-
f_thickness = g_thickness[index];
81+
f_thickness = abs(uv.y) - AA_THICKNESS;
8182
EmitVertex();
8283
}
8384
void emit_vertex(vec3 position, vec2 uv, vec4 color)
@@ -618,10 +619,6 @@ void draw_solid_line(bool isvalid[4])
618619
vec3 p2 = screen_space(gl_in[2].gl_Position); // end of current segment, start of next segment
619620
vec3 p3 = screen_space(gl_in[3].gl_Position); // end of next segment
620621

621-
// linewidth with padding for anti aliasing
622-
float thickness_aa1 = g_thickness[1] + AA_THICKNESS;
623-
float thickness_aa2 = g_thickness[2] + AA_THICKNESS;
624-
625622
// determine the direction of each of the 3 segments (previous, current, next)
626623
vec3 v1 = p2 - p1;
627624
float segment_length = length(v1.xy);
@@ -641,6 +638,14 @@ void draw_solid_line(bool isvalid[4])
641638
vec2 n1 = vec2(-v1.y, v1.x);
642639
vec2 n2 = vec2(-v2.y, v2.x);
643640

641+
// determine stretching of AA border due to linewidth change
642+
float temp = (g_thickness[2] - g_thickness[1]) / segment_length;
643+
float edge_scale = sqrt(1 + temp * temp);
644+
645+
// linewidth with padding for anti aliasing (used for geometry)
646+
float thickness_aa1 = g_thickness[1] + edge_scale * AA_THICKNESS;
647+
float thickness_aa2 = g_thickness[2] + edge_scale * AA_THICKNESS;
648+
644649
// Setup for sharp corners (see above)
645650
vec2 miter_a = normalize(n0 + n1);
646651
vec2 miter_b = normalize(n1 + n2);
@@ -717,6 +722,10 @@ void draw_solid_line(bool isvalid[4])
717722
segment_length += corner_offset;
718723
}
719724

725+
// scaling of uv.y due to different linewidths
726+
// the padding for AA_THICKNESS should always have AA_THICKNESS width in uv
727+
thickness_aa1 = g_thickness[1] / edge_scale + AA_THICKNESS;
728+
thickness_aa2 = g_thickness[2] / edge_scale + AA_THICKNESS;
720729

721730
// Generate line segment
722731
u1 *= px2uv;

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## master
44

5+
- Fix broken AA for lines with strongly varying linewidth [#2953](https://github.com/MakieOrg/Makie.jl/pull/2953)
6+
57
## v0.19.5
68

79
- Add `loop` option for GIF outputs when recording videos with `record` [#2891](https://github.com/MakieOrg/Makie.jl/pull/2891).

0 commit comments

Comments
 (0)