From e37b18336a63213ba0fbffcbdc9dcd2a4b941398 Mon Sep 17 00:00:00 2001 From: Olivier Prat Date: Wed, 25 Apr 2018 12:08:08 +0200 Subject: [PATCH] Improved anti-aliasing on text --- libraries/render-utils/src/sdf_text3D.slf | 31 +++++++++++++++-------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/libraries/render-utils/src/sdf_text3D.slf b/libraries/render-utils/src/sdf_text3D.slf index 2742341628..08e50ee8c5 100644 --- a/libraries/render-utils/src/sdf_text3D.slf +++ b/libraries/render-utils/src/sdf_text3D.slf @@ -20,14 +20,15 @@ uniform vec4 Color; in vec3 _normalWS; in vec2 _texCoord0; -const float gamma = 2.2; -const float smoothing = 32.0; +#define TAA_TEXTURE_LOD_BIAS -3.0 + const float interiorCutoff = 0.8; const float outlineExpansion = 0.2; +const float taaBias = pow(2.0, TAA_TEXTURE_LOD_BIAS); -void main() { +float evalSDF(vec2 texCoord) { // retrieve signed distance - float sdf = texture(Font, _texCoord0).g; + float sdf = textureLod(Font, texCoord, TAA_TEXTURE_LOD_BIAS).g; if (Outline) { if (sdf > interiorCutoff) { sdf = 1.0 - sdf; @@ -35,12 +36,22 @@ void main() { sdf += outlineExpansion; } } - // perform adaptive anti-aliasing of the edges - // The larger we're rendering, the less anti-aliasing we need - float s = smoothing * length(fwidth(_texCoord0)); - float w = clamp(s, 0.0, 0.5); - float a = smoothstep(0.5 - w, 0.5 + w, sdf); - + // Rely on TAA for anti-aliasing + return step(0.5, sdf); +} + +void main() { + vec2 dxTexCoord = dFdx(_texCoord0) * 0.5 * taaBias; + vec2 dyTexCoord = dFdy(_texCoord0) * 0.5 * taaBias; + + // Perform 4x supersampling for anisotropic filtering + float a; + a = evalSDF(_texCoord0); + a += evalSDF(_texCoord0+dxTexCoord); + a += evalSDF(_texCoord0+dyTexCoord); + a += evalSDF(_texCoord0+dxTexCoord+dyTexCoord); + a *= 0.25; + // discard if invisible if (a < 0.01) { discard;