aboutsummaryrefslogtreecommitdiffstats
path: root/code/editor/generated/rect_frag.glsl
blob: d417261f5d82cddd00f30d81f308f10aa7d09e15 (plain) (blame)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#version 330 core

#define Pi32 3.1415926535897
#define v2 vec2
#define v3 vec3
#define v4 vec4 
#define f32 float
#define b32 int

in v2  VS_Pos;
in v4  VS_Dest;
in v2  VS_TexCoord;
in v2  VS_Center;
in v2  VS_HalfSize;
in v4  VS_Color;

in f32 VS_CornerRadius;
in f32 VS_BorderThickness;
in f32 VS_Softness;
flat in f32 VS_HasTexture;

uniform sampler2D Texture;

out v4 FragColor;

v3
HSV2RGB(v3 Color)
{
    v4 K = v4(1.0, 2.0/3.0, 1.0/3.0, 3.0);
    v3 p = abs(fract(Color.xxx + K.xyz) * 6.0 - K.www);
    return Color.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), Color.y);
}

f32
BoxSDF(v2 Pos, v2 HalfDim, f32 CornerRadius)
{
    v2 q = abs(Pos) - (HalfDim - CornerRadius);
    
    f32 Distance = min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - CornerRadius;
    
    return Distance;
}

void main()
{
    v4 Color = VS_Color;
    
    f32 tBorder = 1.0;
    if(VS_BorderThickness > 0.0)
    {
        f32 sBorder = BoxSDF(VS_Pos,
                             VS_HalfSize - v2(VS_Softness*2.0, VS_Softness*2.0) - VS_BorderThickness,
                             max(VS_CornerRadius-VS_BorderThickness, 0.0));
        tBorder = smoothstep(0.0, 2.0*VS_Softness, sBorder);
    }
    if(tBorder < 0.001f)
    {
        discard;
    }
    
    f32 tCorner = 1.0;
    if(VS_CornerRadius > 0.0 || VS_Softness > 0.75f)
    {
        f32 sCorner = BoxSDF(VS_Pos,
                             VS_HalfSize - v2(VS_Softness*2.0, VS_Softness*2.0),
                             VS_CornerRadius);
        tCorner = 1.0-smoothstep(0.0, 2.0*VS_Softness, sCorner);
    }
    
    if(VS_HasTexture > 0)
    {
        f32 Alpha = texture(Texture, VS_TexCoord).r;
        Color.a *= Alpha;;
    }
    
    Color.a *= tBorder;
    Color.a *= tCorner;
    
    FragColor = Color;
}