shader_type spatial;
render_mode unshaded;

global uniform float global_wind_speed     = 1.0; //0.0 = calm, 1.0 = normal, 2.0+ = strong
global uniform vec2  global_wind_direction  = vec2(1.0, 0.0);

uniform float wave_frequency  = 1.0;
uniform float base_amplitude  = 0.5;
uniform float secondary_ratio = 0.6;
uniform float time_scale_base = 3.0;

void vertex() {
    float random_seed_offset = INSTANCE_CUSTOM.x * 100.0;
    float scaled_time = (TIME + random_seed_offset) * (time_scale_base * global_wind_speed);
    float amplitude   = base_amplitude * global_wind_speed;

    vec2 wind_dir  = normalize(global_wind_direction);
    vec2 wind_perp = vec2(-wind_dir.y, wind_dir.x); //90° perpendicular

    float primary_sway   = sin(VERTEX.y * wave_frequency + scaled_time) * amplitude;
    float secondary_sway = sin(VERTEX.y * wave_frequency * 1.3 + scaled_time * 0.7 + 1.57)
                           * (amplitude * secondary_ratio);

    VERTEX.x += primary_sway * wind_dir.x  + secondary_sway * wind_perp.x;
    VERTEX.z += primary_sway * wind_dir.y  + secondary_sway * wind_perp.y;
}

void fragment() {
    ALBEDO = COLOR.rgb;
    ALPHA = COLOR.a;
}