MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hillshade.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 
13 layout (location = 0) in vec2 a_pos;
14 layout (location = 1) in vec2 a_texture_pos;
15 
16 out vec2 v_pos;
17 
18 void main() {
19  gl_Position = u_matrix * vec4(a_pos, 0, 1);
20  v_pos = a_texture_pos / 8192.0;
21 }
22 )";
23  static constexpr const char* fragment = R"(uniform sampler2D u_image;
24 in vec2 v_pos;
25 
26 uniform vec2 u_latrange;
27 uniform vec2 u_light;
28 uniform vec4 u_shadow;
29 uniform vec4 u_highlight;
30 uniform vec4 u_accent;
31 
32 #define PI 3.141592653589793
33 
34 void main() {
35  vec4 pixel = texture(u_image, v_pos);
36 
37  vec2 deriv = ((pixel.rg * 2.0) - 1.0);
38 
39  // We divide the slope by a scale factor based on the cosin of the pixel's approximate latitude
40  // to account for mercator projection distortion. see #4807 for details
41  float scaleFactor = cos(radians((u_latrange[0] - u_latrange[1]) * (1.0 - v_pos.y) + u_latrange[1]));
42  // We also multiply the slope by an arbitrary z-factor of 1.25
43  float slope = atan(1.25 * length(deriv) / scaleFactor);
44  float aspect = deriv.x != 0.0 ? atan(deriv.y, -deriv.x) : PI / 2.0 * (deriv.y > 0.0 ? 1.0 : -1.0);
45 
46  float intensity = u_light.x;
47  // We add PI to make this property match the global light object, which adds PI/2 to the light's azimuthal
48  // position property to account for 0deg corresponding to north/the top of the viewport in the style spec
49  // and the original shader was written to accept (-illuminationDirection - 90) as the azimuthal.
50  float azimuth = u_light.y + PI;
51 
52  // We scale the slope exponentially based on intensity, using a calculation similar to
53  // the exponential interpolation function in the style spec:
54  // https://github.com/mapbox/mapbox-gl-js/blob/master/src/style-spec/expression/definitions/interpolate.js#L217-L228
55  // so that higher intensity values create more opaque hillshading.
56  float base = 1.875 - intensity * 1.75;
57  float maxValue = 0.5 * PI;
58  float scaledSlope = intensity != 0.5 ? ((pow(base, slope) - 1.0) / (pow(base, maxValue) - 1.0)) * maxValue : slope;
59 
60  // The accent color is calculated with the cosine of the slope while the shade color is calculated with the sine
61  // so that the accent color's rate of change eases in while the shade color's eases out.
62  float accent = cos(scaledSlope);
63  // We multiply both the accent and shade color by a clamped intensity value
64  // so that intensities >= 0.5 do not additionally affect the color values
65  // while intensity values < 0.5 make the overall color more transparent.
66  vec4 accent_color = (1.0 - accent) * u_accent * clamp(intensity * 2.0, 0.0, 1.0);
67  float shade = abs(mod((aspect + azimuth) / PI + 0.5, 2.0) - 1.0);
68  vec4 shade_color = mix(u_shadow, u_highlight, shade) * sin(scaledSlope) * clamp(intensity * 2.0, 0.0, 1.0);
69  fragColor = accent_color * (1.0 - shade_color.a) + shade_color;
70 
71 #ifdef OVERDRAW_INSPECTOR
72  fragColor = vec4(1.0);
73 #endif
74 }
75 )";
76 };
77 
78 } // namespace shaders
79 } // 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.