簡體   English   中英

在 XNA 中繪制簡單的圓圈

[英]Draw simple circle in XNA

我想畫一個 2d 的填充圓。 我四處尋找,似乎找不到任何可以遠程幫助我畫圓圈的東西。 我只想在畫布上指定高度、寬度和位置。

有誰知道怎么做?

謝謝!

XNA 通常不知道可以在其上繪畫的畫布。 相反,您可以在您最喜歡的繪畫程序中創建一個圓並將其渲染為精靈,或者在 3D 網格中創建一系列頂點來近似一個圓並渲染它。

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

演示有一個動態紋理生成器,讓他制作圓形和矩形(然后樣本用作物理模擬的可視化)。 你可以重復使用它:-)

有同樣的問題,因為其他人已經建議您需要繪制一個帶有圓形紋理的正方形或矩形。 下面是我創建圓形紋理運行時的方法。 不是最有效或最奇特的方法,但它有效。

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

開箱即用,XNA 不支持此功能。 我假設您來自一些 GDI 背景並且只想看到屏幕上移動的東西。 但是,在真正的游戲中,如果需要的話,這很少見。

這里有一些有用的信息:

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

我給你的建議是直接噴漆或其他東西,自己創建基本形狀並使用內容管道。

似乎有一個使用像素着色器的解決方案 - http://www.xnainfo.com/resources.php?view=Code%20snippets

另一種選擇(如果你想使用更復雜的漸變畫筆或其他東西)是繪制一個與屏幕對齊的四邊形並使用像素着色器。

我為解決這個問題所做的是繪制一個矩形紋理,使不包含圓形的矩形區域保持透明。 您檢查數組中的一個點是否包含在從矩形中心開始的圓內。

使用顏色數據數組有點奇怪,因為它不是二維數組。 我的解決方案是在場景中引入一些二維數組邏輯。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM