Skip to content

Commit

Permalink
shaders: improve corner AA in borders shader
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Nov 5, 2024
1 parent 55ccb1a commit 0920572
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/render/shaders/Border.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <string>
#include <format>
#include "SharedValues.hpp"

// makes a stencil without corners
inline const std::string FRAGBORDER1 = R"#(
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/render/shaders/SharedValues.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

constexpr float SHADER_ROUNDED_SMOOTHING_FACTOR = M_PI / 5.34665792551;
6 changes: 5 additions & 1 deletion src/render/shaders/Textures.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <string>
#include <format>
#include "SharedValues.hpp"

inline static constexpr auto ROUNDED_SHADER_FUNC = [](const std::string colorVarName) -> std::string {
return R"#(
Expand All @@ -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) {
Expand Down

0 comments on commit 0920572

Please sign in to comment.