Shader "URP/Terrain/JR_Scene_Pbr_Terrain"
{
	Properties
	{
		_Color ("Color", Color) = (1,1,1,1)
		//_LightDir("LightDir", vector) = (0.1, 0.44, -0.3, 0)
		//_HColor ("Highlight Color", Color) = (0.785,0.785,0.785,1.0)
		//_LightIntensity("HighLight Intensity",Float) = 1
		_diffuse_intensity ("Diffuse_Intensity", Range(0, 10)) = 1
		[Title(_,Specular)]
		[Sub(_)]_Roughness("Roughness", Range(0, 1)) = 0
		_SpecStrength ("SpecStrength", Range(0, 2)) = 1
		[Title(_,Shadow)]
		//[Sub(_)]_EnvShadow("Env Shadow",Color) = (0.5,0.5,0.5,1)
		[ToggleOff]_Receive_Shadows("Receive Shadows", Float) = 1.0

		[Title(_,Env)]
		//[Sub(Env)]_LM_Strength("lightmap强度", Range(0, 1)) = 0
		//[Sub(Env)]_SM_Strength("shadowmask强度", Range(0, 1)) = 0
		[Sub(_)]_envir_brightness ("反射x", Vector) = (0.2,0,0,0)
		[ToggleOff]_EnvironmentReflections("Environment Reflections", Float) = 0

		[Main(COLOR,_,2)]
		_COLOR("COLOR:基础远处-R通道内远处-G通道远处", float) = 0
		[Color(COLOR,_,_CMaskBottomFar,_CMaskBottomGFar)]
		_ColorBottomFar("远处颜色", Color) = (1, 1, 1, 1)
		[HideInInspector]_CMaskBottomFar("R通道内远处颜色", Color) = (1, 1, 1, 1)
		[HideInInspector]_CMaskBottomGFar("G通道远处颜色", Color) = (1, 1, 1, 1)
		//[HideInInspector]_Flip_CMaskBottomGFar("R通道内G通道远处颜色", Color) = (1, 1, 1, 1)
		[Title(COLOR,R)]
		[Sub(COLOR)]_HueShiftCM("R通道色相", Float) = 0
		[Sub(COLOR)]_SatCM("R通道饱和", range(0,10)) = 1
		[Sub(COLOR)]_ValCM("R通道明度", range(0,5)) = 0.5
		[Sub(COLOR)]_CMaskBottom("R通道颜色", Color) = (1, 1, 1, 1)
		[Title(COLOR,G)]
		[Sub(COLOR)]_HueShift("G通道色相", Float) = 0
		[Sub(COLOR)]_Sat("G通道饱和", range(0,10)) = 1
		[Sub(COLOR)]_Val("G通道明度", range(0,5)) = 0.5
		[Sub(COLOR)]_CMaskBottomG("G通道颜色", Color) = (1, 1, 1, 1)
		[Title(COLOR,ColorMask)]
		[Sub(COLOR)]_ColorStart("ColorStart", Float) = 0
		[Sub(COLOR)]_ColorEnd("ColorEnd", Float) = 50
		[Sub(COLOR)]_ColorMask("Color Mask:TillingX红通道TillingY绿通道", 2D) = "white" {}
		// [Title(HSV,R_G)]
		// [Sub(HSV)]_HueShiftFlipG("R通道内G通道色相", Float) = 0
		// [Sub(HSV)]_SatFlipG("R通道内G通道饱和", range(0,10)) = 1
		// [Sub(HSV)]_ValFlipG("R通道内G通道明度", range(0,5)) = 0.5
		// [Sub(HSV)]_Flip_CMaskBottomG("R通道内G通道颜色", Color) = (1, 1, 1, 1)

		// [Main(ColorMask,_,2)]
		// _CM("ColorMask", float) = 0
		// [Sub(ColorMask)]_ColorStart("ColorStart", Float) = 0
		// [Sub(ColorMask)]_ColorEnd("ColorEnd", Float) = 50
		// [Sub(ColorMask)]_ColorMask("Color Mask", 2D) = "white" {}
		//[Header(Base)]

		[Main(Tex,_,2)]
		_Tex("贴图", float) = 0
		[Sub(Tex)]_UvSize("UvSize:xy为地表尺寸zw为中心坐标", vector) = (200, 200, 0, 0)
		[Sub(Tex)]_Weight("Blend Weight" , Range(0.001,1)) = 0.2 
		[KWEnum(Tex,Uv,_,Uv3,_,Wrold_UV,_)]
		_BlendUV ("贴图uv切换", Float) = 0
		[KWEnum(Tex,Uv,_,Uv3,_,Wrold_UV,_)]
		_ControlUV ("Control控制图uv切换", Float) = 0
		[Sub(Tex)]_ColorR ("ColorR", Color) = (1,1,1,1)
    	[Sub(Tex)]_Splat0 ("Layer 1(RGBA)", 2D) = "white" {}
        [Tex(Tex,_NormalScale0)]_BumpMap ("Layer 1 Normal(Bump)", 2D) = "Bump" {}	
		[HideInInspector]_NormalScale0 ("Layer 1 NormalScale", Float) = 1
		[Sub(Tex)]_ColorG ("ColorG", Color) = (1,1,1,1)
    	[Sub(Tex)]_Splat1 ("Layer 2(RGBA)", 2D) = "white" {}
        [Tex(Tex,_NormalScale1)]_BumpSplat1 ("Layer 2 Normal(Bump)", 2D) = "Bump" {}
		[HideInInspector]_NormalScale1 ("Layer 2 NormalScale", Float) = 1
		[Sub(Tex)]_ColorB ("ColorB", Color) = (1,1,1,1)
    	[Sub(Tex)]_Splat2 ("Layer 3(RGBA)", 2D) = "white" {}
        [Tex(Tex,_NormalScale2)]_BumpSplat2 ("Layer 3 Normal(Bump)", 2D) = "Bump" {}
		[HideInInspector]_NormalScale2 ("Layer 3 NormalScale", Float) = 1
		[Sub(Tex)]_ColorA ("ColorA", Color) = (1,1,1,1)
        [Sub(Tex)]_Splat3 ("Layer 4(RGBA)", 2D) = "white" {}
        [Tex(Tex,_NormalScale3)]_BumpSplat3 ("Layer 4 Normal(Bump)", 2D) = "Bump" {}
		[HideInInspector]_NormalScale3 ("Layer 4 NormalScale", Float) = 1
		[Tex(Tex,_)]_Control ("Control (RGBA)", 2D) = "white" {}
		//_UseHole("Hole",range(-0.6, 0.5)) = -0.5
	}
SubShader {
	     Tags {
			 "SplatCount" = "4"
    	   "RenderType" = "Opaque"   
    	}
		
    Pass {
		Tags{"LightMode" = "UniversalForward"}
		HLSLPROGRAM
		#pragma vertex vert
		#pragma fragment frag_Pbr

		#pragma shader_feature _RECEIVE_SHADOWS_OFF
		#pragma shader_feature _ENVIRONMENTREFLECTIONS_OFF
		#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
		#pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
		#pragma multi_compile _ SHADOWS_SHADOWMASK
		#pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
		#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
		#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
		#pragma multi_compile _ _SHADOWS_SOFT
		#pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE
		#pragma multi_compile _ DIRLIGHTMAP_COMBINED
		#pragma multi_compile _ LIGHTMAP_ON
		#pragma prefer_hlslcc gles
		#pragma exclude_renderers d3d11_9x
		#pragma target 2.0
		#define _NORMALMAP 1
		// #define LIGHTMAP_ON 1
		#define USE_FAST_HSV_COMPUTED 1
		float4 _Splat0_ST,_Splat1_ST,_Splat2_ST,_Splat3_ST;
		half4 _ColorR,_ColorG,_ColorB,_ColorA;  
		half _NormalScale0,_NormalScale1,_NormalScale2,_NormalScale3;
		half _Roughness;
		// #include "../../Common/Function/Scene_PBR.hlsl"
		#include "Assets/URP/Shader/HLSL/Scene/Pbr/JR_Scene_PbrLight.hlsl"
		#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" 
		#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityInput.hlsl"
		#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
		#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
		#include "Assets/URP/Shader/HLSL/Common/Function/Scene_Common.hlsl"
		#include "Assets/URP/Shader/HLSL/Environment/Sky&Fog/VolumetricFogForObject.hlsl"
		
		

		sampler2D _ColorMask;
		uniform half4 _ColorMask_ST;

		uniform half _ColorStart,_ColorEnd;
		uniform half3 _ColorBottomFar;
		uniform half3 _CMaskBottomGFar;
		uniform half3 _CMaskBottomFar;
		uniform float4 _UvSize;
		uniform half _ControlUV,_BlendUV;
		uniform half _UVSec;
		half4 _NightRainValue; // x:emission y:roughness z:color w:normal

		TEXTURE2D(_Splat0); SAMPLER(sampler_Splat0);
		TEXTURE2D(_Control); SAMPLER(sampler_Control);
		TEXTURE2D(_Splat1); SAMPLER(sampler_Splat1);
		TEXTURE2D(_Splat2); SAMPLER(sampler_Splat2);
		TEXTURE2D(_Splat3); SAMPLER(sampler_Splat3);
		TEXTURE2D(_BumpSplat1); SAMPLER(sampler_BumpSplat1);
		TEXTURE2D(_BumpSplat2); SAMPLER(sampler_BumpSplat2);
		TEXTURE2D(_BumpSplat3); SAMPLER(sampler_BumpSplat3);

		struct Attributes
		{
			float4 positionOS   : POSITION;
			half4 color:COLOR;
			float3 normalOS     : NORMAL;
			float4 tangentOS    : TANGENT;
			float2 uv     : TEXCOORD0;
			float2 lightmapUV   : TEXCOORD1;
			float2 uv3   : TEXCOORD2;
			UNITY_VERTEX_INPUT_INSTANCE_ID
		};

		struct v2f
		{
			float4 vertex : SV_POSITION;
			half4 color:COLOR;
			float2 uv : TEXCOORD0;
			float2 ControlUv : TEXCOORD12;
			float2 BlendUv : TEXCOORD13;
			float2 uv_Detail : TEXCOORD10;
			half3x3 tangentTransform:TEXCOORD1;//占用3个语义 2 3 4
			// half3 bitangentWS : TEXCOORD3;
			float4 positionWS : TEXCOORD4;
			half3 normalWS : TEXCOORD5;
			half4 FogAndDistanceAndvPos : TEXCOORD6;
			half3 viewDirWS : TEXCOORD7;
			float4 fogCoord : TEXCOORD11;
			float4 calaPos:TEXCOORD9;
			DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 8);
			UNITY_VERTEX_INPUT_INSTANCE_ID
		    UNITY_VERTEX_OUTPUT_STEREO
		};

		v2f vert (Attributes v)
		{
			v2f o;
			UNITY_SETUP_INSTANCE_ID(v);
			UNITY_TRANSFER_INSTANCE_ID(v, o);
			UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
			VertexPositionInputs vertexInput = GetVertexPositionInputs(v.positionOS.xyz);
			o.positionWS = float4(vertexInput.positionWS,v.positionOS.w);
			o.vertex = vertexInput.positionCS;
			o.viewDirWS = GetCameraPositionWS() - o.positionWS;
			
			o.FogAndDistanceAndvPos.w= mul(UNITY_MATRIX_MV, v.positionOS).z;
			float2 PosW_Uv = float2(o.positionWS.xz/_UvSize.xy+(half2(0.5,0.5)-_UvSize.zw/_UvSize.xy));
			o.BlendUv = (_BlendUV == 0)?v.uv: ((_BlendUV == 1)?v.uv3:PosW_Uv);
			o.ControlUv = (_ControlUV == 0)?v.uv: ((_ControlUV == 1)?v.uv3:PosW_Uv);
			o.uv_Detail = (_UVSec == 0)?v.uv:v.uv3;
			o.uv = v.uv;
			half fogCoord = ComputeFogFactor(vertexInput.positionCS.z);
			VertexNormalInputs normalInput = GetVertexNormalInputs(v.normalOS, v.tangentOS);
			o.normalWS = normalInput.normalWS;
			// o.tangentWS = normalInput.tangentWS;
			// o.bitangentWS = normalInput.bitangentWS;
			o.tangentTransform = half3x3(normalInput.tangentWS, normalInput.bitangentWS, normalInput.normalWS);
			float lineDistance = distance(o.positionWS.xyz, _WorldSpaceCameraPos.xyz);
			o.calaPos = VertCalaShadow(o.positionWS, _DirX, _DirY);
			o.FogAndDistanceAndvPos.xy = float2(fogCoord,lineDistance);
			o.fogCoord = VolumetricFog_GetFogCoord(vertexInput.positionNDC, vertexInput.positionVS.z);
			OUTPUT_LIGHTMAP_UV(v.lightmapUV, unity_LightmapST, o.lightmapUV);
			OUTPUT_SH(normalInput.normalWS.xyz, o.vertexSH);
			o.color = v.color;
			return o;
		}

		PbrInputData InitializeInputData(v2f i, half3 normalWS)
		{
			PbrInputData inputData = (PbrInputData)0;
			inputData.positionWS = i.positionWS;
			inputData.normalWS = normalWS;
			inputData.viewDirectionWS = normalize(i.viewDirWS);
			inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
			inputData.calaPos = i.calaPos;

			inputData.bakedGI = SAMPLE_GI(i.lightmapUV, i.vertexSH, inputData.normalWS);

#if defined(LIGHTMAP_ON) && defined(SHADOWS_SHADOWMASK)  
			inputData.bakedAtten = SampleShadowMask(i.lightmapUV);
#endif
			return inputData;
		}

		half2 hash22(half2 p)
		{
			p = (p * 3.04 + (_Time.y % 77) * 13 +  50.0);
			p = floor(p * 4096);
			half3 p3 = frac(half3(p.xyx) * half3(.1031, .1030, .0973));
			p3 += dot(p3, p3.yzx+33.33);
			return frac((p3.xx+p3.yz)*p3.zy)*2 - 1;
		}

		half4 frag_Blend_Terrain (v2f i,half4 col,half4 Normal,half ColorM)
		{
			//half alpha=0;
			half2 VColorMask;
			VColorMask.xy =tex2Dlod(_ColorMask,float4(i.positionWS.xz/_ColorMask_ST.x,0,0)).rg;
			//i.VColorMask.x *= 1-step(i.VColorMask.y,0);
			half ColorDis = saturate((i.FogAndDistanceAndvPos.y-_ColorStart) / (_ColorEnd-_ColorStart));
	
			half3 CMaskBottomG = col.rgb;
			half3 CMaskBottom = col.rgb;

			// CMaskBottomG = lerp(col.rgb,CMaskBottomG,VColorMask.y);
			//CMaskBottom = lerp(CMaskBottom,Flip_CMaskBottomG,i.VColorMask.z);
			half3 ColorBottom = CMaskBottom;

			half3 ColorBottomFar = lerp(_ColorBottomFar,_CMaskBottomGFar,VColorMask.y);
			//half3 CMaskBottomFar = lerp(_CMaskBottomFar,_Flip_CMaskBottomGFar,i.VColorMask.z*i.VColorMask.x);
			ColorBottomFar = lerp(ColorBottomFar,_CMaskBottomFar,VColorMask.x);

			ColorBottom = lerp(ColorBottom,ColorBottomFar,ColorDis);
			col.rgb = lerp(col.rgb,saturate(ColorBottom),ColorM);
			col *= (1 - _NightRainValue.z);
			//alpha = Normal.a;
			//half3x3 tangentTransform = half3x3(i.tangentWS, i.bitangentWS, i.normalWS);
			Normal.rgb = normalize(mul(Normal.rgb, i.tangentTransform));
			Normal.rgb = lerp(Normal.rgb,half3(0,1,0),ColorM);
			Normal.rg += hash22(i.BlendUv) * _NightRainValue.w;
			Normal.rgb = normalize(Normal.rgb);
			Light main_light = (Light)0;

			PbrInputData inputData = InitializeInputData(i, Normal);
			half roughness = (1 - Normal.a) * _Roughness * (1 - _NightRainValue.y);
			return UniversalFragmentPBR(inputData, col, 0, 0, roughness, 1 , 0, 1);

			return col;
		}

		half4 frag_Pbr(v2f i) : SV_Target
		{
			half4 lay1 = SampleAlbedoAlpha(i.BlendUv*_Splat0_ST, TEXTURE2D_ARGS(_Splat0, sampler_Splat0));
			half4 lay2 = SampleAlbedoAlpha(i.BlendUv*_Splat1_ST, TEXTURE2D_ARGS(_Splat1, sampler_Splat1));
			half4 lay3 = SampleAlbedoAlpha(i.BlendUv*_Splat2_ST, TEXTURE2D_ARGS(_Splat2, sampler_Splat2));
			half4 lay4 = SampleAlbedoAlpha(i.BlendUv*_Splat3_ST, TEXTURE2D_ARGS(_Splat3, sampler_Splat3));
			lay1.rgb *= _ColorR.rgb;lay2.rgb *= _ColorG.rgb;lay3.rgb *= _ColorB.rgb;lay4.rgb *= _ColorA.rgb;
			half4 splat_control = SampleAlbedoAlpha(i.ControlUv, TEXTURE2D_ARGS(_Control, sampler_Control));
			half4 blend = Blend(lay1.a,lay2.a,lay3.a,lay4.a,splat_control);
			half4 col = lay1 * blend.r + lay2 * blend.g + lay3 * blend.b + lay4 * blend.a;
			half4 nor1 = SampleNormalAlpha(i.BlendUv*_Splat0_ST, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap),_NormalScale0);
			half4 nor2 = SampleNormalAlpha(i.BlendUv*_Splat1_ST, TEXTURE2D_ARGS(_BumpSplat1, sampler_BumpSplat1),_NormalScale1);
			half4 nor3 = SampleNormalAlpha(i.BlendUv*_Splat2_ST, TEXTURE2D_ARGS(_BumpSplat2, sampler_BumpSplat2),_NormalScale2);
			half4 nor4 = SampleNormalAlpha(i.BlendUv*_Splat3_ST, TEXTURE2D_ARGS(_BumpSplat3, sampler_BumpSplat3),_NormalScale3);
			half4 Normal = nor1*blend.r + nor2*blend.g + nor3*blend.b + nor4*blend.a;

			half CM = 1-blend.r*_ColorR.a-blend.g*_ColorG.a-blend.b*_ColorB.a-blend.a*_ColorA.a;
			half4 outColor = frag_Blend_Terrain(i,col,Normal,CM);
			return outColor;
		}
		ENDHLSL
	}
	UsePass "URP/Terrain/Mobile_PBR_Blend_4Tex/DepthOnly"
	UsePass "URP/Terrain/Mobile_PBR_Blend_4Tex/ShadowCaster"
}
CustomEditor "FZDL.ShaderDrawer.LWGUI"
} 