11 static constexpr
const char* vertex = R
"(const float PI = 3.141592653589793;
13 layout (location = 0) in vec4 a_pos_offset;
14 layout (location = 1) in vec4 a_data;
15 layout (location = 2) in vec4 a_pixeloffset;
16 layout (location = 3) in vec3 a_projected_pos;
17 layout (location = 4) in float a_fade_opacity;
19 // contents of a_size vary based on the type of property value
20 // used for {text,icon}-size.
21 // For constants, a_size is disabled.
22 // For source functions, we bind only one value per vertex: the value of {text,icon}-size evaluated for the current feature.
23 // For composite functions:
24 // [ text-size(lowerZoomStop, feature),
25 // text-size(upperZoomStop, feature) ]
26 uniform bool u_is_size_zoom_constant;
27 uniform bool u_is_size_feature_constant;
28 uniform highp float u_size_t; // used to interpolate between zoom stops when size is a composite function
29 uniform highp float u_size; // used when size is both zoom and feature constant
30 uniform mat4 u_matrix;
31 uniform mat4 u_label_plane_matrix;
32 uniform mat4 u_coord_matrix;
33 uniform bool u_is_text;
34 uniform bool u_pitch_with_map;
35 uniform highp float u_pitch;
36 uniform bool u_rotate_symbol;
37 uniform highp float u_aspect_ratio;
38 uniform highp float u_camera_to_center_distance;
39 uniform float u_fade_change;
40 uniform vec2 u_texsize;
45 #ifndef HAS_UNIFORM_u_fill_color
46 uniform lowp float u_fill_color_t;
47 layout (location = 5) in highp vec4 a_fill_color;
48 out highp vec4 fill_color;
50 uniform highp vec4 u_fill_color;
52 #ifndef HAS_UNIFORM_u_halo_color
53 uniform lowp float u_halo_color_t;
54 layout (location = 6) in highp vec4 a_halo_color;
55 out highp vec4 halo_color;
57 uniform highp vec4 u_halo_color;
59 #ifndef HAS_UNIFORM_u_opacity
60 uniform lowp float u_opacity_t;
61 layout (location = 7) in lowp vec2 a_opacity;
62 out lowp float opacity;
64 uniform lowp float u_opacity;
66 #ifndef HAS_UNIFORM_u_halo_width
67 uniform lowp float u_halo_width_t;
68 layout (location = 8) in lowp vec2 a_halo_width;
69 out lowp float halo_width;
71 uniform lowp float u_halo_width;
73 #ifndef HAS_UNIFORM_u_halo_blur
74 uniform lowp float u_halo_blur_t;
75 layout (location = 9) in lowp vec2 a_halo_blur;
76 out lowp float halo_blur;
78 uniform lowp float u_halo_blur;
82 #ifndef HAS_UNIFORM_u_fill_color
83 fill_color = unpack_mix_color(a_fill_color, u_fill_color_t);
85 highp vec4 fill_color = u_fill_color;
87 #ifndef HAS_UNIFORM_u_halo_color
88 halo_color = unpack_mix_color(a_halo_color, u_halo_color_t);
90 highp vec4 halo_color = u_halo_color;
92 #ifndef HAS_UNIFORM_u_opacity
93 opacity = unpack_mix_vec2(a_opacity, u_opacity_t);
95 lowp float opacity = u_opacity;
97 #ifndef HAS_UNIFORM_u_halo_width
98 halo_width = unpack_mix_vec2(a_halo_width, u_halo_width_t);
100 lowp float halo_width = u_halo_width;
102 #ifndef HAS_UNIFORM_u_halo_blur
103 halo_blur = unpack_mix_vec2(a_halo_blur, u_halo_blur_t);
105 lowp float halo_blur = u_halo_blur;
108 vec2 a_pos = a_pos_offset.xy;
109 vec2 a_offset = a_pos_offset.zw;
111 vec2 a_tex = a_data.xy;
112 vec2 a_size = a_data.zw;
114 float a_size_min = floor(a_size[0] * 0.5);
115 vec2 a_pxoffset = a_pixeloffset.xy;
117 highp float segment_angle = -a_projected_pos[2];
120 if (!u_is_size_zoom_constant && !u_is_size_feature_constant) {
121 size = mix(a_size_min, a_size[1], u_size_t) / 128.0;
122 } else if (u_is_size_zoom_constant && !u_is_size_feature_constant) {
123 size = a_size_min / 128.0;
128 vec4 projectedPoint = u_matrix * vec4(a_pos, 0, 1);
129 highp float camera_to_anchor_distance = projectedPoint.w;
130 // If the label is pitched with the map, layout is done in pitched space,
131 // which makes labels in the distance smaller relative to viewport space.
132 // We counteract part of that effect by multiplying by the perspective ratio.
133 // If the label isn't pitched with the map, we do layout in viewport space,
134 // which makes labels in the distance larger relative to the features around
135 // them. We counteract part of that effect by dividing by the perspective ratio.
136 highp float distance_ratio = u_pitch_with_map ?
137 camera_to_anchor_distance / u_camera_to_center_distance :
138 u_camera_to_center_distance / camera_to_anchor_distance;
139 highp float perspective_ratio = clamp(
140 0.5 + 0.5 * distance_ratio,
141 0.0, // Prevents oversized near-field symbols in pitched/overzoomed tiles
144 size *= perspective_ratio;
146 float fontScale = u_is_text ? size / 24.0 : size;
148 highp float symbol_rotation = 0.0;
149 if (u_rotate_symbol) {
150 // Point labels with 'rotation-alignment: map' are horizontal with respect to tile units
151 // To figure out that angle in projected space, we draw a short horizontal line in tile
152 // space, project it, and measure its angle in projected space.
153 vec4 offsetProjectedPoint = u_matrix * vec4(a_pos + vec2(1, 0), 0, 1);
155 vec2 a = projectedPoint.xy / projectedPoint.w;
156 vec2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;
158 symbol_rotation = atan((b.y - a.y) / u_aspect_ratio, b.x - a.x);
161 highp float angle_sin = sin(segment_angle + symbol_rotation);
162 highp float angle_cos = cos(segment_angle + symbol_rotation);
163 mat2 rotation_matrix = mat2(angle_cos, -1.0 * angle_sin, angle_sin, angle_cos);
165 vec4 projected_pos = u_label_plane_matrix * vec4(a_projected_pos.xy, 0.0, 1.0);
166 gl_Position = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w + rotation_matrix * (a_offset / 32.0 * fontScale + a_pxoffset), 0.0, 1.0);
167 float gamma_scale = gl_Position.w;
169 vec2 fade_opacity = unpack_opacity(a_fade_opacity);
170 float fade_change = fade_opacity[1] > 0.5 ? u_fade_change : -u_fade_change;
171 float interpolated_fade_opacity = max(0.0, min(1.0, fade_opacity[0] + fade_change));
173 v_data0 = a_tex / u_texsize;
174 v_data1 = vec3(gamma_scale, size, interpolated_fade_opacity);
177 static constexpr
const char* fragment = R
"(#define SDF_PX 8.0
179 uniform bool u_is_halo;
180 uniform sampler2D u_texture;
181 uniform highp float u_gamma_scale;
182 uniform lowp float u_device_pixel_ratio;
183 uniform bool u_is_text;
188 #ifndef HAS_UNIFORM_u_fill_color
189 in highp vec4 fill_color;
191 uniform highp vec4 u_fill_color;
193 #ifndef HAS_UNIFORM_u_halo_color
194 in highp vec4 halo_color;
196 uniform highp vec4 u_halo_color;
198 #ifndef HAS_UNIFORM_u_opacity
199 in lowp float opacity;
201 uniform lowp float u_opacity;
203 #ifndef HAS_UNIFORM_u_halo_width
204 in lowp float halo_width;
206 uniform lowp float u_halo_width;
208 #ifndef HAS_UNIFORM_u_halo_blur
209 in lowp float halo_blur;
211 uniform lowp float u_halo_blur;
215 #ifdef HAS_UNIFORM_u_fill_color
216 highp vec4 fill_color = u_fill_color;
218 #ifdef HAS_UNIFORM_u_halo_color
219 highp vec4 halo_color = u_halo_color;
221 #ifdef HAS_UNIFORM_u_opacity
222 lowp float opacity = u_opacity;
224 #ifdef HAS_UNIFORM_u_halo_width
225 lowp float halo_width = u_halo_width;
227 #ifdef HAS_UNIFORM_u_halo_blur
228 lowp float halo_blur = u_halo_blur;
231 float EDGE_GAMMA = 0.105 / u_device_pixel_ratio;
233 vec2 tex = v_data0.xy;
234 float gamma_scale = v_data1.x;
235 float size = v_data1.y;
236 float fade_opacity = v_data1[2];
238 float fontScale = u_is_text ? size / 24.0 : size;
240 lowp vec4 color = fill_color;
241 highp float gamma = EDGE_GAMMA / (fontScale * u_gamma_scale);
242 lowp float buff = (256.0 - 64.0) / 256.0;
245 gamma = (halo_blur * 1.19 / SDF_PX + EDGE_GAMMA) / (fontScale * u_gamma_scale);
246 buff = (6.0 - halo_width / fontScale) / SDF_PX;
249 lowp float dist = texture(u_texture, tex).a;
250 highp float gamma_scaled = gamma * gamma_scale;
251 highp float alpha = smoothstep(buff - gamma_scaled, buff + gamma_scaled, dist);
253 fragColor = color * (alpha * opacity * fade_opacity);
255 #ifdef OVERDRAW_INSPECTOR
256 fragColor = vec4(1.0);
@ OpenGL
The OpenGL API backend.
BuiltIn
This enum is used with the ShaderSource template to select source code for the desired program and gr...
Select shader source based on a program type and a desired graphics API.