Shader "URP/Terrain/Mobile_PBR_Blend_4Tex"
{
	Properties
	{
		_Color("Color", Color) = (1,1,1,1)
		//_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, 2)) = 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" {}

		[Main(Tex,_,2)]
		_Tex("贴图", float) = 0
		[Sub(Tex)]_UvSize("UvSize:xy为地表尺寸zw为中心坐标", vector) = (200, 200, 0, 0)
		[Sub(Tex)]_CMRange("CMRange",Vector) = (1, 1, 1, 1)
		[Sub(Tex)]_BTiling("Tiling",Vector) = (1, 1, 1, 1)
		[Sub(Tex)]_NormalScale("NormalScale",Vector) = (1, 1, 1, 1)
		[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
		[Tex(Tex,_)]_Splat0 ("Tex(RGBA)", 2D) = "white" {}
        [Tex(Tex,_)]_BumpMap ("Tex Normal(Bump)", 2D) = "Bump" {}
    	[Tex(Tex,_)]_Control ("Control (RGBA)", 2D) = "white" {}
		//_BlockParams("Block Params",Vector) = (41007, 242262, 0.1, 0)
		//_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 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
				#define _Terrain_Moblie 1
				uniform half4 _BTiling;
				uniform half4 _NormalScale;
				uniform half4 _CMRange;
				#include "../../Common/Function/Scene_PBR.hlsl"
				half VectorBlend(half4 Vector,half X,half Y)
				{
					half3 Blend;
					Blend.xy = half2 (lerp(Vector.x,Vector.y,saturate(X-2)),lerp(Vector.z,Vector.w,X));
					Blend.z = lerp(Blend.x,Blend.y,Y);
					return Blend.z;
				}
				void GetColorByIndex(half index, half lodLevel, float2 uv, out half CM, out half4 col,out half4 normal)
				{
					float2 columnAndRow;
					columnAndRow.x = (index % 4.0);
					columnAndRow.y = floor((index % 16.0) / 4.0);
					float Tiling =VectorBlend(_BTiling,columnAndRow.x,columnAndRow.y);
					CM =VectorBlend(saturate(1-_CMRange),columnAndRow.x,columnAndRow.y);
					float4 curUV;
					curUV.xy = ((columnAndRow * 0.25) + ((frac((uv * Tiling)) 
					* 0.242262) + 0.0041007))*2;
					col = SAMPLE_TEXTURE2D_LOD(_Splat0,sampler_Splat0, curUV,lodLevel);
					half4 N = SAMPLE_TEXTURE2D_LOD(_BumpMap,sampler_BumpMap, curUV,lodLevel);
					#ifdef _NORMALMAP
						N = half4(N.rg,1,N.b);
						half scale = VectorBlend(_NormalScale,columnAndRow.x,columnAndRow.y);
						#if BUMP_SCALE_NOT_SUPPORTED
							normal = half4(UnpackNormal(N),N.a);
						#else
							normal = half4(UnpackNormalScale(N, scale),N.a);
						#endif
					#else
						normal = half4(0.0h, 0.0h, 1.0h,1);
					#endif
				}
				half4 frag_Pbr(v2f i) : SV_Target
				{
					half lodLevel = min(-i.FogAndDistanceAndvPos.w * 0.1, 3);
					half4 splat_control = SAMPLE_TEXTURE2D(_Control,sampler_Control, i.ControlUv);
					half4 colorLayer1,colorLayer2,colorLayer3,colorLayer4;
					half4 nor1,nor2,nor3,nor4;half4 CM;
					GetColorByIndex(2, lodLevel, i.BlendUv,CM.x,colorLayer1,nor1);
					GetColorByIndex(3, lodLevel, i.BlendUv,CM.y,colorLayer2,nor2);
					GetColorByIndex(4, lodLevel, i.BlendUv,CM.z,colorLayer3,nor3);
					GetColorByIndex(5, lodLevel, i.BlendUv,CM.w,colorLayer4,nor4);
					half4 blend = Blend(colorLayer1.a,colorLayer2.a,colorLayer3.a,colorLayer4.a,splat_control);
					half4 col = colorLayer1* blend.r + colorLayer2 * blend.g+colorLayer3 * blend.b+colorLayer4 * blend.a;
					half4 Normal = nor1 * blend.r + nor2 * blend.g+nor3 * blend.b+nor4 * blend.a;
					CM.x = CM.x * blend.r + CM.y * blend.g+CM.z * blend.b+CM.w * blend.a;
					half4 outColor = frag_Blend_Common(i,col,Normal,CM.x);
					return outColor;
				}
				ENDHLSL
			}

			Pass
			{
				Name "DepthOnly"
				Tags{ "LightMode" = "DepthOnly" }

				ZWrite On
				ColorMask 0

				HLSLPROGRAM
					// Required to compile gles 2.0 with standard srp library
			#pragma prefer_hlslcc gles
			#pragma exclude_renderers d3d11_9x
			#pragma target 2.0

			#pragma vertex DepthOnlyVertex
			#pragma fragment DepthOnlyFragment

					// -------------------------------------
					// Material Keywords
			//#pragma shader_feature _ALPHATEST_ON
			//#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A

					//--------------------------------------
					// GPU Instancing
			#pragma multi_compile_instancing

			#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
			#include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
					ENDHLSL
				}

				//UsePass "URP/DepthOnly/DepthOnlyCutoff"

				 Pass
		{
			Name "ShadowCaster"
			Tags{"LightMode" = "ShadowCaster"}

			ZWrite On
			ZTest LEqual

			HLSLPROGRAM
					// Required to compile gles 2.0 with standard srp library
					#pragma prefer_hlslcc gles
					#pragma exclude_renderers d3d11_9x
					#pragma target 2.0

					// -------------------------------------
					// Material Keywords
					//#pragma shader_feature _ALPHATEST_ON

					//--------------------------------------
					// GPU Instancing
					//#pragma multi_compile_instancing
					//#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A

					#pragma vertex ShadowPassVertex
					#pragma fragment ShadowPassFragment

					#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
					#include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"
					ENDHLSL
				}
		}
		CustomEditor "FZDL.ShaderDrawer.LWGUI"
}