/* 
NightBloom Shader by SillyyLily
*/ 

//Settings//
#include "/lib/settings.glsl"

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

//Varyings//
varying vec2 texCoord, lmCoord;
varying vec3 normal;
varying vec3 sunVec, upVec, eastVec;
varying vec4 color;
varying float entityId;

//Uniforms//
uniform int frameCounter;
uniform int isEyeInWater;
uniform int worldTime;

uniform float frameTimeCounter;
uniform float nightVision;
uniform float rainStrength;
uniform float screenBrightness; 
uniform float shadowFade;
uniform float timeAngle, timeBrightness;
uniform float viewWidth, viewHeight;

uniform ivec2 eyeBrightnessSmooth;

uniform vec3 cameraPosition;
uniform vec3 relativeEyePosition;

uniform mat4 gbufferProjectionInverse;
uniform mat4 gbufferModelViewInverse;
uniform mat4 shadowProjection;
uniform mat4 shadowModelView;

uniform sampler2D noisetex;
uniform sampler2D tex;

#ifdef DYNAMIC_HANDLIGHT
uniform int heldBlockLightValue;
uniform int heldBlockLightValue2;
#endif

#ifdef MULTICOLORED_BLOCKLIGHT
uniform mat4 gbufferPreviousModelView;
uniform mat4 gbufferPreviousProjection;
uniform vec3 previousCameraPosition;

uniform sampler2D colortex9;
#endif

//Common Variables//
float eBS = eyeBrightnessSmooth.y / 240.0;
float sunVisibility  = clamp((dot( sunVec, upVec) + 0.05) * 10.0, 0.0, 1.0);
float moonVisibility = clamp((dot(-sunVec, upVec) + 0.05) * 10.0, 0.0, 1.0);

#ifdef WORLD_TIME_ANIMATION
float frametime = float(worldTime) * 0.05 * ANIMATION_SPEED;
#else
float frametime = frameTimeCounter * ANIMATION_SPEED;
#endif

vec3 lightVec = sunVec * ((timeAngle < 0.5325 || timeAngle > 0.9675) ? 1.0 : -1.0);

//Common Functions//
float GetLuminance(vec3 color) {
    return dot(color, vec3(0.299, 0.587, 0.114));
}

// Temporary Definitions to Fix Errors
vec3 ToNDC(vec3 pos) {
    vec4 clipPos = vec4(pos, 1.0);
    clipPos = gbufferProjectionInverse * clipPos;
    return clipPos.xyz / clipPos.w;
}

vec3 ToWorld(vec3 pos) {
    vec4 worldPos = gbufferModelViewInverse * vec4(pos, 1.0);
    return worldPos.xyz / worldPos.w;
}

vec2 TAAJitter(vec2 coord, float w) {
    return coord; // Simplified: no jitter
}

#ifdef MULTICOLORED_BLOCKLIGHT
#include "/lib/lighting/coloredBlocklight.glsl"
#endif

void GetLighting(inout vec3 albedo, out vec3 shadow, vec3 viewPos, vec3 worldPos, vec3 normal, vec2 lightmap, float smoothness, float NoL, float vanillaDiffuse, float porosity, float emission, float subsurface, float materialAO) {
    shadow = vec3(0.0);
    albedo *= lightmap.x * lightmap.y * vanillaDiffuse; // Basic lighting
}

// Helper Functions for Block Selection
vec3 hue2(float t) {
    t = fract(t) * 6.0;
    vec3 rgb = clamp(vec3(
        abs(t - 3.0) - 1.0,
        2.0 - abs(t - 2.0),
        2.0 - abs(t - 4.0)
    ), 0.0, 1.0);
    return rgb;
}

vec4 clamp01(vec4 v) {
    return clamp(v, 0.0, 1.0);
}

//Program//
void main() {
    vec4 albedo = color;

    if (albedo.a > 0.001) {
        vec2 lightmap = clamp(lmCoord, vec2(0.0), vec2(1.0));

        vec3 screenPos = vec3(gl_FragCoord.xy / vec2(viewWidth, viewHeight), gl_FragCoord.z);
        #ifdef TAA
        vec3 viewPos = ToNDC(vec3(TAAJitter(screenPos.xy, -0.5), screenPos.z));
        #else
        vec3 viewPos = ToNDC(screenPos);
        #endif
        vec3 worldPos = ToWorld(viewPos);
        
        #ifdef DYNAMIC_HANDLIGHT
        float heldLightValue = max(float(heldBlockLightValue), float(heldBlockLightValue2));
        vec3 heldLightPos = worldPos + relativeEyePosition + vec3(0.0, 0.5, 0.0);
        float handlight = clamp((heldLightValue - 2.0 * length(heldLightPos)) / 15.0, 0.0, 0.9333);
        lightmap.x = log2(exp2(lightmap.x * 8.0) + exp2(handlight * 8.0)) / 8.0;
        #endif

        #ifdef TOON_LIGHTMAP
        lightmap = floor(lightmap * 14.999 * (0.75 + 0.25 * color.a)) / 14.0;
        lightmap = clamp(lightmap, vec2(0.0), vec2(1.0));
        #endif

        albedo.rgb = pow(albedo.rgb, vec3(2.2));
        albedo.a = albedo.a * 0.5 + 0.5;

        #ifdef WHITE_WORLD
        if (albedo.a > 0.9) albedo.rgb = vec3(0.35);
        #endif

        float NoL = clamp(dot(normal, lightVec) * 1.01 - 0.01, 0.0, 1.0);

        float NoU = clamp(dot(normal, upVec), -1.0, 1.0);
        float NoE = clamp(dot(normal, eastVec), -1.0, 1.0);
        float vanillaDiffuse = (0.25 * NoU + 0.75) + (0.667 - abs(NoE)) * (1.0 - abs(NoU)) * 0.15;
              vanillaDiffuse *= vanillaDiffuse;

        #ifdef MULTICOLORED_BLOCKLIGHT
        vec3 blocklightCol = vec3(lightmap.x);
        if (screenPos.z > 0.56) {
            screenPos.xy = Reprojection(screenPos);
        }
        vec3 coloredLight = texture2DLod(colortex9, screenPos.xy, 2).rgb;
        
        #ifndef MCBL_LEGACY_COLOR
        vec3 coloredLightNormalized = coloredLight + 0.000001;
        coloredLightNormalized = normalize(coloredLightNormalized * coloredLightNormalized) * 0.875 + 0.125;
        coloredLightNormalized *= GetLuminance(blocklightCol) * 1.7;
        float coloredLightMix = min((coloredLight.r + coloredLight.g + coloredLight.b) * 2048.0, 1.0);
        #else
        vec3 coloredLightNormalized = normalize(coloredLight + 0.00001);
        coloredLightNormalized *= GetLuminance(blocklightCol) / GetLuminance(coloredLightNormalized);
        float coloredLightMix = min((coloredLight.r + coloredLight.g + coloredLight.b) * 256.0, 1.0);
        #endif
        
        blocklightCol = mix(blocklightCol, coloredLightNormalized, coloredLightMix);
        #endif
        
        vec3 shadow = vec3(0.0);
        GetLighting(albedo.rgb, shadow, viewPos, worldPos, normal, lightmap, 1.0, NoL, 
                    vanillaDiffuse, 1.0, 0.0, 0.0, 0.0);

        if (albedo.rgb == vec3(0.0)) {
            #ifdef COLOR_BLOCK_SELECTOR
            float worldPosCBSA  = worldPos.x + worldPos.y + worldPos.z;
            float cameraPosCBSA = cameraPosition.x + cameraPosition.y + cameraPosition.z;
            float finalPosCBSA  = worldPosCBSA + cameraPosCBSA;

            const float blockSize = 0.1;  

            float pattern = frameTimeCounter * 0.5 + finalPosCBSA * blockSize;
            vec4 color = vec4(hue2(pattern), 1.0);

            albedo = pow(clamp(color, 0.0, 1.0), vec4(2.2)) * 0.5;
            #endif
        }

        #if ALPHA_BLEND == 0
        albedo.rgb = sqrt(max(albedo.rgb, vec3(0.0)));
        #endif
    }

    /* DRAWBUFFERS:0 */
    gl_FragData[0] = albedo;

    #ifdef MULTICOLORED_BLOCKLIGHT
        /* DRAWBUFFERS:08 */
        gl_FragData[1] = vec4(0.0, 0.0, 0.0, 1.0);

        #ifdef ADVANCED_MATERIALS
        /* DRAWBUFFERS:08367 */
        gl_FragData[2] = vec4(0.0, 0.0, 0.0, 1.0);
        gl_FragData[3] = vec4(0.0, 0.0, float(gl_FragCoord.z < 1.0), 1.0);
        gl_FragData[4] = vec4(0.0, 0.0, 0.0, 1.0);
        #endif
    #else
        #ifdef ADVANCED_MATERIALS
        /* DRAWBUFFERS:0367 */
        gl_FragData[1] = vec4(0.0, 0.0, 0.0, 1.0);
        gl_FragData[2] = vec4(0.0, 0.0, float(gl_FragCoord.z < 1.0), 1.0);
        gl_FragData[3] = vec4(0.0, 0.0, 0.0, 1.0);
        #endif
    #endif
}

#endif

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

//Varyings//
varying vec2 texCoord, lmCoord;
varying vec3 normal;
varying vec3 sunVec, upVec, eastVec;
varying vec4 color;
varying float entityId; // Add entityId varying

//Uniforms//
uniform int worldTime;
uniform float frameTimeCounter;
uniform float timeAngle;
uniform vec3 cameraPosition;
uniform mat4 gbufferModelView, gbufferModelViewInverse;
uniform mat4 gbufferProjectionInverse;

#ifdef TAA
uniform int frameCounter;
uniform float viewWidth, viewHeight;
#endif

//Attributes//
attribute vec4 mc_Entity;
attribute vec4 mc_midTexCoord;

//Common Variables//
#ifdef WORLD_TIME_ANIMATION
float frametime = float(worldTime) * 0.05 * ANIMATION_SPEED;
#else
float frametime = frameTimeCounter * ANIMATION_SPEED;
#endif

//Includes//
#ifdef TAA
#include "/lib/util/jitter.glsl"
#endif

#ifdef WORLD_CURVATURE
#include "/lib/vertex/worldCurvature.glsl"
#endif

//Program//
void main() {
    texCoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
    
    lmCoord = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;
    lmCoord = clamp((lmCoord - 0.03125) * 1.06667, vec2(0.0), vec2(0.9333, 1.0));

    normal = normalize(gl_NormalMatrix * gl_Normal);
    
    color = gl_Color;

    entityId = mc_Entity.x; // Pass entity ID to fragment shader

    const vec2 sunRotationData = vec2(cos(sunPathRotation * 0.01745329251994), -sin(sunPathRotation * 0.01745329251994));
    float ang = fract(timeAngle - 0.25);
    ang = (ang + (cos(ang * 3.14159265358979) * -0.5 + 0.5 - ang) / 3.0) * 6.28318530717959;
    sunVec = normalize((gbufferModelView * vec4(vec3(-sin(ang), cos(ang) * sunRotationData) * 2000.0, 1.0)).xyz);

    upVec = normalize(gbufferModelView[1].xyz);
    eastVec = normalize(gbufferModelView[0].xyz);

    #ifdef WORLD_CURVATURE
    vec4 position = gbufferModelViewInverse * gbufferProjectionInverse * ftransform();
    position.y -= WorldCurvature(position.xz);
    gl_Position = gl_ProjectionMatrix * gbufferModelView * position;
    #else
    gl_Position = ftransform();
    #endif
    
    #ifdef TAA
    gl_Position.xy = TAAJitter(gl_Position.xy, gl_Position.w);
    #endif
}

#endif

