Merge pull request #402 from mapbox/line-blur
implement `line-blur` and fix retina antialiasing
This commit is contained in:
@@ -16,6 +16,7 @@ public:
|
||||
void setLineWidth(const std::array<float, 2>& linewidth);
|
||||
void setRatio(float ratio);
|
||||
void setDashArray(const std::array<float, 2>& dasharray);
|
||||
void setBlur(float blur);
|
||||
void setDebug(float debug);
|
||||
|
||||
private:
|
||||
@@ -37,6 +38,9 @@ private:
|
||||
|
||||
std::array<float, 2> dasharray = {{}};
|
||||
int32_t u_dasharray = -1;
|
||||
|
||||
float blur = 0.0f;
|
||||
int32_t u_blur = -1;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -14,11 +14,13 @@ void Painter::renderLine(LineBucket& bucket, std::shared_ptr<StyleLayer> layer_d
|
||||
|
||||
float width = properties.width;
|
||||
float offset = properties.offset / 2;
|
||||
float antialiasing = 1 / map.getState().getPixelRatio();
|
||||
float blur = properties.blur + antialiasing;
|
||||
|
||||
// These are the radii of the line. We are limiting it to 16, which will result
|
||||
// in a point size of 64 on retina.
|
||||
float inset = std::fmin((std::fmax(-1, offset - width / 2 - 0.5) + 1), 16.0f);
|
||||
float outset = std::fmin(offset + width / 2 + 0.5, 16.0f);
|
||||
float inset = std::fmin((std::fmax(-1, offset - width / 2 - antialiasing / 2) + 1), 16.0f);
|
||||
float outset = std::fmin(offset + width / 2 + antialiasing / 2, 16.0f);
|
||||
|
||||
Color color = properties.color;
|
||||
color[0] *= properties.opacity;
|
||||
@@ -80,6 +82,7 @@ void Painter::renderLine(LineBucket& bucket, std::shared_ptr<StyleLayer> layer_d
|
||||
lineShader->setDashArray({{ dash_length, dash_gap }});
|
||||
lineShader->setLineWidth({{ outset, inset }});
|
||||
lineShader->setRatio(map.getState().getPixelRatio());
|
||||
lineShader->setBlur(blur);
|
||||
lineShader->setColor(color);
|
||||
bucket.drawLines(*lineShader);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
uniform vec2 u_linewidth;
|
||||
uniform vec4 u_color;
|
||||
uniform float u_blur;
|
||||
|
||||
uniform vec2 u_dasharray;
|
||||
|
||||
@@ -13,7 +14,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 - (u_linewidth.t - 1.0), u_linewidth.s - dist), 0.0, 1.0);
|
||||
float alpha = clamp(min(dist - (u_linewidth.t - u_blur), u_linewidth.s - dist) / u_blur, 0.0, 1.0);
|
||||
|
||||
// Calculate the antialiasing fade factor based on distance to the dash.
|
||||
// Only affects alpha when line is dashed
|
||||
@@ -21,4 +22,4 @@ void main() {
|
||||
alpha *= max(step(0.0, -u_dasharray.y), clamp(min(pos, u_dasharray.x - pos), 0.0, 1.0));
|
||||
|
||||
gl_FragColor = u_color * alpha;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ LineShader::LineShader()
|
||||
u_color = glGetUniformLocation(program, "u_color");
|
||||
u_ratio = glGetUniformLocation(program, "u_ratio");
|
||||
u_dasharray = glGetUniformLocation(program, "u_dasharray");
|
||||
u_blur = glGetUniformLocation(program, "u_blur");
|
||||
|
||||
// fprintf(stderr, "LineShader:\n");
|
||||
// fprintf(stderr, " - u_matrix: %d\n", u_matrix);
|
||||
@@ -81,3 +82,10 @@ void LineShader::setDashArray(const std::array<float, 2>& new_dasharray) {
|
||||
dasharray = new_dasharray;
|
||||
}
|
||||
}
|
||||
|
||||
void LineShader::setBlur(float new_blur) {
|
||||
if (blur != new_blur) {
|
||||
glUniform1f(u_blur, new_blur);
|
||||
blur = new_blur;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user