MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hillshade_prepare.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 vec2 u_dimension;
13 
14 layout (location = 0) in vec2 a_pos;
15 layout (location = 1) in vec2 a_texture_pos;
16 
17 out vec2 v_pos;
18 
19 void main() {
20  gl_Position = u_matrix * vec4(a_pos, 0, 1);
21 
22  highp vec2 epsilon = 1.0 / u_dimension;
23  float scale = (u_dimension.x - 2.0) / u_dimension.x;
24  v_pos = (a_texture_pos / 8192.0) * scale + epsilon;
25 }
26 )";
27  static constexpr const char* fragment = R"(#ifdef GL_ES
28 precision highp float;
29 #endif
30 
31 uniform sampler2D u_image;
32 in vec2 v_pos;
33 uniform vec2 u_dimension;
34 uniform float u_zoom;
35 uniform float u_maxzoom;
36 uniform vec4 u_unpack;
37 
38 float getElevation(vec2 coord, float bias) {
39  // Convert encoded elevation value to meters
40  vec4 data = texture(u_image, coord) * 255.0;
41  data.a = -1.0;
42  return dot(data, u_unpack) / 4.0;
43 }
44 
45 void main() {
46  vec2 epsilon = 1.0 / u_dimension;
47 
48  // queried pixels:
49  // +-----------+
50  // | | | |
51  // | a | b | c |
52  // | | | |
53  // +-----------+
54  // | | | |
55  // | d | e | f |
56  // | | | |
57  // +-----------+
58  // | | | |
59  // | g | h | i |
60  // | | | |
61  // +-----------+
62 
63  float a = getElevation(v_pos + vec2(-epsilon.x, -epsilon.y), 0.0);
64  float b = getElevation(v_pos + vec2(0, -epsilon.y), 0.0);
65  float c = getElevation(v_pos + vec2(epsilon.x, -epsilon.y), 0.0);
66  float d = getElevation(v_pos + vec2(-epsilon.x, 0), 0.0);
67  float e = getElevation(v_pos, 0.0);
68  float f = getElevation(v_pos + vec2(epsilon.x, 0), 0.0);
69  float g = getElevation(v_pos + vec2(-epsilon.x, epsilon.y), 0.0);
70  float h = getElevation(v_pos + vec2(0, epsilon.y), 0.0);
71  float i = getElevation(v_pos + vec2(epsilon.x, epsilon.y), 0.0);
72 
73  // here we divide the x and y slopes by 8 * pixel size
74  // where pixel size (aka meters/pixel) is:
75  // circumference of the world / (pixels per tile * number of tiles)
76  // which is equivalent to: 8 * 40075016.6855785 / (512 * pow(2, u_zoom))
77  // which can be reduced to: pow(2, 19.25619978527 - u_zoom)
78  // we want to vertically exaggerate the hillshading though, because otherwise
79  // it is barely noticeable at low zooms. to do this, we multiply this by some
80  // scale factor pow(2, (u_zoom - u_maxzoom) * a) where a is an arbitrary value
81  // Here we use a=0.3 which works out to the expression below. see
82  // nickidlugash's awesome breakdown for more info
83  // https://github.com/mapbox/mapbox-gl-js/pull/5286#discussion_r148419556
84  float exaggeration = u_zoom < 2.0 ? 0.4 : u_zoom < 4.5 ? 0.35 : 0.3;
85 
86  vec2 deriv = vec2(
87  (c + f + f + i) - (a + d + d + g),
88  (g + h + h + i) - (a + b + b + c)
89  ) / pow(2.0, (u_zoom - u_maxzoom) * exaggeration + 19.2562 - u_zoom);
90 
91  fragColor = clamp(vec4(
92  deriv.x / 2.0 + 0.5,
93  deriv.y / 2.0 + 0.5,
94  1.0,
95  1.0), 0.0, 1.0);
96 
97 #ifdef OVERDRAW_INSPECTOR
98  fragColor = vec4(1.0);
99 #endif
100 }
101 )";
102 };
103 
104 } // namespace shaders
105 } // 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.