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;
16 layout (location = 0) in vec2 a_pos;
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;
24 uniform highp float u_weight;
26 #ifndef HAS_UNIFORM_u_radius
27 uniform lowp float u_radius_t;
28 layout (location = 2) in mediump vec2 a_radius;
30 uniform mediump float u_radius;
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;
38 // Gaussian kernel coefficient: 1 / sqrt(2 * PI)
39 #define GAUSS_COEF 0.3989422804014327
42 #ifndef HAS_UNIFORM_u_weight
43 weight = unpack_mix_vec2(a_weight, u_weight_t);
45 highp float weight = u_weight;
47 #ifndef HAS_UNIFORM_u_radius
48 mediump float radius = unpack_mix_vec2(a_radius, u_radius_t);
50 mediump float radius = u_radius;
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);
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
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;
69 // Pass the varying in units of radius
70 v_extrude = S * unscaled_extrude;
72 // Scale by radius and the zoom-based scale factor to produce actual
74 vec2 extrude = v_extrude * radius * u_extrude_scale;
76 // multiply a_pos by 0.5, since we had it * 2 in order to sneak
78 vec4 pos = vec4(floor(a_pos * 0.5) + extrude, 0, 1);
80 gl_Position = u_matrix * pos;
83 static constexpr
const char* fragment = R
"(uniform highp float u_intensity;
87 #ifndef HAS_UNIFORM_u_weight
88 in highp float weight;
90 uniform highp float u_weight;
93 // Gaussian kernel coefficient: 1 / sqrt(2 * PI)
94 #define GAUSS_COEF 0.3989422804014327
97 #ifdef HAS_UNIFORM_u_weight
98 highp float weight = u_weight;
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);
105 fragColor = vec4(val, 1.0, 1.0, 1.0);
107 #ifdef OVERDRAW_INSPECTOR
108 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.