﻿Shader "Nostalgia/Tiles/Diffuse"
{
	Properties
	{
		_MainTex ("Tiles Texture", 2D) = "white" {}
		_Color ("Tint", Color) = (1,1,1,1)

		_StencilComp("Comparison", Float) = 0
		_Stencil("Ref", Range(0.0, 255.0)) = 0
		_StencilWriteMask("Write Mask",Range(0.0, 255.0)) = 255
		_StencilReadMask("Read Mask",Range(0.0, 255.0)) = 255
		_StencilPass("Pass",Float) = 0
		_StencilFail("Fail",Float) = 0
		_StencilZFail("ZFail",Float) = 0
		_ColorMask("Color Mask",Float) = 15
		_Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5

		[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
	}

	SubShader
	{
		Tags
		{ 
			"Queue"="Transparent" 
			"IgnoreProjector"="True" 
			"RenderType"="Transparent" 
			"PreviewType"="Plane"
			"CanUseSpriteAtlas"="True"
		}

		Cull Off
		Lighting Off
		ZWrite Off
		Blend One OneMinusSrcAlpha
		ColorMask [_ColorMask]

		Stencil
		{
			Ref[_Stencil]
			ReadMask[_StencilReadMask]
			WriteMask[_StencilWriteMask]
			Comp[_StencilComp]
			Pass[_StencilPass]
			Fail[_StencilFail]
			ZFail[_StencilZFail]
		}

		CGPROGRAM
		#pragma surface surf Lambert vertex:vert nofog keepalpha

		#pragma shader_feature _ALPHATEST_ON

		#pragma multi_compile_local _ PIXELSNAP_ON

		#include "./TileCG.cginc"

		sampler2D _MainTex;
		fixed4 _Color;
		half _Cutoff;

		struct Input
		{
			float2 texcoord;
			float4 color;
		};
		
		void vert (inout appdata_full v, out Input o)
		{
#if defined(PIXELSNAP_ON)
			v.vertex = UnityPixelSnap(v.vertex);
#endif
			
			UNITY_INITIALIZE_OUTPUT(Input, o);
			o.texcoord = AnimateUV(v.texcoord.xy, v.texcoord1);
			o.color = v.color * _Color;
		}

		void surf (Input IN, inout SurfaceOutput o)
		{
			fixed4 color = tex2D(_MainTex, IN.texcoord) * IN.color;

#if defined(_ALPHATEST_ON)
			clip(color.a - _Cutoff);
#endif

			o.Albedo = color.rgb * color.a;
			o.Alpha = color.a;

		}
		ENDCG
	}

	Fallback "Sprites/Diffuse"
	CustomEditor "NostalgiaEditor.TilesDefaultShaderGUI"
}
