// Author:Blastom
// 这个Shader只用于Non-Directional烘培的物体,动态物体使用光照会不正常
// 支持GLES 2.0,包括Shader Level 2.0的机器;
// Layer 3 和 Layer 4 会都是用Layer 3的Uv配置,以减少v2f参数量
Shader "Zhanyou/Scene/T2M 2 Textures Bump" {
	Properties {
		_Splat0 ("Layer 1", 2D) = "white" {}
		_Splat1 ("Layer 2", 2D) = "white" {}
		[NoScaleOffset]_BumpSplat0 ("Layer1Normalmap", 2D) = "bump" {}
		[NoScaleOffset]_BumpSplat1 ("Layer2Normalmap", 2D) = "bump" {}
		[NoScaleOffset]_Control ("Control (RGBA)", 2D) = "white" {}
		_BumpVector ("Normal Strength", Vector) = (0, 0, 0, 0)
		_Specular ("Specular", Range(0, 2)) = 0.25
		_Gloss("Gloss", Vector) = (0.5, 0.5, 0.5, 0.5)
		_Emission("Emission", Float) = 1.0
	}

	CGINCLUDE
	#include "T2MBase.cginc"
	uniform sampler2D  _BumpSplat0, _BumpSplat1;
	
	#define NORMAL_FRAG \
	half3 normal_0 = UnpackNormal(tex2D(_BumpSplat0, uv_0)); \
	half3 normal_1 = UnpackNormal(tex2D(_BumpSplat1, uv_1)); \
	half3 normal = normalize(normal_0 * control.r + normal_1 * control.g)
	ENDCG

	SubShader {
		Tags {
			// 最后绘制,以最大限度剔除高消耗像素
			"Queue"="AlphaTest+1"
			"SplatCount" = "4"
			"RenderType" = "Opaque"
		}
		// Forward Base
		Pass
		{
			Name "FORWARD"
			Tags { "LightMode" = "ForwardBase" }

			CGPROGRAM
			#define UNITY_PASS_FORWARDBASE
			#pragma multi_compile_fwdbase
			#pragma multi_compile_fog
			#pragma vertex vert_Base
			#pragma fragment frag
			#pragma multi_compile __ T2M_NORMAL
			#pragma multi_compile __ T4M_NORMAL_OFF
			#pragma multi_compile __ T4M_UNITY_GI_OFF
            #pragma multi_compile __ PLAYER_HALO_ON

			#if !T4M_NORMAL_OFF
				uniform half4 _BumpVector;
			#endif
			#if !T4M_SPECULAR_OFF
				uniform half _Specular;
				uniform half4 _Gloss;
			#endif
			
			fixed4 frag(v2f_Base i) : SV_TARGET
			{
				COLOR_FRAG;
				// 世界法线
				#if T4M_NORMAL_OFF
					fixed3 worldNormal = fixed3(i.tSpace0.z, i.tSpace1.z, i.tSpace2.z);
				#else
					// 减少两张法线贴图的流程
					NORMAL_FRAG;
					fixed3 worldNormal;
					worldNormal.x = dot(i.tSpace0.xyz, normal);
					worldNormal.y = dot(i.tSpace1.xyz, normal);
					worldNormal.z = dot(i.tSpace2.xyz, normal);
					worldNormal = normalize(worldNormal);
				#endif
				// 标准UnityGi流程
				GET_UNITY_GI_COLOR;

				#if !T4M_NORMAL_OFF
					// 法线强度调整功能
					fixed ndotlVert = LambertTerm(fixed3(i.tSpace0.z, i.tSpace1.z, i.tSpace2.z), lightDir);
					half bumpLerp = _BumpVector.r * control.r + _BumpVector.g * control.g;
					giColor += (ndotl - ndotlVert) * bumpLerp * SIM_LIGHT_COLOR;
				#endif
				#if !T4M_UNITY_GI_OFF
					// 虚拟光源高光反射
					fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
					half gloss = _Gloss.r * control.r + _Gloss.g * control.g;
					half3 h = normalize (_SpecularLight.xyz + worldViewDir);
					half nh = max (0, dot (worldNormal, h));
					half specular = pow (nh, _Specular * 128.0) * gloss * c.a;
					giColor += specular * atten;
				#endif
                #if PLAYER_HALO_ON
				    giColor += _DirectionalLightColor * LightColorFromWorld(worldPos);
                #endif
				// 自发光功能
				c.rgb = giColor + c.rgb * _Emission;
				UNITY_APPLY_FOG(i.fogCoord, c);
				UNITY_OPAQUE_ALPHA(c.a);
				return c;
			}
			ENDCG
		}

		// Forward Add
		// 注:实际光照贴图只通过meta通道实现,Forward Add仅用于编辑器无光照贴图时预览
		// 简单处理,Forward Add也使用顶点计算光照和视野方向,真实光照参数
		// 该通道可以省去镜面高光等所需参数,因此按常规算法做
		Pass
		{
			Name "FORWARD_ADD"
			Tags { "LightMode" = "ForwardAdd" }
			ZWrite Off
			Blend One One
			CGPROGRAM
			#define UNITY_PASS_FORWARDADD
			#pragma multi_compile_fwdadd
			#pragma multi_compile_fog
			#pragma skip_variants INSTANCING_ON
			#pragma vertex vert_Add
			#pragma fragment frag
		
			fixed4 frag(v2f_Add i) : SV_TARGET
			{
				#ifdef LIGHTMAP_ON
					fixed4 c = fixed4(0.0, 0.0, 0.0, 0.0);
				#else
					COLOR_FRAG;
					NORMAL_FRAG;
					UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
					half NtoL = max(0.0, dot(i.lightDir, normal));
					c.rgb = c.rgb * atten * _LightColor0.rgb * NtoL;
				#endif
				UNITY_APPLY_FOG(i.fogCoord, c);
				UNITY_OPAQUE_ALPHA(c.a);
				return c;
			}
			ENDCG
		}
	}
	Fallback "Zhanyou/Scene/T4M 4 Textures"
}