mln-rs-engine 0.0.18

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! The GLSL, for Vulkan and D3D12, of the fill extrusions' programs.

use mln_gfx::shaders::source::GlslSource;

use crate::layers::pattern::glsl::pattern_pos;

/// `wall_normal`, the normal at a wall's end from its outline vertex's
/// `decimals_ed.x`: the one the walls there share, or else `own`, the
/// wall's own.
macro_rules! wall_normal {
    () => {
        r"
// The normal at the end of a fill extrusion wall from its outline vertex's
// packed decimals_ed.x: the one the walls there share, whose angle from -pi
// its bits 2 to 15 hold where bit 1 is set, or else `own`, the wall's.
vec2 wall_normal(const uint bits, const vec2 own) {
    if ((bits & 2u) == 0u) {
        return own;
    }
    const float angle = float(bits >> 2u) * (6.283185307179586 / 16384.0) - 3.141592653589793;
    return vec2(cos(angle), sin(angle));
}
"
    };
}

pub(crate) const FILL_EXTRUSION: GlslSource = GlslSource {
    vertex: r"
invariant gl_Position;

layout(location = 0) out vec4 frag_color;

void main() {
    const FillExtrusionDrawableUBO drawable = drawableVector.elements[constant.ubo_index];

#if defined(HAS_UNIFORM_u_base)
    const float base = max(props.base, 0.0);
#else
    const float base = max(unpack_mix_float(in_base, drawable.base_t), 0.0);
#endif

#if defined(HAS_UNIFORM_u_height)
    const float height = max(props.height, 0.0);
#else
    const float height = max(unpack_mix_float(in_height, drawable.height_t), 0.0);
#endif

#if defined(HAS_UNIFORM_u_color)
    vec4 color = props.color;
#else
    vec4 color = unpack_mix_color(in_color, drawable.color_t);
#endif

    const vec3 normal = vec3(0.0, 0.0, 1.0);
    const float t = 1.0;
    // Over terrain, the extrusion stands on the ground under its polygon's
    // centroid, as GL JS raises it, its base 10 m lower where it is at the
    // ground, so that it meets a slope.
#ifdef TERRAIN
    const float ele = in_ground_height;
    const float z = t > 0.0 ? height + ele : base + ele - (base > 0.0 ? 0.0 : 10.0);
#else
    const float z = t > 0.0 ? height : base;
#endif
    gl_Position = projectTileFor3D(vec2(in_pos), z, projectionVector.elements[constant.ubo_index]);

    // Relative luminance (how dark/bright is the surface color?)
    const float luminance = color.r * 0.2126 + color.g * 0.7152 + color.b * 0.0722;

    vec4 vcolor = vec4(0.0, 0.0, 0.0, 1.0);

    // Add slight ambient lighting so no extrusions are totally black
    color += vec4(0.03, 0.03, 0.03, 1.0);

    // Calculate cos(theta), where theta is the angle between surface normal and diffuse light ray
    const float directionalFraction = clamp(dot(normal, props.light_position), 0.0, 1.0);

    // Adjust directional so that the range of values for highlight/shading is
    // narrower with lower light intensity and with lighter/brighter surface colors
    const float minDirectional = 1.0 - props.light_intensity;
    const float maxDirectional = max(1.0 - luminance + props.light_intensity, 1.0);
    float directional = mix(minDirectional, maxDirectional, directionalFraction);

    // Add gradient along z axis of side surfaces
    if (normal.z == 0.0) {
        // This avoids another branching statement, but multiplies by a constant of 0.84 if no
        // vertical gradient, and otherwise calculates the gradient based on base + height
        // TODO: If we're optimizing to the level of avoiding branches, we should pre-compute
        //       the square root when height is a uniform.
        const float fMin = mix(0.7, 0.98, 1.0 - props.light_intensity);
        const float factor = clamp((t + base) * pow(height / 150.0, 0.5), fMin, 1.0);
        directional *= (1.0 - props.vertical_gradient) + (props.vertical_gradient * factor);
    }

    // Assign final color based on surface + ambient light color, diffuse light directional,
    // and light color with lower bounds adjusted to hue of light so that shading is tinted
    // with the complementary (opposite) color to the light color
    const vec3 light_color = props.light_color;
    const vec3 minLight = mix(vec3(0.0), vec3(0.3), 1.0 - light_color.rgb);
    vcolor += vec4(clamp(color.rgb * directional * light_color.rgb, minLight, vec3(1.0)), 0.0);

    frag_color = vcolor * props.opacity;
}
",
    fragment: r"

layout(location = 0) in vec4 frag_color;
layout(location = 0) out vec4 out_color;

void main() {
    out_color = frag_color;
}
",
};

/// The body of the fill extrusion walls' vertex stages, which reads its
/// wall's corner, outline vertices and paint values from `in_pos`,
/// `in_outline_pos`, `in_decimals_ed`, `in_next_outline_pos`,
/// `in_next_decimals_ed`, `in_color`, `in_base` and `in_height`.
macro_rules! fill_extrusion_wall_main {
    () => {
        r"

    const vec2 instancePos = vec2(in_outline_pos);
    const vec2 instanceDecimalsEd = vec2(in_decimals_ed);

    const FillExtrusionDrawableUBO drawable = drawableVector.elements[constant.ubo_index];

#if defined(HAS_UNIFORM_u_base)
    const float base = max(props.base, 0.0);
#else
    const float base = max(unpack_mix_float(in_base, drawable.base_t), 0.0);
#endif

#if defined(HAS_UNIFORM_u_height)
    const float height = max(props.height, 0.0);
#else
    const float height = max(unpack_mix_float(in_height, drawable.height_t), 0.0);
#endif

#if defined(HAS_UNIFORM_u_color)
    vec4 color = props.color;
#else
    vec4 color = unpack_mix_color(in_color, drawable.color_t);
#endif
    bool isDiscarded = mod(instanceDecimalsEd.x, 2.0) > 0.0;
    if (isDiscarded) {
        gl_Position = vec4(0.0);
        frag_color = vec4(0.0);
        return;
    }

    const vec2 nextInstancePos = vec2(in_next_outline_pos);
    const vec2 nextInstanceDecimalsEd = vec2(in_next_decimals_ed);

    const vec2 p1 = instancePos;
    const vec2 p2 = nextInstancePos;
    const vec2 edgevector = normalize(p2 - p1);
    const vec2 own = vec2(-edgevector.y, edgevector.x);

    const vec3 normal = vec3(in_pos.x == 0.0 ? wall_normal(in_decimals_ed.x, own) : wall_normal(in_next_decimals_ed.x, own), 0.0);
    const float t = float(in_pos.y);
    // Over terrain, the extrusion stands on the ground under its polygon's
    // centroid, as GL JS raises it, its base 10 m lower where it is at the
    // ground, so that it meets a slope.
#ifdef TERRAIN
    const float ele = in_ground_height;
    const float z = t > 0.0 ? height + ele : base + ele - (base > 0.0 ? 0.0 : 10.0);
#else
    const float z = t > 0.0 ? height : base;
#endif

    gl_Position = projectTileFor3D(in_pos.x == 0.0 ? p1 : p2, z, projectionVector.elements[constant.ubo_index]);

    // Relative luminance (how dark/bright is the surface color?)
    const float luminance = color.r * 0.2126 + color.g * 0.7152 + color.b * 0.0722;

    vec4 vcolor = vec4(0.0, 0.0, 0.0, 1.0);

    // Add slight ambient lighting so no extrusions are totally black
    color += vec4(0.03, 0.03, 0.03, 1.0);

    // Calculate cos(theta), where theta is the angle between surface normal and diffuse light ray
    const float directionalFraction = clamp(dot(normal, props.light_position), 0.0, 1.0);

    // Adjust directional so that the range of values for highlight/shading is
    // narrower with lower light intensity and with lighter/brighter surface colors
    const float minDirectional = 1.0 - props.light_intensity;
    const float maxDirectional = max(1.0 - luminance + props.light_intensity, 1.0);
    float directional = mix(minDirectional, maxDirectional, directionalFraction);

    // Add gradient along z axis of side surfaces
    if (normal.z == 0.0) {
        // This avoids another branching statement, but multiplies by a constant of 0.84 if no
        // vertical gradient, and otherwise calculates the gradient based on base + height
        // TODO: If we're optimizing to the level of avoiding branches, we should pre-compute
        //       the square root when height is a uniform.
        const float fMin = mix(0.7, 0.98, 1.0 - props.light_intensity);
        const float factor = clamp((t + base) * pow(height / 150.0, 0.5), fMin, 1.0);
        directional *= (1.0 - props.vertical_gradient) + (props.vertical_gradient * factor);
    }

    // Assign final color based on surface + ambient light color, diffuse light directional,
    // and light color with lower bounds adjusted to hue of light so that shading is tinted
    // with the complementary (opposite) color to the light color
    const vec3 light_color = props.light_color;
    const vec3 minLight = mix(vec3(0.0), vec3(0.3), 1.0 - light_color.rgb);
    vcolor += vec4(clamp(color.rgb * directional * light_color.rgb, minLight, vec3(1.0)), 0.0);

    frag_color = vcolor * props.opacity;
"
    };
}

pub(crate) const FILL_EXTRUSION_INSTANCED: GlslSource = GlslSource {
    vertex: concat!(
        wall_normal!(),
        r"
invariant gl_Position;

layout(location = 0) out vec4 frag_color;

void main() {
",
        fill_extrusion_wall_main!(),
        r"}
"
    ),
    fragment: r"

layout(location = 0) in vec4 frag_color;
layout(location = 0) out vec4 out_color;

void main() {
    out_color = frag_color;
}
",
};

/// Port-only: the walls of the instanced walls as quads without vertex
/// inputs. A vertex reads its wall's outline vertices and paint values from
/// storage buffers, by its index.
pub(crate) const FILL_EXTRUSION_PULLED: GlslSource = GlslSource {
    vertex: concat!(
        wall_normal!(),
        r"
invariant gl_Position;

layout(location = 0) out vec4 frag_color;

void main() {
    // Four vertices a wall, in the order of the static quad's corners.
    const uint wall = uint(gl_VertexIndex) / 4u;
    const uint corner = uint(gl_VertexIndex) % 4u;
    // The bucket's vertices, two words each: the position's x and y in the
    // halves of the first, decimals_ed's in those of the second.
    const uint pos = wallVertices.words[2u * wall];
    const uint decimals_ed = wallVertices.words[2u * wall + 1u];
    const uint next_pos = wallVertices.words[2u * wall + 2u];
    const uint next_decimals_ed = wallVertices.words[2u * wall + 3u];

    // What the instanced walls read as vertex inputs.
    const ivec2 in_pos = ivec2(corner < 2u ? 1 : 0, int(corner & 1u));
    const ivec2 in_outline_pos = ivec2(int(pos << 16u) >> 16, int(pos) >> 16);
    const uvec2 in_decimals_ed = uvec2(decimals_ed & 0xffffu, decimals_ed >> 16u);
    const ivec2 in_next_outline_pos = ivec2(int(next_pos << 16u) >> 16, int(next_pos) >> 16);
    const uvec2 in_next_decimals_ed = uvec2(next_decimals_ed & 0xffffu, next_decimals_ed >> 16u);
#ifdef TERRAIN
    // The height of the ground under the wall's polygon's centroid.
    const float in_ground_height = wallHeights.words[wall];
#endif

    // The wall's paint values are its outline vertex's.
    const uint paint_layout = drawableVector.elements[constant.ubo_index].paint_layout;
    const uint paint = wall * (paint_layout & 0xffu);
#if !defined(HAS_UNIFORM_u_base)
    const uint base_at = paint + ((paint_layout >> 8u) & 0xffu);
    const vec2 in_base = vec2(wallPaint.words[base_at], wallPaint.words[base_at + 1u]);
#endif
#if !defined(HAS_UNIFORM_u_height)
    const uint height_at = paint + ((paint_layout >> 16u) & 0xffu);
    const vec2 in_height = vec2(wallPaint.words[height_at], wallPaint.words[height_at + 1u]);
#endif
#if !defined(HAS_UNIFORM_u_color)
    const uint color_at = paint + (paint_layout >> 24u);
    const vec4 in_color = vec4(wallPaint.words[color_at], wallPaint.words[color_at + 1u],
                               wallPaint.words[color_at + 2u], wallPaint.words[color_at + 3u]);
#endif
",
        fill_extrusion_wall_main!(),
        r"}
"
    ),
    fragment: FILL_EXTRUSION_INSTANCED.fragment,
};

pub(crate) const FILL_EXTRUSION_PATTERN: GlslSource = GlslSource {
    vertex: concat!(
        pattern_pos!(),
        r"
invariant gl_Position;

layout(location = 0) out vec4 frag_lighting;
layout(location = 1) out vec2 frag_pos_a;
layout(location = 2) out vec2 frag_pos_b;

#if !defined(HAS_UNIFORM_u_pattern_from)
layout(location = 3) flat out vec4 frag_pattern_from;
#endif

#if !defined(HAS_UNIFORM_u_pattern_to)
layout(location = 4) flat out vec4 frag_pattern_to;
#endif

void main() {
    const FillExtrusionDrawableUBO drawable = drawableVector.elements[constant.ubo_index];
    const FillExtrusionTilePropsUBO tileProps = tilePropsVector.elements[constant.ubo_index];

#if defined(HAS_UNIFORM_u_base)
    const float base = max(props.base, 0.0);
#else
    const float base = max(unpack_mix_float(in_base, drawable.base_t), 0.0);
#endif

#if defined(HAS_UNIFORM_u_height)
    const float height = max(props.height, 0.0);
#else
    const float height = max(unpack_mix_float(in_height, drawable.height_t), 0.0);
#endif

    const vec3 normal = vec3(0.0, 0.0, 1.0);
    const float t = 1.0;
    // Over terrain, the extrusion stands on the ground under its polygon's
    // centroid, as GL JS raises it, its base 10 m lower where it is at the
    // ground, so that it meets a slope.
#ifdef TERRAIN
    const float ele = in_ground_height;
    const float z = t > 0.0 ? height + ele : base + ele - (base > 0.0 ? 0.0 : 10.0);
#else
    const float z = t > 0.0 ? height : base;
#endif
    gl_Position = projectTileFor3D(vec2(in_pos), z, projectionVector.elements[constant.ubo_index]);

#if defined(HAS_UNIFORM_u_pattern_from)
    const vec4 pattern_from = tileProps.pattern_from;
#else
    const vec4 pattern_from = in_pattern_from;
    frag_pattern_from = in_pattern_from;
#endif

#if defined(HAS_UNIFORM_u_pattern_to)
    const vec4 pattern_to = tileProps.pattern_to;
#else
    const vec4 pattern_to = in_pattern_to;
    frag_pattern_to = in_pattern_to;
#endif

    const vec2 pattern_tl_a = pattern_from.xy;
    const vec2 pattern_br_a = pattern_from.zw;
    const vec2 pattern_tl_b = pattern_to.xy;
    const vec2 pattern_br_b = pattern_to.zw;

    const float pixelRatio = paintParams.pixel_ratio;
    const float tileZoomRatio = drawable.tile_ratio;
    const float fromScale = props.from_scale;
    const float toScale = props.to_scale;

    const vec2 display_size_a = vec2((pattern_br_a.x - pattern_tl_a.x) / pixelRatio, (pattern_br_a.y - pattern_tl_a.y) / pixelRatio);
    const vec2 display_size_b = vec2((pattern_br_b.x - pattern_tl_b.x) / pixelRatio, (pattern_br_b.y - pattern_tl_b.y) / pixelRatio);

    const vec2 pos = vec2(in_pos); // extrusion top

    vec4 lighting = vec4(0.0, 0.0, 0.0, 1.0);
    float directional = clamp(dot(normal, props.light_position), 0.0, 1.0);
    directional = mix((1.0 - props.light_intensity), max((0.5 + props.light_intensity), 1.0), directional);

    if (normal.z == 0.0) {
        // This avoids another branching statement, but multiplies by a constant of 0.84 if no
        // vertical gradient, and otherwise calculates the gradient based on base + height
        // TODO: If we're optimizing to the level of avoiding branches, we should pre-compute
        //       the square root when height is a uniform.
        const float fMin = mix(0.7, 0.98, 1.0 - props.light_intensity);
        const float factor = clamp((t + base) * pow(height / 150.0, 0.5), fMin, 1.0);
        directional *= (1.0 - props.vertical_gradient) + (props.vertical_gradient * factor);    }

    lighting.rgb += clamp(directional * props.light_color, mix(vec3(0.0), vec3(0.3), 1.0 - props.light_color), vec3(1.0));
    lighting *= props.opacity;

    frag_lighting = lighting;
    frag_pos_a = get_pattern_pos(drawable.pixel_coord_upper, drawable.pixel_coord_lower, fromScale * display_size_a, tileZoomRatio, pos);
    frag_pos_b = get_pattern_pos(drawable.pixel_coord_upper, drawable.pixel_coord_lower, toScale * display_size_b, tileZoomRatio, pos);

#if !defined(HAS_UNIFORM_u_pattern_from)
    frag_pattern_from = in_pattern_from;
#endif

#if !defined(HAS_UNIFORM_u_pattern_to)
    frag_pattern_to = in_pattern_to;
#endif
}
",
    ),
    fragment: r"

layout(location = 0) in vec4 frag_lighting;
layout(location = 1) in vec2 frag_pos_a;
layout(location = 2) in vec2 frag_pos_b;

#if !defined(HAS_UNIFORM_u_pattern_from)
layout(location = 3) flat in vec4 frag_pattern_from;
#endif

#if !defined(HAS_UNIFORM_u_pattern_to)
layout(location = 4) flat in vec4 frag_pattern_to;
#endif

layout(location = 0) out vec4 out_color;

void main() {

    const FillExtrusionTilePropsUBO tileProps = tilePropsVector.elements[constant.ubo_index];

#if defined(HAS_UNIFORM_u_pattern_from)
    const vec4 pattern_from = tileProps.pattern_from;
#else
    const vec4 pattern_from = frag_pattern_from;
#endif
#if defined(HAS_UNIFORM_u_pattern_to)
    const vec4 pattern_to = tileProps.pattern_to;
#else
    const vec4 pattern_to = frag_pattern_to;
#endif

    const vec2 pattern_tl_a = pattern_from.xy;
    const vec2 pattern_br_a = pattern_from.zw;
    const vec2 pattern_tl_b = pattern_to.xy;
    const vec2 pattern_br_b = pattern_to.zw;

    // A crossfade at 0 or 1 shows one pattern, so skip the other.
    vec4 color1 = vec4(0.0);
    if (props.fade < 1.0) {
        const vec2 imagecoord = mod(frag_pos_a, 1.0);
        const vec2 pos = atlas_pos(pattern_tl_a / tileProps.texsize, (pattern_br_a - pattern_tl_a) / tileProps.texsize * imagecoord);
        color1 = texture(sampler2D(image_texture, image_sampler), pos);
    }
    vec4 color2 = vec4(0.0);
    if (props.fade > 0.0) {
        const vec2 imagecoord_b = mod(frag_pos_b, 1.0);
        const vec2 pos2 = atlas_pos(pattern_tl_b / tileProps.texsize, (pattern_br_b - pattern_tl_b) / tileProps.texsize * imagecoord_b);
        color2 = texture(sampler2D(image_texture, image_sampler), pos2);
    }

    out_color = mix(color1, color2, props.fade) * frag_lighting;
}
",
};

pub(crate) const FILL_EXTRUSION_PATTERN_INSTANCED: GlslSource = GlslSource {
    vertex: concat!(
        pattern_pos!(),
        wall_normal!(),
        r"
invariant gl_Position;

layout(location = 0) out vec4 frag_lighting;
layout(location = 1) out vec2 frag_pos_a;
layout(location = 2) out vec2 frag_pos_b;

#if !defined(HAS_UNIFORM_u_pattern_from)
layout(location = 3) flat out vec4 frag_pattern_from;
#endif

#if !defined(HAS_UNIFORM_u_pattern_to)
layout(location = 4) flat out vec4 frag_pattern_to;
#endif

void main() {
    const vec2 instancePos = vec2(in_outline_pos);
    const vec2 instanceDecimalsEd = vec2(in_decimals_ed);

    const FillExtrusionDrawableUBO drawable = drawableVector.elements[constant.ubo_index];
    const FillExtrusionTilePropsUBO tileProps = tilePropsVector.elements[constant.ubo_index];

#if defined(HAS_UNIFORM_u_base)
    const float base = max(props.base, 0.0);
#else
    const float base = max(unpack_mix_float(in_base, drawable.base_t), 0.0);
#endif

#if defined(HAS_UNIFORM_u_height)
    const float height = max(props.height, 0.0);
#else
    const float height = max(unpack_mix_float(in_height, drawable.height_t), 0.0);
#endif
    bool isDiscarded = mod(instanceDecimalsEd.x, 2.0) > 0.0;
    if (isDiscarded) {
        gl_Position = vec4(0.0);
        return;
    }

    const vec2 nextInstancePos = vec2(in_next_outline_pos);
    const vec2 nextInstanceDecimalsEd = vec2(in_next_decimals_ed);

    const vec2 p1 = instancePos;
    const vec2 p2 = nextInstancePos;
    const vec2 edgevector = normalize(p2 - p1);
    const vec2 own = vec2(-edgevector.y, edgevector.x);

    const vec3 normal = vec3(in_pos.x == 0.0 ? wall_normal(in_decimals_ed.x, own) : wall_normal(in_next_decimals_ed.x, own), 0.0);
    const float edgedistance = in_pos.x == 0.0 ? nextInstanceDecimalsEd.y : instanceDecimalsEd.y;
    const float t = float(in_pos.y);
    // Over terrain, the extrusion stands on the ground under its polygon's
    // centroid, as GL JS raises it, its base 10 m lower where it is at the
    // ground, so that it meets a slope.
#ifdef TERRAIN
    const float ele = in_ground_height;
    const float z = t > 0.0 ? height + ele : base + ele - (base > 0.0 ? 0.0 : 10.0);
#else
    const float z = t > 0.0 ? height : base;
#endif

    gl_Position = projectTileFor3D(in_pos.x == 0.0 ? p1 : p2, z, projectionVector.elements[constant.ubo_index]);

#if defined(HAS_UNIFORM_u_pattern_from)
    const vec4 pattern_from = tileProps.pattern_from;
#else
    const vec4 pattern_from = in_pattern_from;
    frag_pattern_from = in_pattern_from;
#endif

#if defined(HAS_UNIFORM_u_pattern_to)
    const vec4 pattern_to = tileProps.pattern_to;
#else
    const vec4 pattern_to = in_pattern_to;
    frag_pattern_to = in_pattern_to;
#endif

    const vec2 pattern_tl_a = pattern_from.xy;
    const vec2 pattern_br_a = pattern_from.zw;
    const vec2 pattern_tl_b = pattern_to.xy;
    const vec2 pattern_br_b = pattern_to.zw;

    const float pixelRatio = paintParams.pixel_ratio;
    const float tileZoomRatio = drawable.tile_ratio;
    const float fromScale = props.from_scale;
    const float toScale = props.to_scale;

    const vec2 display_size_a = vec2((pattern_br_a.x - pattern_tl_a.x) / pixelRatio, (pattern_br_a.y - pattern_tl_a.y) / pixelRatio);
    const vec2 display_size_b = vec2((pattern_br_b.x - pattern_tl_b.x) / pixelRatio, (pattern_br_b.y - pattern_tl_b.y) / pixelRatio);

    const vec2 pos = vec2(edgedistance, z * drawable.height_factor); // extrusion side

    vec4 lighting = vec4(0.0, 0.0, 0.0, 1.0);
    float directional = clamp(dot(normal, props.light_position), 0.0, 1.0);
    directional = mix((1.0 - props.light_intensity), max((0.5 + props.light_intensity), 1.0), directional);

    if (normal.z == 0.0) {
        // This avoids another branching statement, but multiplies by a constant of 0.84 if no
        // vertical gradient, and otherwise calculates the gradient based on base + height
        // TODO: If we're optimizing to the level of avoiding branches, we should pre-compute
        //       the square root when height is a uniform.
        const float fMin = mix(0.7, 0.98, 1.0 - props.light_intensity);
        const float factor = clamp((t + base) * pow(height / 150.0, 0.5), fMin, 1.0);
        directional *= (1.0 - props.vertical_gradient) + (props.vertical_gradient * factor);
    }

    lighting.rgb += clamp(directional * props.light_color, mix(vec3(0.0), vec3(0.3), 1.0 - props.light_color), vec3(1.0));
    lighting *= props.opacity;

    frag_lighting = lighting;
    frag_pos_a = get_pattern_pos(drawable.pixel_coord_upper, drawable.pixel_coord_lower, fromScale * display_size_a, tileZoomRatio, pos);
    frag_pos_b = get_pattern_pos(drawable.pixel_coord_upper, drawable.pixel_coord_lower, toScale * display_size_b, tileZoomRatio, pos);

#if !defined(HAS_UNIFORM_u_pattern_from)
    frag_pattern_from = in_pattern_from;
#endif

#if !defined(HAS_UNIFORM_u_pattern_to)
    frag_pattern_to = in_pattern_to;
#endif
}
",
    ),
    fragment: r"

layout(location = 0) in vec4 frag_lighting;
layout(location = 1) in vec2 frag_pos_a;
layout(location = 2) in vec2 frag_pos_b;

#if !defined(HAS_UNIFORM_u_pattern_from)
layout(location = 3) flat in vec4 frag_pattern_from;
#endif

#if !defined(HAS_UNIFORM_u_pattern_to)
layout(location = 4) flat in vec4 frag_pattern_to;
#endif

layout(location = 0) out vec4 out_color;

void main() {

    const FillExtrusionTilePropsUBO tileProps = tilePropsVector.elements[constant.ubo_index];

#if defined(HAS_UNIFORM_u_pattern_from)
    const vec4 pattern_from = tileProps.pattern_from;
#else
    const vec4 pattern_from = frag_pattern_from;
#endif
#if defined(HAS_UNIFORM_u_pattern_to)
    const vec4 pattern_to = tileProps.pattern_to;
#else
    const vec4 pattern_to = frag_pattern_to;
#endif

    const vec2 pattern_tl_a = pattern_from.xy;
    const vec2 pattern_br_a = pattern_from.zw;
    const vec2 pattern_tl_b = pattern_to.xy;
    const vec2 pattern_br_b = pattern_to.zw;

    // A crossfade at 0 or 1 shows one pattern, so skip the other.
    vec4 color1 = vec4(0.0);
    if (props.fade < 1.0) {
        const vec2 imagecoord = mod(frag_pos_a, 1.0);
        const vec2 pos = atlas_pos(pattern_tl_a / tileProps.texsize, (pattern_br_a - pattern_tl_a) / tileProps.texsize * imagecoord);
        color1 = texture(sampler2D(image_texture, image_sampler), pos);
    }
    vec4 color2 = vec4(0.0);
    if (props.fade > 0.0) {
        const vec2 imagecoord_b = mod(frag_pos_b, 1.0);
        const vec2 pos2 = atlas_pos(pattern_tl_b / tileProps.texsize, (pattern_br_b - pattern_tl_b) / tileProps.texsize * imagecoord_b);
        color2 = texture(sampler2D(image_texture, image_sampler), pos2);
    }

    out_color = mix(color1, color2, props.fade) * frag_lighting;
}
",
};