From d203ef76900da98b706891cc9a455c202b328cbb Mon Sep 17 00:00:00 2001 From: Chris Loer Date: Wed, 5 Apr 2017 15:01:46 -0700 Subject: [PATCH] WIP on curved labels: calculate their zoom adjustment based on the label anchor instead of the glyph anchor, so all the glyphs can behave consistently. --- src/data/bucket/symbol_bucket.js | 24 ++++++++++++++-------- src/shaders/symbol_sdf.vertex.glsl | 32 ++++++++++-------------------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/data/bucket/symbol_bucket.js b/src/data/bucket/symbol_bucket.js index f6ccdf8dd1b..7aee3cce190 100644 --- a/src/data/bucket/symbol_bucket.js +++ b/src/data/bucket/symbol_bucket.js @@ -32,6 +32,7 @@ const elementArrayType = createElementArrayType(); const layoutAttributes = [ {name: 'a_pos_offset', components: 4, type: 'Int16'}, + {name: 'a_label_pos', components: 2, type: 'Int16'}, {name: 'a_data', components: 4, type: 'Uint16'} ]; @@ -68,7 +69,7 @@ const symbolInterfaces = { } }; -function addVertex(array, x, y, ox, oy, tx, ty, sizeVertex, minzoom, maxzoom, labelminzoom, labelangle) { +function addVertex(array, x, y, ox, oy, labelX, labelY, tx, ty, sizeVertex, minzoom, maxzoom, labelminzoom, labelangle) { array.emplaceBack( // a_pos_offset x, @@ -76,6 +77,10 @@ function addVertex(array, x, y, ox, oy, tx, ty, sizeVertex, minzoom, maxzoom, la Math.round(ox * 64), Math.round(oy * 64), + // a_label_pos + labelX, + labelY, + // a_data tx / 4, // x coordinate of symbol on glyph atlas texture ty / 4, // y coordinate of symbol on glyph atlas texture @@ -608,7 +613,8 @@ class SymbolBucket { textAlongLine, collisionTile.angle, symbolInstance.featureProperties, - symbolInstance.writingModes); + symbolInstance.writingModes, + symbolInstance.anchor); } } @@ -629,7 +635,9 @@ class SymbolBucket { layout['icon-keep-upright'], iconAlongLine, collisionTile.angle, - symbolInstance.featureProperties + symbolInstance.featureProperties, + null, + symbolInstance.anchor ); } } @@ -639,7 +647,7 @@ class SymbolBucket { if (showCollisionBoxes) this.addToDebugBuffers(collisionTile); } - addSymbols(arrays, quads, scale, sizeVertex, keepUpright, alongLine, placementAngle, featureProperties, writingModes) { + addSymbols(arrays, quads, scale, sizeVertex, keepUpright, alongLine, placementAngle, featureProperties, writingModes, labelAnchor) { const elementArray = arrays.elementArray; const layoutVertexArray = arrays.layoutVertexArray; @@ -676,10 +684,10 @@ class SymbolBucket { const segment = arrays.prepareSegment(4); const index = segment.vertexLength; - addVertex(layoutVertexArray, anchorPoint.x, anchorPoint.y, tl.x, tl.y, tex.x, tex.y, sizeVertex, minZoom, maxZoom, placementZoom, glyphAngle); - addVertex(layoutVertexArray, anchorPoint.x, anchorPoint.y, tr.x, tr.y, tex.x + tex.w, tex.y, sizeVertex, minZoom, maxZoom, placementZoom, glyphAngle); - addVertex(layoutVertexArray, anchorPoint.x, anchorPoint.y, bl.x, bl.y, tex.x, tex.y + tex.h, sizeVertex, minZoom, maxZoom, placementZoom, glyphAngle); - addVertex(layoutVertexArray, anchorPoint.x, anchorPoint.y, br.x, br.y, tex.x + tex.w, tex.y + tex.h, sizeVertex, minZoom, maxZoom, placementZoom, glyphAngle); + addVertex(layoutVertexArray, anchorPoint.x, anchorPoint.y, tl.x, tl.y, labelAnchor.x, labelAnchor.y, tex.x, tex.y, sizeVertex, minZoom, maxZoom, placementZoom, glyphAngle); + addVertex(layoutVertexArray, anchorPoint.x, anchorPoint.y, tr.x, tr.y, labelAnchor.x, labelAnchor.y, tex.x + tex.w, tex.y, sizeVertex, minZoom, maxZoom, placementZoom, glyphAngle); + addVertex(layoutVertexArray, anchorPoint.x, anchorPoint.y, bl.x, bl.y, labelAnchor.x, labelAnchor.y, tex.x, tex.y + tex.h, sizeVertex, minZoom, maxZoom, placementZoom, glyphAngle); + addVertex(layoutVertexArray, anchorPoint.x, anchorPoint.y, br.x, br.y, labelAnchor.x, labelAnchor.y, tex.x + tex.w, tex.y + tex.h, sizeVertex, minZoom, maxZoom, placementZoom, glyphAngle); elementArray.emplaceBack(index, index + 1, index + 2); elementArray.emplaceBack(index + 1, index + 2, index + 3); diff --git a/src/shaders/symbol_sdf.vertex.glsl b/src/shaders/symbol_sdf.vertex.glsl index 423460f07e7..40999906a88 100644 --- a/src/shaders/symbol_sdf.vertex.glsl +++ b/src/shaders/symbol_sdf.vertex.glsl @@ -1,6 +1,7 @@ const float PI = 3.141592653589793; attribute vec4 a_pos_offset; +attribute vec2 a_label_pos; attribute vec4 a_data; // contents of a_size vary based on the type of property value @@ -99,8 +100,10 @@ void main() { float fontScale = u_is_text ? v_size / 24.0 : v_size; - highp float perspective_ratio = 1.0; - highp float camera_to_anchor_distance; + vec4 projectedPoint = u_matrix * vec4(a_label_pos, 0, 1); + highp float camera_to_anchor_distance = projectedPoint.w; + highp float perspective_ratio = 1.0 + (1.0 - u_pitch_scale)*((camera_to_anchor_distance / u_camera_to_center_distance) - 1.0);; + //mediump float z = clipUnusedGlyphAngles(v_size, layoutSize, a_minzoom, a_maxzoom); // pitch-alignment: map // rotation-alignment: map | viewport @@ -110,13 +113,9 @@ void main() { lowp float acos = cos(angle); mat2 RotationMatrix = mat2(acos, asin, -1.0 * asin, acos); vec2 offset = RotationMatrix * a_offset; - vec2 extrude = fontScale * u_extrude_scale * (offset / 64.0); - vec4 projectedPoint = u_matrix * vec4(a_pos, 0, 1); - camera_to_anchor_distance = projectedPoint.w; + vec2 extrude = fontScale * u_extrude_scale * perspective_ratio * (offset / 64.0); - perspective_ratio += (1.0 - u_pitch_scale)*((camera_to_anchor_distance / u_camera_to_center_distance) - 1.0); - extrude *= perspective_ratio; - gl_Position+= u_matrix * vec4(a_pos + extrude, 0, 1); + gl_Position = u_matrix * vec4(a_pos + extrude, 0, 1); gl_Position.z += clipUnusedGlyphAngles(v_size*perspective_ratio, layoutSize, a_minzoom, a_maxzoom) * gl_Position.w; // pitch-alignment: viewport // rotation-alignment: map @@ -140,25 +139,16 @@ void main() { vec2 offset = RotationMatrix * (vec2(foreshortening, 1.0) * a_offset); vec2 extrude = fontScale * u_extrude_scale * (offset / 64.0); - gl_Position = u_matrix * vec4(a_pos, 0, 1); - camera_to_anchor_distance = gl_Position.w; - perspective_ratio += (1.0 - u_pitch_scale)*((camera_to_anchor_distance / u_camera_to_center_distance) - 1.0); - extrude *= perspective_ratio; - gl_Position += vec4(extrude, 0, 0); + //extrude *= perspective_ratio; + gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0); gl_Position.z += clipUnusedGlyphAngles(v_size*perspective_ratio, layoutSize, a_minzoom, a_maxzoom) * gl_Position.w; // pitch-alignment: viewport // rotation-alignment: viewport } else { - gl_Position = u_matrix * vec4(a_pos, 0, 1); - camera_to_anchor_distance = gl_Position.w; - - vec2 extrude = fontScale * u_extrude_scale * (a_offset / 64.0); - perspective_ratio += (1.0 - u_pitch_scale)*((camera_to_anchor_distance / u_camera_to_center_distance) - 1.0); - extrude *= perspective_ratio; - - gl_Position += vec4(extrude, 0, 0); + vec2 extrude = fontScale * u_extrude_scale * perspective_ratio * (a_offset / 64.0); + gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0); } v_gamma_scale = gl_Position.w / perspective_ratio;