 
     gl_FragColor = finalColor;
 }
+
+#ifdef GROUND_ATMOSPHERE
+vec3 computeGroundAtmosphereColor(vec3 fogColor, vec4 finalColor, vec3 atmosphereLightDirection, float cameraDist)
+{
+#if defined(PER_FRAGMENT_GROUND_ATMOSPHERE) && defined(DYNAMIC_ATMOSPHERE_LIGHTING) && (defined(ENABLE_DAYNIGHT_SHADING) || defined(ENABLE_VERTEX_LIGHTING))
+    float mpp = czm_metersPerPixel(vec4(0.0, 0.0, -czm_currentFrustum.x, 1.0), 1.0);
+    vec2 xy = gl_FragCoord.xy / czm_viewport.zw * 2.0 - vec2(1.0);
+    xy *= czm_viewport.zw * mpp * 0.5;
+
+    vec3 direction = normalize(vec3(xy, -czm_currentFrustum.x));
+    czm_ray ray = czm_ray(vec3(0.0), direction);
+
+    vec3 ellipsoid_center = czm_view[3].xyz;
+
+    czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid_center, czm_ellipsoidInverseRadii);
+
+    vec3 ellipsoidPosition = czm_pointAlongRay(ray, intersection.start);
+    ellipsoidPosition = (czm_inverseView * vec4(ellipsoidPosition, 1.0)).xyz;
+    AtmosphereColor atmosColor = computeGroundAtmosphereFromSpace(ellipsoidPosition, true, atmosphereLightDirection);
+
+    vec3 groundAtmosphereColor = colorCorrect(atmosColor.mie) + finalColor.rgb * colorCorrect(atmosColor.rayleigh);
+#ifndef HDR
+    groundAtmosphereColor = vec3(1.0) - exp(-fExposure * groundAtmosphereColor);
+#endif
+
+    fadeInDist = u_nightFadeDistance.x;
+    fadeOutDist = u_nightFadeDistance.y;
+
+    float sunlitAtmosphereIntensity = clamp((cameraDist - fadeOutDist) / (fadeInDist - fadeOutDist), 0.0, 1.0);
+
+#ifdef HDR
+    // Some tweaking to make HDR look better
+    sunlitAtmosphereIntensity = max(sunlitAtmosphereIntensity * sunlitAtmosphereIntensity, 0.03);
+#endif
+
+    groundAtmosphereColor = mix(groundAtmosphereColor, fogColor, sunlitAtmosphereIntensity);
+#else
+    vec3 groundAtmosphereColor = fogColor;
+#endif
+
+#ifdef HDR
+    // Some tweaking to make HDR look better
+    groundAtmosphereColor = czm_saturation(groundAtmosphereColor, 1.6);
+#endif
+
+    return groundAtmosphereColor;
+}
+#endif
 
 #ifdef SHOW_REFLECTIVE_OCEAN
 

        return;
    }

#if defined(PER_FRAGMENT_GROUND_ATMOSPHERE) && defined(DYNAMIC_ATMOSPHERE_LIGHTING) && (defined(ENABLE_DAYNIGHT_SHADING) || defined(ENABLE_VERTEX_LIGHTING))
    float mpp = czm_metersPerPixel(vec4(0.0, 0.0, -czm_currentFrustum.x, 1.0), 1.0);
    vec2 xy = gl_FragCoord.xy / czm_viewport.zw * 2.0 - vec2(1.0);
    xy *= czm_viewport.zw * mpp * 0.5;

    vec3 direction = normalize(vec3(xy, -czm_currentFrustum.x));
    czm_ray ray = czm_ray(vec3(0.0), direction);

    vec3 ellipsoid_center = czm_view[3].xyz;

    czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid_center, czm_ellipsoidInverseRadii);

    vec3 ellipsoidPosition = czm_pointAlongRay(ray, intersection.start);
    ellipsoidPosition = (czm_inverseView * vec4(ellipsoidPosition, 1.0)).xyz;
    AtmosphereColor atmosColor = computeGroundAtmosphereFromSpace(ellipsoidPosition, true, atmosphereLightDirection);

    vec3 groundAtmosphereColor = colorCorrect(atmosColor.mie) + finalColor.rgb * colorCorrect(atmosColor.rayleigh);
#ifndef HDR
    groundAtmosphereColor = vec3(1.0) - exp(-fExposure * groundAtmosphereColor);
#endif

    fadeInDist = u_nightFadeDistance.x;
    fadeOutDist = u_nightFadeDistance.y;

    float sunlitAtmosphereIntensity = clamp((cameraDist - fadeOutDist) / (fadeInDist - fadeOutDist), 0.0, 1.0);

#ifdef HDR
    // Some tweaking to make HDR look better
    sunlitAtmosphereIntensity = max(sunlitAtmosphereIntensity * sunlitAtmosphereIntensity, 0.03);
#endif

    groundAtmosphereColor = mix(groundAtmosphereColor, fogColor, sunlitAtmosphereIntensity);
#else
    vec3 groundAtmosphereColor = fogColor;
#endif

#ifdef HDR
    // Some tweaking to make HDR look better
    groundAtmosphereColor = czm_saturation(groundAtmosphereColor, 1.6);
#endif

    finalColor = vec4(mix(finalColor.rgb, groundAtmosphereColor, fade), finalColor.a);
#endif

#ifdef UNDERGROUND_COLOR

vec4 computeDayColor(vec4 initialColor, vec3 textureCoordinates);
vec4 computeWaterColor(vec3 positionEyeCoordinates, vec2 textureCoordinates, mat3 enuToEye, vec4 imageryColor, float specularMapValue, float fade);

#ifdef GROUND_ATMOSPHERE
vec3 computeGroundAtmosphereColor(vec3 fogColor, vec4 finalColor, vec3 atmosphereLightDirection, float cameraDist);
#endif

const float fExposure = 2.0;

void main()
{
#ifdef TILE_LIMIT_RECTANGLE
