#define DRAW_SUN //if not using custom sky


vec3 drawSun(float cosY, float sunInt){
	#ifdef DRAW_SUN
	return (clamp(cosY/0.0005-0.999/0.0005,0.,1.)*1000.*sunInt)*nsunlight*(1.0-rainStrength*0.992);
	#endif
	#ifndef DRAW_SUN
	return vec3(0.);
	#endif
}

vec3 getFogColor(vec3 sVector,float cosT) {
	// The End: deep blue-violet zenith with a soft purple horizon.
	float h = clamp(sVector.y * 0.5 + 0.5, 0.0, 1.0);
	float t = smoothstep(0.18, 0.95, h);
	// MEGABOUND THE END: 0=Perak, 1=Merah, 2=Biru, 3=Kuning,
	// 4=Ungu (default), 5=Hitam, 6=Putih.
#define END_SKY_COLOR 4 //[0 1 2 3 4 5 6]
#define END_SKY_BRIGHTNESS 1.0 //[0.40 0.60 0.75 0.90 1.00 1.15 1.30]

	vec3 horizon;
	vec3 zenith;
	if (END_SKY_COLOR == 0) { // Perak
		horizon = vec3(0.28, 0.30, 0.34);
		zenith  = vec3(0.055, 0.065, 0.085);
	} else if (END_SKY_COLOR == 1) { // Merah
		horizon = vec3(0.38, 0.050, 0.060);
		zenith  = vec3(0.080, 0.015, 0.025);
	} else if (END_SKY_COLOR == 2) { // Biru
		horizon = vec3(0.060, 0.22, 0.48);
		zenith  = vec3(0.012, 0.035, 0.12);
	} else if (END_SKY_COLOR == 3) { // Kuning
		horizon = vec3(0.40, 0.28, 0.055);
		zenith  = vec3(0.10, 0.055, 0.018);
	} else if (END_SKY_COLOR == 4) { // Ungu (default)
		horizon = vec3(0.180, 0.055, 0.280);
		zenith  = vec3(0.025, 0.030, 0.120);
	} else if (END_SKY_COLOR == 5) { // Hitam
		horizon = vec3(0.050, 0.045, 0.060);
		zenith  = vec3(0.004, 0.004, 0.008);
	} else { // Putih
		horizon = vec3(0.52, 0.52, 0.56);
		zenith  = vec3(0.12, 0.13, 0.16);
	}
	vec3 sky = mix(horizon, zenith, t) * END_SKY_BRIGHTNESS;

	// Subtle edge glow keeps the horizon from looking flat.
	float edge = pow(1.0 - abs(sVector.y), 3.0);
	sky += mix(vec3(0.045, 0.012, 0.070), (horizon + zenith) * 0.12, 0.35) * edge * END_SKY_BRIGHTNESS;

	return sky;
}

vec3 getSkyColor(vec3 sVector, float sunInt,float moonInt,float cosT) {
	const vec3 moonlight = vec3(0.8, 1.1, 1.4) * 0.06;

	float mCosT = clamp(cosT,0.0,1.);
	float absCosT = 1.0-max(cosT*0.82+0.26,0.2);
	float cosY = clamp(dot(sunVec,sVector),-1,1.);
	float Y = fastAcos(cosY);

	const float a = -1.;
	const float b = -0.32;
	const float c = 10.0;
	const float d = -3.0;
	const float e = 0.45;

	//luminance (cie model)
	float L0 = (1.0+a*exp(b/mCosT))*(1.0+c*(exp(d*Y)-exp(d*3.1415/2.))+e*cosY*cosY);

	//gradient
	vec3 grad3 = getFogColor(sVector,cosT);

	cosY = -cosY;
	Y = PI - Y;
	const float c2 = 5.0;
	float L0Moon = (1.0+a*exp(b/mCosT))*(1.0+c2*(exp(d*Y)-exp(d*3.1415/2.))+e*cosY*cosY);

	return grad3;

}


vec3 drawStars(vec3 pos){
	float elevation = clamp(pos.y,0.,1.);
	vec2 uv = pos.xz/pos.y*sqrt(elevation)*2.;
	float noise  = texture2D(noisetex, uv).x;

	
	return max(noise-0.975,0.)*vec3(1.)*elevation*(1.0 - rainStrength*0.999999);
}

vec3 calcAtmFog(in vec3 fogClr,in vec3 fposition,in vec3 color, float sunInt,float moonInt){

	float d = length(fposition);
	float fogFactorIn = exp(-d/120.);

	
	return mix(fogClr*0.4,color,fogFactorIn);
}