/////////////////////////////////////////////////////////////////////
// __                      ____    __                             ///
///\ \                    /\  _`\ /\ \      __                    ///
//\ \ \         __   __  _\ \,\L\_\ \ \___ /\_\    ___     ___    ///
// \ \ \  __  /'__`\/\ \/'\\/_\__ \\ \  _ `\/\ \ /' _ `\  / __`\  ///
//  \ \ \L\ \/\  __/\/>  </  /\ \L\ \ \ \ \ \ \ \/\ \/\ \/\ \L\ \ ///
//   \ \____/\ \____\/\_/\_\ \ `\____\ \_\ \_\ \_\ \_\ \_\ \____/ ///
//    \/___/  \/____/\//\/_/  \/_____/\/_/\/_/\/_/\/_/\/_/\/___/  ///
//                                                                ///
//                                                                ///
//             ____    __                   __                    ///
//            /\  _`\ /\ \                 /\ \                   ///
//            \ \,\L\_\ \ \___      __     \_\ \     __   _ __    ///
//             \/_\__ \\ \  _ `\  /'__`\   /'_` \  /'__`\/\`'__\  ///
//               /\ \L\ \ \ \ \ \/\ \L\.\_/\ \L\ \/\  __/\ \ \/   ///
//               \ `\____\ \_\ \_\ \__/.\_\ \___,_\ \____\\ \_\   ///
//                \/_____/\/_/\/_/\/__/\/_/\/__,_ /\/____/ \/_/   ///
//                                                                ///
/////////////////////////////////////////////////////////////////////
//																   //
//							    WARNING							   //
//																   //
//	/!\  If you want to use my code, please ask me first!!  /!\	   //
//																   //
//																   //
/////////////////////////////////////////////////////////////////////  

#include "animalcrossing.glsl"
const int 	shadowMapResolution 	= 512; // [512 1024 2048 3072 4096 5120 6144 7128 8192 9216 10240] //Resolution en Pixel des Ombres

const vec3 heldtorch = vec3(1.,.7,REGLAGE_COULEUR);
#ifdef COLORED_LIGHT_ITEMS
const vec3 sealantern = vec3(.3,.7,1.);
const vec3 endrod = vec3(1,0.4,1);
const vec3 redstonetorch = vec3(0.9,0.18,0.18);
const vec3 glowstone = vec3(1.,0.7,0.09);
const vec3 beacon = vec3(0.05,0.61,0.9);
#endif

const vec2 circle_offsets[25] = vec2[25](vec2(-0.4894566f,-0.3586783f),
									vec2(-0.1717194f,0.6272162f),
									vec2(-0.4709477f,-0.01774091f),
									vec2(-0.9910634f,0.03831699f),
									vec2(-0.2101292f,0.2034733f),
									vec2(-0.7889516f,-0.5671548f),
									vec2(-0.1037751f,-0.1583221f),
									vec2(-0.5728408f,0.3416965f),
									vec2(-0.1863332f,0.5697952f),
									vec2(0.3561834f,0.007138769f),
									vec2(0.2868255f,-0.5463203f),
									vec2(-0.4640967f,-0.8804076f),
									vec2(0.1969438f,0.6236954f),
									vec2(0.6999109f,0.6357007f),
									vec2(-0.3462536f,0.8966291f),
									vec2(0.172607f,0.2832828f),
									vec2(0.4149241f,0.8816f),
									vec2(0.136898f,-0.9716249f),
									vec2(-0.6272043f,0.6721309f),
									vec2(-0.8974028f,0.4271871f),
									vec2(0.5551881f,0.324069f),
									vec2(0.9487136f,0.2605085f),
									vec2(0.7140148f,-0.312601f),
									vec2(0.0440252f,0.9363738f),
									vec2(0.620311f,-0.6673451f)
									);

float getOmbres(vec3 position)
{	float shading = 1.0f;
	#ifdef OMBRES

	position.y += getAnimalCrossingOffset(position.xyz);

	vec4 fragpos = gbufferModelView * vec4(position,1.0);
	float dist = length(fragpos.xyz);
    float shadingsharp = 0.0f;
	
    vec4 shadowposition = shadowModelView * vec4(position,1.0);
	float comparedepth = -shadowposition.z;
	shadowposition = shadowProjection * shadowposition;
	shadowposition /= shadowposition.w;
	
	float distb = sqrt(shadowposition.x * shadowposition.x + shadowposition.y * shadowposition.y);
	float distortFactor = (1.0f - SHADOW_MAP_BIAS) + distb * SHADOW_MAP_BIAS;
	shadowposition.xy *= 1.0f / distortFactor;
	shadowposition = shadowposition * 0.5f + 0.5f;
	float skybrightness= eyeBrightnessSmooth.y /255.0;
	
	float diffthresh = 4*distortFactor;
	
	float step = 1.0/shadowMapResolution;
	
		if (dist < shadowDistance) 
			{
			#ifdef FILTRE_OMBRES
				for(int i = 0; i < 25; i++){
					shadingsharp += (clamp(comparedepth - (0.05 + (texture2D(shadowtex1, shadowposition.st + circle_offsets[i]*step).z) * (256.0 - 0.05)), 0.0, diffthresh)/(diffthresh));
				}
				shadingsharp /= 25.0;
				shading = 1.0-(shadingsharp*skybrightness);
			#else
				shading = (clamp(comparedepth - (0.05 + (texture2D(shadowtex1, shadowposition.st).z) * (256.0 - 0.05)), 0.0, diffthresh)/(diffthresh));
				shading = 1.0-(shading*skybrightness);
			#endif
			}
		shading = mix(OPACITE_OMBRE,1.0,shading);
	#endif
    return shading;
}
const vec3 daylight = vec3(1.0,1.0,1.0);
#ifdef NUIT_CLAIRE
const vec3 nightlight = vec3(0.225, 0.3, 0.4);
#else
const vec3 nightlight = vec3(0.075, 0.1, 0.2);
#endif
const vec3 rainylight = vec3(0.3,0.3,0.35);


vec3 GetEclairage(vec3 position,bool hand,float emissive)
{
//////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////    ECLAIRAGE    ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
	float adjustedTime = abs(mod(worldTime / 1000.0 + 6.0, 24.0) - 12.0);
	float day = clamp(6.7 - adjustedTime, 0.0, 1.0);
	float night = clamp(adjustedTime - 6.7, 0.0, 1.0);
	vec3 lightcolor;
	if (emissive >0.0)
	{
		float brillance;
		if(hand) brillance = BRILLANCE_BLOC_DES_MAINS*emissive;
		else brillance = BRILLANCE_BLOCS_SOL*emissive;
		lightcolor = vec3(1.0+brillance);
	}
	else
	{
		vec3 dynlight= vec3(0.0);
		vec3 handlightcolor=vec3(0.0);
		
		#ifdef LUMIERE_DYNAMIQUE
			if (!hand)
			{
				
				if(heldItemId == 10050 ||heldItemId2 == 10050 ){
					handlightcolor = heldtorch;
				}
				#ifdef COLORED_LIGHT_ITEMS
				if(heldItemId == 10052 || heldItemId == 10053 ||  heldItemId == 10089 || heldItemId == 10091 || heldItemId == 10327 || heldItemId == 10377 || heldItemId == 10369 || heldItemId == 10378 || heldItemId == 	10385 ||
				heldItemId2 == 10052 || heldItemId2 == 10053 || heldItemId2 == 10089 || heldItemId2 == 10091 || heldItemId2 == 10327 || heldItemId2 == 10377 || heldItemId2 == 10369 || heldItemId2 == 10378 || heldItemId2 == 	10385){
					handlightcolor = heldtorch;
				}
				if(heldItemId == 10169 || heldItemId2 == 10169){
					handlightcolor = sealantern;
				}
				if(heldItemId == 10198 || heldItemId2 == 10198){
					handlightcolor = endrod;
				}
				if(heldItemId == 10076 || heldItemId2 == 10076){
					handlightcolor = redstonetorch;
				}
				if(heldItemId == 10089 || heldItemId2 == 10089 || heldItemId == 10052 || heldItemId == 10053 || heldItemId2 == 10052 || heldItemId2 == 10053){
					handlightcolor = glowstone;
				}
				if(heldItemId == 10138 || heldItemId2 == 10138){
					handlightcolor = beacon;
				}
				#endif
				dynlight = DISTANCE_ECLAIRAGE*handlightcolor/(length(.100*position.xyz)*length(.100*position.xyz));
				if (length(dynlight)>length(ECLAIRAGE_BLOC_DES_MAINS*handlightcolor)) dynlight=ECLAIRAGE_BLOC_DES_MAINS*handlightcolor;
			}
		#endif


		vec2 newlmcoord = lmcoord.st;

		newlmcoord.s = min(clamp(1.0-((1.0-newlmcoord.s) / ECLAIRAGE_BLOCS_SOL),0.0,1.0),clamp(newlmcoord.s * ECLAIRAGE_BLOCS_SOL,0.0,1.0));
		if(length(dynlight)>0.1)
		{
		newlmcoord.s = mix(0,newlmcoord.s,clamp(length(.100*position.xyz)*length(.100*position.xyz)/DISTANCE_ECLAIRAGE,0.0,1.0));
		}


		vec3 skylight= mix(mix(daylight,rainylight,rainStrength),nightlight,((1.0+night-day)/2.0) )*newlmcoord.t*newlmcoord.t*getOmbres(position);

		#ifdef EMISSIVE_RECOLOR

			vec3 Lex= vec3(1.0,0.8,0.5) * 1.2;
			vec3 blocklight = Lex*newlmcoord.s*newlmcoord.s;
	
		#else
			vec3 blocklight = vec3(newlmcoord.s*newlmcoord.s);
		#endif

		vec3 terrainlight = max(dynlight,blocklight);
		lightcolor = (terrainlight+skylight) / 1.5 ;
	}
	
	return lightcolor;
}
