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 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;