Skip to content

Commit

Permalink
Made line blur an attribute / varying pair
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Wojciechowski committed Jun 11, 2015
1 parent ba36586 commit 88f138a
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 19 deletions.
13 changes: 6 additions & 7 deletions js/render/draw_line.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ module.exports = function drawLine(painter, layer, posMatrix, tile) {
gl.switchShader(shader, vtxMatrix, tile.exMatrix);

gl.uniform1f(shader.u_ratio, ratio);
gl.uniform1f(shader.u_blur, blur);

var posA = painter.lineAtlas.getDash(dasharray.from, layer.layout['line-cap'] === 'round');
var posB = painter.lineAtlas.getDash(dasharray.to, layer.layout['line-cap'] === 'round');
Expand Down Expand Up @@ -109,7 +108,6 @@ module.exports = function drawLine(painter, layer, posMatrix, tile) {
gl.switchShader(shader, vtxMatrix, tile.exMatrix);

gl.uniform1f(shader.u_ratio, ratio);
gl.uniform1f(shader.u_blur, blur);

gl.uniform2fv(shader.u_pattern_size_a, [imagePosA.size[0] * factor * image.fromScale, imagePosB.size[1] ]);
gl.uniform2fv(shader.u_pattern_size_b, [imagePosB.size[0] * factor * image.toScale, imagePosB.size[1] ]);
Expand All @@ -125,20 +123,21 @@ module.exports = function drawLine(painter, layer, posMatrix, tile) {
gl.switchShader(shader, vtxMatrix, tile.exMatrix);

gl.uniform1f(shader.u_ratio, ratio);
gl.uniform1f(shader.u_blur, blur);
gl.uniform1f(shader.u_extra, extra);
gl.uniformMatrix2fv(shader.u_antialiasingmatrix, false, antialiasingMatrix);
}

// linepattern does not have a color attribute
if (shader.a_color !== undefined) {
gl.disableVertexAttribArray(shader.a_color);
gl.vertexAttrib4fv(shader.a_color, color);
}

if (shader.a_linewidth !== undefined) {
gl.disableVertexAttribArray(shader.a_linewidth);
gl.vertexAttrib2f(shader.a_linewidth, outset, inset);
}
gl.disableVertexAttribArray(shader.a_linewidth);
gl.vertexAttrib2f(shader.a_linewidth, outset, inset);

gl.disableVertexAttribArray(shader.a_blur);
gl.vertexAttrib1f(shader.a_blur, blur);

var vertex = tile.buffers.lineVertex;
vertex.bind(gl);
Expand Down
12 changes: 6 additions & 6 deletions js/render/painter.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ Painter.prototype.setup = function() {
['u_matrix', 'u_brightness_low', 'u_brightness_high', 'u_saturation_factor', 'u_spin_weights', 'u_contrast_factor', 'u_opacity0', 'u_opacity1', 'u_image0', 'u_image1', 'u_tl_parent', 'u_scale_parent', 'u_buffer_scale']);

this.lineShader = gl.initializeShader('line',
['a_pos', 'a_data', 'a_color', 'a_linewidth'],
['u_matrix', 'u_ratio', 'u_blur', 'u_extra', 'u_antialiasingmatrix']);
['a_pos', 'a_data', 'a_color', 'a_linewidth', 'a_blur'],
['u_matrix', 'u_ratio', 'u_extra', 'u_antialiasingmatrix']);

this.linepatternShader = gl.initializeShader('linepattern',
['a_pos', 'a_data', 'a_linewidth'],
['u_matrix', 'u_exmatrix', 'u_ratio', 'u_pattern_size_a', 'u_pattern_size_b', 'u_pattern_tl_a', 'u_pattern_br_a', 'u_pattern_tl_b', 'u_pattern_br_b', 'u_blur', 'u_fade', 'u_opacity']);
['a_pos', 'a_data', 'a_linewidth', 'a_blur'],
['u_matrix', 'u_exmatrix', 'u_ratio', 'u_pattern_size_a', 'u_pattern_size_b', 'u_pattern_tl_a', 'u_pattern_br_a', 'u_pattern_tl_b', 'u_pattern_br_b', 'u_fade', 'u_opacity']);

this.linesdfpatternShader = gl.initializeShader('linesdfpattern',
['a_pos', 'a_data', 'a_color', 'a_linewidth'],
['u_matrix', 'u_exmatrix', 'u_ratio', 'u_blur', 'u_patternscale_a', 'u_tex_y_a', 'u_patternscale_b', 'u_tex_y_b', 'u_image', 'u_sdfgamma', 'u_mix']);
['a_pos', 'a_data', 'a_color', 'a_linewidth', 'a_blur'],
['u_matrix', 'u_exmatrix', 'u_ratio', 'u_patternscale_a', 'u_tex_y_a', 'u_patternscale_b', 'u_tex_y_b', 'u_image', 'u_sdfgamma', 'u_mix']);

this.dotShader = gl.initializeShader('dot',
['a_pos'],
Expand Down
4 changes: 2 additions & 2 deletions shaders/line.fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
uniform float u_blur;

uniform vec2 u_dasharray;

Expand All @@ -7,6 +6,7 @@ varying vec2 v_normal;
varying float v_linesofar;
varying float gamma_scale;
varying vec2 v_linewidth;
varying float v_blur;

void main() {
// Calculate the distance of the pixel from the line in pixels.
Expand All @@ -15,7 +15,7 @@ void main() {
// 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 * gamma_scale;
float blur = v_blur * gamma_scale;
float alpha = clamp(min(dist - (v_linewidth.t - blur), v_linewidth.s - dist) / blur, 0.0, 1.0);

gl_FragColor = v_color * alpha;
Expand Down
3 changes: 3 additions & 0 deletions shaders/line.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ attribute vec2 a_pos;
attribute vec4 a_data;
attribute vec4 a_color;
attribute vec2 a_linewidth;
attribute float a_blur;

// matrix is for the vertex position, exmatrix is for rotating and projecting
// the extrusion vector.
Expand All @@ -26,6 +27,7 @@ varying float v_linesofar;
varying vec4 v_color;
varying float gamma_scale;
varying vec2 v_linewidth;
varying float v_blur;

void main() {
vec2 a_extrude = a_data.xy;
Expand Down Expand Up @@ -61,4 +63,5 @@ void main() {

v_color = a_color;
v_linewidth = a_linewidth;
v_blur = a_blur;
}
4 changes: 2 additions & 2 deletions shaders/linepattern.fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
uniform float u_point;
uniform float u_blur;

uniform vec2 u_pattern_size_a;
uniform vec2 u_pattern_size_b;
Expand All @@ -15,6 +14,7 @@ uniform sampler2D u_image;
varying vec2 v_normal;
varying float v_linesofar;
varying vec2 v_linewidth;
varying float v_blur;

void main() {
// Calculate the distance of the pixel from the line in pixels.
Expand All @@ -23,7 +23,7 @@ void main() {
// 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 alpha = clamp(min(dist - (v_linewidth.t - u_blur), v_linewidth.s - dist) / u_blur, 0.0, 1.0);
float alpha = clamp(min(dist - (v_linewidth.t - v_blur), v_linewidth.s - dist) / v_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);
Expand Down
3 changes: 3 additions & 0 deletions shaders/linepattern.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
attribute vec2 a_pos;
attribute vec4 a_data;
attribute vec2 a_linewidth;
attribute float a_blur;

// matrix is for the vertex position, exmatrix is for rotating and projecting
// the extrusion vector.
Expand All @@ -21,6 +22,7 @@ uniform float u_ratio;
varying vec2 v_normal;
varying float v_linesofar;
varying vec2 v_linewidth;
varying float v_blur;

void main() {
vec2 a_extrude = a_data.xy;
Expand All @@ -47,4 +49,5 @@ void main() {
v_linesofar = a_linesofar;// * u_ratio;

v_linewidth = a_linewidth;
v_blur = a_blur;
}
4 changes: 2 additions & 2 deletions shaders/linesdfpattern.fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
uniform float u_blur;
uniform sampler2D u_image;
uniform float u_sdfgamma;
uniform float u_mix;
Expand All @@ -8,6 +7,7 @@ varying vec2 v_tex_a;
varying vec2 v_tex_b;
varying vec4 v_color;
varying vec2 v_linewidth;
varying float v_blur;

void main() {
// Calculate the distance of the pixel from the line in pixels.
Expand All @@ -16,7 +16,7 @@ void main() {
// 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 alpha = clamp(min(dist - (v_linewidth.t - u_blur), v_linewidth.s - dist) / u_blur, 0.0, 1.0);
float alpha = clamp(min(dist - (v_linewidth.t - v_blur), v_linewidth.s - dist) / v_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
3 changes: 3 additions & 0 deletions shaders/linesdfpattern.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ attribute vec2 a_pos;
attribute vec4 a_data;
attribute vec4 a_color;
attribute vec2 a_linewidth;
attribute float a_blur;

// matrix is for the vertex position, exmatrix is for rotating and projecting
// the extrusion vector.
Expand All @@ -27,6 +28,7 @@ varying vec2 v_tex_a;
varying vec2 v_tex_b;
varying vec4 v_color;
varying vec2 v_linewidth;
varying float v_blur;

void main() {
vec2 a_extrude = a_data.xy;
Expand Down Expand Up @@ -55,4 +57,5 @@ void main() {

v_color = a_color;
v_linewidth = a_linewidth;
v_blur = a_blur;
}

0 comments on commit 88f138a

Please sign in to comment.