//////////////////////////////////////////////////////////////////////////////
//    ##      #####   ######   ######     ##     ####     #######  ##  ##   //
//   ####    ##   ##  # ## #    ##  ##   ####     ##       ##   #  ##  ##   //
//  ##  ##   #          ##      ##  ##  ##  ##    ##       ## #     ####    //
//  ##  ##    #####     ##      #####   ##  ##    ##       ####      ##     //
//  ######        ##    ##      ## ##   ######    ##   #   ## #     ####    //
//  ##  ##   ##   ##    ##      ##  ##  ##  ##    ##  ##   ##   #  ##  ##   //
//  ##  ##    #####    ####    #### ##  ##  ##   #######  #######  ##  ##   //
//--------------------------------------------------------------------------//
//--------------------------------------------------------------------------//
//      #####   ##   ##    ##     #####    #######  ######    #####         //
//     ##   ##  ##   ##   ####     ## ##    ##   #   ##  ##  ##   ##        //
//     #        ##   ##  ##  ##    ##  ##   ## #     ##  ##  #              //
//      #####   #######  ##  ##    ##  ##   ####     #####    #####         //
//          ##  ##   ##  ######    ##  ##   ## #     ## ##        ##        //
//          ##  ##   ##  ######    ##  ##   ## #     ## ##        ##        //
//     ##   ##  ##   ##  ##  ##    ## ##    ##   #   ##  ##  ##   ##        //
//      #####   ##   ##  ##  ##   #####    #######  #### ##   #####         //
/////(BSL Shaders Edit)//////////////////////////////////////By LexBoosT//////


float CloudNoiseEnd(vec2 coord, vec2 wind){
		float noise = texture2D(noisetex, coord*1        + wind * 0.55).x;
		  noise+= texture2D(noisetex, coord*0.5      + wind * 0.45).x * -4.0;
		  noise+= texture2D(noisetex, coord*0.25     + wind * 0.35).x * 8.0;
		  noise+= texture2D(noisetex, coord*0.125    + wind * 0.25).x * -7.0;
		  noise+= texture2D(noisetex, coord*0.0625   + wind * 0.15).x * 25.0;
		  noise+= texture2D(noisetex, coord*0.03125  + wind * 0.05).x * 25.0;
		  noise+= texture2D(noisetex, coord*0.015625 + wind * 0.05).x * -10.0;
		return noise;
}

float CloudCoverageEnd(float noise,float cosT,float coverage){
	float noiseMix=mix(noise,21.,.33*rainStrengthS);
	float noiseFade=clamp(sqrt(cosT*10.),0.,1.);
	float noiseCoverage=((coverage*coverage)+ (CLOUD_AMOUNT_END*1.3));
	float multiplier=1.-.5*rainStrengthS;
	
	return max(noiseMix*noiseFade-noiseCoverage,0.)*multiplier;
}

vec4 DrawEndCloud(vec3 viewPos,float dither,vec3 lightCol){
	float cosT=dot(normalize(viewPos),upVec);
	float cosS=dot(normalize(viewPos),sunVec);
	
	#if AA > 0
	dither=fract(16.*frameTimeCounter+dither);
	#endif
	
	float cloud=0.;
	float cloudGradient=0.;
	float gradientMix=dither*.5;
	float colorMultiplier=CLOUD_BRIGHTNESS_END*(.5-.25*(1.-sunVisibility)*(1.-rainStrengthS));
	float noiseMultiplier=CLOUD_THICKNESS_END*.9;
	float scattering=pow(cosS*.5+.5,6.);
	
	vec2 wind=vec2(frameTimeCounter*CLOUD_SPEED_END*.005,
		sin(frameTimeCounter*CLOUD_SPEED_END*.05)*.002)*CLOUD_HEIGHT_END/25.;
		
		vec3 cloudcolor=vec3(0.);
		
	if(cosT>0.025){
		vec3 wpos=normalize((gbufferModelViewInverse*vec4(viewPos,1.)).xyz);
		for(int i=0;i<CLOUD_QUALITY_END;i++){
			if(cloud>.99)break;
			vec3 planeCoord=wpos*((CLOUD_HEIGHT_END+(i+dither))/wpos.y)*.005*pow(cosT,-.1);
			vec2 coord=cameraPosition.xz*.00025+planeCoord.xz;
			float coverage=float(i-5.+dither)*.725;
			
			float noise=CloudNoiseEnd(coord,wind);
			noise=CloudCoverageEnd(noise,cosT,coverage)*noiseMultiplier;
			noise = noise / sqrt(noise * noise + 0.25);
			
			cloudGradient = mix(cloudGradient,
									mix(gradientMix * gradientMix, 1.0 - noise, 0.25),
									noise * (1.0 - cloud));
				
				cloud += max(noise - cloud * 1.0, 0.0);
				gradientMix += 0.6;
			}
			float meFactorP = min((1.0 - min(sunVisibility, 0.6) / 0.6) * 0.115, 0.075);
			vec3 meColor = vec3(0.0);

			float mecosT = 1.0 - cosT;
			float meFactor = meFactorP * mecosT * mecosT * 12.0;

			cloudcolor=lightCol*vec3(6.4,6.8,5.)*(1.+scattering)*pow(cloudGradient,.75);
			cloudcolor=pow(cloudcolor,vec3(2))*2*vec3(1.4,1.8,1.);
			cloud*=min(pow(cosT*2,2.),.42);
		}

		
		return vec4(cloudcolor* clamp(colorMultiplier,0.0,1.0)*(.6+(pow(sunVisibility,.1))*.4),pow(cloud,2)*1.0*CLOUD_OPACITY_END*(2-(pow(sunVisibility,.1))));
}