#include "/lib/config.glsl"

/* Color utils */

#ifdef THE_END
    #include "/lib/color_utils_end.glsl"
#elif defined NETHER
    #include "/lib/color_utils_nether.glsl"
#else
    #include "/lib/color_utils.glsl"
#endif

/* Uniforms */

uniform mat4 gbufferProjectionInverse;
uniform mat4 gbufferModelViewInverse;
uniform float pixelSizeX;
uniform float pixelSizeY;
uniform float rainStrength;
uniform float frameTimeCounter;

/* Ins / Outs */

varying vec3 upVector;
varying vec3 zenithSkyColor;
varying vec3 horizonSkyColor;

/* Utility functions */

#include "/lib/dither.glsl"
#ifdef THE_END
    #include "/lib/end_space.glsl"
#endif


// MAIN FUNCTION ------------------

void main() {
    #if defined THE_END
        vec4 fragpos =
            gbufferProjectionInverse *
            (vec4(gl_FragCoord.xy * vec2(pixelSizeX, pixelSizeY), 1.0, 1.0) * 2.0 - 1.0);
        vec3 viewDir = normalize(fragpos.xyz);
        vec3 worldDir = normalize((gbufferModelViewInverse * vec4(viewDir, 0.0)).xyz);
        vec3 blockColor = prismaEndSpaceSky(worldDir);
        vec3 prismaFogOnly = prismaEndFogSky(worldDir);
    #elif defined NETHER
        vec3 blockColor = ZENITH_DAY_COLOR;
    #else

        #if AA_TYPE > 0
            float dither = shiftedRDither(gl_FragCoord.xy);
        #else
            float dither = dither13(gl_FragCoord.xy);
        #endif

        dither = (dither - .5) * 0.0625;

        vec4 fragpos =
            gbufferProjectionInverse *
            (vec4(gl_FragCoord.xy * vec2(pixelSizeX, pixelSizeY), gl_FragCoord.z, 1.0) * 2.0 - 1.0);
        vec3 nfragpos = normalize(fragpos.xyz);
        float n_u = clamp(dot(nfragpos, upVector) + dither, 0.0, 1.0);
        vec3 blockColor =
            mix(horizonSkyColor, zenithSkyColor, smoothstep(0.0, 1.0, pow(n_u, 0.333)));

        blockColor = xyzToRgb(blockColor);
    #endif
    
    #if defined THE_END && defined SET_FOG_COLOR
        blockColor = clamp(blockColor, vec3(0.0), vec3(50.0));
        prismaFogOnly = clamp(prismaFogOnly, vec3(0.0), vec3(50.0));
        /* DRAWBUFFERS:17 */
        gl_FragData[0] = vec4(blockColor, 1.0);
        gl_FragData[1] = vec4(prismaFogOnly, 1.0);
    #else
        #include "/src/writebuffers.glsl"
    #endif
}