[英]2D XNA - Draw a Circle
哟,大家好
有什么办法画圆吗? 我不想使用“纹理/精灵”画一个圆,因为“播放器”是“圆”,所以圆应该移动...而且我也正尝试使其“播放器/圆”的大小越来越大吃一些食物等等
无论如何,如果有人知道该怎么做,请告诉我。
否则:有一种方法可以更改纹理的高度/宽度,然后我将做一个简单的圆形纹理并更改其高度/宽度。
谢谢。
您可以使用已发布的'Someone':>之类的3D图元,也可以使用C3.XNA.Primitives2D库,在其中可以使用SpriteBatch的扩展名绘制圆
public static void DrawCircle(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, Color color, float thickness);
如果您对半径和厚度使用相同的值,则圆将显示为实心。 我没有找到官方的下载链接,但是在sourceforge也有上传。
您也可以通过以下代码动态生成一个圆:
public static Texture2D GenerateCircleTexture(GraphicsDevice graphicsDevice, int radius, Color color, float sharpness)
{
int diameter = radius * 2;
Texture2D circleTexture = new Texture2D(graphicsDevice, diameter, diameter, false, SurfaceFormat.Color);
Color[] colorData = new Color[circleTexture.Width * circleTexture.Height];
Vector2 center = new Vector2(radius);
for (int colIndex = 0; colIndex < circleTexture.Width; colIndex++)
{
for (int rowIndex = 0; rowIndex < circleTexture.Height; rowIndex++)
{
Vector2 position = new Vector2(colIndex, rowIndex);
float distance = Vector2.Distance(center, position);
// hermite iterpolation
float x = distance / diameter;
float edge0 = (radius * sharpness) / (float)diameter;
float edge1 = radius / (float)diameter;
float temp = MathHelper.Clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
float result = temp * temp * (3.0f - 2.0f * temp);
colorData[rowIndex * circleTexture.Width + colIndex] = color * (1f - result);
}
}
circleTexture.SetData<Color>(colorData);
return circleTexture;
}
低于1f的清晰度会使圆圈模糊。
绘制图元(例如圆形)的唯一方法是在3D中: https : //msdn.microsoft.com/en-us/library/bb196414.aspx
或者,您可以加载1 * 1的纹理,将其拉伸为一条线,然后使用一堆这些线制成一个圆。 采用:
public void Draw (
Texture2D texture,
Vector2 position,
Nullable<Rectangle> sourceRectangle,
Color color,
float rotation,
Vector2 origin,
Vector2 scale,
SpriteEffects effects,
float layerDepth
)
拉伸纹理。
或者,您可以仅使用圆形纹理并将其拉伸。
如果要制作类似agar.io的东西,则可能需要将纹理与圆图元结合使用,以便使圆“摇摆”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.