Shader "HiddenURP/Environment/WeatherSystem/CloudShadow"
{
	Properties
	{
	//_MainTex("Base (RGB)", 2D) = "white" {}
	//_CloudTex("Cloud Texture (RGB)", 2D) = "black" {}
	//_Cloud("Bloom (RGB)", 2D) = "black" {}
	//_ShadowColor ("XYZ:ColorMultiplier", Color) = (1,1,1,1)
	[HideInInSpector]_ShadowIntensity("Intensity",float) = 1
	[HideInInSpector]_ShadowFadeOut("Fade out parameter",float) = 1
	[HideInInSpector]_CloudFactor ("XY:WindSpeed ZW:CloudTiling", Vector) = (0.05,0.05,2,2)
	}

	SubShader
	{
		Tags{ "Queue" = "Transparent" "RenderPipeline" = "LightweightPipeline" "RenderType" = "Transparent" "IgnoreProjector" = "True" }
		Cull Back	//Front
		LOD 200

		HLSLINCLUDE
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
			//#include "Common/SurfaceInput.hlsl"
			//#include "Common/Lighting.hlsl"
			//#include "Common/UnityInstancing.hlsl"

		CBUFFER_START(UnityPerMaterial)
			half4	_CloudFactor;
			//float4	_MainTex_ST;
			//float4 _WorldOffset;
			half	_ShadowIntensity;
			half	_ShadowFadeOut;
			//half	_NonMetal;
			//half	_Alpha;
		CBUFFER_END

		TEXTURE2D(_MainTex);				SAMPLER(sampler_MainTex);
		TEXTURE2D(_CloudTex);				SAMPLER(sampler_CloudTex);
		TEXTURE2D(_CameraDepthTexture);		SAMPLER(sampler_CameraDepthTexture);
		
		ENDHLSL

		Pass
		{
			Cull Back
			ZWrite Off
			ZTest Always

			HLSLPROGRAM
			#pragma target 3.0
			#pragma vertex vert
			#pragma fragment frag

			//#include "UnityCG.cginc"

			struct v2f
			{
				float4 pos : SV_POSITION;
				float2 uv  : TEXCOORD0;
				float3 viewDir : TEXCOORD1;
			};

			v2f vert(float3 vertex : POSITION)
			{
				float far = _ProjectionParams.z;
				float4 clipPos = float4(vertex.xy, 0.0, 1.0);
				float3 viewDir = mul(unity_CameraInvProjection, clipPos.xyzz * far).xyz;
				//float2 orthoSize = unity_OrthoParams.xy;
				//float3 viewDir = float3(orthoSize * clipPos.xy, 0);

				v2f o;
	#if UNITY_UV_STARTS_AT_TOP
				o.pos = float4(clipPos.x, -clipPos.y, 1, 1);
	#else
				o.pos = float4(clipPos.x, clipPos.y, 1, 1);
	#endif
				o.uv = (clipPos.xy + 1) / 2;
				o.viewDir = viewDir;
				return o;
			}


			float4 frag(v2f i) : SV_Target
			{
				//float linearDepth = Linear01Depth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv));
				//float3 posView = i.viewDir * linearDepth;
				//float4 world = mul(unity_CameraToWorld, float4(posView,1));
				float rawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.uv);
				float linearDepth = Linear01Depth(rawDepth, _ZBufferParams);
			
				float camPosZ = _ProjectionParams.y + (_ProjectionParams.z - _ProjectionParams.y) * linearDepth;
				float height = 2 * camPosZ / unity_CameraProjection._m11;
				float width = _ScreenParams.x / _ScreenParams.y * height;

				float camPosX = width * i.uv.x - width / 2;
				float camPosY = height * i.uv.y - height / 2;
				float4 camPos = float4(camPosX, camPosY, camPosZ, 1.0);
				float4 world = mul(unity_CameraToWorld, camPos);

				float2 cloud_uv = world.xz*0.005;
				float2 cloud_wind = _CloudFactor.xy;
				float2 cloud_tiling = _CloudFactor.zw;
				cloud_uv = (cloud_uv + cloud_wind) * cloud_tiling;

				float4  cloud = SAMPLE_TEXTURE2D(_CloudTex, sampler_CloudTex, cloud_uv).r;
				//return cloud * _ShadowIntensity;
				half4 sceneColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex,i.uv);
				float depthOut = max(0, 1 - linearDepth * _ShadowFadeOut * 100);
				return sceneColor * (1 - cloud * _ShadowIntensity * depthOut);
			}
			ENDHLSL
	  }
	}
}