MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fill_extrusion_pattern.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_pixel_coord_upper;
13 uniform vec2 u_pixel_coord_lower;
14 uniform float u_height_factor;
15 uniform vec4 u_scale;
16 uniform float u_vertical_gradient;
17 uniform lowp float u_opacity;
18 
19 uniform vec3 u_lightcolor;
20 uniform lowp vec3 u_lightpos;
21 uniform lowp float u_lightintensity;
22 
23 layout (location = 0) in vec2 a_pos;
24 layout (location = 1) in vec4 a_normal_ed;
25 
26 out vec2 v_pos_a;
27 out vec2 v_pos_b;
28 out vec4 v_lighting;
29 
30 #ifndef HAS_UNIFORM_u_base
31 uniform lowp float u_base_t;
32 layout (location = 2) in lowp vec2 a_base;
33 out lowp float base;
34 #else
35 uniform lowp float u_base;
36 #endif
37 #ifndef HAS_UNIFORM_u_height
38 uniform lowp float u_height_t;
39 layout (location = 3) in lowp vec2 a_height;
40 out lowp float height;
41 #else
42 uniform lowp float u_height;
43 #endif
44 #ifndef HAS_UNIFORM_u_pattern_from
45 uniform lowp float u_pattern_from_t;
46 layout (location = 4) in lowp vec4 a_pattern_from;
47 out lowp vec4 pattern_from;
48 #else
49 uniform lowp vec4 u_pattern_from;
50 #endif
51 #ifndef HAS_UNIFORM_u_pattern_to
52 uniform lowp float u_pattern_to_t;
53 layout (location = 5) in lowp vec4 a_pattern_to;
54 out lowp vec4 pattern_to;
55 #else
56 uniform lowp vec4 u_pattern_to;
57 #endif
58 
59 void main() {
60  #ifndef HAS_UNIFORM_u_base
61 base = unpack_mix_vec2(a_base, u_base_t);
62 #else
63 lowp float base = u_base;
64 #endif
65  #ifndef HAS_UNIFORM_u_height
66 height = unpack_mix_vec2(a_height, u_height_t);
67 #else
68 lowp float height = u_height;
69 #endif
70  #ifndef HAS_UNIFORM_u_pattern_from
71 pattern_from = a_pattern_from;
72 #else
73 mediump vec4 pattern_from = u_pattern_from;
74 #endif
75  #ifndef HAS_UNIFORM_u_pattern_to
76 pattern_to = a_pattern_to;
77 #else
78 mediump vec4 pattern_to = u_pattern_to;
79 #endif
80 
81  vec2 pattern_tl_a = pattern_from.xy;
82  vec2 pattern_br_a = pattern_from.zw;
83  vec2 pattern_tl_b = pattern_to.xy;
84  vec2 pattern_br_b = pattern_to.zw;
85 
86  float pixelRatio = u_scale.x;
87  float tileRatio = u_scale.y;
88  float fromScale = u_scale.z;
89  float toScale = u_scale.w;
90 
91  vec3 normal = a_normal_ed.xyz;
92  float edgedistance = a_normal_ed.w;
93 
94  vec2 display_size_a = vec2((pattern_br_a.x - pattern_tl_a.x) / pixelRatio, (pattern_br_a.y - pattern_tl_a.y) / pixelRatio);
95  vec2 display_size_b = vec2((pattern_br_b.x - pattern_tl_b.x) / pixelRatio, (pattern_br_b.y - pattern_tl_b.y) / pixelRatio);
96 
97  base = max(0.0, base);
98  height = max(0.0, height);
99 
100  float t = mod(normal.x, 2.0);
101  float z = t > 0.0 ? height : base;
102 
103  gl_Position = u_matrix * vec4(a_pos, z, 1);
104 
105  vec2 pos = normal.x == 1.0 && normal.y == 0.0 && normal.z == 16384.0
106  ? a_pos // extrusion top
107  : vec2(edgedistance, z * u_height_factor); // extrusion side
108 
109  v_pos_a = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, fromScale * display_size_a, tileRatio, pos);
110  v_pos_b = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, toScale * display_size_b, tileRatio, pos);
111 
112  v_lighting = vec4(0.0, 0.0, 0.0, 1.0);
113  float directional = clamp(dot(normal / 16383.0, u_lightpos), 0.0, 1.0);
114  directional = mix((1.0 - u_lightintensity), max((0.5 + u_lightintensity), 1.0), directional);
115 
116  if (normal.y != 0.0) {
117  // This avoids another branching statement, but multiplies by a constant of 0.84 if no vertical gradient,
118  // and otherwise calculates the gradient based on base + height
119  directional *= (
120  (1.0 - u_vertical_gradient) +
121  (u_vertical_gradient * clamp((t + base) * pow(height / 150.0, 0.5), mix(0.7, 0.98, 1.0 - u_lightintensity), 1.0)));
122  }
123 
124  v_lighting.rgb += clamp(directional * u_lightcolor, mix(vec3(0.0), vec3(0.3), 1.0 - u_lightcolor), vec3(1.0));
125  v_lighting *= u_opacity;
126 }
127 )";
128  static constexpr const char* fragment = R"(uniform vec2 u_texsize;
129 uniform float u_fade;
130 
131 uniform sampler2D u_image;
132 
133 in vec2 v_pos_a;
134 in vec2 v_pos_b;
135 in vec4 v_lighting;
136 
137 #ifndef HAS_UNIFORM_u_base
138 in lowp float base;
139 #else
140 uniform lowp float u_base;
141 #endif
142 #ifndef HAS_UNIFORM_u_height
143 in lowp float height;
144 #else
145 uniform lowp float u_height;
146 #endif
147 #ifndef HAS_UNIFORM_u_pattern_from
148 in lowp vec4 pattern_from;
149 #else
150 uniform lowp vec4 u_pattern_from;
151 #endif
152 #ifndef HAS_UNIFORM_u_pattern_to
153 in lowp vec4 pattern_to;
154 #else
155 uniform lowp vec4 u_pattern_to;
156 #endif
157 
158 void main() {
159  #ifdef HAS_UNIFORM_u_base
160 lowp float base = u_base;
161 #endif
162  #ifdef HAS_UNIFORM_u_height
163 lowp float height = u_height;
164 #endif
165  #ifdef HAS_UNIFORM_u_pattern_from
166 mediump vec4 pattern_from = u_pattern_from;
167 #endif
168  #ifdef HAS_UNIFORM_u_pattern_to
169 mediump vec4 pattern_to = u_pattern_to;
170 #endif
171 
172  vec2 pattern_tl_a = pattern_from.xy;
173  vec2 pattern_br_a = pattern_from.zw;
174  vec2 pattern_tl_b = pattern_to.xy;
175  vec2 pattern_br_b = pattern_to.zw;
176 
177  vec2 imagecoord = mod(v_pos_a, 1.0);
178  vec2 pos = mix(pattern_tl_a / u_texsize, pattern_br_a / u_texsize, imagecoord);
179  vec4 color1 = texture(u_image, pos);
180 
181  vec2 imagecoord_b = mod(v_pos_b, 1.0);
182  vec2 pos2 = mix(pattern_tl_b / u_texsize, pattern_br_b / u_texsize, imagecoord_b);
183  vec4 color2 = texture(u_image, pos2);
184 
185  vec4 mixedColor = mix(color1, color2, u_fade);
186 
187  fragColor = mixedColor * v_lighting;
188 
189 #ifdef OVERDRAW_INSPECTOR
190  fragColor = vec4(1.0);
191 #endif
192 }
193 )";
194 };
195 
196 } // namespace shaders
197 } // 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.