

#include "/lib/basefiles.glsl"

varying vec2 lmcoord;
varying vec2 texcoord;
varying vec3 normal;
varying vec4 glcolor;
varying float blockID;
varying vec4 view_pos;
varying vec4 clip_pos;
varying mat3 tbnMatrix;
varying vec3 lightcol;
varying vec3 upskylight;

#ifdef VSH

    #if MC_VERSION >= 11500
        layout(location = 11) in vec4 mc_Entity;
    #else
        layout(location = 10) in vec4 mc_Entity;
    #endif

    in vec4 at_tangent;

    void main() {
        vec3 model_pos = gl_Vertex.xyz;
        view_pos = gl_ModelViewMatrix * vec4(model_pos, 1.0);
        clip_pos = gl_ProjectionMatrix * view_pos;
        #ifdef TAA
	        clip_pos.xy += taaJitter * TAA_Intensity * clip_pos.w;
        #endif
        gl_Position = clip_pos;

        texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
        lmcoord = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;
        lmcoord = clamp(lmcoord, 0.5 / 16.0, 15.5 / 16.0);
        normal = gl_NormalMatrix * gl_Normal;
        normal = normalize(mat3(gbufferModelViewInverse) * normal);
        glcolor = gl_Color;
        blockID = mc_Entity.x;

        vec3 tangent = gl_NormalMatrix * normalize(at_tangent.xyz);
        tangent = normalize(mat3(gbufferModelViewInverse) * tangent);
        tbnMatrix = mat3(tangent, normalize(cross(tangent, normal) * at_tangent.w), normal);

        lightcol = texelFetch(colortex5, ivec2(viewSize - 1.0) - ivec2(2, 0), 0).rgb;
        upskylight = texelFetch(colortex5, ivec2(viewSize - 1.0) - ivec2(6, 0), 0).rgb;
    }

#endif

#ifdef FSH

    #include "/lib/water.glsl"
    #include "/lib/rainripple.glsl"

    /* RENDERTARGETS: 0,1,2,3 */
    layout(location = 0) out vec4 color0;
    layout(location = 1) out vec4 color1;
    layout(location = 2) out vec4 color2;
    layout(location = 3) out vec4 color3;

    void main() {
        vec4 texcolor = texture(gtexture, texcoord);
        if(texcolor.a < alphaTestRef) discard;
        texcolor.rgb *= glcolor.rgb;
        texcolor.rgb = GammaToLinear(texcolor.rgb);

        vec3 outcol = vec3(0.0);
        vec3 worldNormal = normal;
        vec3 worldOriNormal = normal;
        float F0 = 0.02;

        if(abs(blockID - 8.0) < 0.5 || abs(blockID - 9.0) < 0.5) {
            vec3 viewPos = view_pos.xyz;
            vec3 worldPos = matrixMultiply(gbufferModelViewInverse, vec4(viewPos, 1.0));
            vec3 worldDir = normalize(mat3(gbufferModelViewInverse) * viewPos);
            vec3 mcPos = worldPos + cameraPosition;

            float currHeight;
            vec2 waterUV = waveParallaxMapping(mcPos.xz, transpose(tbnMatrix) * worldDir, currHeight);
            vec3 waterNormal = getWaterNormal(mcPos.xz);
            worldNormal = tbnMatrix * waterNormal;

            #ifdef RAIN_SPLASH
                float rWet = clamp(lmcoord.y * 2.0 - 1.0, 0.0, 1.0) * rainStrength;
                worldNormal = rainR(mcPos, worldOriNormal, worldNormal, rWet);
            #endif

            vec3 screenPos = (clip_pos.xyz / clip_pos.w) * 0.5 + 0.5;
            vec2 distortCoord = screenPos.xy + waterNormal.xy * 0.05;
            outcol = texture(colortex4, distortCoord).rgb;

            float viewdepth = abs(
                linearDepth(texture(depthtex1, screenPos.xy).r) * float(isEyeInWater == 0) -
                linearDepth(screenPos.z)
            );
            outcol = waterScatter(outcol, worldDir, viewdepth, lightcol, upskylight);
            F0 = 0.02;
        } else if(abs(blockID - 79.0) < 0.5) {
            #ifdef RAIN_SPLASH
                vec3 worldPos = matrixMultiply(gbufferModelViewInverse, vec4(view_pos.xyz, 1.0));
                float rWet = clamp(lmcoord.y * 2.0 - 1.0, 0.0, 1.0) * rainStrength;
                worldNormal = rainR(worldPos + cameraPosition, normal, worldNormal, rWet);
            #endif
            outcol = vec3(0.02, 0.025, 0.03) * lightcol * lmcoord.y;
            F0 = 0.04;
        } else if(abs(blockID - 95.0) < 0.5) {
            vec3 screenPos = (clip_pos.xyz / clip_pos.w) * 0.5 + 0.5;
            #ifdef RAIN_SPLASH
                vec3 worldPos = matrixMultiply(gbufferModelViewInverse, vec4(view_pos.xyz, 1.0));
                float rWet = clamp(lmcoord.y * 2.0 - 1.0, 0.0, 1.0) * rainStrength;
                worldNormal = rainR(worldPos + cameraPosition, normal, worldNormal, rWet);
            #endif
            outcol = texture(colortex4, screenPos.xy).rgb * texcolor.rgb;
            F0 = 0.04;
        } else {
            vec3 screenPos = (clip_pos.xyz / clip_pos.w) * 0.5 + 0.5;
            outcol = texture(colortex4, screenPos.xy).rgb;
            outcol = mix(outcol, GammaToLinear(texcolor.rgb) * lightcol * 0.5, texcolor.a * 0.2);
            color0 = vec4(outcol, 1.0);
            color1 = vec4(lmcoord, -1.0, 1.0);
            color2 = vec4(0.0);
            color3 = vec4(0.0);
            return;
        }

        color0 = vec4(outcol, 1.0);
        color1 = vec4(lmcoord, blockID / 10000.0, 1.0);
        color2 = vec4(normalEncode(worldOriNormal), normalEncode(worldNormal));
        color3 = vec4(1.0, F0, 0.0, 1.0);
    }

#endif