在XNA中绘制简单的圆形

11
我想绘制一个2D填充圆。我已经到处查找,但似乎找不到任何可以帮助我绘制圆的东西。我只需要在画布上指定一个高度、宽度和位置即可。
有人知道怎么做吗?
谢谢!
7个回答

14

XNA通常没有可以绘图的画布概念。相反,您可以在喜爱的绘图程序中创建一个圆形,并将其呈现为精灵;或者创建一系列顶点在3D网格中近似表示一个圆并进行渲染。


9

我遇到了相同的问题,正如其他人已经建议的那样,您需要画一个带有圆形纹理的正方形或矩形。以下是我创建圆形纹理的方法。这不是最有效或最花哨的方式,但可以解决问题。

Texture2D createCircleText(int radius)
{
    Texture2D texture = new Texture2D(GraphicsDevice, radius, radius);
    Color[] colorData = new Color[radius*radius];

    float diam = radius / 2f;
    float diamsq = diam * diam;

    for (int x = 0; x < radius; x++)
    {
        for (int y = 0; y < radius; y++)
        {
            int index = x * radius + y;
            Vector2 pos = new Vector2(x - diam, y - diam);
            if (pos.LengthSquared() <= diamsq)
            {
                colorData[index] = Color.White;
            }
            else
            {
                colorData[index] = Color.Transparent;
            }
        }
    }

    texture.SetData(colorData);
    return texture;
}

4
直径定义为半径乘以2。如果你交换这些变量名会更好。 - cor

8

你还可以查看Jeff Weber在Farseer中使用的示例框架:
http://www.codeplex.com/FarseerPhysics

这些演示程序有一个动态纹理生成器,可以让他创建圆形和矩形(然后样本将其用作物理模拟的可视化)。你也可以直接复用它 :-)


3

开箱即用,XNA 并不支持此功能。我假设你来自一些 GDI 背景,并且只是想在屏幕上看到一些移动的东西。但在真正的游戏中,这很少需要。

这里有一些有用的信息:

http://forums.xna.com/forums/t/7414.aspx

我的建议是,您可以打开画图或其他软件,自己创建基本形状并使用内容管道。该管道可帮助您更轻松地将资源添加到游戏中。

2

如果你想使用更复杂的渐变刷或其他东西,另一个选择是绘制一个与屏幕对齐的四边形并使用像素着色器。


1
我为了解决这个问题,绘制了一个矩形纹理,使不包含圆的矩形区域透明。您可以检查数组中的点是否包含在以矩形中心为起点的圆内。
使用颜色数据数组有点奇怪,因为它不是一个二维数组。我的解决方案是将一些二维数组逻辑引入到场景中。
public Texture2D GetColoredCircle(float radius, Color desiredColor)
    {
        radius = radius / 2;
        int width = (int)radius * 2;
        int height = width;

        Vector2 center = new Vector2(radius, radius);

        Circle circle = new Circle(center, radius,false);

        Color[] dataColors = new Color[width * height];
        int row = -1; //increased on first iteration to zero!
        int column = 0;
        for (int i = 0; i < dataColors.Length; i++)
        {
            column++;
            if(i % width == 0) //if we reach the right side of the rectangle go to the next row as if we were using a 2D array.
            {
                row++;
                column = 0;
            }
            Vector2 point = new Vector2(row, column); //basically the next pixel.
            if(circle.ContainsPoint(point))
            {
                dataColors[i] = desiredColor; //point lies within the radius. Paint it.
            }
            else
            {
                dataColors[i] = Color.Transparent; //point lies outside, leave it transparent.
            }
            
        }
        Texture2D texture = new Texture2D(GraphicsDevice, width, height);
        texture.SetData(0, new Rectangle(0, 0, width, height), dataColors, 0, width * height);
        return texture;
    }

以下是检查一个点是否包含在你的圆内的方法:

 public bool ContainsPoint(Vector2 point)
    {
        return ((point - this.Center).Length() <= this.Radius);
    }

希望这有所帮助!

-1
 public Texture2D createCircleText(int radius, GraphicsDevice Devise,Color color,int tickenes)
    {
        Texture2D texture = new Texture2D(Devise, radius, radius);
        Color[] colorData = new Color[radius * radius];
        if (tickenes >= radius) tickenes = radius - 5;
        float diam = radius / 2f;
        float diamsq = diam * diam;
        float intdiam = (radius-tickenes) / 2f;
        float intdiamsq = intdiam * intdiam;

        for (int x = 0; x < radius; x++)
        {
            for (int y = 0; y < radius; y++)
            {
                int index = x * radius + y;
                Vector2 pos = new Vector2(x - diam, y - diam);
                if (pos.LengthSquared() <= diamsq)
                {
                    colorData[index] = color;
                }
                else
                {
                    colorData[index] = Color.Transparent;
                }
                if (pos.LengthSquared() <= intdiamsq)
                {
                    colorData[index] = Color.Transparent; 
                }
            }
        }

        texture.SetData(colorData);
        return texture;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接