#include "/include/uniforms.glsl"
#include "/include/config.glsl"
#include "/include/constants.glsl"
#include "/include/common.glsl"

#ifdef fsh

in VSOUT 
{
    vec2 texcoord;
    vec3 vertexColor;
    flat uint blockId;
} vsout;

/* RENDERTARGETS: 1 */
layout (location = 0) out uvec4 colortex1Out;

void main ()
{
    #if TAA_UPSCALING_FACTOR < 100
        if (any(greaterThan(gl_FragCoord.xy + 0.5, internalScreenSize))) {
            return;
        }
    #endif
    
    vec4 albedo = texture(gtexture, vsout.texcoord) * vec4(vsout.vertexColor, 1.0);

    #ifdef STAGE_WEATHER
        // === RINTIK HUJAN v1.6.8 ===
        float texAlpha = albedo.a;

        // Threshold lebih rendah → lebih banyak partikel hujan terlihat
        if (texAlpha < 0.02) discard;

        // Warna rintik: putih-kebiruan transparan (lebih realistis seperti air)
        vec3 rainColor = mix(albedo.rgb, vec3(0.72, 0.80, 0.94), 0.35);

        // Brightness boost saat hujan deras
        rainColor *= 1.0 + 0.3 * rainStrength;

        // Opacity rintik: lebih terlihat (0.80 = cukup solid)
        float rainAlpha = texAlpha * 0.80;

        // Edge fade: lebih halus → ujung partikel memudar natural
        float edgeFade = smoothstep(0.0, 0.12, texAlpha) * smoothstep(1.0, 0.50, texAlpha);
        rainAlpha *= (0.55 + 0.45 * edgeFade);

        // Variasi ukuran: rintik bawah layar sedikit lebih besar/terang
        float heightVar = smoothstep(0.0, 1.0, gl_FragCoord.y / 540.0);
        rainAlpha *= (0.85 + 0.15 * heightVar);

        albedo = vec4(rainColor, clamp(rainAlpha, 0.03, 0.92));

        if (albedo.a < 0.03) discard;
    #endif

    colortex1Out = uvec4(packUnorm4x8(albedo.a > 0.1 ? albedo : vec4(1.0, 1.0, 1.0, 0.1)), vsout.blockId, 0u, 1u);
}

#endif

#ifdef vsh

attribute vec2 mc_Entity;

out VSOUT 
{
    vec2 texcoord;
    vec3 vertexColor;
    flat uint blockId;
} vsout;

void main ()
{   
    gl_Position = ftransform();
    
    #if defined STAGE_HAND && HAND_FOV > 0   
        gl_Position.xy *= handScale / gl_ProjectionMatrix[1].y;
    #endif
    
    gl_Position.xy += gl_Position.w * taaOffset;
    
    #if TAA_UPSCALING_FACTOR < 100
        gl_Position.xy = mix(-gl_Position.ww, gl_Position.xy, TAAU_RENDER_SCALE);
    #endif

    vsout.texcoord = mat4x2(gl_TextureMatrix[0]) * gl_MultiTexCoord0;
    vsout.vertexColor = gl_Color.rgb;
    vsout.blockId = (pack2x8(octEncode(alignNormal(transpose(mat3(gbufferModelView)) * gl_NormalMatrix * gl_Normal, 0.01))) << 16u) |

    #ifdef STAGE_HAND
        (currentRenderedItemId & 16383u) | 0x00004000u;
    #else
        uint(mc_Entity.x) & 16383u;
    #endif

    #ifdef STAGE_WEATHER
        // === Rain wind sway v1.6.8 ===
        // Subtle horizontal wind effect on rain particles
        float windPhase = gl_Vertex.x * 0.3 + gl_Vertex.z * 0.5 + frameTimeCounter * 2.5;
        float windSway = sin(windPhase) * 0.4 * rainStrength;
        gl_Position.x += gl_Position.w * windSway * 0.003;
    #endif
}

#endif