// Boxy texture вже оголошено в Atmosphere.glsl, не потрібно тут



vec3 boxyWind = vec3(1.5e-3, 0.0, 0.8e-3) * worldTimeCounter * BOXY_CLOUDS_SPEED;

// Sample boxy cloud texture with tiling
float SampleBoxyCloud(vec3 worldPos) {
    vec2 uv = worldPos.xz * BOXY_CLOUDS_SCALE - boxyWind.xz;
    uv = fract(uv);
    
    float heightFactor = (worldPos.y - BOXY_CLOUDS_ALTITUDE) / BOXY_CLOUDS_THICKNESS;
    if (heightFactor < 0.0 || heightFactor > 1.0) return 0.0;
    
    // Sample texture
    float boxySample = texture(colortex12, uv).r;
    
    // Height attenuation
    float heightAtten = saturate(heightFactor * 4.0) * saturate((1.0 - heightFactor) * 2.0);
    
    return boxySample * heightAtten * BOXY_CLOUDS_DENSITY;
}

// Render boxy clouds
vec4 RenderBoxyClouds(vec3 worldDir, float dither, vec4 phases, float time, vec3 sunIllum, vec3 moonIllum, vec3 skyIllum) {
    if (worldDir.y <= 0.0) return vec4(0.0, 0.0, 0.0, 1.0);
    
    vec3 planeOrigin = vec3(0.0, planetRadius + eyeAltitude, 0.0);
    
    // Check if we're below clouds
    if (eyeAltitude >= BOXY_CLOUDS_ALTITUDE + BOXY_CLOUDS_THICKNESS) {
        return vec4(0.0, 0.0, 0.0, 1.0);
    }
    
    // Ray-sphere intersection
    vec2 bottomInt = RaySphereIntersection(planeOrigin, worldDir, planetRadius + BOXY_CLOUDS_ALTITUDE);
    vec2 topInt = RaySphereIntersection(planeOrigin, worldDir, planetRadius + BOXY_CLOUDS_ALTITUDE + BOXY_CLOUDS_THICKNESS);
    
    float startDist = bottomInt.y;
    float endDist = topInt.y;
    
    if (startDist <= 0.0) return vec4(0.0, 0.0, 0.0, 1.0);
    
    // Marching parameters
    const uint steps = 16u;
    float rayLength = (endDist - startDist) / float(steps);
    vec3 rayStep = worldDir * rayLength;
    vec3 rayPos = worldDir * (startDist + rayLength * dither) + cameraPosition;
    
    float LdotV = dot(worldDir, worldLightVector);
    float phase = HenyeyGreensteinPhase(LdotV, 0.6) * 0.8 + 
                  HenyeyGreensteinPhase(LdotV, -0.4) * 0.2;
    
    float scattering = 0.0;
    float transmittance = 1.0;
    
    // Ray marching
    for (uint i = 0u; i < steps; ++i, rayPos += rayStep) {
        if (transmittance < 0.01) break;
        
        float density = SampleBoxyCloud(rayPos);
        if (density < 1e-4) continue;
        
        // Simple lighting
        vec3 lightPos = rayPos + worldLightVector * 200.0;
        float lightDensity = SampleBoxyCloud(lightPos);
        float lightAtten = exp(-lightDensity * 2.0);
        
        float stepTrans = exp(-density * 0.15 * rayLength);
        scattering += phase * lightAtten * transmittance * (1.0 - stepTrans);
        transmittance *= stepTrans;
    }
    
    if (transmittance > 0.99) return vec4(0.0, 0.0, 0.0, 1.0);
    
    // Lighting
    bool moonlit = worldSunVector.y < -0.04;
    vec3 cloudColor = scattering * 18.0 * (moonlit ? moonIlluminance : sunIlluminance);
    cloudColor += (1.0 - transmittance) * 0.1 * skyIlluminance;
    
    // Atmospheric fade
    float dist = distance(rayPos, cameraPosition);
    float atmosFade = exp(-dist * 0.0001);
    
    return vec4(cloudColor * atmosFade, transmittance);
}