summaryrefslogtreecommitdiff
path: root/src/extras/shaders/es2/distancefieldtext.frag
diff options
context:
space:
mode:
Diffstat (limited to 'src/extras/shaders/es2/distancefieldtext.frag')
-rw-r--r--src/extras/shaders/es2/distancefieldtext.frag36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/extras/shaders/es2/distancefieldtext.frag b/src/extras/shaders/es2/distancefieldtext.frag
new file mode 100644
index 000000000..88ead1f68
--- /dev/null
+++ b/src/extras/shaders/es2/distancefieldtext.frag
@@ -0,0 +1,36 @@
+#define FP highp
+
+uniform FP sampler2D distanceFieldTexture;
+uniform FP float minAlpha;
+uniform FP float maxAlpha;
+uniform FP float textureSize;
+uniform FP vec4 color;
+
+varying FP vec3 position;
+varying FP vec2 texCoord;
+
+void main()
+{
+ // determine the scale of the glyph texture within pixel-space coordinates
+ // (that is, how many pixels are drawn for each texel)
+ FP vec2 texelDeltaX = abs(dFdx(texCoord));
+ FP vec2 texelDeltaY = abs(dFdy(texCoord));
+ FP float avgTexelDelta = textureSize * 0.5 * (texelDeltaX.x + texelDeltaX.y + texelDeltaY.x + texelDeltaY.y);
+ FP float texScale = 1.0 / avgTexelDelta;
+
+ // scaled to interval [0.0, 0.15]
+ FP float devScaleMin = 0.00;
+ FP float devScaleMax = 0.15;
+ FP float scaled = (clamp(texScale, devScaleMin, devScaleMax) - devScaleMin) / (devScaleMax - devScaleMin);
+
+ // thickness of glyphs should increase a lot for very small glyphs to make them readable
+ FP float base = 0.5;
+ FP float threshold = base * scaled;
+ FP float range = 0.06 / texScale;
+
+ FP float minAlpha = threshold - range;
+ FP float maxAlpha = threshold + range;
+
+ FP float distVal = texture2D(distanceFieldTexture, texCoord).r;
+ gl_FragColor = color * smoothstep(minAlpha, maxAlpha, distVal);
+}