Shader "URP/SceneBase/Effect-Model" {
	Properties {
		_Color("Main Color", Color) = (1,1,1,1)
		_BaseMap ("Base (RGB)", 2D) = "white" {}
		[Toggle]_Light_On("Light On",Float) = 0

		[Main(Emission)]
		_Illumin_On ("Emission", Float ) = 0
		[Sub(Emission)]_EmissionMap("Emission", 2D) = "white" {}
		[Sub(Emission)]_TimeScale ("Time Scale", Float ) = 1
		[Sub(Emission)]_IlluminStrength("Emission Strength" , Range(0,30)) = 0
		[Sub(Emission)][HDR]_IlluminColor("Emission Color", Color) = (1,1,1,1)

		[Main(Rim)]
		_Rim("Rim", float) = 0
		[Sub(Rim)][HDR]_RimColor("Rim Color", color) = (0,0,0,1)
		[Sub(Rim)]_RimWidth("Rim Width", Range(0,1)) = 0
		[SubToggle(Rim,FLASH_SWITCH)]_FLASH_SWITCH("FLASH SWITCH", Float) = 0
		[Sub(Rim)]_FlashTex("FlashTex", 2D) = "white" {}
		[Sub(Rim)]_FlashFactor("FlashFactor", Vector) = (0, -2, 1, 1)
		[Sub(Rim)]_FlashStrength("FlashStrength", Range(0, 5)) = 1

		[Main(DISSOLVE)]
		Dissolve("Dissolve", Float) = 0
		[Sub(DISSOLVE)]_DissolveTex("DissolveTex", 2D) = "white" {}
		[Sub(DISSOLVE)][HDR]_DissColor("DissColor", color) = (1,1,1,1)
		[Sub(DISSOLVE)]_DissolveFactor("DissolveFactor",Vector) = (0, 0.5, 1, 1)
	}

	SubShader {
		Tags { "RenderType" = "Opaque" "LightMode" = "UniversalForward"}
		LOD 200

		HLSLINCLUDE
		#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
		#include "../../Environment/Sky&Fog/VolumetricFogForObject.hlsl"
		ENDHLSL
		Pass{
			Tags{"LightMode" = "UniversalForward"}
			HLSLPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma shader_feature FLASH_SWITCH
			#pragma shader_feature DISSOLVE_ON

			struct unlit_v2f 
			{
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
				float fogCoord : TEXCOORD1;
				float3 normalWS : TEXCOORD2;
				float3 worldPos : TEXCOORD3;
			};

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float3 normal : NORMAL;
			};
			half _Light_On,_Rim;
			half4 _IlluminColor,_Color;;
			half4 _RimColor,_BackRimColor;
			half _RimWidth,_BackRimWidth;
			half _IlluminStrength,_TimeScale,_Illumin_On;
			sampler2D _FlashTex;
			half4 _FlashFactor;
			half _FlashStrength;
		#ifdef DISSOLVE_ON 
			uniform sampler2D _DissolveTex;
			uniform half3 _DissolveFactor;
			uniform half3 _DissColor;
		#endif
			unlit_v2f vert(appdata v)
			{
				unlit_v2f o;

				VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
				o.pos = vertexInput.positionCS;
				o.worldPos = vertexInput.positionWS;
				o.normalWS = normalize(v.normal);
				o.fogCoord = VolumetricFog_GetFogCoord(vertexInput.positionNDC, vertexInput.positionVS.z);
				o.uv = v.uv;
				return o;
			}
			void Rim(inout half3 col,float2 uv,float nv,half Width,out half rimStep)
			{
				rimStep = smoothstep(-Width+1-_RimWidth, 1, nv);
				rimStep = rimStep * 0.5*(1-step(_RimWidth,0));
				half3 rim = rimStep * _RimColor;
				#ifdef FLASH_SWITCH
				half2 flashuv = uv * _FlashFactor.zw + _FlashFactor.xy * _Time.y;
				half3 flash = tex2D(_FlashTex, flashuv);
				col += lerp(0,flash.r * _FlashStrength,rim)*_Rim;
				#else
				col += rim*_Rim;
				#endif
			}
			half4 frag(unlit_v2f i) : SV_Target
			{
				half4 col = SampleAlbedoAlpha(TRANSFORM_TEX(i.uv, _BaseMap), TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
			#ifdef DISSOLVE_ON 
				half dissolveDegree = tex2D(_DissolveTex, i.uv*_DissolveFactor.zz).r;
				half dissMask = lerp(dissolveDegree - _DissolveFactor.x,1,step(_DissolveFactor.x,0));
				col.rgb *= lerp(_Color,(_DissolveFactor.y / dissMask)*_DissColor,_DissolveFactor.y*(1-step(_DissolveFactor.x,0)));
				clip(dissolveDegree-_DissolveFactor.x);
			#endif
				half4 EmissionMap = SampleAlbedoAlpha(i.uv, TEXTURE2D_ARGS(_EmissionMap, sampler_EmissionMap));
				float3 worldViewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
				half NdhV = saturate(dot(TransformObjectToWorldNormal(i.normalWS), worldViewDir));
				half w = fwidth(NdhV)*2.0;
				//FXrim
				half rimA;
				Rim(col.rgb,i.uv,1-NdhV,w,rimA);
				//自发光
				_IlluminStrength = abs((( (sin((_Time.y*_TimeScale)) + 1.0)/1.0) / 2.0))* _IlluminStrength*_Illumin_On;
				col.rgb += EmissionMap.r * EmissionMap.a * _IlluminStrength * _IlluminColor;
				col.rgb *= lerp(1,_MainLightColor.w,_Light_On);
				return col;
			}
		ENDHLSL
		}
	}
	CustomEditor "FZDL.ShaderDrawer.LWGUI"
}
