Skip to content

Commit

Permalink
Enable property functions for icon-size
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Mar 27, 2017
1 parent b4ad4c7 commit 5802621
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
10 changes: 8 additions & 2 deletions debug/textsize.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@
"text-anchor": "left",
"text-allow-overlap": true,
"text-ignore-placement": true,
"icon-allow-overlap": true,
"icon-ignore-placement": true,
"text-max-angle": 180,
"text-field": "abcdefghijklmnopqrstuvwxyz",
"text-font": [
"Open Sans Semibold",
"Arial Unicode MS Bold"
],
"text-size": textSize
"text-size": textSize,
"icon-size": Object.assign({}, textSize, {
stops: textSize.stops.map(s => [s[0], s[1] / 3])
}),
"icon-image": "airfield-11"
},
"paint": {
"text-color": color,
Expand Down Expand Up @@ -85,7 +91,7 @@
}
}
},

"sprite": "mapbox://sprites/mapbox/streets-v10",
"glyphs": "mapbox://fonts/mapbox/{fontstack}/{range}.pbf",
"layers": [
layer('symbol-camera', 0, {
Expand Down
43 changes: 40 additions & 3 deletions src/shaders/symbol_icon.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ attribute vec4 a_pos_offset;
attribute vec2 a_texture_pos;
attribute vec4 a_data;

// icon-size data (see symbol_sdf.vertex.glsl for more)
attribute vec3 a_size;
uniform bool u_is_size_zoom_constant;
uniform bool u_is_size_feature_constant;
uniform mediump float u_size_t; // used to interpolate between zoom stops when size is a composite function
uniform mediump float u_size; // used when size is both zoom and feature constant
uniform mediump float u_layout_size; // used when size is feature constant

#pragma mapbox: define lowp float opacity

// matrix is for the vertex position.
uniform mat4 u_matrix;

uniform bool u_is_text;
uniform mediump float u_zoom;
uniform bool u_rotate_with_map;
uniform vec2 u_extrude_scale;
Expand All @@ -29,10 +38,38 @@ void main() {
mediump float a_minzoom = a_zoom[0];
mediump float a_maxzoom = a_zoom[1];

// u_zoom is the current zoom level adjusted for the change in font size
mediump float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom));
float size;
// In order to accommodate placing labels around corners in
// symbol-placement: line, each glyph in a label could have multiple
// "quad"s only one of which should be shown at a given zoom level.
// The min/max zoom assigned to each quad is based on the font size at
// the vector tile's zoom level, which might be different than at the
// currently rendered zoom level if text-size is zoom-dependent.
// Thus, we compensate for this difference by calculating an adjustment
// based on the scale of rendered text size relative to layout text size.
mediump float layoutSize;
if (!u_is_size_zoom_constant && !u_is_size_feature_constant) {
size = mix(a_size[0], a_size[1], u_size_t) / 10.0;
layoutSize = a_size[2] / 10.0;
} else if (u_is_size_zoom_constant && !u_is_size_feature_constant) {
size = a_size[0] / 10.0;
layoutSize = size;
} else if (!u_is_size_zoom_constant && u_is_size_feature_constant) {
size = u_size;
layoutSize = u_layout_size;
} else {
size = u_size;
layoutSize = u_size;
}

float fontScale = u_is_text ? size / 24.0 : size;

mediump float zoomAdjust = log2(size / layoutSize);
mediump float adjustedZoom = (u_zoom - zoomAdjust) * 10.0;
// result: z = 0 if a_minzoom <= adjustedZoom < a_maxzoom, and 1 otherwise
mediump float z = 2.0 - step(a_minzoom, adjustedZoom) - (1.0 - step(a_maxzoom, adjustedZoom));

vec2 extrude = u_extrude_scale * (a_offset / 64.0);
vec2 extrude = fontScale * u_extrude_scale * (a_offset / 64.0);
if (u_rotate_with_map) {
gl_Position = u_matrix * vec4(a_pos + extrude, 0, 1);
gl_Position.z += z * gl_Position.w;
Expand Down
1 change: 1 addition & 0 deletions src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@
"minimum": 0,
"function": "interpolated",
"zoom-function": true,
"property-function": true,
"doc": "Scale factor for icon. 1 is original size, 3 triples the size.",
"requires": [
"icon-image"
Expand Down

0 comments on commit 5802621

Please sign in to comment.