我想绘制一个2D填充圆。我已经到处查找,但似乎找不到任何可以帮助我绘制圆的东西。我只需要在画布上指定一个高度、宽度和位置即可。
有人知道怎么做吗?
谢谢!
有人知道怎么做吗?
谢谢!
XNA通常没有可以绘图的画布概念。相反,您可以在喜爱的绘图程序中创建一个圆形,并将其呈现为精灵;或者创建一系列顶点在3D网格中近似表示一个圆并进行渲染。
我遇到了相同的问题,正如其他人已经建议的那样,您需要画一个带有圆形纹理的正方形或矩形。以下是我创建圆形纹理的方法。这不是最有效或最花哨的方式,但可以解决问题。
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;
}
你还可以查看Jeff Weber在Farseer中使用的示例框架:
http://www.codeplex.com/FarseerPhysics
这些演示程序有一个动态纹理生成器,可以让他创建圆形和矩形(然后样本将其用作物理模拟的可视化)。你也可以直接复用它 :-)
开箱即用,XNA 并不支持此功能。我假设你来自一些 GDI 背景,并且只是想在屏幕上看到一些移动的东西。但在真正的游戏中,这很少需要。
这里有一些有用的信息:
我的建议是,您可以打开画图或其他软件,自己创建基本形状并使用内容管道。该管道可帮助您更轻松地将资源添加到游戏中。如果你想使用更复杂的渐变刷或其他东西,另一个选择是绘制一个与屏幕对齐的四边形并使用像素着色器。
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);
}
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;
}