3d XNA-绘制二维线

3d XNA-绘制二维线,3d,xna,2d,vertex-array,3d,Xna,2d,Vertex Array,我已经看过了 在这里,他们解释了如何在xna中绘制2D线条,但我得到了一个示例(参见脚本) { int points=3;//我尝试了不同的值(1,2,3,4) VertexPositionColor[]原语列表=新的VertexPositionColor[点]; 对于(int x=0;x

我已经看过了 在这里,他们解释了如何在xna中绘制2D线条,但我得到了一个示例(参见脚本)

{
int points=3;//我尝试了不同的值(1,2,3,4)
VertexPositionColor[]原语列表=新的VertexPositionColor[点];
对于(int x=0;x)既然你真的没有提供错误或任何东西,我就给你演示一下我是如何画它们的

我使用这种扩展方法画一条线,你需要一个白色的1x1纹理

public static void DrawLine(this SpriteBatch spriteBatch, Vector2 begin, Vector2 end, Color color, int width = 1)
{
    Rectangle r = new Rectangle((int)begin.X, (int)begin.Y, (int)(end - begin).Length()+width, width);
    Vector2 v = Vector2.Normalize(begin - end);
    float angle = (float)Math.Acos(Vector2.Dot(v, -Vector2.UnitX));
    if (begin.Y > end.Y) angle = MathHelper.TwoPi - angle;
    spriteBatch.Draw(Pixel, r, null, color, angle, Vector2.Zero, SpriteEffects.None, 0);
}
这也将绘制一个由点组成的形状,
closed
定义该形状是否应该闭合

    public static void DrawPolyLine(this SpriteBatch spriteBatch, Vector2[] points, Color color, int width = 1, bool closed = false)
    {


        for (int i = 0; i < points.Length - 1; i++)
            spriteBatch.DrawLine(points[i], points[i + 1], color, width);
        if (closed)
            spriteBatch.DrawLine(points[points.Length - 1], points[0], color, width);

    }
public static void DrawPolyLine(此SpriteBatch SpriteBatch,矢量2[]点,颜色,int width=1,bool closed=false)
{
对于(int i=0;i
第一件事是顶点的定义。这看起来有点奇怪。如果
3
,那么外循环从
0..0
开始,内循环从
0..1
开始。因此有两个顶点。这些点只能画一条线。如果指定
点=4
,则可以实际上有四点

似乎您希望绘制一条没有重复顶点的连续线。因此,您实际上不需要索引表示(带有索引缓冲区)。逐个指定顶点就足够了

为了画一条连续的线,你应该使用
PrimitiveType.LineStrip
。这将自动将顶点与线连接起来。因此你需要
点-1
索引(如果你真的想使用它们)

在调用
DrawUserIndexedPrimitives()
时,有一些错误的参数导致异常:

  • 你说顶点数是8,这绝对不是真的。它是
    (点/2)*2
  • 你说原语的数量是7,这也不是真的。这应该是
    点-1
  • 但同样,您对顶点和索引的定义不一致。在继续之前,您必须纠正这一点。

    I使用此库
        public static void DrawPolyLine(this SpriteBatch spriteBatch, Vector2[] points, Color color, int width = 1, bool closed = false)
        {
    
    
            for (int i = 0; i < points.Length - 1; i++)
                spriteBatch.DrawLine(points[i], points[i + 1], color, width);
            if (closed)
                spriteBatch.DrawLine(points[points.Length - 1], points[0], color, width);
    
        }