Skip to content

Commit

Permalink
Improve how points with small radii or stroke widths are rendered.
Browse files Browse the repository at this point in the history
Before, a point with a radius < 1.5 would look odd, and a point with a
stroke width < 1.5 wouldn't be as antialiased as it should have been.
  • Loading branch information
manthey committed Sep 3, 2019
1 parent 682c421 commit 30920e4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## Unreleased

### Improvements
- Points with small radii or thin strokes are rendered better (#1021)

## Version 0.19.6

### Features
Expand Down
10 changes: 6 additions & 4 deletions src/webgl/pointFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,12 @@ var webgl_pointFeature = function (arg) {
}
s_updateStyleFromArray(key, styleArray, false);
});
if (m_this.visible() && needsRefresh) {
m_this.draw();
} else if (needsRender) {
m_this.renderer()._render();
if (refresh) {
if (m_this.visible() && needsRefresh) {
m_this.draw();
} else if (needsRender) {
m_this.renderer()._render();
}
}
return m_this;
};
Expand Down
18 changes: 10 additions & 8 deletions src/webgl/pointFeaturePoly.frag
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,26 @@ void main () {
discard;
// If there is no stroke, the fill region should transition to nothing
if (strokeVar == 0.0) {
strokeColor = vec4 (fillColorVar.rgb, 0.0);
strokeColor = vec4(fillColorVar.rgb, 0.0);
endStep = 1.0;
} else {
strokeColor = strokeColorVar;
endStep = radiusVar / (radiusVar + strokeWidthVar);
}
// Likewise, if there is no fill, the stroke should transition to nothing
if (fillVar == 0.0)
fillColor = vec4 (strokeColor.rgb, 0.0);
fillColor = vec4(strokeColor.rgb, 0.0);
else
fillColor = fillColorVar;
// Distance to antialias over
float antialiasDist = 3.0 / (2.0 * radiusVar);
// Distance to antialias over. First number is in pixels
float antialiasDist = 1.5 / (radiusVar + strokeWidthVar);
if (rad < endStep) {
float step = smoothstep (endStep - antialiasDist, endStep, rad);
gl_FragColor = mix (fillColor, strokeColor, step);
float step = smoothstep(max(0.0, endStep - antialiasDist), endStep, rad);
vec4 color = mix(fillColor, strokeColor, step);
float step2 = smoothstep(max(0.0, 1.0 - antialiasDist), 1.0, rad);
gl_FragColor = mix(color, vec4(color.rgb, 0.0), step2);
} else {
float step = smoothstep (1.0 - antialiasDist, 1.0, rad);
gl_FragColor = mix (strokeColor, vec4 (strokeColor.rgb, 0.0), step);
float step = smoothstep(max(0.0, 1.0 - antialiasDist), 1.0, rad);
gl_FragColor = mix(strokeColor, vec4(strokeColor.rgb, 0.0), step);
}
}
20 changes: 11 additions & 9 deletions src/webgl/pointFeatureSprite.frag
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,31 @@ void main () {
// No stroke or fill implies nothing to draw
if (fillVar == 0.0 && strokeVar == 0.0)
discard;
float rad = 2.0 * length (gl_PointCoord - vec2(0.5)); // distinct for sprite
float rad = 2.0 * length(gl_PointCoord - vec2(0.5)); // distinct for sprite
if (rad > 1.0)
discard;
// If there is no stroke, the fill region should transition to nothing
if (strokeVar == 0.0) {
strokeColor = vec4 (fillColorVar.rgb, 0.0);
strokeColor = vec4(fillColorVar.rgb, 0.0);
endStep = 1.0;
} else {
strokeColor = strokeColorVar;
endStep = radiusVar / (radiusVar + strokeWidthVar);
}
// Likewise, if there is no fill, the stroke should transition to nothing
if (fillVar == 0.0)
fillColor = vec4 (strokeColor.rgb, 0.0);
fillColor = vec4(strokeColor.rgb, 0.0);
else
fillColor = fillColorVar;
// Distance to antialias over
float antialiasDist = 3.0 / (2.0 * radiusVar);
// Distance to antialias over. First number is in pixels
float antialiasDist = 1.5 / (radiusVar + strokeWidthVar);
if (rad < endStep) {
float step = smoothstep (endStep - antialiasDist, endStep, rad);
gl_FragColor = mix (fillColor, strokeColor, step);
float step = smoothstep(max(0.0, endStep - antialiasDist), endStep, rad);
vec4 color = mix(fillColor, strokeColor, step);
float step2 = smoothstep(max(0.0, 1.0 - antialiasDist), 1.0, rad);
gl_FragColor = mix(color, vec4(color.rgb, 0.0), step2);
} else {
float step = smoothstep (1.0 - antialiasDist, 1.0, rad);
gl_FragColor = mix (strokeColor, vec4 (strokeColor.rgb, 0.0), step);
float step = smoothstep(max(0.0, 1.0 - antialiasDist), 1.0, rad);
gl_FragColor = mix(strokeColor, vec4(strokeColor.rgb, 0.0), step);
}
}

0 comments on commit 30920e4

Please sign in to comment.