Skip to content

Commit

Permalink
Fix dash color and background blending (#2137)
Browse files Browse the repository at this point in the history
* YamlUtil::getColorAsVec4 sets alpha to 1 if 4th value missing

* Enable separate alpha values for line dash color and background
  • Loading branch information
matteblair authored Feb 3, 2020
1 parent f849f88 commit 2202740
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
22 changes: 11 additions & 11 deletions core/shaders/polyline.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ void main(void) {
#endif

#ifdef TANGRAM_LINE_TEXTURE
vec2 line_st = vec2(v_texcoord.x, fract(v_texcoord.y * TANGRAM_DASHLINE_TEX_SCALE / u_texture_ratio));
vec2 line_st = vec2(v_texcoord.x, fract(v_texcoord.y * TANGRAM_DASH_TEX_SCALE / u_texture_ratio));
vec4 line_color = texture2D(u_texture, line_st);

if (line_color.a < TANGRAM_ALPHA_TEST) {
#ifdef TANGRAM_LINE_BACKGROUND_COLOR
color.rgb = TANGRAM_LINE_BACKGROUND_COLOR;
#elif !defined(TANGRAM_BLEND_OVERLAY) && !defined(TANGRAM_BLEND_INLAY)
discard;
#else
color.a = 0.0;
#endif
} else {
#if defined(TANGRAM_LINE_DASH)
color = mix(TANGRAM_LINE_BACKGROUND_COLOR, color, line_color.a);
#else
color *= line_color;
}
#endif

#if defined(TANGRAM_BLEND_OPAQUE)
if (color.a < TANGRAM_ALPHA_TEST) {
discard;
}
#endif
#endif

#ifdef TANGRAM_RASTER_TEXTURE_NORMAL
Expand Down
17 changes: 8 additions & 9 deletions core/src/style/polylineStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ void PolylineStyle::onBeginDrawFrame(RenderState& rs, const View& _view) {

void PolylineStyle::setDashBackgroundColor(const glm::vec4 _dashBackgroundColor) {
m_dashBackgroundColor = _dashBackgroundColor;
m_dashBackground = true;
}

void PolylineStyle::constructShaderProgram() {
Expand All @@ -126,22 +125,22 @@ void PolylineStyle::constructShaderProgram() {
reinterpret_cast<GLubyte*>(pixels.data()),
pixels.size() * sizeof(GLuint));

if (m_dashBackground) {
m_shaderSource->addSourceBlock("defines", "#define TANGRAM_LINE_BACKGROUND_COLOR vec3(" +
ff::to_string(m_dashBackgroundColor.r) + ", " +
ff::to_string(m_dashBackgroundColor.g) + ", " +
ff::to_string(m_dashBackgroundColor.b) + ")\n");
}
m_shaderSource->addSourceBlock("defines", "#define TANGRAM_LINE_BACKGROUND_COLOR vec4(" +
ff::to_string(m_dashBackgroundColor.r) + ", " +
ff::to_string(m_dashBackgroundColor.g) + ", " +
ff::to_string(m_dashBackgroundColor.b) + ", " +
ff::to_string(m_dashBackgroundColor.a) + ")\n");
}

if (m_dashArray.size() > 0 || m_texture) {
m_shaderSource->addSourceBlock("defines", "#define TANGRAM_LINE_TEXTURE\n", false);
m_shaderSource->addSourceBlock("defines", "#define TANGRAM_ALPHA_TEST 0.25\n", false);
if (m_dashArray.size() > 0) {
m_shaderSource->addSourceBlock("defines", "#define TANGRAM_DASHLINE_TEX_SCALE " +
m_shaderSource->addSourceBlock("defines", "#define TANGRAM_LINE_DASH");
m_shaderSource->addSourceBlock("defines", "#define TANGRAM_DASH_TEX_SCALE " +
ff::to_string(dash_scale) + "\n", false);
} else {
m_shaderSource->addSourceBlock("defines", "#define TANGRAM_DASHLINE_TEX_SCALE 1.0\n", false);
m_shaderSource->addSourceBlock("defines", "#define TANGRAM_DASH_TEX_SCALE 1.0\n", false);
}
}

Expand Down
3 changes: 1 addition & 2 deletions core/src/style/polylineStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class PolylineStyle : public Style {

std::vector<float> m_dashArray;
std::shared_ptr<Texture> m_texture;
bool m_dashBackground = false;
glm::vec4 m_dashBackgroundColor;
glm::vec4 m_dashBackgroundColor = {};

UniformLocation m_uTexture{"u_texture"};
UniformLocation m_uTextureRatio{"u_texture_ratio"};
Expand Down
3 changes: 3 additions & 0 deletions core/src/util/yamlUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ glm::vec4 getColorAsVec4(const YAML::Node& node) {
if (node.IsSequence()) {
glm::vec4 vec;
if (parseVec(node, vec)) {
if (node.size() < 4) {
vec.w = 1.0;
}
return vec;
}
}
Expand Down

0 comments on commit 2202740

Please sign in to comment.