1#version 150 core
2
3uniform sampler2D distanceFieldTexture;
4uniform float minAlpha;
5uniform float maxAlpha;
6uniform float textureSize;
7uniform vec4 color;
8
9in vec2 texCoord;
10in float zValue;
11
12out vec4 fragColor;
13
14void main()
15{
16    // determine the scale of the glyph texture within pixel-space coordinates
17    // (that is, how many pixels are drawn for each texel)
18    vec2 texelDeltaX = abs(dFdx(texCoord));
19    vec2 texelDeltaY = abs(dFdy(texCoord));
20    float avgTexelDelta = textureSize * 0.5 * (texelDeltaX.x + texelDeltaX.y + texelDeltaY.x + texelDeltaY.y);
21    float texScale = 1.0 / avgTexelDelta;
22
23    // scaled to interval [0.0, 0.15]
24    float devScaleMin = 0.00;
25    float devScaleMax = 0.15;
26    float scaled = (clamp(texScale, devScaleMin, devScaleMax) - devScaleMin) / (devScaleMax - devScaleMin);
27
28    // thickness of glyphs should increase a lot for very small glyphs to make them readable
29    float base = 0.5;
30    float threshold = base * scaled;
31    float range = 0.06 / texScale;
32
33    float minAlpha = threshold - range;
34    float maxAlpha = threshold + range;
35
36    float distVal = texture(distanceFieldTexture, texCoord).r;
37    fragColor = vec4(color.rgb, color.a * smoothstep(minAlpha, maxAlpha, distVal));
38    gl_FragDepth = gl_FragCoord.z - zValue * 0.000001;
39}
40