this code is useful for drawing 2D lines, some calculations can be done in the initialization call, but I prefer this example to keep everything together.
public void DrawLine(VertexPositionColor[] Vertices) { Game.GraphicsDevice.DepthStencilState = DepthStencilState.Default; Vector2 center; center.X = Game.GraphicsDevice.Viewport.Width * 0.5f; center.Y = Game.GraphicsDevice.Viewport.Height * 0.5f; Matrix View = Matrix.CreateLookAt( new Vector3( center, 0 ), new Vector3( center, 1 ), new Vector3( 0, -1, 0 ) ); Matrix Projection = Matrix.CreateOrthographic( center.X * 2, center.Y * 2, -0.5f, 1 ); Effect EffectLines = Game.Content.Load<Effect>( "lines" ); EffectLines.CurrentTechnique = EffectLines.Techniques["Lines"]; EffectLines.Parameters["xViewProjection"].SetValue( View * Projection ); EffectLines.Parameters["xWorld"].SetValue( Matrix.Identity ); foreach ( EffectPass pass in EffectLines.CurrentTechnique.Passes ) { pass.Apply( ); Game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor> ( PrimitiveType.LineList, Vertices, 0, Vertices.Length/2 ); } }
LINES.FX
uniform float4x4 xWorld; uniform float4x4 xViewProjection; void VS_Basico(in float4 inPos : POSITION, in float4 inColor: COLOR0, out float4 outPos: POSITION, out float4 outColor:COLOR0 ) { float4 tmp = mul (inPos, xWorld); outPos = mul (tmp, xViewProjection); outColor = inColor; } float4 PS_Basico(in float4 inColor:COLOR) :COLOR { return inColor; } technique Lines { pass Pass0 { VertexShader = compile vs_2_0 VS_Basico(); PixelShader = compile ps_2_0 PS_Basico(); FILLMODE = SOLID; CULLMODE = NONE; } }
source share