summaryrefslogtreecommitdiffstats
path: root/src/runtime/shaders/distancefieldtext.vert
blob: fa96ff78945a016577477b2fbb8535489777d314 (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
uniform highp mat4 mvp;
uniform highp float fontScale;
uniform int textureWidth;
uniform int textureHeight;

attribute highp vec4 vCoord;
attribute highp vec2 tCoord;

varying highp vec2 sampleCoord;
varying highp vec2 alphas;

highp float thresholdFunc(highp float scale)
{
    highp float base = 0.5;
    highp float baseDev = 0.065;
    highp float devScaleMin = 0.15;
    highp float devScaleMax = 0.3;
    return base - ((clamp(scale, devScaleMin, devScaleMax) - devScaleMin) / (devScaleMax - devScaleMin) * -baseDev + baseDev);
}

highp float spreadFunc(highp float scale)
{
    return 0.06 / scale;
}

highp vec2 alphaRange(highp float scale)
{
    highp float base = thresholdFunc(scale);
    highp float range = spreadFunc(scale);
    highp float alphaMin = max(0.0, base - range);
    highp float alphaMax = min(base + range, 1.0);
    return vec2(alphaMin, alphaMax);
}

highp float determinantOfSubmatrix(highp mat4 m, int col0, int col1, int row0, int row1)
{
    return m[col0][row0] * m[col1][row1] - m[col0][row1] * m[col1][row0];
}

highp float determinantOfSubmatrix(highp mat4 m, int col0, int col1, int col2, int row0, int row1, int row2)
{
    highp float det = m[col0][row0] * determinantOfSubmatrix(m, col1, col2, row1, row2);
    det      -= m[col1][row0] * determinantOfSubmatrix(m, col0, col2, row1, row2);
    det      += m[col2][row0] * determinantOfSubmatrix(m, col0, col1, row1, row2);
    return det;
}

highp float determinant(highp mat4 m)
{
    highp float det = m[0][0] * determinantOfSubmatrix(m, 1, 2, 3, 1, 2, 3);
    det      -= m[1][0] * determinantOfSubmatrix(m, 0, 2, 3, 1, 2, 3);
    det      += m[2][0] * determinantOfSubmatrix(m, 0, 1, 3, 1, 2, 3);
    det      -= m[3][0] * determinantOfSubmatrix(m, 0, 1, 2, 1, 2, 3);
    return det;
}

void main()
{
    highp float scale = fontScale * sqrt(abs(determinant(mvp)));
    alphas = alphaRange(scale);
    sampleCoord = tCoord * vec2(1.0 / float(textureWidth), 1.0 / float(textureHeight));
    gl_Position = mvp * vCoord;
}