//https://iquilezles.org/www/articles/smin/smin.htm
highp float smin(in highp float a, in highp float b, in highp float k){
highp float h = saturate(0.5+0.5*(b-a)/k);
return mix(b, a, h) - (1.0-h)*h*k;}
highp float MiePhases(highp float ldv, float g){
return ((1.0+ldv*ldv)*(1.0-g*g)*1.5)/((2.0+g*g)*(1.0+g*g-2.0*g*ldv)*sqrt(1.0+g*g-2.0*g*ldv));}
const float pi = 3.141592653589793;

vec3 rayleigh = vec3(0.2,0.4,1.0);

vec3 sunLight(highp vec3 s, highp float x){
return max(exp2(-rayleigh*0.4)*saturate(s.y*40.0)*exp(-(rayleigh+0.5*isWetness)*(isDensity/(0.5*s.y)-isDensity/(exp(s.y*80.0*x)*(0.5*s.y)))),0.0);
}

vec3 Atmosphere(highp vec3 View, highp vec3 Sun, highp float x){

highp float Mie = max(dot(View, Sun),0.0);
highp float ScatterMie = MiePhases(Mie,mix(0.6,0.1,isWetness));
highp float RayleighMie = MiePhases(Mie,0.0);
highp float HeightWFEarthShadow = View.y;
highp float RadialDistributionF = 0.5-isRadialDistributionF*(0.5*-Mie+0.5);
highp float Transmission = (isSun + (-1.0 + exp(-1000000.0*RadialDistributionF*isSun))*HeightWFEarthShadow);

vec3 absorb = (exp(isRpM*isDensity*(Transmission/isSun-1.0)/(RadialDistributionF*HeightWFEarthShadow))*isSun)/(isRpM*Transmission) - (exp((isRpM*isDensity*(-1.0 + Transmission/(exp(RadialDistributionF*HeightWFEarthShadow*x*80.0)*isSun)))/(RadialDistributionF*HeightWFEarthShadow))*isSun)/(isRpM*Transmission);

vec3 MultipleScattering = pow(isRpM,vec3(0.454545)) * 0.05;

return 2.5 * (isBlueTwilight * (absorb * rayleigh * RayleighMie + absorb * ScatterMie * isMie) + MultipleScattering) * isEnergyloss;
}

vec3 HumanLowlight(vec3 amb, vec3 tl){
return mix(amb, dot(amb,vec3(0.3333)) * vec3(0.2,0.6,1.0), 0.8 * saturate(-sunVector.y*200.0))+tl;
}