Skip to content

Commit

Permalink
Deploying to gh-pages from @ 7830516 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
toji committed Mar 18, 2024
1 parent dd8c64d commit c70388f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
20 changes: 13 additions & 7 deletions sample/textRenderingMsdf/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample/textRenderingMsdf/main.js.map

Large diffs are not rendered by default.

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 c70388f

Please sign in to comment.