#version 450

layout(binding = 0, std140) uniform UniformBufferObject {
    mat4 mvp;
    layout(offset = 528) float gameTime;
} ubo;

layout(push_constant) uniform PushConstants {
    mat4 model;
} pushConstants;

layout(location = 0) in vec3 inPos;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec2 inUV;
layout(location = 3) in float inTexIndex;

layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 fragUV;
layout(location = 2) out float fragTexIndex;
layout(location = 3) out float fragFogDist;

void main() {
    if (inTexIndex > -2.1 && inTexIndex < -1.9) {
        // UI / crosshair (texIndex == -2.0): position is already in NDC, skip MVP
        gl_Position = vec4(inPos.xy, 0.0, 1.0);
        fragFogDist = 0.0;
    } else {
        vec4 wpos = pushConstants.model * vec4(inPos, 1.0);

        // Waving foliage: leaves only (tiles 42-47)
        bool isLeaves = (inTexIndex >= 42.0 && inTexIndex < 48.0);
        if (isLeaves) {
            float t    = ubo.gameTime;
            float px   = wpos.x;
            float pz   = wpos.z;
            float swayX = sin(t * 1.4 + px * 0.53 + pz * 0.31) * 0.07
                        + sin(t * 2.1 + px * 0.27)              * 0.03;
            float swayZ = cos(t * 1.2 + pz * 0.47 + px * 0.29) * 0.07
                        + cos(t * 1.8 + pz * 0.22)              * 0.03;
            wpos.x += swayX;
            wpos.z += swayZ;
        }

        // Water vertical waves: tiles 60-65
        bool isWater = (inTexIndex >= 60.0 && inTexIndex <= 65.0);
        if (isWater) {
            float t  = ubo.gameTime;
            float px = wpos.x;
            float pz = wpos.z;
            float wave = sin(t * 2.0 + px * 0.72 + pz * 0.51) * 0.055
                       + sin(t * 1.3 + px * 0.38 - pz * 0.83) * 0.030
                       + sin(t * 3.1 + px * 1.10 + pz * 0.30) * 0.015;
            wpos.y += wave;
        }

        gl_Position = ubo.mvp * wpos;
        fragFogDist = gl_Position.w;
    }
    fragColor = inColor;
    fragUV = inUV;
    fragTexIndex = inTexIndex;
}