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

Commit

Permalink
[native] Port "combine line-width and line-gap-width in shader"
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed May 25, 2016
1 parent d3cdb7f commit 4a567b4
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 154 deletions.
13 changes: 0 additions & 13 deletions src/line.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,19 @@ uniform lowp vec4 u_color;
uniform lowp float u_opacity;
uniform float u_blur;

#ifndef MAPBOX_GL_JS
uniform vec2 u_linewidth;
#else
varying vec2 v_linewidth;
#endif

varying vec2 v_normal;
varying float v_gamma_scale;

void main() {
// Calculate the distance of the pixel from the line in pixels.
#ifndef MAPBOX_GL_JS
float dist = length(v_normal) * u_linewidth.s;
#else
float dist = length(v_normal) * v_linewidth.s;
#endif

// 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;
#ifndef MAPBOX_GL_JS
float alpha = clamp(min(dist - (u_linewidth.t - blur), u_linewidth.s - dist) / blur, 0.0, 1.0);
#else
float alpha = clamp(min(dist - (v_linewidth.t - blur), v_linewidth.s - dist) / blur, 0.0, 1.0);
#endif

gl_FragColor = u_color * (alpha * u_opacity);

Expand Down
33 changes: 2 additions & 31 deletions src/line.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,17 @@ attribute vec2 a_pos;
attribute vec4 a_data;

uniform mat4 u_matrix;
#ifndef MAPBOX_GL_JS

// shared
uniform float u_ratio;
uniform vec2 u_linewidth;
uniform float u_offset;

uniform float u_extra;
#else
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;
#endif
uniform mat2 u_antialiasingmatrix;
#ifdef MAPBOX_GL_JS
uniform mediump float u_offset;
uniform mediump float u_blur;
#endif

varying vec2 v_normal;
#ifdef MAPBOX_GL_JS
varying vec2 v_linewidth;
#endif
varying float v_gamma_scale;

void main() {
Expand All @@ -57,18 +43,12 @@ void main() {
normal.y = sign(normal.y - 0.5);
v_normal = normal;

#ifdef MAPBOX_GL_JS
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;

#endif
// Scale the extrusion vector down to a normal and then up by the line width
// of this vertex.
#ifndef MAPBOX_GL_JS
vec2 dist = u_linewidth.s * a_extrude * scale;
#else
mediump vec4 dist = vec4(outset * a_extrude * scale, 0.0, 0.0);
#endif
mediump vec2 dist = outset * a_extrude * scale;

// 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 @@ -79,15 +59,8 @@ void main() {
mediump vec2 offset = u_offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);

// Remove the texture normal bit of the position before scaling it with the
#ifndef MAPBOX_GL_JS
// model/view matrix. Add the extrusion vector *after* the model/view matrix
// because we're extruding the line in pixel space, regardless of the current
// tile's zoom level.
gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist) / u_ratio, 0.0, 1.0);
#else
// model/view matrix.
gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist.xy) / u_ratio, 0.0, 1.0);
#endif
gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist) / u_ratio, 0.0, 1.0);

// position of y on the screen
float y = gl_Position.y / gl_Position.w;
Expand All @@ -98,8 +71,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));

#ifdef MAPBOX_GL_JS
v_linewidth = vec2(outset, inset);
#endif
v_gamma_scale = perspective_scale * squish_scale;
}
20 changes: 0 additions & 20 deletions src/linepattern.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ precision mediump float;
#define highp
#endif

#ifndef MAPBOX_GL_JS
uniform vec2 u_linewidth;
#endif
uniform float u_blur;

uniform vec2 u_pattern_size_a;
Expand All @@ -23,43 +20,26 @@ uniform float u_opacity;
uniform sampler2D u_image;

varying vec2 v_normal;
#ifdef MAPBOX_GL_JS
varying vec2 v_linewidth;
#endif
varying float v_linesofar;
varying float v_gamma_scale;

void main() {
// Calculate the distance of the pixel from the line in pixels.
#ifndef MAPBOX_GL_JS
float dist = length(v_normal) * u_linewidth.s;
#else
float dist = length(v_normal) * v_linewidth.s;
#endif

// 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;
#ifndef MAPBOX_GL_JS
float alpha = clamp(min(dist - (u_linewidth.t - blur), u_linewidth.s - dist) / blur, 0.0, 1.0);
#else
float alpha = clamp(min(dist - (v_linewidth.t - blur), v_linewidth.s - dist) / blur, 0.0, 1.0);
#endif

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);
#ifndef MAPBOX_GL_JS
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);
vec2 pos_a = mix(u_pattern_tl_a, u_pattern_br_a, vec2(x_a, y_a));
vec2 pos_b = mix(u_pattern_tl_b, u_pattern_br_b, vec2(x_b, y_b));
#else
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_a = mix(u_pattern_tl_a, u_pattern_br_a, vec2(x_a, y_a));
vec2 pos_b = mix(u_pattern_tl_b, u_pattern_br_b, vec2(x_b, y_b));
#endif

vec4 color = mix(texture2D(u_image, pos_a), texture2D(u_image, pos_b), u_fade);

Expand Down
44 changes: 2 additions & 42 deletions src/linepattern.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,17 @@ precision highp float;
attribute vec2 a_pos;
attribute vec4 a_data;

#ifndef MAPBOX_GL_JS
// matrix is for the vertex position, exmatrix is for rotating and projecting
// the extrusion vector.
#endif
uniform mat4 u_matrix;
#ifndef MAPBOX_GL_JS
uniform mat4 u_exmatrix;

// shared
uniform float u_ratio;
uniform vec2 u_linewidth;
uniform float u_offset;
uniform vec4 u_color;

uniform float u_extra;
#else
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;
#endif
uniform mat2 u_antialiasingmatrix;
#ifdef MAPBOX_GL_JS
uniform mediump float u_offset;
#endif

varying vec2 v_normal;
#ifdef MAPBOX_GL_JS
varying vec2 v_linewidth;
#endif
varying float v_linesofar;
varying float v_gamma_scale;

Expand All @@ -64,27 +44,16 @@ void main() {
// transform y so that 0 => -1 and 1 => 1
// In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap
// y is 1 if the normal points up, and -1 if it points down
#ifndef MAPBOX_GL_JS
vec2 normal = mod(a_pos, 2.0);
#else
mediump vec2 normal = mod(a_pos, 2.0);
#endif
normal.y = sign(normal.y - 0.5);
v_normal = normal;

#ifdef MAPBOX_GL_JS
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;

#endif
// Scale the extrusion vector down to a normal and then up by the line width
// of this vertex.
#ifndef MAPBOX_GL_JS
vec2 dist = u_linewidth.s * a_extrude * scale;
#else
mediump vec2 extrude = a_extrude * scale;
mediump vec2 dist = outset * extrude;
#endif
mediump vec2 dist = outset * a_extrude * scale;

// 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 @@ -95,15 +64,8 @@ void main() {
mediump vec2 offset = u_offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);

// Remove the texture normal bit of the position before scaling it with the
#ifndef MAPBOX_GL_JS
// model/view matrix. Add the extrusion vector *after* the model/view matrix
// because we're extruding the line in pixel space, regardless of the current
// tile's zoom level.
gl_Position = u_matrix * vec4(floor(a_pos / 2.0) + (offset + dist) / u_ratio, 0.0, 1.0);
#else
// model/view matrix.
gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist.xy) / u_ratio, 0.0, 1.0);
#endif
gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist) / u_ratio, 0.0, 1.0);
v_linesofar = a_linesofar;

// position of y on the screen
Expand All @@ -119,8 +81,6 @@ void main() {
float perspective_scale = 1.0 / (1.0 - min(y * u_extra, 0.9));
#endif

#ifdef MAPBOX_GL_JS
v_linewidth = vec2(outset, inset);
#endif
v_gamma_scale = perspective_scale * squish_scale;
}
15 changes: 1 addition & 14 deletions src/linesdfpattern.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,21 @@ uniform sampler2D u_image;
uniform float u_sdfgamma;
uniform float u_mix;

#ifndef MAPBOX_GL_JS
uniform vec2 u_linewidth;
#else
varying vec2 v_linewidth;
#endif

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.
#ifndef MAPBOX_GL_JS
float dist = length(v_normal) * u_linewidth.s;
#else
float dist = length(v_normal) * v_linewidth.s;
#endif

// 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;
#ifndef MAPBOX_GL_JS
float alpha = clamp(min(dist - (u_linewidth.t - blur), u_linewidth.s - dist) / blur, 0.0, 1.0);
#else
float alpha = clamp(min(dist - (v_linewidth.t - blur), v_linewidth.s - dist) / blur, 0.0, 1.0);
#endif

float sdfdist_a = texture2D(u_image, v_tex_a).a;
float sdfdist_b = texture2D(u_image, v_tex_b).a;
Expand Down
36 changes: 2 additions & 34 deletions src/linesdfpattern.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,21 @@ precision highp float;
attribute vec2 a_pos;
attribute vec4 a_data;

#ifndef MAPBOX_GL_JS
// matrix is for the vertex position, exmatrix is for rotating and projecting
// the extrusion vector.
#endif
uniform mat4 u_matrix;
#ifndef MAPBOX_GL_JS
uniform mat4 u_exmatrix;

// shared
uniform float u_ratio;
uniform vec2 u_linewidth;
uniform float u_offset;
#else
uniform mediump float u_ratio;
uniform mediump float u_linewidth;
uniform mediump float u_gapwidth;
uniform mediump float u_antialiasing;
#endif
uniform vec2 u_patternscale_a;
uniform float u_tex_y_a;
uniform vec2 u_patternscale_b;
uniform float u_tex_y_b;
uniform float u_extra;
uniform mat2 u_antialiasingmatrix;
#ifdef MAPBOX_GL_JS
uniform mediump float u_offset;
#endif

varying vec2 v_normal;
#ifdef MAPBOX_GL_JS
varying vec2 v_linewidth;
#endif
varying vec2 v_tex_a;
varying vec2 v_tex_b;
varying float v_gamma_scale;
Expand All @@ -70,18 +53,12 @@ void main() {
normal.y = sign(normal.y - 0.5);
v_normal = normal;

#ifdef MAPBOX_GL_JS
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;

#endif
// Scale the extrusion vector down to a normal and then up by the line width
// of this vertex.
#ifndef MAPBOX_GL_JS
vec2 dist = u_linewidth.s * a_extrude * scale;
#else
mediump vec4 dist = vec4(outset * a_extrude * scale, 0.0, 0.0);
#endif
mediump vec2 dist = outset * a_extrude * scale;

// 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 @@ -92,15 +69,8 @@ void main() {
mediump vec2 offset = u_offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);

// Remove the texture normal bit of the position before scaling it with the
#ifndef MAPBOX_GL_JS
// model/view matrix. Add the extrusion vector *after* the model/view matrix
// because we're extruding the line in pixel space, regardless of the current
// tile's zoom level.
gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist) / u_ratio, 0.0, 1.0);
#else
// model/view matrix.
gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist.xy) / u_ratio, 0.0, 1.0);
#endif
gl_Position = u_matrix * vec4(floor(a_pos * 0.5) + (offset + dist) / u_ratio, 0.0, 1.0);

v_tex_a = vec2(a_linesofar * u_patternscale_a.x, normal.y * u_patternscale_a.y + u_tex_y_a);
v_tex_b = vec2(a_linesofar * u_patternscale_b.x, normal.y * u_patternscale_b.y + u_tex_y_b);
Expand All @@ -114,8 +84,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));

#ifdef MAPBOX_GL_JS
v_linewidth = vec2(outset, inset);
#endif
v_gamma_scale = perspective_scale * squish_scale;
}

0 comments on commit 4a567b4

Please sign in to comment.