// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "HiddenURP/Environment/Tree/Transparent-With-Shadow-Plant" {
	SubShader{

		HLSLINCLUDE

		#include "../../Common/Template Shader/Transforms.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.universal/ShaderLibrary/Lighting.hlsl"
		#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
		#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
		#include "../../Common/Template Shader/Define.hlsl"
		#include "../../Common/Template Shader/Hole.hlsl"
		#include "../../Common/Function/Scene_Common.hlsl"
		#include "../../Common/Function/BlackHole.hlsl"
		#include "../Sky&Fog/VolumetricFogForObject.hlsl"

		uniform TEXTURE2D(_CollisionTex);	SAMPLER(sampler_CollisionTex);

		struct unlit_v2f {
			half4 pos : SV_POSITION;
			half2 texcoord : TEXCOORD0;
			float4 fogCoord : TEXCOORD1;
			half3 normalWS : TEXCOORD2;
			half4 positionWS : TEXCOORD3;
			DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 5);
			half4 color : COLOR;

			UNITY_VERTEX_INPUT_INSTANCE_ID
		    UNITY_VERTEX_OUTPUT_STEREO

		};
		#define v2f unlit_v2f
		#include "../../Common/Function/Lightmap_Output.hlsl"
		uniform half _SceneBloomFactor;

		uniform sampler2D _AlphaMask;
		uniform half4 _Color;
		uniform half4 _env_shadow_color,_ShadowUpColor,_SColor;
		uniform half4 _PlayerPos;
		uniform half _RampThreshold;
		uniform half _RampSmooth;
		uniform half4 _HColor;

		uniform half _LightIntensity;
		uniform half _Intensity;
		uniform half _AlphaCut;
		uniform half _MainAlpha;
		#ifdef _USE_Wind
		uniform half4 _echoRNormal;		
		uniform half _echoRSpeed;
		uniform half _echoRHeight;
		uniform half _echoRAmount;
		uniform half _echoRCenterX;
		uniform half _echoRCenterY;
		#endif
		uniform half _Shadow_Ao;
		uniform half _ShadowFalloff;
		uniform half _UseHole;

		//�Ȳ�
		half4 GetCollision(float3 worldPos)
		{
			half4 ret = half4(0, 0, 0, 0);

			float2 uvDisplacement = worldPos.xz - _PlayerPos.xz;
			uvDisplacement /= 12.8;
			uvDisplacement += float2(0.5, 0.5);

			if (uvDisplacement.x >= 0 && uvDisplacement.x <= 1 && uvDisplacement.y >= 0 && uvDisplacement.y <= 1)
			{
#if UNITY_UV_STARTS_AT_TOP
				// DX
				uvDisplacement.y = 1.0 - uvDisplacement.y;
				// OpenGL, don't need 1.0 - y
#endif

				ret = SAMPLE_TEXTURE2D_LOD(_CollisionTex, sampler_CollisionTex, uvDisplacement, 0);
				ret.yw = 0;

				float d = _PlayerPos.y - worldPos.y;
				if (d >= 0)
				{
					d += 1.0;
					d = pow(d, -3);
					ret *= d;
				}

			}

			return ret;
		}
		struct appdata
		{
			half4 vertex : POSITION;
			half3 normal : NORMAL;
			half4 tangentOS    : TANGENT;
			half2 uv : TEXCOORD0;
			half4 color : COLOR;
			half2 lightmapUV   : TEXCOORD1;
			UNITY_VERTEX_INPUT_INSTANCE_ID
		};
		unlit_v2f unlit_vert(appdata v)
		{
			unlit_v2f o;
			UNITY_SETUP_INSTANCE_ID(v);
			UNITY_TRANSFER_INSTANCE_ID(v, o);
			UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

			#ifdef _USE_Wind
			half ripple;
			ripple 			= EchoRipple ( v.uv, _echoRAmount, _echoRSpeed * (1.1 - (v.color.rgb/2)), _echoRHeight * v.color.rgb, 0, 0 );
			v.vertex.xyz 	=  v.vertex.xyz + ( half3(ripple,ripple,ripple) * _echoRNormal.xyz );
			#endif
			
			VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
			o.positionWS.xyz = vertexInput.positionWS;
			o.positionWS.w = v.vertex.w;
			//o.worldpos = mul(unity_ObjectToWorld, v.vertex);

			o.pos = vertexInput.positionCS;
			//o.fogCoord = ComputeFogFactor(vertexInput.positionCS.z);
			o.fogCoord = VolumetricFog_GetFogCoord(vertexInput.positionNDC, vertexInput.positionVS.z);
			half3 objLightDir = ObjSpaceLightDir(v.vertex);
			VertexNormalInputs normalInput = GetVertexNormalInputs(v.normal, v.tangentOS);
			o.normalWS = normalInput.normalWS;
			OUTPUT_LIGHTMAP_UV(v.lightmapUV, unity_LightmapST, o.lightmapUV);
			OUTPUT_SH(o.normalWS.xyz, o.vertexSH);
			o.texcoord = v.uv;
			o.color = v.color;

			half4 ret = GetCollision(o.positionWS.xyz);
			//o.pos.xz += sin(10 * _Time.y) *cos(5 * _Time.y)* ret.x * 0.02f;
			return o;
		}
		
		half4 frag_Common(unlit_v2f i) : SV_Target
		{	
			UNITY_SETUP_INSTANCE_ID(i);
			UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
			half4 holeColor = GetHoleColor(i.positionWS);
			clip(holeColor.r - (0.5f + _UseHole));
			half4 col = SampleAlbedoAlpha(i.texcoord, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
			// Alpha clip
			clip(col.a - _AlphaCut);
			Light light = MainLight(i.positionWS);
		#ifdef _COLOR_HSV
			col.rgb = replace_color_with_hsv (col.rgb, 1, _HueShift, _Sat, _Val, 1);
		#endif
			half3 bakedGI;
			LightmapResult(light,i,bakedGI,i.normalWS,i.positionWS.xyz);
			
			half4 posW = VertCalaShadow(float4(i.positionWS), _DirX, _DirY);
			light.shadowAttenuation =FragCalaShadowAtten(light.shadowAttenuation, posW, _Time.x, _Speed, _ShadowAlphaRatio, _ShadowTex);
			half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
			half3 diffuseColor = bakedGI + LightingLambert(attenuatedLightColor, light.direction, i.normalWS);
			//half3 diffuseColor = lerp(bakedGI,half3(1,1,1),i.color.a*_ShadowFalloff) + LightingLambert(attenuatedLightColor, light.direction, i.normalWS);
			col.rgb *= diffuseColor*_Color*_Intensity;	

			//col.rgb *= _MainLightColor.w;
			col.rgb *= BalckHole_GetLightIntensity(i.positionWS);
			//col.rgb = MixFog(col.rgb, i.fogCoord);
			//col.rgb = VolumetricFog_GetFog(col.rgb, i.fogCoord);
			//col.a = _SceneBloomFactor;
			return col;
		}
		ENDHLSL
		Pass{
			Name "UNLIT_COLOR_HSV_ZWRITE_ON_TRANSPARENT"
			Tags{"LightMode" = "UniversalForward"}
			Cull[_Cull_ZOn]
			HLSLPROGRAM
			#pragma vertex unlit_vert
			#pragma fragment frag_Common
			//#pragma multi_compile_fog
			#pragma multi_compile_instancing
			#pragma shader_feature _COLOR_HSV
			#pragma shader_feature _USE_Wind
			//#pragma shader_feature _USE_HOLE
			#pragma multi_compile _ LIGHTMAP_ON
			#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
			#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
			#pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
			#pragma multi_compile _ SHADOWS_SHADOWMASK
			#pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE
			//#pragma multi_compile _ DIRLIGHTMAP_COMBINED
			#pragma multi_compile _ _SHADOWS_SOFT
			#pragma shader_feature _RECEIVE_SHADOWS_OFF
		ENDHLSL
		}
//                      ShadowCaster                                    //
		Pass
		{
		Name "ShadowCaster"
			Tags
			{
				"LightMode" = "ShadowCaster"
			}
			Cull[_Cull_ZOn]
			HLSLPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_shadowcaster
			#pragma shader_feature _USE_Wind
			#pragma multi_compile _ALPHATEST_ON
			#pragma multi_compile_instancing
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
			#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
			half3 _LightDirection;
			half4 GetShadowPositionHClip(appdata input)
				{
					half3 positionWS = TransformObjectToWorld(input.vertex.xyz);
					half3 normalWS = TransformObjectToWorldNormal(input.normal);

					half4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));

				#if UNITY_REVERSED_Z
					positionCS.z = min(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
				#else
					positionCS.z = max(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
				#endif

					return positionCS;
				}

			unlit_v2f vert(appdata v)
		{
			unlit_v2f o;
		
			UNITY_SETUP_INSTANCE_ID(v);
			UNITY_TRANSFER_INSTANCE_ID(v, o);
			UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
		#ifdef _USE_Wind
			// red channel
			half ripple;
			ripple = EchoRipple(v.uv, _echoRAmount, _echoRSpeed * (1.1 - (v.color.a / 2)), _echoRHeight * v.color.r, _echoRCenterX, _echoRCenterY);
			v.vertex.xyz = v.vertex.xyz + (half3(ripple,ripple,ripple) * _echoRNormal.xyz);
		#endif

			o.pos = GetShadowPositionHClip(v);
			o.texcoord = TRANSFORM_TEX(v.uv, _BaseMap);
			return o;
		}

			half4 frag(unlit_v2f i) : SV_Target
			{
				UNITY_SETUP_INSTANCE_ID(i);
				UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
				Alpha(SampleAlbedoAlpha(i.texcoord, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a, _Color, _Cutoff);
				return 0;
			}

			ENDHLSL
		}

		Pass
		{
			Name "DepthOnly"
			Tags
			{
				"LightMode" = "DepthOnly"
			}
			Cull[_Cull_ZOn]
			ZWrite On
			HLSLPROGRAM
			#pragma vertex unlit_vert
			#pragma fragment frag
			#pragma multi_compile_shadowcaster
			#pragma shader_feature _USE_Wind
			//#pragma multi_compile _ALPHATEST_ON
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
			#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"

			half4 frag(unlit_v2f i) : SV_Target
			{
				UNITY_SETUP_INSTANCE_ID(i);
				UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);

				float4 holeColor = GetHoleColor(i.positionWS);
				clip(holeColor.r - (0.5f + _UseHole));

				half4 col = SampleAlbedoAlpha(i.texcoord, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
		#ifdef _COLOR_HSV
				col.rgb = replace_color_with_hsv(col.rgb, 1, _HueShift, _Sat, _Val, 1);
		#endif
				col *= _Color;

				col.rgb *= _Intensity;
				//col.a *= tex2D(_AlphaMask, i.texcoord.xy).r;
				// Alpha clip
				clip(col.a - _AlphaCut);
				return col;
			}


			ENDHLSL
		}
	}
}
