MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
heatmap.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"(uniform mat4 u_matrix;
12 uniform float u_extrude_scale;
13 uniform float u_opacity;
14 uniform float u_intensity;
15 
16 layout (location = 0) in vec2 a_pos;
17 out vec2 v_extrude;
18 
19 #ifndef HAS_UNIFORM_u_weight
20 uniform lowp float u_weight_t;
21 layout (location = 1) in highp vec2 a_weight;
22 out highp float weight;
23 #else
24 uniform highp float u_weight;
25 #endif
26 #ifndef HAS_UNIFORM_u_radius
27 uniform lowp float u_radius_t;
28 layout (location = 2) in mediump vec2 a_radius;
29 #else
30 uniform mediump float u_radius;
31 #endif
32 
33 // Effective "0" in the kernel density texture to adjust the kernel size to;
34 // this empirically chosen number minimizes artifacts on overlapping kernels
35 // for typical heatmap cases (assuming clustered source)
36 const highp float ZERO = 1.0 / 255.0 / 16.0;
37 
38 // Gaussian kernel coefficient: 1 / sqrt(2 * PI)
39 #define GAUSS_COEF 0.3989422804014327
40 
41 void main(void) {
42  #ifndef HAS_UNIFORM_u_weight
43 weight = unpack_mix_vec2(a_weight, u_weight_t);
44 #else
45 highp float weight = u_weight;
46 #endif
47  #ifndef HAS_UNIFORM_u_radius
48 mediump float radius = unpack_mix_vec2(a_radius, u_radius_t);
49 #else
50 mediump float radius = u_radius;
51 #endif
52 
53  // unencode the extrusion vector that we snuck into the a_pos vector
54  vec2 unscaled_extrude = vec2(mod(a_pos, 2.0) * 2.0 - 1.0);
55 
56  // This 'extrude' comes in ranging from [-1, -1], to [1, 1]. We'll use
57  // it to produce the vertices of a square mesh framing the point feature
58  // we're adding to the kernel density texture. We'll also pass it as
59  // a varying, so that the fragment shader can determine the distance of
60  // each fragment from the point feature.
61  // Before we do so, we need to scale it up sufficiently so that the
62  // kernel falls effectively to zero at the edge of the mesh.
63  // That is, we want to know S such that
64  // weight * u_intensity * GAUSS_COEF * exp(-0.5 * 3.0^2 * S^2) == ZERO
65  // Which solves to:
66  // S = sqrt(-2.0 * log(ZERO / (weight * u_intensity * GAUSS_COEF))) / 3.0
67  float S = sqrt(-2.0 * log(ZERO / weight / u_intensity / GAUSS_COEF)) / 3.0;
68 
69  // Pass the varying in units of radius
70  v_extrude = S * unscaled_extrude;
71 
72  // Scale by radius and the zoom-based scale factor to produce actual
73  // mesh position
74  vec2 extrude = v_extrude * radius * u_extrude_scale;
75 
76  // multiply a_pos by 0.5, since we had it * 2 in order to sneak
77  // in extrusion data
78  vec4 pos = vec4(floor(a_pos * 0.5) + extrude, 0, 1);
79 
80  gl_Position = u_matrix * pos;
81 }
82 )";
83  static constexpr const char* fragment = R"(uniform highp float u_intensity;
84 
85 in vec2 v_extrude;
86 
87 #ifndef HAS_UNIFORM_u_weight
88 in highp float weight;
89 #else
90 uniform highp float u_weight;
91 #endif
92 
93 // Gaussian kernel coefficient: 1 / sqrt(2 * PI)
94 #define GAUSS_COEF 0.3989422804014327
95 
96 void main() {
97  #ifdef HAS_UNIFORM_u_weight
98 highp float weight = u_weight;
99 #endif
100 
101  // Kernel density estimation with a Gaussian kernel of size 5x5
102  float d = -0.5 * 3.0 * 3.0 * dot(v_extrude, v_extrude);
103  float val = weight * u_intensity * GAUSS_COEF * exp(d);
104 
105  fragColor = vec4(val, 1.0, 1.0, 1.0);
106 
107 #ifdef OVERDRAW_INSPECTOR
108  fragColor = vec4(1.0);
109 #endif
110 }
111 )";
112 };
113 
114 } // namespace shaders
115 } // 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.