#ifndef WEATHER_INCLUDED
#define WEATHER_INCLUDED

// ----------------------------下雨---------------------------------
//half rainCurAmount;
half4 rainParams0; // x: Amount y: puddleAmout z: RipplesScale
half4 rainParams1;  // x,y: offsets z: RipplesAmount w: DiffuseDarkening
TEXTURE2D(rainRipplesTex);	SAMPLER(sampler_rainRipplesTex);

void ApplyRainningEffect(inout half3 normalWS, inout half smoothness, inout half3 albedo, float3 posWS, half occMask)
{
	if (rainParams0.x < 0.001)
		return;

	half fAttenuation = occMask;// GetAttenuation(_WorldSpaceCameraPos - posWS, rainParams0.x, rainParams0.y);
	//fAttenuation *= occMask;

	half isUpSurface = saturate(normalWS.y * 5 + 4.5);
	half rippleAmount = saturate(normalWS.y * 5 - 4);
	half puddleBlendAmount = rainParams0.y * isUpSurface;
	half puddleStrength = saturate(puddleBlendAmount/* * (puddleMask * 0.5h + 0.5h)*/);
	if (fAttenuation < 0.001 || puddleBlendAmount < 0.001)
		return;
	
	// Ripples
	half2 ripples = SAMPLE_TEXTURE2D(rainRipplesTex, sampler_rainRipplesTex, posWS.xz * rainParams0.z + rainParams1.xy).xy * 2 - 1;
	//normalWS.xz = lerp(normalWS.xz, ripples * rainParams1.z * rainParams0.y, saturate(rippleAmount));
	normalWS.xz = normalWS.xz + ripples * rainParams1.z * rainParams0.y *saturate(puddleBlendAmount);
	normalWS = normalize(normalWS);

	// Albedo
	half porosity = saturate(((1-smoothness) - 0.5) / 0.3); //根据粗糙度计算空隙
	albedo *= lerp(1, lerp(1, 0.2, saturate(porosity * rainParams1.w)), puddleStrength * fAttenuation);

	// Smoothness
	smoothness = lerp(smoothness, max(lerp(0.9, 0.70, porosity), smoothness), puddleStrength * fAttenuation);
}

void ApplyRainningByTerrain(inout half3 normalWS, inout half smoothness, inout half4 albedo, float3 posWS, half occMask)
{
	if (rainParams0.x < 0.001)
		return;
	
	half fAttenuation = occMask;// GetAttenuation(_WorldSpaceCameraPos - posWS, rainParams0.x, rainParams0.y);
								//fAttenuation *= occMask;
	half distance = length(_WorldSpaceCameraPos - posWS);
	fAttenuation *= 1 - saturate(distance / 400);

	half isUpSurface = saturate(normalWS.y * 5 + 4.5);
	half rippleAmount = saturate(normalWS.y * 5 - 4);
	half puddleBlendAmount = rainParams0.y * isUpSurface;
	half puddleStrength = saturate(puddleBlendAmount/* * (puddleMask * 0.5h + 0.5h)*/);
	

	if (normalWS.y <= 0.6)
		return;

	if (fAttenuation < 0.001 || puddleBlendAmount < 0.001)
		return;

	// Ripples
	half2 ripples = SAMPLE_TEXTURE2D(rainRipplesTex, sampler_rainRipplesTex, posWS.xz * rainParams0.z + rainParams1.xy).xy * 2 - 1;
	//normalWS.xz = lerp(normalWS.xz, ripples * rainParams1.z * rainParams0.y, saturate(rippleAmount));
	normalWS.xz = normalWS.xz + ripples * rainParams1.z * rainParams0.y *saturate(puddleBlendAmount);
	normalWS = normalize(normalWS);

	// Albedo
	half porosity = saturate(((1 - smoothness) - 0.5) / 0.3); //根据粗糙度计算空隙
	albedo *= lerp(1, lerp(1, 0.2, saturate(porosity * rainParams1.w)), puddleStrength * fAttenuation);

	// Smoothness
	smoothness = lerp(smoothness, max(lerp(0.9, 0.7, porosity), smoothness), puddleStrength * fAttenuation);

}

void ApplyRainningByWater(inout half3 normalWS, float3 posWS)
{
	if (rainParams0.x < 0.001)
		return;

	half fAttenuation = 1;// GetAttenuation(_WorldSpaceCameraPos - posWS, rainParams0.x, rainParams0.y);
								//fAttenuation *= occMask;

	//half isUpSurface = saturate(normalWS.y * 5.h - 4.h);
	half puddleBlendAmount = rainParams0.y /** isUpSurface*/;
	half puddleStrength = saturate(puddleBlendAmount/* * (puddleMask * 0.5h + 0.5h)*/);
	if (fAttenuation < 0.001 || puddleBlendAmount < 0.001)
		return;

	// Ripples
	half2 ripples = SAMPLE_TEXTURE2D(rainRipplesTex, sampler_rainRipplesTex, posWS.xz * rainParams0.z + rainParams1.xy).xy * 2 - 1;
	normalWS.xz = normalWS.xz + ripples * rainParams1.z * rainParams0.y *saturate(puddleBlendAmount);
	normalWS = normalize(normalWS);
}

//TEXTURE2D_SHADOW(_RainOcclusionTexture);
//SAMPLER_CMP(sampler_RainOcclusionTexture);
TEXTURE2D_SHADOW(_RainOcclusionTexture);	SAMPLER_CMP(sampler_RainOcclusionTexture);
float4x4 _WorldToRainOcclusion;
#define RAINNING_OCCLUSION_BIAS 0.5f

float4 GetRainningOcclusionCoord(float3 positionWS)
{
	positionWS.y += RAINNING_OCCLUSION_BIAS;
	return mul(_WorldToRainOcclusion, float4(positionWS, 1.0));
}

//#if UNITY_REVERSED_Z
//#define BEYOND_OCCLUSION_FAR(coord) coord.z <= UNITY_RAW_FAR_CLIP_VALUE
//#else
//#define BEYOND_OCCLUSION_FAR(coord) coord.z >= UNITY_RAW_FAR_CLIP_VALUE
//#endif

half GetRainningOcclusion(float4 coord)
{
	//coord.z = 1-coord.z;
	half attenuation = SAMPLE_TEXTURE2D_SHADOW(_RainOcclusionTexture, sampler_RainOcclusionTexture, coord.xyz);
	return attenuation;
}

#endif