HEADER
{
	Description = "Template Shader for S&box";
}

FEATURES
{
    #include "common/features.hlsl"

	Feature(F_PIXELATED, 0..1, "Sprite");
}

COMMON
{
	#include "common/shared.hlsl"
}

struct VertexInput
{
	#include "common/vertexinput.hlsl"
};

struct PixelInput
{
	#include "common/pixelinput.hlsl"
};

VS
{
	#include "common/vertex.hlsl"

	float2 g_vSpriteScale < UiType( VectorText ); Default2( 1.0, 1.0 ); UiGroup( "Transform,10/10" ); Attribute( "SpriteScale" ); >;
	float2 g_vSpritePivot < UiType( VectorText ); Default2( 0.5, 0.5 ); UiGroup( "Transform,10/20" ); Attribute( "SpritePivot" ); >;
	float2 g_vUvMin < UiType( VectorText ); Default2( 0.0, 0.0 ); UiGroup( "Transform,10/20" ); Attribute( "UvMin" ); >;
	float2 g_vUvMax < UiType( VectorText ); Default2( 1.0, 1.0 ); UiGroup( "Transform,10/30" ); Attribute( "UvMax" ); >;

	PixelInput MainVs( VertexInput i )
	{
		i.vPositionOs.xy += (g_vSpritePivot - float2(0.5, 0.5)) * 100.0;
		i.vPositionOs.xy *= g_vSpriteScale;

		PixelInput o = ProcessVertex( i );

		o.vTextureCoords.xy = g_vUvMin + o.vTextureCoords.xy * (g_vUvMax - g_vUvMin);

		// Add your vertex manipulation functions here
		return FinalizeVertex( o );
	}
}

//=========================================================================================================================

PS
{
    #include "common/pixel.hlsl"

	SamplerState CustomTextureFiltering < Filter( NEAREST ); >;

	CreateInputTexture2D( Texture, Srgb, 8, "", "", "Color", Default3( 1.0, 1.0, 1.0 ) );
	CreateTexture2DInRegister( g_tColor, 0 ) < Channel( RGBA, None( Texture ), Srgb ); OutputFormat( DXT5 ); SrgbRead( true ); Filter( NEAREST ); >;
	TextureAttribute( RepresentativeTexture, g_tColor );
	
	float2 g_vTextureSize < UiType( VectorText ); Default2( 1.0, 1.0 ); UiGroup( "Transform,10/40" ); Attribute( "TextureSize" ); >;
	float4 g_vColorFill < UiType( Color ); Default4( 0.0, 0.0, 0.0, 0.0 ); UiGroup( "Color,10/10" ); Attribute( "ColorFill" ); >;
	float4 g_vColorMultiply < UiType( Color ); Default4( 1.0, 1.0, 1.0, 1.0 ); UiGroup( "Color,10/20" ); Attribute( "ColorMultiply" ); >;

	StaticCombo( S_PIXELATED, F_PIXELATED, Sys( ALL ) );

	RenderState( BlendEnable, true );
	RenderState( SrcBlend, SRC_ALPHA );
	RenderState( DstBlend, INV_SRC_ALPHA );

	RenderState( DepthEnable, false );
	RenderState( DepthClipEnable, false );
	RenderState( DepthWriteEnable, false );

	// Always write rgba
	RenderState( ColorWriteEnable0, RGBA );
	RenderState( FillMode, SOLID );

	// Never cull
	RenderState( CullMode, NONE );

	float4 MainPs( PixelInput i ) : SV_Target0
	{
		float2 vUV = i.vTextureCoords.xy;

        float4 vRMA = float4( 1.0f, 0.0f, 1.0f, 1.0f );

		Material m = Material::From( i,
                                    Tex2DS( g_tColor, CustomTextureFiltering, vUV ), 
                                    Tex2DS( g_tNormal, CustomTextureFiltering, vUV  ), 
                                    vRMA, 
                                    g_flTintColor * i.vVertexColor.rgb );

		float4 vColor = ShadingModelStandard::Shade(i, m);

		/* m.Metalness = 1.0f; // Forces the object to be metalic */
		return vColor;
	}

}
