MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
collision_circle.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"(layout (location = 0) in vec2 a_pos;
12 layout (location = 1) in vec2 a_anchor_pos;
13 layout (location = 2) in vec2 a_extrude;
14 layout (location = 3) in vec2 a_placed;
15 
16 uniform mat4 u_matrix;
17 uniform vec2 u_extrude_scale;
18 uniform float u_camera_to_center_distance;
19 
20 out float v_placed;
21 out float v_notUsed;
22 out float v_radius;
23 out vec2 v_extrude;
24 out vec2 v_extrude_scale;
25 
26 void main() {
27  vec4 projectedPoint = u_matrix * vec4(a_anchor_pos, 0, 1);
28  highp float camera_to_anchor_distance = projectedPoint.w;
29  highp float collision_perspective_ratio = clamp(
30  0.5 + 0.5 * (u_camera_to_center_distance / camera_to_anchor_distance),
31  0.0, // Prevents oversized near-field circles in pitched/overzoomed tiles
32  4.0);
33 
34  gl_Position = u_matrix * vec4(a_pos, 0.0, 1.0);
35 
36  highp float padding_factor = 1.2; // Pad the vertices slightly to make room for anti-alias blur
37  gl_Position.xy += a_extrude * u_extrude_scale * padding_factor * gl_Position.w * collision_perspective_ratio;
38 
39  v_placed = a_placed.x;
40  v_notUsed = a_placed.y;
41  v_radius = abs(a_extrude.y); // We don't pitch the circles, so both units of the extrusion vector are equal in magnitude to the radius
42 
43  v_extrude = a_extrude * padding_factor;
44  v_extrude_scale = u_extrude_scale * u_camera_to_center_distance * collision_perspective_ratio;
45 }
46 )";
47  static constexpr const char* fragment = R"(uniform float u_overscale_factor;
48 
49 in float v_placed;
50 in float v_notUsed;
51 in float v_radius;
52 in vec2 v_extrude;
53 in vec2 v_extrude_scale;
54 
55 void main() {
56  float alpha = 0.5;
57 
58  // Red = collision, hide label
59  vec4 color = vec4(1.0, 0.0, 0.0, 1.0) * alpha;
60 
61  // Blue = no collision, label is showing
62  if (v_placed > 0.5) {
63  color = vec4(0.0, 0.0, 1.0, 0.5) * alpha;
64  }
65 
66  if (v_notUsed > 0.5) {
67  // This box not used, fade it out
68  color *= .2;
69  }
70 
71  float extrude_scale_length = length(v_extrude_scale);
72  float extrude_length = length(v_extrude) * extrude_scale_length;
73  float stroke_width = 15.0 * extrude_scale_length / u_overscale_factor;
74  float radius = v_radius * extrude_scale_length;
75 
76  float distance_to_edge = abs(extrude_length - radius);
77  float opacity_t = smoothstep(-stroke_width, 0.0, -distance_to_edge);
78 
79  fragColor = opacity_t * color;
80 }
81 )";
82 };
83 
84 } // namespace shaders
85 } // 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.