Skip to content

Commit

Permalink
Merge pull request #405 from rconde01/improve-text-anti-aliasing
Browse files Browse the repository at this point in the history
switch to MSDF text antialiasing method by Paul Houx
  • Loading branch information
toji authored Mar 18, 2024
2 parents 5c37623 + c7f8747 commit 7830516
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions sample/textRenderingMsdf/msdfText.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,24 @@ fn sampleMsdf(texcoord: vec2f) -> f32 {
return max(min(c.r, c.g), min(max(c.r, c.g), c.b));
}

// Antialiasing technique from https://drewcassidy.me/2020/06/26/sdf-antialiasing/
// Antialiasing technique from Paul Houx
// https://github.com/Chlumsky/msdfgen/issues/22#issuecomment-234958005
@fragment
fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
let dist = 0.5 - sampleMsdf(input.texcoord);
// pxRange (AKA distanceRange) comes from the msdfgen tool. Don McCurdy's tool
// uses the default which is 4.
let pxRange = 4.0;
let sz = vec2f(textureDimensions(fontTexture, 0));
let dx = sz.x*length(vec2f(dpdxFine(input.texcoord.x), dpdyFine(input.texcoord.x)));
let dy = sz.y*length(vec2f(dpdxFine(input.texcoord.y), dpdyFine(input.texcoord.y)));
let toPixels = pxRange * inverseSqrt(dx * dx + dy * dy);
let sigDist = sampleMsdf(input.texcoord) - 0.5;
let pxDist = sigDist * toPixels;

// sdf distance per pixel (gradient vector)
let ddist = vec2f(dpdx(dist), dpdy(dist));
let edgeWidth = 0.5;

// distance to edge in pixels (scalar)
let pixelDist = dist / length(ddist);
let alpha = smoothstep(-edgeWidth, edgeWidth, pxDist);

let alpha = saturate(0.5 - pixelDist);
if (alpha < 0.001) {
discard;
}
Expand Down

0 comments on commit 7830516

Please sign in to comment.