Skip to content
This repository has been archived by the owner on Mar 23, 2019. It is now read-only.

Commit

Permalink
combine line-width and line-gap-width in shader
Browse files Browse the repository at this point in the history
This makes the values for u_linewidth and u_gapwidth independent of
eachother, which clears the way for making these properties data-driven.
  • Loading branch information
ansis committed Apr 29, 2016
1 parent a8d549b commit a9559e9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
7 changes: 3 additions & 4 deletions line.fragment.glsl
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
17 changes: 12 additions & 5 deletions line.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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;
}
10 changes: 5 additions & 5 deletions linepattern.fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
precision mediump float;

uniform vec2 u_linewidth;
uniform float u_point;
uniform float u_blur;

Expand All @@ -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));

Expand Down
13 changes: 10 additions & 3 deletions linepattern.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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;
}
6 changes: 3 additions & 3 deletions linesdfpattern.fragment.glsl
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
11 changes: 9 additions & 2 deletions linesdfpattern.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
}

0 comments on commit a9559e9

Please sign in to comment.