MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
symbol_sdf_text.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 vec4 a_pixeloffset;
16 layout (location = 3) in vec3 a_projected_pos;
17 layout (location = 4) in float a_fade_opacity;
18 
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;
41 
42 out vec2 v_data0;
43 out vec3 v_data1;
44 
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;
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 = 6) 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 = 7) 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 = 8) 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 = 9) 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  vec2 a_pxoffset = a_pixeloffset.xy;
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 = u_is_text ? size / 24.0 : size;
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 + a_pxoffset), 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 = a_tex / u_texsize;
174  v_data1 = vec3(gamma_scale, size, interpolated_fade_opacity);
175 }
176 )";
177  static constexpr const char* fragment = R"(#define SDF_PX 8.0
178 
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;
184 
185 in vec2 v_data0;
186 in vec3 v_data1;
187 
188 #ifndef HAS_UNIFORM_u_fill_color
189 in highp vec4 fill_color;
190 #else
191 uniform highp vec4 u_fill_color;
192 #endif
193 #ifndef HAS_UNIFORM_u_halo_color
194 in highp vec4 halo_color;
195 #else
196 uniform highp vec4 u_halo_color;
197 #endif
198 #ifndef HAS_UNIFORM_u_opacity
199 in lowp float opacity;
200 #else
201 uniform lowp float u_opacity;
202 #endif
203 #ifndef HAS_UNIFORM_u_halo_width
204 in lowp float halo_width;
205 #else
206 uniform lowp float u_halo_width;
207 #endif
208 #ifndef HAS_UNIFORM_u_halo_blur
209 in lowp float halo_blur;
210 #else
211 uniform lowp float u_halo_blur;
212 #endif
213 
214 void main() {
215  #ifdef HAS_UNIFORM_u_fill_color
216 highp vec4 fill_color = u_fill_color;
217 #endif
218  #ifdef HAS_UNIFORM_u_halo_color
219 highp vec4 halo_color = u_halo_color;
220 #endif
221  #ifdef HAS_UNIFORM_u_opacity
222 lowp float opacity = u_opacity;
223 #endif
224  #ifdef HAS_UNIFORM_u_halo_width
225 lowp float halo_width = u_halo_width;
226 #endif
227  #ifdef HAS_UNIFORM_u_halo_blur
228 lowp float halo_blur = u_halo_blur;
229 #endif
230 
231  float EDGE_GAMMA = 0.105 / u_device_pixel_ratio;
232 
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];
237 
238  float fontScale = u_is_text ? size / 24.0 : size;
239 
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;
243  if (u_is_halo) {
244  color = halo_color;
245  gamma = (halo_blur * 1.19 / SDF_PX + EDGE_GAMMA) / (fontScale * u_gamma_scale);
246  buff = (6.0 - halo_width / fontScale) / SDF_PX;
247  }
248 
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);
252 
253  fragColor = color * (alpha * opacity * fade_opacity);
254 
255 #ifdef OVERDRAW_INSPECTOR
256  fragColor = vec4(1.0);
257 #endif
258 }
259 )";
260 };
261 
262 } // namespace shaders
263 } // 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.