Skip to content

Commit

Permalink
WIP on curved labels: calculate their zoom adjustment based on the
Browse files Browse the repository at this point in the history
 label anchor instead of the glyph anchor, so all the glyphs can behave
 consistently.
  • Loading branch information
ChrisLoer committed Apr 5, 2017
1 parent 7673e88 commit d203ef7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
24 changes: 16 additions & 8 deletions src/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
];

Expand Down Expand Up @@ -68,14 +69,18 @@ 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,
y,
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
Expand Down Expand Up @@ -608,7 +613,8 @@ class SymbolBucket {
textAlongLine,
collisionTile.angle,
symbolInstance.featureProperties,
symbolInstance.writingModes);
symbolInstance.writingModes,
symbolInstance.anchor);
}
}

Expand All @@ -629,7 +635,9 @@ class SymbolBucket {
layout['icon-keep-upright'],
iconAlongLine,
collisionTile.angle,
symbolInstance.featureProperties
symbolInstance.featureProperties,
null,
symbolInstance.anchor
);
}
}
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
32 changes: 11 additions & 21 deletions src/shaders/symbol_sdf.vertex.glsl
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit d203ef7

Please sign in to comment.