// A simple rim-lighting overlay that draws after everything else.

Shader "Unlit/TileHighlight"
{
	Properties
	{
		_Color ("Main Color", Color) = (1,1,1,1)
	}
	SubShader
	{
		Tags { "RenderType"="Transparent" "Queue"="Overlay" }
		ZWrite On ZTest Always
		Cull Back
		Blend Zero SrcColor

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float3 normal : NORMAL;
			};

			struct v2f
			{
				float4 vertex : SV_POSITION;
				float3 posWorld : TEXCOORD0;
				float3 normal : NORMAL;
			};

			float4 _Color;

			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.posWorld = mul(unity_ObjectToWorld, v.vertex);
				o.normal = normalize(mul( float4(v.normal, 0.0f), unity_WorldToObject).xyz);
				return o;
			}

			fixed4 frag (v2f i) : SV_Target
			{
				float3 normalDir = i.normal;
				float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
				float rim = 1 - 0.5f*abs(dot(viewDir, normalDir));
				return lerp(float4(1,1,1,1), _Color, rim*0.7);
			}
			ENDCG
		}
	}
}
