11 static constexpr
const char* vertex = R
"(uniform mat4 u_matrix;
13 layout (location = 0) in vec2 a_pos;
14 layout (location = 1) in vec2 a_texture_pos;
19 gl_Position = u_matrix * vec4(a_pos, 0, 1);
20 v_pos = a_texture_pos / 8192.0;
23 static constexpr
const char* fragment = R
"(uniform sampler2D u_image;
26 uniform vec2 u_latrange;
28 uniform vec4 u_shadow;
29 uniform vec4 u_highlight;
30 uniform vec4 u_accent;
32 #define PI 3.141592653589793
35 vec4 pixel = texture(u_image, v_pos);
37 vec2 deriv = ((pixel.rg * 2.0) - 1.0);
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);
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;
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;
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;
71 #ifdef OVERDRAW_INSPECTOR
72 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.