From f294c850e7cca8b1325c88d5d0df47b609b60280 Mon Sep 17 00:00:00 2001 From: rconde01 Date: Sat, 16 Mar 2024 12:41:36 -0400 Subject: [PATCH 1/4] switch to MSDF text antialiasing method by Paul Houx --- sample/textRenderingMsdf/msdfText.wgsl | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/sample/textRenderingMsdf/msdfText.wgsl b/sample/textRenderingMsdf/msdfText.wgsl index 87dfeb36..e65bed5d 100644 --- a/sample/textRenderingMsdf/msdfText.wgsl +++ b/sample/textRenderingMsdf/msdfText.wgsl @@ -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 = vec2(textureDimensions(fontTexture, 0)); + let dx = sz.x*length(vec2(dpdxFine(input.texcoord.x), dpdyFine(input.texcoord.x))); + let dy = sz.y*length(vec2(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; } From fe55fbdef65b766fe32c8c5ff86127875d681d25 Mon Sep 17 00:00:00 2001 From: rconde01 Date: Mon, 18 Mar 2024 12:41:02 -0400 Subject: [PATCH 2/4] Update sample/textRenderingMsdf/msdfText.wgsl Co-authored-by: Brandon Jones --- sample/textRenderingMsdf/msdfText.wgsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample/textRenderingMsdf/msdfText.wgsl b/sample/textRenderingMsdf/msdfText.wgsl index e65bed5d..05f19160 100644 --- a/sample/textRenderingMsdf/msdfText.wgsl +++ b/sample/textRenderingMsdf/msdfText.wgsl @@ -67,7 +67,7 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f { // uses the default which is 4. let pxRange = 4.0; let sz = vec2(textureDimensions(fontTexture, 0)); - let dx = sz.x*length(vec2(dpdxFine(input.texcoord.x), dpdyFine(input.texcoord.x))); + let dx = sz.x*length(vec2f(dpdxFine(input.texcoord.x), dpdyFine(input.texcoord.x))); let dy = sz.y*length(vec2(dpdxFine(input.texcoord.y), dpdyFine(input.texcoord.y))); let toPixels = pxRange * inverseSqrt(dx * dx + dy * dy); let sigDist = sampleMsdf(input.texcoord) - 0.5; From 078e431bd375d36e9a6e23a06ee6ce4b3e138342 Mon Sep 17 00:00:00 2001 From: rconde01 Date: Mon, 18 Mar 2024 12:41:15 -0400 Subject: [PATCH 3/4] Update sample/textRenderingMsdf/msdfText.wgsl Co-authored-by: Brandon Jones --- sample/textRenderingMsdf/msdfText.wgsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample/textRenderingMsdf/msdfText.wgsl b/sample/textRenderingMsdf/msdfText.wgsl index 05f19160..f8eb73ca 100644 --- a/sample/textRenderingMsdf/msdfText.wgsl +++ b/sample/textRenderingMsdf/msdfText.wgsl @@ -66,7 +66,7 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f { // pxRange (AKA distanceRange) comes from the msdfgen tool. Don McCurdy's tool // uses the default which is 4. let pxRange = 4.0; - let sz = vec2(textureDimensions(fontTexture, 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(vec2(dpdxFine(input.texcoord.y), dpdyFine(input.texcoord.y))); let toPixels = pxRange * inverseSqrt(dx * dx + dy * dy); From c7f8747374f3c5c1168594de6001bec0527bf58b Mon Sep 17 00:00:00 2001 From: rconde01 Date: Mon, 18 Mar 2024 12:42:27 -0400 Subject: [PATCH 4/4] Update sample/textRenderingMsdf/msdfText.wgsl Co-authored-by: Brandon Jones --- sample/textRenderingMsdf/msdfText.wgsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample/textRenderingMsdf/msdfText.wgsl b/sample/textRenderingMsdf/msdfText.wgsl index f8eb73ca..94bec68e 100644 --- a/sample/textRenderingMsdf/msdfText.wgsl +++ b/sample/textRenderingMsdf/msdfText.wgsl @@ -68,7 +68,7 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f { 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(vec2(dpdxFine(input.texcoord.y), dpdyFine(input.texcoord.y))); + 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;