/*
====================================================================
  SXGG: Realis — Shader ultra-realistica per Minecraft
  Autore : Samax.GG
  File   : lib/waving.glsl
  Scopo  : Waving Foliage (Modulo 6) — vento sinusoidale + FBM.
  Richiede: lib/settings.glsl, lib/noise.glsl
====================================================================
*/

#ifndef WAVING_GLSL
#define WAVING_GLSL

vec3 windField(vec3 worldPos, float weight, float time, float rain) {
    float gustStrength = 1.0 + rain * 1.6;
    float speed        = WAVING_SPEED * (1.0 + rain * 0.8);
    float t            = time * speed;

    float phase = dot(worldPos.xz, vec2(0.5, 0.8));
    float mainWave  = sin(t * 1.15 + phase) * 0.6
                    + sin(t * 0.73 + phase * 1.7 + 1.3) * 0.4;

    float gust = fbm2D(worldPos.xz * 0.08 + vec2(t * 0.15, t * 0.09));
    gust = gust * gust * 2.0;

    float flutter = sin(t * 3.7 + hash12(floor(worldPos.xz)) * TAU) * 0.15;

    float amp = 0.045 * WAVING_AMPLITUDE * weight * gustStrength;

    vec3 offset;
    offset.x = (mainWave + flutter) * gust * amp;
    offset.z = (sin(t * 0.9 + phase * 1.3 + 2.1) + flutter) * gust * amp * 0.7;
    offset.y = sin(t * 1.4 + phase * 2.0) * gust * amp * 0.25 * step(0.9, weight);

    return offset;
}

float wavingWeight(float blockId, float topVertex) {
    float weight = 0.0;

    #ifdef WAVING_PLANTS
        if (abs(blockId - ID_PLANT_TOP)    < 0.5) weight = topVertex;
        if (abs(blockId - ID_CROPS)        < 0.5) weight = topVertex * 0.6;
        if (abs(blockId - ID_DOUBLE_LOWER) < 0.5) weight = topVertex * 0.5;
        if (abs(blockId - ID_DOUBLE_UPPER) < 0.5) weight = 0.5 + topVertex * 0.5;
        if (abs(blockId - ID_LILYPAD)      < 0.5) weight = 0.15;
    #endif

    #ifdef WAVING_LEAVES
        if (abs(blockId - ID_LEAVES) < 0.5) weight = 1.0;
        if (abs(blockId - ID_VINES)  < 0.5) weight = 0.8 + topVertex * 0.2;
    #endif

    return weight;
}

#endif // WAVING_GLSL
