                 cloud.w = clamp(cloud.w + cloud_density, 0.0, 1.0);
             }
 
-            const float minDist = 100.0;
-            cloud.w *= clamp(1.0 - (dist - minDist) / (NEAR_FAR.y - minDist), 0.0, 1.0);
+            const float minDist = 1000.0;
+            cloud.w *= clamp(1.0 - (hit_dist - minDist) / (NEAR_FAR.y - minDist), 0.0, 1.0);
         }
     }
 

        {
            // be in clouds
            ray_start_pos = vec3(0.0, 0.0, 0.0);
        }
        else if(0.0 < view_direction.y)
        {
            // under the sky
            ray_start_pos = view_direction * abs(height_diff / view_direction.y);
        }
        else
        {
            // above the sky
            ray_start_pos = view_direction * abs((height_diff + cloud_thickness) / view_direction.y);
        }

        float dist = clamp(length(ray_start_pos), 0.0, NEAR_FAR.y);

        ray_start_pos += CAMERA_POSITION.xyz;

        if(dist < NEAR_FAR.y)
        {
            float march_height = cloud_thickness;

            if(0.0 < view_direction.y)
            {
                // Looking up at the sky
                march_height = clamp(cloud_thickness + height_diff, 0.0, cloud_thickness);
            }
            else
            {
                // Looking down at the sky
                march_height = clamp(-height_diff, 0.0, cloud_thickness);
            }

            const float absorption = 0.8;
            const vec3 cloud_color = vec3(0.5, 0.5, 0.7);
            const int march_count = 50;
            const vec3 cloud_speed = vec3(0.03, 0.03, 0.0) * TIME;
            const int light_march_count = 10;

            float march_step = abs(march_height / view_direction.y) / float(march_count);
            // When you are in the clouds, march steps are likely to be too large.
            //if(in_the_cloud)
            {
                march_step = min(march_step, 5.0);
            }

            for(int i=0; i<march_count; ++i)
            {


    // Cloud
    vec4 cloud = vec4(0.0);
    const float cloud_height = 500.0;
    const float cloud_thickness = 200.0;
    float height_diff = cloud_height - CAMERA_POSITION.y;
    //if(0.0 < view_direction.y && 0.0 < (height_diff + cloud_thickness) || view_direction.y < 0.0 && height_diff < 0.0)
    {
        // relative ray march start pos from the camera
        vec3 ray_start_pos;
        float hit_dist;

        const bool in_the_cloud = -cloud_thickness < height_diff && height_diff < 0.0;

