MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
symbol_text_and_icon.hpp
Go to the documentation of this file.
1 // Generated code, do not modify this file!
2 // Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
3 
4 #pragma once
6 
7 namespace mbgl {
8 namespace shaders {
9 
11  static constexpr const char* vertex = R"(const float PI = 3.141592653589793;
12 
13 layout (location = 0) in vec4 a_pos_offset;
14 layout (location = 1) in vec4 a_data;
15 layout (location = 2) in vec3 a_projected_pos;
16 layout (location = 3) in float a_fade_opacity;
17 
18 // contents of a_size vary based on the type of property value
19 // used for {text,icon}-size.
20 // For constants, a_size is disabled.
21 // For source functions, we bind only one value per vertex: the value of {text,icon}-size evaluated for the current feature.
22 // For composite functions:
23 // [ text-size(lowerZoomStop, feature),
24 // text-size(upperZoomStop, feature) ]
25 uniform bool u_is_size_zoom_constant;
26 uniform bool u_is_size_feature_constant;
27 uniform highp float u_size_t; // used to interpolate between zoom stops when size is a composite function
28 uniform highp float u_size; // used when size is both zoom and feature constant
29 uniform mat4 u_matrix;
30 uniform mat4 u_label_plane_matrix;
31 uniform mat4 u_coord_matrix;
32 uniform bool u_is_text;
33 uniform bool u_pitch_with_map;
34 uniform highp float u_pitch;
35 uniform bool u_rotate_symbol;
36 uniform highp float u_aspect_ratio;
37 uniform highp float u_camera_to_center_distance;
38 uniform float u_fade_change;
39 uniform vec2 u_texsize;
40 uniform vec2 u_texsize_icon;
41 
42 out vec4 v_data0;
43 out vec4 v_data1;
44 
45 #ifndef HAS_UNIFORM_u_fill_color
46 uniform lowp float u_fill_color_t;
47 layout (location = 4) in highp vec4 a_fill_color;
48 out highp vec4 fill_color;
49 #else
50 uniform highp vec4 u_fill_color;
51 #endif
52 #ifndef HAS_UNIFORM_u_halo_color
53 uniform lowp float u_halo_color_t;
54 layout (location = 5) in highp vec4 a_halo_color;
55 out highp vec4 halo_color;
56 #else
57 uniform highp vec4 u_halo_color;
58 #endif
59 #ifndef HAS_UNIFORM_u_opacity
60 uniform lowp float u_opacity_t;
61 layout (location = 6) in lowp vec2 a_opacity;
62 out lowp float opacity;
63 #else
64 uniform lowp float u_opacity;
65 #endif
66 #ifndef HAS_UNIFORM_u_halo_width
67 uniform lowp float u_halo_width_t;
68 layout (location = 7) in lowp vec2 a_halo_width;
69 out lowp float halo_width;
70 #else
71 uniform lowp float u_halo_width;
72 #endif
73 #ifndef HAS_UNIFORM_u_halo_blur
74 uniform lowp float u_halo_blur_t;
75 layout (location = 8) in lowp vec2 a_halo_blur;
76 out lowp float halo_blur;
77 #else
78 uniform lowp float u_halo_blur;
79 #endif
80 
81 void main() {
82  #ifndef HAS_UNIFORM_u_fill_color
83 fill_color = unpack_mix_color(a_fill_color, u_fill_color_t);
84 #else
85 highp vec4 fill_color = u_fill_color;
86 #endif
87  #ifndef HAS_UNIFORM_u_halo_color
88 halo_color = unpack_mix_color(a_halo_color, u_halo_color_t);
89 #else
90 highp vec4 halo_color = u_halo_color;
91 #endif
92  #ifndef HAS_UNIFORM_u_opacity
93 opacity = unpack_mix_vec2(a_opacity, u_opacity_t);
94 #else
95 lowp float opacity = u_opacity;
96 #endif
97  #ifndef HAS_UNIFORM_u_halo_width
98 halo_width = unpack_mix_vec2(a_halo_width, u_halo_width_t);
99 #else
100 lowp float halo_width = u_halo_width;
101 #endif
102  #ifndef HAS_UNIFORM_u_halo_blur
103 halo_blur = unpack_mix_vec2(a_halo_blur, u_halo_blur_t);
104 #else
105 lowp float halo_blur = u_halo_blur;
106 #endif
107 
108  vec2 a_pos = a_pos_offset.xy;
109  vec2 a_offset = a_pos_offset.zw;
110 
111  vec2 a_tex = a_data.xy;
112  vec2 a_size = a_data.zw;
113 
114  float a_size_min = floor(a_size[0] * 0.5);
115  float is_sdf = a_size[0] - 2.0 * a_size_min;
116 
117  highp float segment_angle = -a_projected_pos[2];
118  float size;
119 
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;
124  } else {
125  size = u_size;
126  }
127 
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
142  4.0);
143 
144  size *= perspective_ratio;
145 
146  float fontScale = size / 24.0;
147 
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);
154 
155  vec2 a = projectedPoint.xy / projectedPoint.w;
156  vec2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;
157 
158  symbol_rotation = atan((b.y - a.y) / u_aspect_ratio, b.x - a.x);
159  }
160 
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);
164 
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), 0.0, 1.0);
167  float gamma_scale = gl_Position.w;
168 
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));
172 
173  v_data0.xy = a_tex / u_texsize;
174  v_data0.zw = a_tex / u_texsize_icon;
175  v_data1 = vec4(gamma_scale, size, interpolated_fade_opacity, is_sdf);
176 }
177 )";
178  static constexpr const char* fragment = R"(#define SDF_PX 8.0
179 
180 #define SDF 1.0
181 #define ICON 0.0
182 
183 uniform bool u_is_halo;
184 uniform sampler2D u_texture;
185 uniform sampler2D u_texture_icon;
186 uniform highp float u_gamma_scale;
187 uniform lowp float u_device_pixel_ratio;
188 
189 in vec4 v_data0;
190 in vec4 v_data1;
191 
192 #ifndef HAS_UNIFORM_u_fill_color
193 in highp vec4 fill_color;
194 #else
195 uniform highp vec4 u_fill_color;
196 #endif
197 #ifndef HAS_UNIFORM_u_halo_color
198 in highp vec4 halo_color;
199 #else
200 uniform highp vec4 u_halo_color;
201 #endif
202 #ifndef HAS_UNIFORM_u_opacity
203 in lowp float opacity;
204 #else
205 uniform lowp float u_opacity;
206 #endif
207 #ifndef HAS_UNIFORM_u_halo_width
208 in lowp float halo_width;
209 #else
210 uniform lowp float u_halo_width;
211 #endif
212 #ifndef HAS_UNIFORM_u_halo_blur
213 in lowp float halo_blur;
214 #else
215 uniform lowp float u_halo_blur;
216 #endif
217 
218 void main() {
219  #ifdef HAS_UNIFORM_u_fill_color
220 highp vec4 fill_color = u_fill_color;
221 #endif
222  #ifdef HAS_UNIFORM_u_halo_color
223 highp vec4 halo_color = u_halo_color;
224 #endif
225  #ifdef HAS_UNIFORM_u_opacity
226 lowp float opacity = u_opacity;
227 #endif
228  #ifdef HAS_UNIFORM_u_halo_width
229 lowp float halo_width = u_halo_width;
230 #endif
231  #ifdef HAS_UNIFORM_u_halo_blur
232 lowp float halo_blur = u_halo_blur;
233 #endif
234 
235  float fade_opacity = v_data1[2];
236 
237  if (v_data1.w == ICON) {
238  vec2 tex_icon = v_data0.zw;
239  lowp float alpha = opacity * fade_opacity;
240  fragColor = texture(u_texture_icon, tex_icon) * alpha;
241 
242 #ifdef OVERDRAW_INSPECTOR
243  fragColor = vec4(1.0);
244 #endif
245  return;
246  }
247 
248  vec2 tex = v_data0.xy;
249 
250  float EDGE_GAMMA = 0.105 / u_device_pixel_ratio;
251 
252  float gamma_scale = v_data1.x;
253  float size = v_data1.y;
254 
255  float fontScale = size / 24.0;
256 
257  lowp vec4 color = fill_color;
258  highp float gamma = EDGE_GAMMA / (fontScale * u_gamma_scale);
259  lowp float buff = (256.0 - 64.0) / 256.0;
260  if (u_is_halo) {
261  color = halo_color;
262  gamma = (halo_blur * 1.19 / SDF_PX + EDGE_GAMMA) / (fontScale * u_gamma_scale);
263  buff = (6.0 - halo_width / fontScale) / SDF_PX;
264  }
265 
266  lowp float dist = texture(u_texture, tex).a;
267  highp float gamma_scaled = gamma * gamma_scale;
268  highp float alpha = smoothstep(buff - gamma_scaled, buff + gamma_scaled, dist);
269 
270  fragColor = color * (alpha * opacity * fade_opacity);
271 
272 #ifdef OVERDRAW_INSPECTOR
273  fragColor = vec4(1.0);
274 #endif
275 }
276 )";
277 };
278 
279 } // namespace shaders
280 } // namespace mbgl
@ OpenGL
The OpenGL API backend.
BuiltIn
This enum is used with the ShaderSource template to select source code for the desired program and gr...
Definition: actor.hpp:15
Select shader source based on a program type and a desired graphics API.