Shader "URP/Environment/Water/URPWater_Depth"
{
	Properties
	{
		[Header(____________________Color___________________)]
		[Space]
		_ShallowColor("浅滩颜色:A通道透明", Color) = (0, 0.7176471, 0.3529412, 0.2196078)
        _DeepColor("深水颜色:A通道透明", Color) = (0, 0.1254902, 0.3215686, 1)
		_ColorDepth("深度", Range(0, 4)) = 0.32
		_NearAlpha("近距离水半透范围", Range(0, 0.2)) = 0.2
		_EnvShadow("阴影颜色",Color) = (0.5,0.5,0.5,1)
		[Header(____________________Foam___________________)]
		[Space]
		_WaveParams ("海浪参数(X:范围,Y:Tiling,Z:速度,W:透明度)", vector) = (0.32,1,1,1)
		[Header(___________________Specular_________________)]
		[Space]
		[HDR]_SpecColor ("高光颜色", color) = (0.4,0.4,0.4,1)
		_Specular("X:菲涅尔Y:粗糙度Z:速度W:强度", Vector) = (0, 0.129, 0.1, 0.252)
        _BloomFactor ("BloomFactor", Range(0, 1)) = 0.5
		[Header(_____________________Ref_____________________)]
		[Space]
		_SkyColor ("天空颜色", color) = (1,1,1,1)
		_ReflectionStrength("反射强度", Range(0,1)) = 0
		_Refraction("X:扭曲速度y:扭曲TilingZ:扭曲强度W:深度范围", Vector) = (0.83, 0.15, 0.07, 1)
		[HideInInspector]_ReflectionTex("_ReflectionTex",2D) = "white" {}
		[Header(___________________Fresnel_____________________)]
		[Space]
		[ToggleOff] _EnvironmentReflections("Environment Reflections", Float) = 0
		_FresnelColor ("菲涅尔颜色", color) = (1,1,1,1)
		_FresnelMip("模糊度",Range(0, 1)) = 1
		[Header(___________________Caustics__________________)]
		[Space]
		[Toggle(CAUSTICS)]_Caustics("焦散开关",Float) = 0
		_CausticsVisuals("X:强度Y:色彩分离Z:速度W:Tiling", Vector) = (0, 0, 0, 1)
        _CausticsDepth("焦散范围", Range(0, 2)) = 0.1
		[Header(____________________Texture___________________)]
		[Space]
		_FoamTex("R:波浪,G:焦散", 2D) = "white" {}
		[Normal]_NormalTex("法线贴图", 2D) = "bump" {}
		[Header(__________________VertexColor_________________)]
		[Space]
		[ToggleOff]_VertexColor("顶点色开关R:波浪G:焦散A:透明", Float) = 0
	}
	SubShader
	{
		Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
		LOD 200
		Pass
		{
			Tags{"LightMode" = "TransparentDepthFog"}
			Blend SrcAlpha OneMinusSrcAlpha
			Cull Back
			ZWrite Off
			HLSLPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma shader_feature CAUSTICS
			#pragma shader_feature _ENVIRONMENTREFLECTIONS_OFF
			#pragma multi_compile_fog
			#pragma multi_compile_instancing
			#pragma target 3.0
			#define REQUIRE_DEPTH_TEXTURE
            //#define REQUIRE_OPAQUE_TEXTURE
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
			#include "../../Common/Function/Scene_Common.hlsl"
			#include "Water_Standard_Output.hlsl"

			half4 frag (v2f i) : SV_Target
			{
				UNITY_SETUP_INSTANCE_ID(i);
				UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
				Light light = MainLight(i.positionWS);
				SurfaceDescriptionInputs surfaceDescriptionInputs = BuildSurfaceDescriptionInputs(i);
				SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs);
				half3 N = Normals_float(i.positionWS.xz,_NormalTex_ST.xy, _Specular.w, _Specular.z);
				half3x3 tangentTransform = transpose(half3x3(i.WorldSpaceTangent, i.WorldSpaceBiTangent, i.WorldSpaceNormal));
				N = GetAbsolutePositionWS(mul(tangentTransform, N.xyz)).xyz;

				half4 col;
				col.rgb = surfaceDescription.Color;
			//采样反射天空盒
				half3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.positionWS);
				half3 refl = -reflect(viewDir, N);
				half3 indirectSpecular = half3(0,0,0);
				half mip = PerceptualRoughnessToMipmapLevel(_FresnelMip);
		#if !defined(_ENVIRONMENTREFLECTIONS_OFF)
				half4 encodedIrradiance = SAMPLE_TEXTURECUBE_LOD(unity_SpecCube0, samplerunity_SpecCube0, refl, mip);
			#if !defined(UNITY_USE_NATIVE_HDR)
				half3 irradiance = DecodeHDREnvironment(encodedIrradiance, unity_SpecCube0_HDR);
			#else
				half3 irradiance = encodedIrradiance.rgb;
			#endif
				indirectSpecular = irradiance*_FresnelColor;
		#endif
				half3 V = max(dot(viewDir, N),0.05);
				half vdn = saturate(pow(V, _Specular.x));
				col.rgb = lerp(indirectSpecular, col.rgb, vdn);
			//计算高光
				half Specular = Specular_float(light,_Specular.y,N,viewDir);
				col.rgb += light.color*_SpecColor*Specular;
				half4 posW = VertCalaShadow(float4(i.positionWS,0), _DirX, _DirY);
				light.shadowAttenuation = FragCalaShadowAtten(light.shadowAttenuation, posW, _Time.x, _Speed, _ShadowAlphaRatio, _ShadowTex);
				//half3 shadowFactored = lerp(_EnvShadow, half3(1.0, 1.0, 1.0), dot(N, normalize(light.direction))*light.shadowAttenuation);
				half3 bakedGI = SampleSHPixel(SampleSHVertex(N), N);
				half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
				half3 diffuseColor = bakedGI + LightingLambert(attenuatedLightColor, light.direction, N);
				col.rgb *=diffuseColor;
				col.rgb *= _MainLightColor.w;
			// apply fog
				// half density = ComputeFogFactor(i.fogCoord);
				// col.rgb = MixFog(col.rgb, density);
				//col.rgb = VolumetricFog_GetFog(col.rgb, i.fogCoord);
				col.a = surfaceDescription.alpha;
				return col;
			}
			ENDHLSL
		}
		Pass
		{
			Name "DepthOnly"
			Tags
			{
				"LightMode" = "DepthOnly"
			}
			Blend SrcAlpha OneMinusSrcAlpha
			ZWrite Off
			Cull Back
			HLSLPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_fog
			#pragma multi_compile_instancing
			#pragma target 3.0
			#define REQUIRE_DEPTH_TEXTURE
            #define REQUIRE_OPAQUE_TEXTURE
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
			#include "../../Common/Function/Scene_Common.hlsl"
			#include "Water_Standard_Output.hlsl"
			half4 frag (v2f i) : SV_Target
			{
				SurfaceDescriptionInputs surfaceDescriptionInputs = BuildSurfaceDescriptionInputs(i);
				SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs);
				half4 col;
				col.rgb = surfaceDescription.Color;
				col.a = surfaceDescription.alpha;
				return col;
			}
			ENDHLSL
		}
		Pass
            {
			Name "a"
			Tags{ "LightMode" = "SRPDefaultUnlit" }
			ZWrite Off
			Blend SrcAlpha  OneMinusSrcAlpha
			ColorMask A
			HLSLPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
			#include "../../Common/Function/Scene_Common.hlsl"
			#include "Water_Standard_Output.hlsl"
			uniform half _BloomFactor,_SceneBloomFactor;
            half4 frag(v2f i) : SV_Target
            { 
				Light light = MainLight(i.positionWS);

				half3 N = Normals_float(i.positionWS.xz,_NormalTex_ST.xy, _Specular.w, _Specular.z);
				half3x3 tangentTransform = transpose(half3x3(i.WorldSpaceTangent, i.WorldSpaceBiTangent, i.WorldSpaceNormal));
				half3 Transform = GetAbsolutePositionWS(mul(tangentTransform, N.xyz)).xyz;
				half3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.positionWS);
				half Specular = Specular_float(light,_Specular.y,Transform,viewDir);
				half4 col = half4(1,1,1,1);
				col.a = saturate(Specular * _BloomFactor)*_SceneBloomFactor;
				return col;
        	}
        ENDHLSL
    }
	}
}
