dashed_thick_lines.fs 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. #version 150
  2. // see as reference: https://github.com/mhalber/Lines/blob/master/geometry_shader_lines.h
  3. // https://stackoverflow.com/questions/52928678/dashed-line-in-opengl3
  4. const float aa_radius = 0.5;
  5. uniform float dash_size;
  6. uniform float gap_size;
  7. uniform vec4 uniform_color;
  8. in float line_width;
  9. // x = v tex coord, y = s coord
  10. in vec2 seg_params;
  11. out vec4 out_color;
  12. void main()
  13. {
  14. float inv_stride = 1.0 / (dash_size + gap_size);
  15. if (gap_size > 0.0 && fract(seg_params.y * inv_stride) > dash_size * inv_stride)
  16. discard;
  17. // We render a quad that is fattened by r, giving total width of the line to be w+r. We want smoothing to happen
  18. // around w, so that the edge is properly smoothed out. As such, in the smoothstep function we have:
  19. // Far edge : 1.0 = (w+r) / (w+r)
  20. // Close edge : 1.0 - (2r / (w+r)) = (w+r)/(w+r) - 2r/(w+r)) = (w-r) / (w+r)
  21. // This way the smoothing is centered around 'w'.
  22. out_color = uniform_color;
  23. float inv_line_width = 1.0 / line_width;
  24. float aa = 1.0 - smoothstep(1.0 - (2.0 * aa_radius * inv_line_width), 1.0, abs(seg_params.x * inv_line_width));
  25. out_color.a *= aa;
  26. }