#ifndef SCENE_COMMON_INCLUDED
#define SCENE_COMMON_INCLUDED
#include "URP_Function.hlsl"
//uniform half _LM_Strength, _SM_Strength;
// void LightmapResult(inout Light light,half3 bakedGI,half3 positionWS,half2 lightmapUV,half3 N)
// {
// #if defined(LIGHTMAP_ON) && defined(SHADOWS_SHADOWMASK)
// 	half4 shadowmask = lerp(SampleShadowMask(lightmapUV), half4(1, 1, 1, 1), _SM_Strength);
// 	bakedGI = saturate(bakedGI+_LM_Strength);
// #else
// 	half4 shadowmask = half4(0,0,0,0);
// #endif
// 	MixRealtimeAndBakedGI(light, N, bakedGI,shadowmask,positionWS);
// }
inline half4 Blend(half depth1 ,half depth2,half depth3,half depth4 , half4 control) 
{
	half4 blend ;
	
	blend.r =depth1 * control.r;
	blend.g =depth2 * control.g;
	blend.b =depth3 * control.b;
	blend.a =depth4 * control.a;
	
	half ma = max(blend.r, max(blend.g, max(blend.b, blend.a)));
	blend = max(blend - ma +_Weight , 0) * control;
	return blend/(blend.r + blend.g + blend.b + blend.a);
}
half4 SampleNormalAlpha(float2 uv, TEXTURE2D_PARAM(bumpMap, sampler_bumpMap), half scale = 1.0h)
{
	half4 n = SAMPLE_TEXTURE2D(bumpMap, sampler_bumpMap, uv);
	n = half4(n.rg,1,n.b);
#ifdef _NORMALMAP
	#if BUMP_SCALE_NOT_SUPPORTED
		return half4(UnpackNormal(n),n.a);
	#else
		return half4(UnpackNormalScale(n, scale),n.a);
	#endif
#else
	return half4(0.0h, 0.0h, 1.0h,1);
#endif
}
inline float EchoRipple ( float2 itexuv, float iamount, float ispeed, float iheight, float icenterx, float icentery )
{
	itexuv.x += icenterx;
	itexuv.y += icentery;

	return sin ( length ( itexuv ) * iamount - ispeed * _Time ) * iheight;	
}
// float currentTimePassed(int _Interval)
// {
// 	float currentTime = _Time.y;
// 	int currentTimeInt = _Time.y / _Interval;
// 	currentTimeInt *= _Interval;
// 	float Time = currentTime - currentTimeInt;
// 	return Time;
// }

void SceneNdot(inout Light light,float3 Normal,float4 positionWS,out float3 viewDir, out float NdotH,out float NdotL,out float VdotH,out float NdotV)
{
	float3 L = normalize(light.direction*light.distanceAttenuation);
	#if defined(_MIXED_LIGHTING_SUBTRACTIVE) && defined(LIGHTMAP_ON)
	L = normalize(light.direction);
	#endif
	viewDir = normalize(_WorldSpaceCameraPos.xyz - positionWS.xyz);
	float3 halfDir = normalize(viewDir + L);
	NdotH = saturate(dot(Normal, halfDir));
	NdotL = saturate(dot(Normal, L));
	VdotH = saturate(dot(viewDir, halfDir));
	NdotV = abs(dot(Normal, viewDir));
}
uniform half _FogStart;
uniform half _FogEnd;
uniform half4 _FogColor;
uniform half4 _HFogColor;
uniform half _HFogStart;
uniform half _HFogEnd;
uniform half _StartPoint;
uniform half U_Density;
uniform half UGradiant;
float LinearFog(float distance)
{
    float fogAlpha=(distance-_FogStart)/(_FogEnd-_FogStart);
    fogAlpha=clamp(fogAlpha,0.0,1.0);
    return fogAlpha;
}
float ExpFog(float distance)
{
    float fogAlpha=exp(-distance * U_Density);
    fogAlpha=1.0-clamp(fogAlpha,0.0,1.0);
    return fogAlpha;
}
float Exp2Fog(float distance)
{
    float fogAlpha=exp(-pow(distance * U_Density,UGradiant));
    fogAlpha=1.0-clamp(fogAlpha,0.0,1.0);
    return fogAlpha;
}
float applyFog(float distance,float _ExpFog)
{	
    float fogAlpha = lerp(1-LinearFog(distance),ExpFog(distance),_ExpFog);
    return fogAlpha;
}
half3 HeightFog(half3 col,float positionWS,float distance)
{	
    float hValue = saturate((positionWS - _HFogStart) / (_HFogEnd - _HFogStart));
	_HFogColor.rgb = lerp(_HFogColor.rgb, col.rgb, hValue);
	col.rgb = lerp(col.rgb, lerp(col.rgb, _HFogColor.rgb, distance) , step(_StartPoint, positionWS));
    return col;
}

#endif

