diff --git a/line.fragment.glsl b/line.fragment.glsl index 44220e3..15d093a 100644 --- a/line.fragment.glsl +++ b/line.fragment.glsl @@ -1,23 +1,22 @@ precision mediump float; -uniform vec2 u_linewidth; uniform lowp vec4 u_color; uniform lowp float u_opacity; uniform float u_blur; varying vec2 v_normal; -varying float v_linesofar; +varying vec2 v_linewidth; varying float v_gamma_scale; void main() { // Calculate the distance of the pixel from the line in pixels. - float dist = length(v_normal) * u_linewidth.s; + float dist = length(v_normal) * v_linewidth.s; // Calculate the antialiasing fade factor. This is either when fading in // the line in case of an offset line (v_linewidth.t) or when fading out // (v_linewidth.s) float blur = u_blur * v_gamma_scale; - float alpha = clamp(min(dist - (u_linewidth.t - blur), u_linewidth.s - dist) / blur, 0.0, 1.0); + float alpha = clamp(min(dist - (v_linewidth.t - blur), v_linewidth.s - dist) / blur, 0.0, 1.0); gl_FragColor = u_color * (alpha * u_opacity); diff --git a/line.vertex.glsl b/line.vertex.glsl index d4b7697..d82c9d3 100644 --- a/line.vertex.glsl +++ b/line.vertex.glsl @@ -12,14 +12,17 @@ attribute vec2 a_pos; attribute vec4 a_data; uniform mat4 u_matrix; -uniform float u_ratio; -uniform mediump vec2 u_linewidth; -uniform float u_extra; +uniform mediump float u_ratio; +uniform mediump float u_linewidth; +uniform mediump float u_gapwidth; +uniform mediump float u_antialiasing; +uniform mediump float u_extra; uniform mat2 u_antialiasingmatrix; uniform mediump float u_offset; +uniform mediump float u_blur; varying vec2 v_normal; -varying float v_linesofar; +varying vec2 v_linewidth; varying float v_gamma_scale; void main() { @@ -34,9 +37,12 @@ void main() { normal.y = sign(normal.y - 0.5); v_normal = normal; + float inset = u_gapwidth + (u_gapwidth > 0.0 ? u_antialiasing : 0.0); + float outset = u_gapwidth + u_linewidth * (u_gapwidth > 0.0 ? 2.0 : 1.0) + u_antialiasing; + // Scale the extrusion vector down to a normal and then up by the line width // of this vertex. - mediump vec4 dist = vec4(u_linewidth.s * a_extrude * scale, 0.0, 0.0); + mediump vec4 dist = vec4(outset * a_extrude * scale, 0.0, 0.0); // Calculate the offset when drawing a line that is to the side of the actual line. // We do this by creating a vector that points towards the extrude, but rotate @@ -59,5 +65,6 @@ void main() { // how much features are squished in all directions by the perspectiveness float perspective_scale = 1.0 / (1.0 - min(y * u_extra, 0.9)); + v_linewidth = vec2(outset, inset); v_gamma_scale = perspective_scale * squish_scale; } diff --git a/linepattern.fragment.glsl b/linepattern.fragment.glsl index 2ec8117..b66ded7 100644 --- a/linepattern.fragment.glsl +++ b/linepattern.fragment.glsl @@ -1,6 +1,5 @@ precision mediump float; -uniform vec2 u_linewidth; uniform float u_point; uniform float u_blur; @@ -16,23 +15,24 @@ uniform float u_opacity; uniform sampler2D u_image; varying vec2 v_normal; +varying vec2 v_linewidth; varying float v_linesofar; varying float v_gamma_scale; void main() { // Calculate the distance of the pixel from the line in pixels. - float dist = length(v_normal) * u_linewidth.s; + float dist = length(v_normal) * v_linewidth.s; // Calculate the antialiasing fade factor. This is either when fading in // the line in case of an offset line (v_linewidth.t) or when fading out // (v_linewidth.s) float blur = u_blur * v_gamma_scale; - float alpha = clamp(min(dist - (u_linewidth.t - blur), u_linewidth.s - dist) / blur, 0.0, 1.0); + float alpha = clamp(min(dist - (v_linewidth.t - blur), v_linewidth.s - dist) / blur, 0.0, 1.0); float x_a = mod(v_linesofar / u_pattern_size_a.x, 1.0); float x_b = mod(v_linesofar / u_pattern_size_b.x, 1.0); - float y_a = 0.5 + (v_normal.y * u_linewidth.s / u_pattern_size_a.y); - float y_b = 0.5 + (v_normal.y * u_linewidth.s / u_pattern_size_b.y); + float y_a = 0.5 + (v_normal.y * v_linewidth.s / u_pattern_size_a.y); + float y_b = 0.5 + (v_normal.y * v_linewidth.s / u_pattern_size_b.y); vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, vec2(x_a, y_a)); vec2 pos2 = mix(u_pattern_tl_b, u_pattern_br_b, vec2(x_b, y_b)); diff --git a/linepattern.vertex.glsl b/linepattern.vertex.glsl index 907049a..12f7293 100644 --- a/linepattern.vertex.glsl +++ b/linepattern.vertex.glsl @@ -17,12 +17,15 @@ attribute vec4 a_data; uniform mat4 u_matrix; uniform mediump float u_ratio; -uniform mediump vec2 u_linewidth; -uniform float u_extra; +uniform mediump float u_linewidth; +uniform mediump float u_gapwidth; +uniform mediump float u_antialiasing; +uniform mediump float u_extra; uniform mat2 u_antialiasingmatrix; uniform mediump float u_offset; varying vec2 v_normal; +varying vec2 v_linewidth; varying float v_linesofar; varying float v_gamma_scale; @@ -39,10 +42,13 @@ void main() { normal.y = sign(normal.y - 0.5); v_normal = normal; + float inset = u_gapwidth + (u_gapwidth > 0.0 ? u_antialiasing : 0.0); + float outset = u_gapwidth + u_linewidth * (u_gapwidth > 0.0 ? 2.0 : 1.0) + u_antialiasing; + // Scale the extrusion vector down to a normal and then up by the line width // of this vertex. mediump vec2 extrude = a_extrude * scale; - mediump vec2 dist = u_linewidth.s * extrude; + mediump vec2 dist = outset * extrude; // Calculate the offset when drawing a line that is to the side of the actual line. // We do this by creating a vector that points towards the extrude, but rotate @@ -66,5 +72,6 @@ void main() { // how much features are squished in all directions by the perspectiveness float perspective_scale = 1.0 / (1.0 - min(y * u_extra, 0.9)); + v_linewidth = vec2(outset, inset); v_gamma_scale = perspective_scale * squish_scale; } diff --git a/linesdfpattern.fragment.glsl b/linesdfpattern.fragment.glsl index b07f08d..b71be39 100644 --- a/linesdfpattern.fragment.glsl +++ b/linesdfpattern.fragment.glsl @@ -1,6 +1,5 @@ precision mediump float; -uniform vec2 u_linewidth; uniform lowp vec4 u_color; uniform lowp float u_opacity; uniform float u_blur; @@ -9,19 +8,20 @@ uniform float u_sdfgamma; uniform float u_mix; varying vec2 v_normal; +varying vec2 v_linewidth; varying vec2 v_tex_a; varying vec2 v_tex_b; varying float v_gamma_scale; void main() { // Calculate the distance of the pixel from the line in pixels. - float dist = length(v_normal) * u_linewidth.s; + float dist = length(v_normal) * v_linewidth.s; // Calculate the antialiasing fade factor. This is either when fading in // the line in case of an offset line (v_linewidth.t) or when fading out // (v_linewidth.s) float blur = u_blur * v_gamma_scale; - float alpha = clamp(min(dist - (u_linewidth.t - blur), u_linewidth.s - dist) / blur, 0.0, 1.0); + float alpha = clamp(min(dist - (v_linewidth.t - blur), v_linewidth.s - dist) / blur, 0.0, 1.0); float sdfdist_a = texture2D(u_image, v_tex_a).a; float sdfdist_b = texture2D(u_image, v_tex_b).a; diff --git a/linesdfpattern.vertex.glsl b/linesdfpattern.vertex.glsl index f5650aa..78bf6a4 100644 --- a/linesdfpattern.vertex.glsl +++ b/linesdfpattern.vertex.glsl @@ -16,8 +16,10 @@ attribute vec2 a_pos; attribute vec4 a_data; uniform mat4 u_matrix; -uniform mediump vec2 u_linewidth; uniform mediump float u_ratio; +uniform mediump float u_linewidth; +uniform mediump float u_gapwidth; +uniform mediump float u_antialiasing; uniform vec2 u_patternscale_a; uniform float u_tex_y_a; uniform vec2 u_patternscale_b; @@ -27,6 +29,7 @@ uniform mat2 u_antialiasingmatrix; uniform mediump float u_offset; varying vec2 v_normal; +varying vec2 v_linewidth; varying vec2 v_tex_a; varying vec2 v_tex_b; varying float v_gamma_scale; @@ -44,9 +47,12 @@ void main() { normal.y = sign(normal.y - 0.5); v_normal = normal; + float inset = u_gapwidth + (u_gapwidth > 0.0 ? u_antialiasing : 0.0); + float outset = u_gapwidth + u_linewidth * (u_gapwidth > 0.0 ? 2.0 : 1.0) + u_antialiasing; + // Scale the extrusion vector down to a normal and then up by the line width // of this vertex. - mediump vec4 dist = vec4(u_linewidth.s * a_extrude * scale, 0.0, 0.0); + mediump vec4 dist = vec4(outset * a_extrude * scale, 0.0, 0.0); // Calculate the offset when drawing a line that is to the side of the actual line. // We do this by creating a vector that points towards the extrude, but rotate @@ -72,5 +78,6 @@ void main() { // how much features are squished in all directions by the perspectiveness float perspective_scale = 1.0 / (1.0 - min(y * u_extra, 0.9)); + v_linewidth = vec2(outset, inset); v_gamma_scale = perspective_scale * squish_scale; }