Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单对策中的矩形绘制_C#_Geometry_Monogame_Rectangles_Drawrectangle - Fatal编程技术网

C# 单对策中的矩形绘制

C# 单对策中的矩形绘制,c#,geometry,monogame,rectangles,drawrectangle,C#,Geometry,Monogame,Rectangles,Drawrectangle,如何在MonoGame中绘制形状,例如矩形和圆形,而不必将预先绘制的形状保存在内容文件夹中 DrawRectangle()和DrawEllipse()用于Windows窗体,在OpenGL中不起作用,我正在使用OpenGL。使用3D原语和2D投影 下面是一个简单的例子,并给出了解释 我定义了一个10x10的矩形,并设置了世界矩阵,使其看起来像一个二维投影: 注意:basicefect是绘制原语的工具 protected override void LoadContent() { _ver

如何在MonoGame中绘制形状,例如矩形和圆形,而不必将预先绘制的形状保存在内容文件夹中

DrawRectangle()和DrawEllipse()用于Windows窗体,在OpenGL中不起作用,我正在使用OpenGL。

使用3D原语和2D投影 下面是一个简单的例子,并给出了解释

我定义了一个10x10的矩形,并设置了世界矩阵,使其看起来像一个二维投影:

注意:
basicefect
是绘制原语的工具

protected override void LoadContent()
{
    _vertexPositionColors = new[]
    {
        new VertexPositionColor(new Vector3(0, 0, 1), Color.White),
        new VertexPositionColor(new Vector3(10, 0, 1), Color.White),
        new VertexPositionColor(new Vector3(10, 10, 1), Color.White),
        new VertexPositionColor(new Vector3(0, 10, 1), Color.White)
    };
    _basicEffect = new BasicEffect(GraphicsDevice);
    _basicEffect.World = Matrix.CreateOrthographicOffCenter(
        0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1);
}
然后我画了整个东西:D

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    EffectTechnique effectTechnique = _basicEffect.Techniques[0];
    EffectPassCollection effectPassCollection = effectTechnique.Passes;
    foreach (EffectPass pass in effectPassCollection)
    {
        pass.Apply();
        GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineStrip, _vertexPositionColors, 0, 4);
    }
    base.Draw(gameTime);
}
这就是你的长方形

现在这只是冰山一角

  • 绘制填充矩形:绘制2个三角形基本体
  • 对于椭圆/圆,请参见以下内容:和
或者如上面的一篇文章所述,您可以使用一个着色器来代替……

我不久前需要画画,最后画了这个着色器:

正如你在文章中看到的,超椭圆不仅画椭圆,还画其他形状,甚至可能是圆(我没有测试过),所以你可能会对它感兴趣

最终,您需要一些类/方法来隐藏所有这些细节,因此您只需调用类似于
DrawCircle()
的东西

小贴士:通过发布@,你可能会得到更多关于单一游戏相关问题的答案


:D

如果需要在2D中创建矩形,可以执行以下操作:

 Color[] data = new Color[rectangle.Width * rectangle.Height];
 Texture2D rectTexture = new Texture2D(GraphicsDevice, rectangle.Width, rectangle.Height);

 for (int i = 0; i < data.Length; ++i) 
      data[i] = Color.White;

 rectTexture.SetData(data);
 var position = new Vector2(rectangle.Left, rectangle.Top);

 spriteBatch.Draw(rectTexture, position, Color.White);
Color[]data=新颜色[rectangle.Width*rectangle.Height];
Texture2D rectTexture=新的Texture2D(图形设备,矩形。宽度,矩形。高度);
对于(int i=0;i

在某些情况下,可能比Aybe的答案简单一点。这将创建一个实心矩形

我找到了一个绘制填充和非填充形状的简单解决方案,我不知道它是否耗电,但无论如何,它在这里:

    {
        //Filled
        Texture2D _texture;

        _texture = new Texture2D(graphicsDevice, 1, 1);
        _texture.SetData(new Color[] { Color.White });

        spriteBatch.Draw(_texture, Rect, Color.White);
    }


    {
        //Non filled
        Texture2D _texture;

        _texture = new Texture2D(graphicsDevice, 1, 1);
        _texture.SetData(new Color[] { Color.White });

        spriteBatch.Draw(_texture, new Rectangle(Rect.Left, Rect.Top, Rect.Width, 1), Color.White);
        spriteBatch.Draw(_texture, new Rectangle(Rect.Right, Rect.Top, 1, Rect.Height), Color.White);
        spriteBatch.Draw(_texture, new Rectangle(Rect.Left, Rect.Bottom, Rect.Width, 1), Color.White);
        spriteBatch.Draw(_texture, new Rectangle(Rect.Left, Rect.Top, 1, Rect.Height), Color.White);
    }

顶点数组不应该包含5个顶点以绘制4条基本线吗?不应该,因为它是一个线条带:DFrom MSDN:
数据按线段序列排序;每条线段由一个新顶点和上一条线段的最后一个顶点描述。计数可以是任何正整数。
。顶点1和2构成线段,顶点2和3,顶点3和4。。。难道第四条线段不需要第五个顶点来连接顶点4吗?我同意这有点令人困惑,但它确实有效。。。计算条数:共有4条。当我们通过
4
时,该方法知道使用最后一个顶点和第一个顶点在数组上循环绘制最后一条带。您也可以使用该方法绘制线。只需拉伸并旋转矩形即可绘制“线”。