time = (TIME * 6.0) / 1200.0 + 4.6;

/*
0.0 mid day

1.5 sunset
*/

vec3 sunPosition = vec3(-sin(time), cos(time), cos(time)*0.9);
vec3 moonPosition = -sunPosition;
vec3 lightPosition = mix(sunPosition, moonPosition, float(sunPosition.y < 0.0));
vec3 upPosition = vec3(0.0,1.0,0.0);
sunVector = normalize(sunPosition);
moonVector = normalize(moonPosition);
lightVector = normalize(lightPosition);
upVector = normalize(upPosition);

isWetness = 0.0;
isMie = mix(0.05,0.1,isWetness);
isDensity = 0.14;
isSun = -smin(-0.01,-sunVector.y, 0.07);
isEnergyloss = max(pow(saturate(exp(sunVector.y*5.0)),5.0),0.0);
isTwilight = 0.005/(0.01+saturate(sunVector.y));
isTransmission = exp(isSun/-1e-6)-1.0;
isRadialDistributionF = max(-sunVector.y+0.01,0.0)*4.0;
isMultipleScattering = max(pow(saturate(exp(sunVector.y*20.0)),5.0),0.0);
isBlueTwilight = pow(vec3(0.4,0.4,1.0),vec3(isTwilight));
isRpM = rayleigh + isMie;

vec3 sunlight = sunLight(sunVector,1.0);
sunlight *= 10.0;
vec3 moonlight = sunLight(moonVector,1.0);
moonlight = vec3(moonlight.r);
moonlight *= 0.05;
colorSunlight = sunlight + moonlight;
colorSkylight = Atmosphere(normalize(vec3(0.0,1.0,0.0)), sunVector, 1.0)*4.0;//* (3.0 - 2.0 * saturate(0.05/(0.05+saturate(abs(sunVector.y)))));

#ifndef SKY
colorTorchlight = ColorTemperatureToRGB(2300.0) * 2.0;

atmos = pow(colorSkylight,vec3(1.55)) * 8.0;
atmos += colorSunlight * MiePhases(dot(normalize(world.xyz), lightVector), mix(0.6,0.1,isWetness)) * 0.5;
atmos *= pow(1.0 - exp2(-length(view.xyz) * 0.02), 2.0)/80.;

vec3 absorb = vec3(0.25, 0.04, 0.01)*1.0;


highp float x = distance(normalize(world.xyz), world.xyz) / 1.0;
x *= saturate((1.0 - uv1.y) - 0.085);
x *= float(uv1.y < 0.85);

highp float v = 0.0;

isWaterAbsorption = exp(-absorb*x - 0.07*x);
isWaterFog = -(0.0105/(exp(0.0*(0.07 + absorb))*((-1.0 + v)*(0.07 + absorb)))) + (0.0105)/(exp((0.07 + absorb)*(0.0 + x - v*x))*((-1.0 + v)*(0.07 + absorb)));
isWaterFog = max(isWaterFog*(colorSkylight * 0.25 + colorSunlight * (1.0 - isWetness * 0.75)), 0.0);
#endif