/*//////////////////////////////////////////////////////////////////////////////
//    ##      #####   ######   ######     ##     ####     #######  ##  ##     //
//   ####    ##   ##  # ## #    ##  ##   ####     ##       ##   #  ##  ##     //
//  ##  ##   #          ##      ##  ##  ##  ##    ##       ## #     ####      //
//  ##  ##    #####     ##      #####   ##  ##    ##       ####      ##       //
//  ######        ##    ##      ## ##   ######    ##   #   ## #     ####      //
//  ##  ##   ##   ##    ##      ##  ##  ##  ##    ##  ##   ##   #  ##  ##     //
//  ##  ##    #####    ####    #### ##  ##  ##   #######  #######  ##  ##     //
//----------------------------------------------------------------------------//
//----------------------------------------------------------------------------//
//      #####   ##   ##    ##     #####    #######  ######    #####           //
//     ##   ##  ##   ##   ####     ## ##    ##   #   ##  ##  ##   ##          //
//     #        ##   ##  ##  ##    ##  ##   ## #     ##  ##  #                //
//      #####   #######  ##  ##    ##  ##   ####     #####    #####           //
//          ##  ##   ##  ######    ##  ##   ## #     ## ##        ##          //
//          ##  ##   ##  ######    ##  ##   ## #     ## ##        ##          //
//     ##   ##  ##   ##  ##  ##    ## ##    ##   #   ##  ##  ##   ##          //
//      #####   ##   ##  ##  ##   #####    #######  #### ##   #####           //
/////(BSL Shaders Edit)//////////////////////////////////////By LexBoosT//////*/

//Settings//

//#define DEBUG_ARMOR_GLINT

#include "/lib/util/fastMath.glsl"

#include "/settings/globalSettings.glsl"

//Varyings//
varying vec2 texCoord;
varying vec4 color;
varying vec4 position;

//Fragment Shader///////////////////////////////////////////////////////////////////////////////////
#ifdef FSH

    //Uniforms//
    uniform ivec2 eyeBrightnessSmooth;
    uniform sampler2D texture;
    uniform float frameTimeCounter;

    //Includes//
    #ifdef MAGICAL_GLINT
        #include "/lib/color/hue.glsl"
    #endif

    float eBS = eyeBrightnessSmooth.y / 240.0;
    float sunVisibility = calculateSunVisibility(sunVec, upVec);

    //Program//
    void main(){

        vec4 multiplier = color;
        vec4 albedoNight = vec4(1.0);
        vec4 albedoSun = vec4(1.0);
        vec4 Finalalbedo = vec4(1.0);

        #ifdef MAGICAL_GLINT
            multiplier = vec4(hue2(frameTimeCounter * 0.1 * 4.154), 1.0);
        #endif

        vec4 albedo = textureLod(texture, texCoord, 0);
        
        #ifdef MAGICAL_GLINT
            albedo.rgb = vec3((albedo.r + albedo.g + albedo.b) / 2.2);
        #endif

        albedo *= multiplier;

        #if MC_VERSION >= 11500
            albedoNight = pow(albedo * 0.2, vec4(1.6));
            albedoSun = pow(albedo * 0.2, vec4(1.6)) / (3.5 - 2.0 * eBS);
        #else
            albedoNight = pow(albedo, vec4(2.2));
            albedoSun = pow(albedo, vec4(2.2)) / (3.5 - 2.0 * eBS);
        #endif

        float intensity = GLINT_INTENSITY * 2.0;
        float FinalIntensity = mix(intensity, intensity * 3.0, sunVisibility);

        #if (MC_VERSION >= 11904 && !defined RP_COMPATIBILITY)
            FinalIntensity *= 2.5;
        #endif

        Finalalbedo = mix(albedoNight, albedoSun, sunVisibility);

        Finalalbedo *= FinalIntensity;
        Finalalbedo = clamp01(Finalalbedo);

        albedo = Finalalbedo;

        /*DRAWBUFFERS:0*/

        #ifdef DEBUG_ARMOR_GLINT
            gl_FragData[0]=vec4(0.898, 1.0, 0.0, 0.75);
        #else
            gl_FragData[0] = vec4(albedo);
        #endif

    }

#endif

//Vertex Shader/////////////////////////////////////////////////////////////////////////////////////
#ifdef VSH

    //Uniforms//

    uniform mat4 gbufferModelViewInverse;

    uniform float frameTimeCounter;

    //Program//
    void main(){
            texCoord=(gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;

            float angle = PI * GLINT_DIRECTION / 180;
            vec2 nativeDirectionApprox = vec2(0.07655, -0.266);
            vec2 additionnalDirection = vec2(sin_approx(angle),cos_approx(angle));
            texCoord += frameTimeCounter * (additionnalDirection * GLINT_SPEED * 0.265 + nativeDirectionApprox);

            color=gl_Color;

            position=gbufferModelViewInverse * gl_ModelViewMatrix * gl_Vertex;

            vec4 temppos=gl_ProjectionMatrix * gbufferModelView * position;

            if((temppos.z/temppos.w) < 0.56)//hand
            {

                #ifdef MOUVEMENTS_MAINS
                    position-=vec4(0.03 * sin_approx(frameTimeCounter * 3.0 * SPEED_MOOVE), 0.015 * cos_approx(frameTimeCounter * 4.0 * SPEED_MOOVE), 0.0, 0.0) * gbufferModelView;
                #endif

                #ifdef MOUVEMENTS_MAINS_2
                    position.y+=sin_approx(frameTimeCounter * 3.0 * SPEED_MOOVE) * 0.015;
                    position.z-=cos_approx(frameTimeCounter * 4.0 * SPEED_MOOVE) * 0.0015;
                #endif

                position=gbufferModelView * position;

                position.z-=ADVANCE_HAND_POS;
                position.y+=ADVANCE_HAND_POS2;
                position.x+=DECALLAGE_MAINS;

                gl_Position=gl_ProjectionMatrix * position;
            }
            else
            {
                gl_Position = ftransform();
            }

            upVec  =normalize(gbufferModelView[1].xyz);
            eastVec=normalize(gbufferModelView[0].xyz);
            vec2 sunRotationData = getSunRotationData();
            vec3 usunvec = getUsunvec(sunRotationData);
            sunVec = getSunVec(usunvec, sunRotationData);
    }
#endif