Code/MeshSliceRegion.cs
namespace Sandbox;

public readonly struct MeshSliceRegion
{
	public readonly float StartX { get; }
	public readonly float StartY { get; }
	public readonly float EndX { get; }
	public readonly float EndY { get; }

	public static MeshSliceRegion Full { get; } = new( 0.0f, 0.0f, 1.0f, 1.0f );

	public MeshSliceRegion( float startX, float startY, float endX, float endY )
	{
		StartX = startX;
		StartY = startY;
		EndX = endX;
		EndY = endY;
	}

	public Vector2 Map( Vector2 uv )
	{
		return Map( uv.x, uv.y );
	}

	public Vector2 Map( float x, float y )
	{
		return new Vector2(
			MapRange( x, 0.0f, 1.0f, StartX, EndX ),
			MapRange( y, 0.0f, 1.0f, StartY, EndY ) );
	}

	private static float MapRange( float value, float sourceMin, float sourceMax, float targetMin, float targetMax )
	{
		return (value - sourceMin) * (targetMax - targetMin) / (sourceMax - sourceMin) + targetMin;
	}
}