#ifdef OVERWORLD
void SunGlare(inout vec3 color, vec3 viewPos, vec3 lightCol){
	float cosS = dot(normalize(viewPos), lightVec);
	float visfactor = 0.05 * (1.0 - 0.8 * timeBrightness) * (3.0 * rainStrength + 1.0);
	float invvisfactor = 1.0 - visfactor;
	
	float visibility = clamp(cosS * 0.5 + 0.5, 0.0, 1.0);
	visibility = visfactor / (1.0 - invvisfactor * visibility) - visfactor;
	visibility = clamp(visibility * 1.015 / invvisfactor - 0.015, 0.0, 1.0);
	visibility = mix(1.0, visibility, 0.25 * eBS + 0.75) * (1.0 - rainStrength * eBS * 0.875);
	
	
	#ifdef LIGHT_SHAFT
	color += 0.225 * lightCol * visibility * shadowFade * (1 - rainStrength) * timeBrightness;
	#else
	if (isEyeInWater == 1) color += 0.225 * lightCol * visibility * shadowFade * (1 - rainStrength);
	#endif
}

vec3 GetFogColor(vec3 viewPos){
	vec3 fogCol=fogCol;
	vec3 nViewPos=normalize(viewPos);
	float lViewPos=length(viewPos)/64.;
	lViewPos=1.-exp(-lViewPos*lViewPos);
	
	float NdotU=clamp(dot(nViewPos,upVec),0.,1.);
	float halfNdotU=clamp(dot(nViewPos,upVec)*.5+.5,0.,1.);
	float NdotS=dot(nViewPos,sunVec)*.5+.5;
	
	float lightmix=NdotS*NdotS*(1.-NdotU);
	lightmix*=(pow(1.-timeBrightness,3.)*.9+.1)*(1.-rainStrength)*lViewPos;
	
	float top=exp(-1.4*halfNdotU*halfNdotU*(1.+sunVisibility)*(1.-rainStrength));
	
	float mult=(.5*sunVisibility+.3)*(1.-.75*rainStrength)*top+
	.1*(1.+rainStrength);
	
	fogCol*=1.-sqrt(lightmix);
	fogCol=mix(fogCol,lightCol*sqrt(lightCol),lightmix)*sunVisibility;
	fogCol+=lightNight*lightNight*.4;
	
	vec3 fogWeather=weatherCol.rgb*weatherCol.rgb;
	fogWeather*=GetLuminance(ambientCol/fogWeather)*1.2;
	fogCol=mix(fogCol,fogWeather,rainStrength)*mult;
	
	return pow(fogCol,vec3(1.125));
}
#endif

void NormalFog(inout vec3 color,vec3 viewPos){
	#ifdef OVERWORLD
	float fog=length(viewPos)*FOG_DENSITY/256.;
	float clearDay=sunVisibility*(1.-rainStrength);
	fog*=(.5*rainStrength+1.)/(3.*clearDay+1.);
	fog=1.-exp(-2.*pow(fog,.25*clearDay+1.25)*eBS);
	vec3 fogColor=GetFogColor(viewPos);
	#endif
	
	#ifdef NETHER
	float viewLength=length(viewPos);
	float fog=2.*pow(viewLength*FOG_DENSITY/256.,1.5)+
	5.*pow(viewLength*1.5/far,4.);
	fog=1.-exp(-fog);
	vec3 fogColor=netherCol.rgb*.04;
	#endif
	
	#ifdef END
	float fog=length(viewPos)*FOG_DENSITY/128.;
	fog=1.-exp(-.8*fog*fog);
	vec3 fogColor=endCol*.025;
	#endif
	
	color=mix(color,fogColor,fog);
}

void BlindFog(inout vec3 color,vec3 viewPos){
	float fog=length(viewPos)* 0.05 *(5./blindFactor);
	fog=(1.-exp(-6.*fog*fog*fog))*blindFactor;
	color=mix(color,vec3(0.),fog);
}

void LavaFog(inout vec3 color,vec3 viewPos){
	float fog=length(viewPos)*.3;
	fog=(1.-exp(-4.*fog*fog*fog));
	#ifdef EMISSIVE_RECOLOR
	color=mix(color,pow(blocklightCol/BLOCKLIGHT_I,vec3(4.))*2.,fog);
	#else
	color=mix(color,vec3(1.,.25,.01),fog);
	#endif
}

void Fog(inout vec3 color,vec3 viewPos){
	NormalFog(color,viewPos);
	if(isEyeInWater==2)LavaFog(color,viewPos);
	if(blindFactor>0.)BlindFog(color,viewPos);
}