

#include "/lib/basefiles.glsl"

varying vec2 texcoord;

#ifdef VSH

    void main() {
        gl_Position = ftransform();
        texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
    }

#endif

#ifdef FSH

    uniform sampler2D vxDepthTexTrans;

    /* RENDERTARGETS: 0,4 */
    layout(location = 0) out vec4 color0;
    layout(location = 1) out vec4 color4;

    void main() {
        vec3 outcol = texelFetch(colortex0, texelUV, 0).rgb;

        float vanillaDepth = texelFetch(depthtex1, texelUV, 0).r;
        float voxyTransDepth = texelFetch(vxDepthTexTrans, texelUV, 0).r;
        bool voxyVisible = vanillaDepth >= 1.0 && voxyTransDepth < 1.0;

        if (voxyVisible) {
            vec4 voxyColor = texelFetch(colortex16, texelUV, 0);
            vec2 voxyNormalEnc = texelFetch(colortex17, texelUV, 0).xy;
            vec3 normal = normalDecode(voxyNormalEnc);

            float fresnel = pow(1.0 - max(dot(normal, vec3(0,1,0)), 0.0), 5.0);
            vec3 skyTint = vec3(0.4, 0.55, 0.7);
            vec3 tinted = mix(voxyColor.rgb, skyTint, fresnel * 0.5);

            outcol = mix(outcol, tinted, voxyColor.a);
        }

        color0 = vec4(outcol, 1.0);
        color4 = vec4(outcol, 1.0);
    }

#endif