float atmosphere_mieg = 0.758;

//vec3 mcolor = vec3(0.34, 0.81, 1.00) * 0.015;
vec3 mcolor = vec3(0.54, 0.81, 1.00)*0.06;

flat out vec3 atmosSun;

vec3 atmosDensity(in float height, in bool isTransmittance) {
    float rayleigh = 0.0;
    float mie = 0.0;
    if(isTransmittance) {
        rayleigh = exp(-height / atmosphere_scaleHeights.x + atmosphere_scaledPlanetRadius.x);
        mie = exp(-height / atmosphere_scaleHeights.y + atmosphere_scaledPlanetRadius.y);
    } else {
        rayleigh = exp(-height / atmosphere_scaleHeights.x);
        mie = exp(-height / atmosphere_scaleHeights.y);
    }
    float ozone = exp(-max(0.0, (35000.0 - height) - atmosphere_atmosphereRadius) /  5000.0)
	            * exp(-max(0.0, (height - 35000.0) - atmosphere_atmosphereRadius) / 15000.0);
    return vec3(rayleigh, mie, ozone);
}
vec3 atmosTransmittance(in vec3 position, in vec3 direction, float steps) {
    float rayLength = dot(position, direction);
          rayLength = sqrt(rayLength * rayLength + atmosphere_atmosphereRadiusSquared - dot(position, position)) - rayLength;
    vec3 increment = direction * (rayLength / 2);
    position += increment * 0.5;
    vec3 transmittance = atmosphere_coefficientsAttenuation * rayLength * atmosDensity(length(position), true);
    return exp(-transmittance);
}
vec3 atmosSunlight() {
    vec3 scolor = colAtmosSunlight * 1e1 * 2.2;
    vec3 sunLight = dot(sunPosition, upPosition) < 0.5 ? atmosTransmittance(vec3(0, atmosphere_planetRadius, 0), mat3(gbufferModelViewInverse) * moonVector, 6) * (mcolor): atmosTransmittance(vec3(0, atmosphere_planetRadius, 0), mat3(gbufferModelViewInverse) * sunVector, 6) * (scolor);
    return sunLight / pi;
}

void getAtmosSun() {
    atmosSun = atmosSunlight();
}