【问题标题】:Draw Rectangle in MonoGame在 MonoGame 中绘制矩形
【发布时间】:2014-04-26 01:37:27
【问题描述】:

如何在 MonoGame 中绘制形状,例如矩形和圆形,而无需将预先绘制的形状保存在 Content 文件夹中?

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

【问题讨论】:

    标签: c# geometry monogame rectangles drawrectangle


    【解决方案1】:

    使用 3D 图元和 2D 投影

    这里有一个简单的例子和​​解释

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

    注意:BasicEffect 是你的原语

    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);
    }
    

    你有你的矩形!

    现在这只是冰山一角,

    或者如上面的一篇文章中所述,您可以使用着色器来代替...

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

    Drawing a SuperEllipse in HLSL

    正如您在帖子中看到的那样,Superellipse 不仅可以绘制椭圆,还可以绘制其他形状,甚至可能是圆形(我没有测试过),所以您可能会对它感兴趣。

    最终你会想要一些类/方法来隐藏所有这些细节,所以你只需要调用像DrawCircle() 这样的东西。

    提示:通过发帖@https://gamedev.stackexchange.com/,您可能会获得更多与 Monogame 相关的问题的答案

    :D

    【讨论】:

    • 顶点数组不应该包含5个顶点才能绘制4条原始线吗?
    • 不,因为它是一条线:D
    • 来自 MSDN:The data is ordered as a sequence of line segments; each line segment is described by one new vertex and the last vertex from the previous line seqment. The count may be any positive integer.。顶点1和2做一条线段,顶点2和3,顶点3和4……第4条线段不是需要第5个顶点连接顶点4吗?
    • 我同意这有点令人困惑,但它确实有效......计算条带的数量:其中有 4 个。当我们传递4 时,该方法知道要针对数组循环 以使用最后一个和第一个顶点绘制最后一个条带。
    • 有一些图书馆可以为我做所有这些事情吗?我正在使用monogame进行数学可视化(因为喜欢c#并且不知道py),老实说根本不想学习hlsl。
    【解决方案2】:

    如果您需要在 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);
    

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

    【讨论】:

    • 你也可以用这个方法画(出)线。只需拉伸和旋转矩形即可绘制“线条”。
    【解决方案3】:

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

        {
            //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);
        }
    

    【讨论】:

      猜你喜欢
      • 2016-01-13
      • 2014-01-13
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多