From 0920572e703201368c9fbcc9108ce1888600ffa2 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 5 Nov 2024 16:11:05 +0000 Subject: [PATCH] shaders: improve corner AA in borders shader --- src/render/shaders/Border.hpp | 10 ++++++++-- src/render/shaders/SharedValues.hpp | 3 +++ src/render/shaders/Textures.hpp | 6 +++++- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 src/render/shaders/SharedValues.hpp diff --git a/src/render/shaders/Border.hpp b/src/render/shaders/Border.hpp index a33694a7..1f4a1d97 100644 --- a/src/render/shaders/Border.hpp +++ b/src/render/shaders/Border.hpp @@ -1,6 +1,8 @@ #pragma once #include +#include +#include "SharedValues.hpp" // makes a stencil without corners inline const std::string FRAGBORDER1 = R"#( @@ -72,18 +74,22 @@ void main() { pixCoordOuter += vec2(1.0, 1.0) / fullSize; if (min(pixCoord.x, pixCoord.y) > 0.0 && radius > 0.0) { + // smoothing constant for the edge: more = blurrier, but smoother + const float SMOOTHING_CONSTANT = )#" + + std::format("{:.7f}", SHADER_ROUNDED_SMOOTHING_FACTOR) + R"#(; + float dist = length(pixCoord); float distOuter = length(pixCoordOuter); float h = (thick / 2.0); if (dist < radius - h) { // lower - float normalized = smoothstep(0.0, 1.0, dist - radius + thick + 0.5); + float normalized = smoothstep(0.0, 1.0, (dist - radius + thick + SMOOTHING_CONSTANT) / (SMOOTHING_CONSTANT * 2.0)); additionalAlpha *= normalized; done = true; } else if (min(pixCoordOuter.x, pixCoordOuter.y) > 0.0) { // higher - float normalized = 1.0 - smoothstep(0.0, 1.0, distOuter - radiusOuter + 0.5); + float normalized = 1.0 - smoothstep(0.0, 1.0, (distOuter - radiusOuter + SMOOTHING_CONSTANT) / (SMOOTHING_CONSTANT * 2.0)); additionalAlpha *= normalized; done = true; } else if (distOuter < radiusOuter - h) { diff --git a/src/render/shaders/SharedValues.hpp b/src/render/shaders/SharedValues.hpp new file mode 100644 index 00000000..9a74c892 --- /dev/null +++ b/src/render/shaders/SharedValues.hpp @@ -0,0 +1,3 @@ +#pragma once + +constexpr float SHADER_ROUNDED_SMOOTHING_FACTOR = M_PI / 5.34665792551; \ No newline at end of file diff --git a/src/render/shaders/Textures.hpp b/src/render/shaders/Textures.hpp index e99d0a49..b203ad04 100644 --- a/src/render/shaders/Textures.hpp +++ b/src/render/shaders/Textures.hpp @@ -1,6 +1,8 @@ #pragma once #include +#include +#include "SharedValues.hpp" inline static constexpr auto ROUNDED_SHADER_FUNC = [](const std::string colorVarName) -> std::string { return R"#( @@ -13,7 +15,9 @@ inline static constexpr auto ROUNDED_SHADER_FUNC = [](const std::string colorVar pixCoord -= fullSize * 0.5 - radius; pixCoord += vec2(1.0, 1.0) / fullSize; // center the pix dont make it top-left - const float SMOOTHING_CONSTANT = 0.651724; // smoothing constant for the edge: more = blurrier, but smoother + // smoothing constant for the edge: more = blurrier, but smoother + const float SMOOTHING_CONSTANT = )#" + + std::format("{:.7f}", SHADER_ROUNDED_SMOOTHING_FACTOR) + R"#(; if (pixCoord.x + pixCoord.y > radius) {