aboutsummaryrefslogtreecommitdiff
path: root/src/main/resources/assets/skyhanni/shaders/rounded_texture.fsh
blob: a38ebc4eac00ae1529a2637999137dee0b171aae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#version 120

uniform float scaleFactor;
uniform float radius;
uniform float smoothness;
uniform vec2 halfSize;
uniform vec2 centerPos;

uniform sampler2D outTexture;

varying vec2 outTextureCoords;
varying vec4 outColor;

// From https://www.shadertoy.com/view/WtdSDs
float roundedRectSDF(vec2 center, vec2 halfSize, float radius) {
    return length(max(abs(center) - halfSize + radius, 0.0)) - radius;
}

void main() {
    float xScale = gl_ModelViewMatrix[0][0];
    float yScale = gl_ModelViewMatrix[1][1];
    float xTranslation = gl_ModelViewMatrix[3][0];
    float yTranslation = gl_ModelViewMatrix[3][1];

    vec2 newHalfSize = vec2(halfSize.x * xScale, halfSize.y * yScale);

    float newCenterPosY = centerPos.y;
    if (yScale > 1.0) {
        newCenterPosY = centerPos.y - (halfSize.y * (yScale - 1));
    }

    vec2 newCenterPos = vec2((centerPos.x * xScale) + (xTranslation * scaleFactor), newCenterPosY - (yTranslation * scaleFactor));

    float distance = roundedRectSDF(gl_FragCoord.xy - newCenterPos, newHalfSize, radius);
    float smoothed = 1.0 - smoothstep(0.0, smoothness, distance);
    gl_FragColor = (texture2D(outTexture, outTextureCoords) * outColor) * vec4(1.0, 1.0, 1.0, smoothed);
}