Improved anti-aliasing on text

This commit is contained in:
Olivier Prat 2018-04-25 12:08:08 +02:00
parent f332849d9a
commit e37b18336a

View file

@ -20,14 +20,15 @@ uniform vec4 Color;
in vec3 _normalWS; in vec3 _normalWS;
in vec2 _texCoord0; in vec2 _texCoord0;
const float gamma = 2.2; #define TAA_TEXTURE_LOD_BIAS -3.0
const float smoothing = 32.0;
const float interiorCutoff = 0.8; const float interiorCutoff = 0.8;
const float outlineExpansion = 0.2; const float outlineExpansion = 0.2;
const float taaBias = pow(2.0, TAA_TEXTURE_LOD_BIAS);
void main() { float evalSDF(vec2 texCoord) {
// retrieve signed distance // retrieve signed distance
float sdf = texture(Font, _texCoord0).g; float sdf = textureLod(Font, texCoord, TAA_TEXTURE_LOD_BIAS).g;
if (Outline) { if (Outline) {
if (sdf > interiorCutoff) { if (sdf > interiorCutoff) {
sdf = 1.0 - sdf; sdf = 1.0 - sdf;
@ -35,11 +36,21 @@ void main() {
sdf += outlineExpansion; sdf += outlineExpansion;
} }
} }
// perform adaptive anti-aliasing of the edges // Rely on TAA for anti-aliasing
// The larger we're rendering, the less anti-aliasing we need return step(0.5, sdf);
float s = smoothing * length(fwidth(_texCoord0)); }
float w = clamp(s, 0.0, 0.5);
float a = smoothstep(0.5 - w, 0.5 + w, 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 // discard if invisible
if (a < 0.01) { if (a < 0.01) {