From 765a1e9710d8e2f61556a057bf324ffab48db071 Mon Sep 17 00:00:00 2001 From: Peter Spackman Date: Thu, 29 Aug 2024 14:25:41 +0800 Subject: [PATCH] Fix fringes of text rendering --- shaders/billboard.frag | 9 +++++++++ src/graphics/scene.cpp | 1 + src/graphics/signed_distance_field.h | 1 + 3 files changed, 11 insertions(+) diff --git a/shaders/billboard.frag b/shaders/billboard.frag index 2f9bb0f..d73a27b 100644 --- a/shaders/billboard.frag +++ b/shaders/billboard.frag @@ -12,6 +12,8 @@ uniform float u_textSDFOutline; void main() { + vec2 clampedTexCoord = clamp(v_texcoord, 0.0, 1.0); + float distance = texture(u_texture, v_texcoord).r; distance -= u_textSDFBuffer; float smoothing = u_textSDFSmoothing * fwidth(distance); @@ -21,10 +23,17 @@ void main() distance); vec3 color = mix(u_textOutlineColor, u_textColor, textAlpha); float alpha = max(textAlpha, outlineAlpha); + + // Additional alpha fade near texture edges + vec2 edgeDistance = min(clampedTexCoord, 1.0 - clampedTexCoord); + float edgeFade = smoothstep(0.0, 0.02, min(edgeDistance.x, edgeDistance.y)); + alpha *= edgeFade; + fragColor = vec4(color, alpha); // Discard nearly transparent fragments if (alpha < 0.01) { discard; } + } diff --git a/src/graphics/scene.cpp b/src/graphics/scene.cpp index 467b317..29af5a1 100644 --- a/src/graphics/scene.cpp +++ b/src/graphics/scene.cpp @@ -44,6 +44,7 @@ void Scene::init() { screenGammaChanged(); materialChanged(); + textSettingsChanged(); lightSettingsChanged(); if (m_structure) { diff --git a/src/graphics/signed_distance_field.h b/src/graphics/signed_distance_field.h index bac5985..72970a6 100644 --- a/src/graphics/signed_distance_field.h +++ b/src/graphics/signed_distance_field.h @@ -31,6 +31,7 @@ QImage eigenMatrixToQImage(const Eigen::MatrixXf& matrix) { int height = matrix.rows(); int width = matrix.cols(); QImage result(width, height, QImage::Format_Grayscale8); + result.fill(Qt::white); float minVal = matrix.minCoeff(); float maxVal = matrix.maxCoeff();