Debian Codegraph @

src/extras/shaders/es2/distancefieldtext.frag

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