Shader "URP/Effects/PropGlass"
{
	Properties
	{
		[Enum(Both,0,Front,1,Back,2)] _CullMode("Cull Mode", int) = 2 
		_Color("Color",color) = (1,1,1,1)
		_EnvShadow("EnvShadow",color) = (0.5,0.5,0.5,1)
		_MainAlpha ("整体透明值", Range(0,1)) = 1
		_Fresnel("X:菲涅尔Y:亮度",Vector) = (0.5,1,0.5,1)
		_NormalTex("Normal",2D) = "bump"{}
		_NormalScale("NormalScale", Float) = 1
		_MatCap ("MatCap (RGB)", 2D) = "white" {}
		[ToggleOff]_Receive_Shadows("Receive Shadows", Float) = 0.0
	}
		SubShader
	{
		Tags { "RenderType" = "Transparent"
		"Queue" = "Transparent"}
		Pass
		{
			Tags{"LightMode" = "UniversalForward"}
			Cull [_CullMode]
			Zwrite Off
			Blend SrcAlpha OneMinusSrcAlpha
			HLSLPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma shader_feature _RECEIVE_SHADOWS_OFF
			#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
			#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
			#pragma multi_compile _ _SHADOWS_SOFT
			#pragma target 2.0
			#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"
			struct Attributes
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float3 normal : NORMAL;
				float4 tangentOS    : TANGENT;
			};
			struct v2f
			{
				float4 vertex : SV_POSITION;
				float2 uv : TEXCOORD0;
				float3 normal : TEXCOORD1;
				float4 wPos : TEXCOORD2;
				 float3 WorldSpaceNormal : TEXCOORD3;
				 float3 WorldSpaceTangent : TEXCOORD4;
				 float3 WorldSpaceBiTangent : TEXCOORD5;
				 //float4 shadowCoord : TEXCOORD6;
			};
			 half3 _EnvShadow;
			half4 _Color;
			 sampler2D _NormalTex;
			 half2 _NormalTex_ST;
			 sampler2D _MatCap;
			 float4 _Fresnel;
			 float _MainAlpha;
			 float _NormalScale;
			v2f vert(Attributes v)
			{
				v2f o;
				VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
				o.vertex = vertexInput.positionCS;
				o.uv = v.uv;
				o.normal = TransformObjectToWorldNormal(v.normal);
				o.wPos = half4(vertexInput.positionWS,1);
				VertexNormalInputs normalInput = GetVertexNormalInputs(v.normal, v.tangentOS);
				o.WorldSpaceNormal =            normalInput.normalWS.xyz;
				o.WorldSpaceTangent =           normalInput.tangentWS.xyz;
				half tangentSign = v.tangentOS.w * unity_WorldTransformParams.w;
				o.WorldSpaceBiTangent =         cross(normalInput.normalWS.xyz, normalInput.tangentWS.xyz) * tangentSign;
				//o.shadowCoord = GetShadowCoord(vertexInput);
				return o;
			}
			half4 frag(v2f i) : SV_Target
			{
				Light light = MainLight(i.wPos.xyz);
				_Fresnel.xzw = saturate(_Fresnel.xzw);
				float3 nor = UnpackNormalScale(tex2D(_NormalTex, i.uv* _NormalTex_ST.xy), _NormalScale);
				float3x3 tangentTransform = transpose(float3x3(i.WorldSpaceTangent, i.WorldSpaceBiTangent, i.WorldSpaceNormal));
				float3 worldNorm;
				worldNorm.x = dot(half3(i.WorldSpaceTangent.x, i.WorldSpaceBiTangent.x, i.WorldSpaceNormal.x), nor);
				worldNorm.y = dot(half3(i.WorldSpaceTangent.y, i.WorldSpaceBiTangent.y, i.WorldSpaceNormal.y), nor);
				worldNorm.z = dot(half3(i.WorldSpaceTangent.z, i.WorldSpaceBiTangent.z, i.WorldSpaceNormal.z), nor);
				worldNorm = mul((float3x3)UNITY_MATRIX_V, worldNorm);
				float3 mc = tex2D(_MatCap, worldNorm.xy * 0.5 + 0.5).rgb;
				nor = GetAbsolutePositionWS(mul(tangentTransform, nor.xyz)).xyz;
				float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.wPos);
				float rim = 1 - pow(max(0,dot(nor,viewDir)), _Fresnel.x * 6);
				half4 col;
				//float ndhL = dot(i.normal, light.direction);
				//ndhL = smoothstep(0,1,ndhL);
				col.rgb = rim *mc*_Color.rgb*light.color*_Fresnel.y;
				half4 posW = VertCalaShadow(i.wPos, _DirX, _DirY);
				light.shadowAttenuation =FragCalaShadowAtten(light.shadowAttenuation, posW, _Time.x, _Speed, _ShadowAlphaRatio, _ShadowTex);
				float3 shadowFactored = lerp(_EnvShadow, half3(1.0, 1.0, 1.0), light.shadowAttenuation);
				col.rgb *= shadowFactored;
				col.a = saturate(rim+mc.r)*_MainAlpha;
				col.rgb *= _MainLightColor.w;
				//col = half4(ndhL.rrr,1);
				return col;
			}
			ENDHLSL
		}
	}
}