#ifdef OVERWORLD
vec3 GetFogColor(vec3 viewPos) {
	vec3 fogCol = fogCol;
	vec3 nViewPos = normalize(viewPos);
	float lViewPos = length(viewPos) / 64.0;
	lViewPos = 1.0 - exp(-lViewPos * lViewPos);

	float VoU = clamp(dot(nViewPos, upVec), 0.0, 1.0);
    float halfVoU = clamp(dot(nViewPos, upVec) * 0.5 + 0.5, 0.0, 1.0);
	float VoL = dot(nViewPos, sunVec) * 0.5 + 0.5;

	float lightmix = VoL * VoL * (1.0 - VoU) * lViewPos;
	lightmix *= (pow(1.0 - 0.7 * timeBrightness, 3.0) * 0.9 + 0.1) * (1.0 - rainStrength);

	float top = exp(-1.4 * halfVoU * halfVoU * (1.0 + sunVisibility) * (1.0 - rainStrength));

	float mult = (0.5 * sunVisibility + 0.3) * (1.0 - 0.75 * rainStrength) * top +
				 0.1 * (1.0 + rainStrength);

	fogCol *= 1.0 - sqrt(lightmix);
	fogCol = mix(fogCol, lightCol * sqrt(lightCol), lightmix) * sunVisibility;
	fogCol += lightNight * lightNight * 0.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


vec3 NormalFog(inout vec3 color,vec3 viewPos){
	#ifdef OVERWORLD
	float fogDensity=FOG_DENSITY;

	#if SUNVISIBILITY_FOG==0
	#endif
	
	#if SUNVISIBILITY_FOG==1
	fogDensity*=3-sunVisibility*2;
	#endif
	#if SUNVISIBILITY_FOG==2
	fogDensity*=4-sunVisibility*3;
	#endif
	
	#if SUNVISIBILITY_FOG==3
	fogDensity*=5-sunVisibility*4;
	#endif
	
	#if SUNVISIBILITY_FOG==4
	fogDensity*=6-sunVisibility*5;
	#endif
	
	float fog=length(viewPos)*fogDensity/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);
	
	#if DISTANT_FADE > 0
	if(isEyeInWater == 0.0){
		#if DISTANT_FADE == 1
		float fogFactor = length(viewPos);
		#else
		vec4 worldPos = gbufferModelViewInverse * vec4(viewPos, 1.0);
		worldPos.xyz /= worldPos.w;
		float fogFactor = length(worldPos.xz);
		#endif
		float vanillaFog = clamp(1.0 - (far - (fogFactor + 20.0)) * 5.0 / far, 0.0, 1.0);
		if(vanillaFog > 0.0){
			vec3 vanillaFogColor = GetSkyColor(viewPos, lightCol, false);
			vanillaFogColor *= (4.0 - 3.0 * eBS) * (1.0 + nightVision);

			fogColor *= fog;
			
			fog = mix(fog, 1.0, vanillaFog);
			if(fog > 0.0) fogColor = mix(fogColor, vanillaFogColor, vanillaFog) / fog;
		}
	}
	#endif
	#endif
	
	#ifdef NETHER
	float viewLength=length(viewPos);
	float fog=2.*pow(viewLength*FOG_DENSITY/256.,1.5)+
			  6.*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(+0.8*fog*fog);
	vec3 fogColor=endCol.rgb*.025;
	#endif
	
	color.rgb=mix(color.rgb,fogColor,fog);

	return vec3(color.rgb);
}

vec3 BlindFog(inout vec3 color, vec3 viewPos) {
	float fog = length(viewPos) * (blindFactor * 0.2);
	fog = (1.0 - exp(-6.0 * fog * fog * fog)) * blindFactor;
	color.rgb = mix(color.rgb, vec3(0.0), fog);

	return vec3(color.rgb);
}

vec3 LavaFog(inout vec3 color, vec3 viewPos) {
	float fog = length(viewPos) * 0.5;
	fog = (1.0 - exp(-4.0 * fog * fog * fog));
	#ifdef EMISSIVE_RECOLOR
	color.rgb = mix(color.rgb, pow(blocklightCol / BLOCKLIGHT_I, vec3(4.0)) * 2.0, fog);
	#else
	color.rgb = mix(color.rgb, vec3(1.0, 0.3, 0.01), fog);
	#endif

	return vec3(color.rgb);
}

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

	return vec3(color.rgb);
}