From 4a567b438be303fbbf13e5ddf49d72d8debd811d Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 24 May 2016 18:28:05 -0700 Subject: [PATCH] [native] Port "combine line-width and line-gap-width in shader" https://github.com/mapbox/mapbox-gl-shaders/commit/a9559e911b11a155135888f733a96e5110f8cef6 https://github.com/mapbox/mapbox-gl-js/commit/fcb9bcebae6e00f24ebfde8d7ec09adb95f6b1a3 --- src/line.fragment.glsl | 13 ---------- src/line.vertex.glsl | 33 ++---------------------- src/linepattern.fragment.glsl | 20 --------------- src/linepattern.vertex.glsl | 44 ++------------------------------ src/linesdfpattern.fragment.glsl | 15 +---------- src/linesdfpattern.vertex.glsl | 36 ++------------------------ 6 files changed, 7 insertions(+), 154 deletions(-) diff --git a/src/line.fragment.glsl b/src/line.fragment.glsl index 397d2ba..ed7a14f 100644 --- a/src/line.fragment.glsl +++ b/src/line.fragment.glsl @@ -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); diff --git a/src/line.vertex.glsl b/src/line.vertex.glsl index 52be63d..87a2e97 100644 --- a/src/line.vertex.glsl +++ b/src/line.vertex.glsl @@ -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() { @@ -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 @@ -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; @@ -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; } diff --git a/src/linepattern.fragment.glsl b/src/linepattern.fragment.glsl index 4d74600..4f1815f 100644 --- a/src/linepattern.fragment.glsl +++ b/src/linepattern.fragment.glsl @@ -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; @@ -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); diff --git a/src/linepattern.vertex.glsl b/src/linepattern.vertex.glsl index cac84a9..720de53 100644 --- a/src/linepattern.vertex.glsl +++ b/src/linepattern.vertex.glsl @@ -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; @@ -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 @@ -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 @@ -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; } diff --git a/src/linesdfpattern.fragment.glsl b/src/linesdfpattern.fragment.glsl index ef0d635..246547a 100644 --- a/src/linesdfpattern.fragment.glsl +++ b/src/linesdfpattern.fragment.glsl @@ -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; diff --git a/src/linesdfpattern.vertex.glsl b/src/linesdfpattern.vertex.glsl index 51e4a24..2db91eb 100644 --- a/src/linesdfpattern.vertex.glsl +++ b/src/linesdfpattern.vertex.glsl @@ -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; @@ -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 @@ -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); @@ -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; }