public override void render( Graphics graphics, Camera camera )
{
// TODO: make culling smarter and only render the lines that are actually on the screen rather than all or nothing
var width = _points.GetLength( 0 );
var height = _points.GetLength( 1 );
for( var y = 1; y < height; y++ )
{
for( var x = 1; x < width; x++ )
{
var left = new Vector2();
var up = new Vector2();
var p = projectToVector2( _points[x, y].position );
if( x > 1 )
{
float thickness;
Color gridColor;
if( y % gridMajorPeriodY == 1 )
{
thickness = gridMajorThickness;
gridColor = gridMajorColor;
}
else
{
thickness = gridMinorThickness;
gridColor = gridMinorColor;
}
// use Catmull-Rom interpolation to help smooth bends in the grid
left = projectToVector2( _points[x - 1, y].position );
var clampedX = Math.Min( x + 1, width - 1 );
var mid = Vector2.CatmullRom( projectToVector2( _points[x - 2, y].position ), left, p, projectToVector2( _points[clampedX, y].position ), 0.5f );
// If the grid is very straight here, draw a single straight line. Otherwise, draw lines to our new interpolated midpoint
if( Vector2.DistanceSquared( mid, ( left + p ) / 2 ) > 1 )
{
drawLine( graphics.batcher, left, mid, gridColor, thickness );
drawLine( graphics.batcher, mid, p, gridColor, thickness );
}
else
{
drawLine( graphics.batcher, left, p, gridColor, thickness );
}
}
if( y > 1 )
{
float thickness;
Color gridColor;
if( x % gridMajorPeriodX == 1 )
{
thickness = gridMajorThickness;
gridColor = gridMajorColor;
}
else
{
thickness = gridMinorThickness;
gridColor = gridMinorColor;
}
up = projectToVector2( _points[x, y - 1].position );
var clampedY = Math.Min( y + 1, height - 1 );
var mid = Vector2.CatmullRom( projectToVector2( _points[x, y - 2].position ), up, p, projectToVector2( _points[x, clampedY].position ), 0.5f );
if( Vector2.DistanceSquared( mid, ( up + p ) / 2 ) > 1 )
{
drawLine( graphics.batcher, up, mid, gridColor, thickness );
drawLine( graphics.batcher, mid, p, gridColor, thickness );
}
else
{
drawLine( graphics.batcher, up, p, gridColor, thickness );
}
}
// Add interpolated lines halfway between our point masses. This makes the grid look
// denser without the cost of simulating more springs and point masses.
if( x > 1 && y > 1 )
{
var upLeft = projectToVector2( _points[x - 1, y - 1].position );
drawLine( graphics.batcher, 0.5f * ( upLeft + up ), 0.5f * ( left + p ), gridMinorColor, gridMinorThickness ); // vertical line
drawLine( graphics.batcher, 0.5f * ( upLeft + left ), 0.5f * ( up + p ), gridMinorColor, gridMinorThickness ); // horizontal line
}
}
}
}