Refactoring in shader dashed_thick_lines

This commit is contained in:
enricoturri1966 2022-04-05 12:14:01 +02:00
parent 7b3db7a88a
commit 17293056d5
2 changed files with 20 additions and 30 deletions

View File

@ -3,21 +3,20 @@
// see as reference: https://github.com/mhalber/Lines/blob/master/geometry_shader_lines.h // see as reference: https://github.com/mhalber/Lines/blob/master/geometry_shader_lines.h
// https://stackoverflow.com/questions/52928678/dashed-line-in-opengl3 // https://stackoverflow.com/questions/52928678/dashed-line-in-opengl3
const vec2 aa_radius = vec2(0.5); const float aa_radius = 0.5;
uniform float dash_size; uniform float dash_size;
uniform float gap_size; uniform float gap_size;
uniform vec4 uniform_color; uniform vec4 uniform_color;
in float line_width; in float line_width;
in float line_length; in vec2 seg_params;
in vec3 uvs;
out vec4 out_color; out vec4 out_color;
void main() void main()
{ {
if (gap_size > 0.0 && fract(uvs.z / (dash_size + gap_size)) > dash_size / (dash_size + gap_size)) if (gap_size > 0.0 && fract(seg_params.y / (dash_size + gap_size)) > dash_size / (dash_size + gap_size))
discard; discard;
// We render a quad that is fattened by r, giving total width of the line to be w+r. We want smoothing to happen // We render a quad that is fattened by r, giving total width of the line to be w+r. We want smoothing to happen
@ -27,7 +26,7 @@ void main()
// This way the smoothing is centered around 'w'. // This way the smoothing is centered around 'w'.
out_color = uniform_color; out_color = uniform_color;
float au = 1.0 - smoothstep(1.0 - (2.0 * aa_radius[0] / line_width), 1.0, abs(uvs.x / line_width)); float inv_line_width = 1.0 / line_width;
float av = 1.0 - smoothstep(1.0 - (2.0 * aa_radius[1] / line_length), 1.0, abs(uvs.y / line_length)); float au = 1.0 - smoothstep(1.0 - (2.0 * aa_radius * inv_line_width), 1.0, abs(seg_params.x * inv_line_width));
out_color.a *= min(av, au); out_color.a *= au;
} }

View File

@ -6,7 +6,7 @@
layout(lines) in; layout(lines) in;
layout(triangle_strip, max_vertices = 4) out; layout(triangle_strip, max_vertices = 4) out;
const vec2 aa_radius = vec2(0.5); const float aa_radius = 0.5;
uniform vec2 viewport_size; uniform vec2 viewport_size;
uniform float width; uniform float width;
@ -14,44 +14,35 @@ uniform float width;
in float coord_s[]; in float coord_s[];
out float line_width; out float line_width;
out float line_length; out vec2 seg_params;
out vec3 uvs;
void main() void main()
{ {
float u_width = viewport_size[0];
float u_height = viewport_size[1];
vec2 ndc_0 = gl_in[0].gl_Position.xy / gl_in[0].gl_Position.w; vec2 ndc_0 = gl_in[0].gl_Position.xy / gl_in[0].gl_Position.w;
vec2 ndc_1 = gl_in[1].gl_Position.xy / gl_in[1].gl_Position.w; vec2 ndc_1 = gl_in[1].gl_Position.xy / gl_in[1].gl_Position.w;
vec2 viewport_line_vector = 0.5 * (ndc_1 - ndc_0) * viewport_size; vec2 dir = normalize((ndc_1 - ndc_0) * viewport_size);
vec2 dir = normalize(viewport_line_vector);
vec2 normal_dir = vec2(-dir.y, dir.x); vec2 normal_dir = vec2(-dir.y, dir.x);
line_width = max(1.0, width) + 2.0 * aa_radius[0]; line_width = max(1.0, width) + 2.0 * aa_radius;
line_length = length(viewport_line_vector) + 2.0 * aa_radius[1];
vec2 normal = vec2(line_width / u_width, line_width / u_height) * normal_dir;
vec2 extension = vec2(aa_radius[1] / u_width, aa_radius[1] / u_height) * dir;
float half_line_width = 0.5 * line_width; float half_line_width = 0.5 * line_width;
float half_line_length = 0.5 * line_length;
vec2 normal = vec2(line_width / viewport_size[0], line_width / viewport_size[1]) * normal_dir;
uvs = vec3(-half_line_width, half_line_length, coord_s[0]); seg_params = vec2(-half_line_width, coord_s[0]);
gl_Position = vec4((ndc_0 + normal - extension) * gl_in[0].gl_Position.w, gl_in[0].gl_Position.zw); gl_Position = vec4((ndc_0 + normal) * gl_in[0].gl_Position.w, gl_in[0].gl_Position.zw);
EmitVertex(); EmitVertex();
uvs = vec3(-half_line_width, -half_line_length, coord_s[0]); seg_params = vec2(-half_line_width, coord_s[0]);
gl_Position = vec4((ndc_0 - normal - extension) * gl_in[0].gl_Position.w, gl_in[0].gl_Position.zw); gl_Position = vec4((ndc_0 - normal) * gl_in[0].gl_Position.w, gl_in[0].gl_Position.zw);
EmitVertex(); EmitVertex();
uvs = vec3(half_line_width, half_line_length, coord_s[1]); seg_params = vec2(half_line_width, coord_s[1]);
gl_Position = vec4((ndc_1 + normal + extension) * gl_in[1].gl_Position.w, gl_in[1].gl_Position.zw); gl_Position = vec4((ndc_1 + normal) * gl_in[1].gl_Position.w, gl_in[1].gl_Position.zw);
EmitVertex(); EmitVertex();
uvs = vec3(half_line_width, -half_line_length, coord_s[1]); seg_params = vec2(half_line_width, coord_s[1]);
gl_Position = vec4((ndc_1 - normal + extension) * gl_in[1].gl_Position.w, gl_in[1].gl_Position.zw); gl_Position = vec4((ndc_1 - normal) * gl_in[1].gl_Position.w, gl_in[1].gl_Position.zw);
EmitVertex(); EmitVertex();
EndPrimitive(); EndPrimitive();