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

Shader "Lit/Diffuse With Shadows"
{
	Properties
	{
	}
		SubShader
		{
			Pass
			{
				name "DEPTH"
				// Render out *only depth information*
				// This allows us to just shade the items we want in the next pass
				Tags {"LightMode" = "Always"}

				ZWrite On
				ZTest LEqual

				ColorMask 0

				CGPROGRAM
				#include "UnityCG.cginc"
				#pragma vertex vert
				#pragma fragment frag

				#pragma multi_compile_fwdadd_fullshadows nolightmap nodirlightmap nodynlightmap novertexlight

				struct v2f
				{
					float4 pos : SV_POSITION;
				};
				v2f vert(appdata_base v)
				{
					v2f o;
					o.pos = UnityObjectToClipPos(v.vertex);
					return o;
				}

				fixed4 frag(v2f i) : SV_Target
				{
					return fixed4(0,0,0,0);
				}
				ENDCG
			}
			Pass
			{
				Name "FORWARD"
				// Point shadows happen in the Add pass of forward rendering
				Tags {"LightMode" = "ForwardAdd"}

				ZWrite Off
				ZTest Equal

				Blend SrcAlpha OneMinusSrcAlpha

				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#include "UnityCG.cginc"
				#include "Lighting.cginc"

				#pragma multi_compile_fwdadd_fullshadows nolightmap nodirlightmap nodynlightmap novertexlight

				#include "AutoLight.cginc"

				struct v2f
				{
					
					float4 pos : SV_POSITION;
					float3 worldNormal:NORMAL;
					float3 worldPos:TEXTURE0;
					SHADOW_COORDS(1) // put shadows data into TEXCOORD1
				};
				v2f vert(appdata_base v)
				{
					v2f o;
					o.pos = UnityObjectToClipPos(v.vertex);
					o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
					o.worldNormal = UnityObjectToWorldNormal(v.normal);
					TRANSFER_SHADOW(o)
					return o;
				}

				fixed4 frag(v2f i) : SV_Target
				{
					fixed shadow = SHADOW_ATTENUATION(i);

					float3 lightDir = normalize(i.worldPos - _WorldSpaceLightPos0.xyz);
					float nl = max(0, dot(i.worldNormal, -lightDir));

					// only want nl if the dot produce is > 0 (anything facing the light)
					nl = 1 - step(nl, 0);

					float light = min(nl, shadow);

					return fixed4(0, 0, 0, 1 - light);
				}
				ENDCG
			}

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