Shader "URP/SceneBase/URP_GlassRefraction"
{
	Properties
	{
		_Color("颜色",color) = (1,1,1,1)
		_Skybottom("天空底部颜色",color) = (0,0,0,1)
		_MainAlpha ("整体透明值", Range(0,1)) = 1
		_SpecColor("高光颜色",color) = (1,1,1,1)
		_Fresnel("X:菲涅尔强度Y:高光强度Z:高光硬度W:天空模糊",Vector) = (0.5,1,0.5,1)
		_NormalTex("Normal",2D) = "bump"{}
		//_Cubemap("Reflect",cube) = ""{}
		[ToggleOff] _EnvironmentReflections("Environment Reflections", Float) = 0
		[HideInInspector]_ReflectionTex("_ReflectionTex",2D) = "black" {}
		_RefractAmount("X:反射亮度Y:偏移W:底部颜色范围",Vector) = (1,0.1,0,1)
		//_RefractAmount("X:反射Y:偏移Z:近距离透明硬度W:近距离透明强度",Vector) = (1,0.1,0,1)
		//[ToggleOff]_ReflectionAlpha("ReflectionAlpha", Float) = 0
		//_RefAlphaStrength("RefAlphaStrength", Float) = 1
		_NormalScale("NormalScale", Float) = 1
	}
		SubShader
	{
		Tags { "RenderType" = "Transparent"
		"Queue" = "Transparent"}
		Pass
		{
			Tags{"LightMode" = "UniversalForward"}
			Cull Back
			Zwrite Off
			Blend SrcAlpha OneMinusSrcAlpha
			HLSLPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma shader_feature _ENVIRONMENTREFLECTIONS_OFF
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
			struct Attributes
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float3 normal : NORMAL;
				float4 tangentOS    : TANGENT;
			};
			struct v2f
			{
				float4 vertex : SV_POSITION;
				float2 uv : TEXCOORD1;
				float3 normal : TEXCOORD3;
				float4 wPos : TEXCOORD4;
				float4 ScreenPosition : TEXCOORD5;
				//float vPos : TEXCOORD6;
				 float3 WorldSpaceNormal : TEXCOORD8;
				 float3 WorldSpaceTangent : TEXCOORD9;
				 float3 WorldSpaceBiTangent : TEXCOORD7;
			};
			uniform half3 _Skybottom;
			uniform half4 _Color;
			uniform half4 _SpecColor;
			uniform sampler2D _NormalTex;
			uniform half2 _NormalTex_ST;
			uniform float4 _Fresnel;
			//uniform samplerCUBE _Cubemap;
			uniform float _MainAlpha;
			//uniform float _ReflectionAlpha;
			uniform float _RefAlphaStrength;
			uniform float _NormalScale;
			uniform half4 _RefractAmount;
			uniform sampler2D _RefractionTex;
			uniform float4 _RefractionTex_TexelSize;
			TEXTURE2D(_ReflectionTex); SAMPLER(sampler_ReflectionTex); half4 _ReflectionTex_TexelSize;
			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);
				//o.vPos= mul(UNITY_MATRIX_MV, v.vertex).z;
				o.ScreenPosition = ComputeScreenPos(TransformWorldToHClip(vertexInput.positionWS), _ProjectionParams.x);
				 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;
				return o;
			}
			half4 frag(v2f i) : SV_Target
			{
				Light light = GetMainLight();
				//_RefractAmount = saturate(_RefractAmount);
				_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));
				nor = GetAbsolutePositionWS(mul(tangentTransform, nor.xyz)).xyz;
				//nor = normalize(i.normal + nor.xxy);
				float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.wPos);
				float spec = max(0, dot(nor, normalize(viewDir + normalize(light.direction*light.distanceAttenuation))));
				spec = pow(spec, _Fresnel.z * _Fresnel.z * 200);
				float rim = 1 - pow(max(0,dot(nor,viewDir)), _Fresnel.x * 6);
				half3 reflectVector = -reflect(viewDir, nor);
				//half4 refl = texCUBE(_Cubemap, reflectVector);
				//half3 indirectSpecular = refl.rgb;
				half3 indirectSpecular = half3(0,0,0);
				half mip = PerceptualRoughnessToMipmapLevel(_Fresnel.w);
		#if !defined(_ENVIRONMENTREFLECTIONS_OFF)
				half4 encodedIrradiance = SAMPLE_TEXTURECUBE_LOD(unity_SpecCube0, samplerunity_SpecCube0, reflectVector+half3(nor.xy,0)* saturate(_RefractAmount.y), mip);
				encodedIrradiance.rgb += lerp(0,_Skybottom,saturate(-reflectVector.y-_RefractAmount.w));
			#if !defined(UNITY_USE_NATIVE_HDR)
				half3 irradiance = DecodeHDREnvironment(encodedIrradiance, unity_SpecCube0_HDR);
			#else
				half3 irradiance = encodedIrradiance.rgb;
			#endif
				indirectSpecular = irradiance;
		#endif
				// half vPos = saturate(-i.vPos * _RefractAmount.w);
				// vPos = smoothstep(_RefractAmount.z,1,vPos);

				half4 ScreenPos = half4(i.ScreenPosition.xy / i.ScreenPosition.w, 0, 0);
				half4 flectionTex = SAMPLE_TEXTURE2D(_ReflectionTex, sampler_ReflectionTex, ScreenPos.xy);

				half4 col;
				col.rgb = saturate(_SpecColor * spec * _Fresnel.y+rim *indirectSpecular*_Color.rgb+flectionTex.rgb*_RefractAmount.x);
				col.a = saturate(max(rim, max(spec*_Fresnel.y, flectionTex.a)))*_MainAlpha;
				//col.a = saturate(max(rim, max(spec*_Fresnel.y, flectionTex.a*_ReflectionAlpha)+flectionTex.a*(1-_ReflectionAlpha)))*_MainAlpha;
				return col;
			}
			ENDHLSL
		}
	}
}