Debian Codegraph @

src/extras/shaders/rhi/distancefieldtext.frag

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