summaryrefslogtreecommitdiffstats
path: root/cwd/assets/altcraft/shaders/frag/light.fs
blob: a0cda0206e5a9b509f78e360acbb2d02726080c2 (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
82
83
#version 330 core

out vec4 fragColor;

in vec2 uv;

uniform sampler2D depthStencil;
uniform sampler2D color;
uniform sampler2D normal;
uniform sampler2D light;
uniform sampler2D ssao;

uniform int renderBuff;
uniform bool applySsao;

layout (std140) uniform Globals {
    mat4 projView;
    mat4 proj;
    mat4 invProj;
    mat4 view;
    uvec2 viewportSize;
    vec4 ssaoKernels[64];
    float globalTime;
    float dayTime;
    float gamma;
};

vec3 RecoverViewWorldPos(vec2 screenPos, float depth) {
    vec4 viewPos = invProj * vec4(screenPos * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
    return viewPos.xyz / viewPos.w;
}

void main() {
    vec4 c = texture(color, uv);
    vec4 n = texture(normal, uv);
    n += 1.0f;
    n /= 2.0f;

    vec4 l = texture(light, uv);
    float depth = texture(depthStencil, uv).r;
    float d = (1.0f - depth) * 16.0f;
    vec4 s = texture(ssao, uv);

    float faceLight = l.r;
    float skyLight = l.g;
    float lightLevel = clamp(faceLight + skyLight * dayTime, 0.01f, 1.0f);
    lightLevel = pow(lightLevel, 3);
    if (applySsao) {
        lightLevel *= pow(s.r, 2);
    }
    lightLevel = clamp(lightLevel, 0.005f, 1.0f);

    vec4 finalColor = vec4(c.rgb * lightLevel, 1.0f);

    finalColor.rgb = pow(finalColor.rgb, vec3(1.0f / gamma));

    switch(renderBuff) {
        case 0:
            fragColor = finalColor;
            break;
        case 1:
            fragColor = c;
            break;
        case 2:
            fragColor = n;
            break;
        case 3:
            fragColor = vec4(RecoverViewWorldPos(uv, depth), 1.0f);
            break;
        case 4:
            fragColor = vec4(0.5f);
            break;
        case 5:
            fragColor = vec4(l.r, l.g, 1.0f - l.b, 1.0f);
            break;
        case 6:
            fragColor = vec4(vec3(d), 1.0f);
            break;
        case 7:
            fragColor = vec4(pow(s.r, 2));
            break;
    }
}