summaryrefslogtreecommitdiff
path: root/src/mbgl/shader/sdf.vertex.glsl
blob: c5166ae46f850f0e913f427cc8b806a1ad8ecb23 (plain)
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
attribute vec2 a_pos;
attribute vec2 a_offset;
attribute vec2 a_tex;
attribute float a_angle;
attribute float a_minzoom;
attribute float a_maxzoom;
attribute float a_rangeend;
attribute float a_rangestart;
attribute float a_labelminzoom;


// matrix is for the vertex position, exmatrix is for rotating and projecting
// the extrusion vector.
uniform mat4 u_matrix;
uniform mat4 u_exmatrix;
uniform float u_angle;
uniform float u_zoom;
uniform float u_flip;
uniform float u_fadedist;
uniform float u_minfadezoom;
uniform float u_maxfadezoom;
uniform float u_fadezoom;

uniform vec2 u_texsize;

varying vec2 v_tex;
varying float v_alpha;

void main() {

    float rev = 0.0;

    // u_angle is angle of the map, -128..128 representing 0..2PI
    // a_angle is angle of the label, 0..256 representing 0..2PI, where 0 is horizontal text
    float rotated = mod(a_angle + u_angle, 256.0);
    // if the label rotates with the map, and if the rotated label is upside down, hide it
    if (u_flip > 0.0 && rotated >= 64.0 && rotated < 192.0) rev = 1.0;

    // If the label should be invisible, we move the vertex outside
    // of the view plane so that the triangle gets clipped. This makes it easier
    // for us to create degenerate triangle strips.
    // u_zoom is the current zoom level adjusted for the change in font size
    float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom)) + rev;

    // fade out labels
    float alpha = clamp((u_fadezoom - a_labelminzoom) / u_fadedist, 0.0, 1.0);

    if (u_fadedist >= 0.0) {
        v_alpha = alpha;
    } else {
        v_alpha = 1.0 - alpha;
    }
    if (u_maxfadezoom < a_labelminzoom) {
        v_alpha = 0.0;
    }
    if (u_minfadezoom >= a_labelminzoom) {
        v_alpha = 1.0;
    }

    // if label has been faded out, clip it
    z += step(v_alpha, 0.0);

    // all the angles are 0..256 representing 0..2PI
    // hide if (angle >= a_rangeend && angle < rangestart)
    z += step(a_rangeend, u_angle) * (1.0 - step(a_rangestart, u_angle));

    gl_Position = u_matrix * vec4(a_pos, 0, 1) + u_exmatrix * vec4(a_offset / 64.0, z, 0);
    v_tex = a_tex / u_texsize;
}