// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Lit/Diffuse With Shadows"
{
	Properties
	{
		[NoScaleOffset] _MainTex("Texture", 2D) = "white" {}
		_Color("Color", Color) = (1,1,1,1)
	}
		SubShader
	{
		Pass
		{
			Tags {"LightMode" = "ForwardBase"}
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			#include "Lighting.cginc"

		// compile shader into multiple variants, with and without shadows
		// (we don't care about any lightmaps yet, so skip these variants)
		#pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
		// shadow helper functions and macros
		#include "AutoLight.cginc"

			float4 _Color;

		struct v2f
		{
			float2 uv : TEXCOORD0;
			SHADOW_COORDS(1) // put shadows data into TEXCOORD1
			fixed3 diff : COLOR0;
			fixed3 ambient : COLOR1;
			float4 pos : SV_POSITION;
		};
		v2f vert(appdata_base v)
		{
			v2f o;
			o.pos = UnityObjectToClipPos(v.vertex);
			o.uv = v.texcoord;
			half3 worldNormal = UnityObjectToWorldNormal(v.normal);
			half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
			o.diff = nl * _LightColor0.rgb;
			o.ambient = ShadeSH9(half4(worldNormal,1));
			// compute shadows data
			TRANSFER_SHADOW(o)
			return o;
		}

		sampler2D _MainTex;

		

		fixed4 frag(v2f i) : SV_Target
		{
			fixed4 col = tex2D(_MainTex, i.uv) * _Color;
		// compute shadow attenuation (1.0 = fully lit, 0.0 = fully shadowed)
		//fixed shadow = SHADOW_ATTENUATION(i);
		//// darken light's illumination with shadow, keep ambient intact
		//fixed3 lighting = i.diff * shadow + i.ambient;
		//col.rgb *= lighting;
		return col;
	}
	ENDCG
}

// Forward additive pass (only needed if you care about more lights than 1 directional).
		// Can remove if no point/spot light support needed.
		Pass
		{
			Name "FORWARD"
			Tags { "LightMode" = "ForwardAdd" }
			ZWrite Off Blend One Zero

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
		// Include shadowing support for point/spot
		#pragma multi_compile_fwdadd_fullshadows
		#include "UnityCG.cginc"
		#include "Lighting.cginc"
		#include "AutoLight.cginc"

		struct v2f
		{
			float4 pos : SV_POSITION;
			float3 worldPos : TEXCOORD0;
			SHADOW_COORDS(1)
		};

		v2f vert(appdata_full v)
		{
			v2f o;
			o.pos = UnityObjectToClipPos(v.vertex);
			o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
			TRANSFER_SHADOW(o); // pass shadow coordinates to pixel shader
			return o;
		}

		uniform float4 _Color;

		fixed4 frag(v2f IN) : SV_Target
		{
			fixed4 atten = SHADOW_ATTENUATION(IN); // atten, IN, IN.worldPos)
			fixed4 c = atten;
			// might want to take light color into account?
			c.rgb *= /*_LightColor0.rgb **/ _Color.rgb;
			return c;
		}

		ENDCG
	}

		// shadow casting support
		UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
	}
}