11

我的 XNA 项目中有一个标准的 800x600 窗口。我的目标是根据包含布尔值的矩形数组为每个单独的像素着色。目前我正在使用 1x1 纹理并在我的数组中绘制每个精灵。

我对 XNA 非常陌生,并且来自 GDI 背景,所以我正在做我在 GDI 中会做的事情,但它的扩展性不是很好。我在另一个问题中被告知要使用着色器,但经过大量研究,我仍然无法找到如何实现这一目标。

我的应用程序循环遍历我的矩形数组的 X 和 Y 坐标,根据每个值进行计算,然后重新分配/移动数组。最后,我需要用新值更新我的“画布”。我的数组的较小样本如下所示:

0,0,0,0,0,0,0
0,0,0,0,0,0,0
0,0,0,0,0,0,0
1,1,1,1,1,1,1
1,1,1,1,1,1,1

如何使用着色器为每个像素着色?

一个非常简化的计算版本是:

        for (int y = _horizon; y >= 0; y--)  // _horizon is my ending point
        {
            for (int x = _width; x >= 0; x--) // _width is obviously my x length.
            {
                if (grains[x, y] > 0)
                {
                    if (grains[x, y + 1] == 0)
                    {
                        grains[x, y + 1] = grains[x, y];
                        grains[x, y] = 0;
                    }
                }
            }
        }

..每次调用更新方法时,都会执行计算,在上述循环的示例中,更新可能如下所示:

最初的:

0,0,0,1,0,0,0
0,0,0,0,0,0,0
0,0,0,0,0,0,0
1,1,1,0,1,1,1
1,1,1,1,1,1,1

第一的:

0,0,0,0,0,0,0
0,0,0,1,0,0,0
0,0,0,0,0,0,0
1,1,1,0,1,1,1
1,1,1,1,1,1,1

第二:

0,0,0,0,0,0,0
0,0,0,0,0,0,0
0,0,0,1,0,0,0
1,1,1,0,1,1,1
1,1,1,1,1,1,1

最终的:

0,0,0,0,0,0,0
0,0,0,0,0,0,0
0,0,0,0,0,0,0
1,1,1,1,1,1,1
1,1,1,1,1,1,1

更新:

应用 Render2DTarget 代码并放置我的像素后,我的像素上出现了一个不需要的边框,总是在左边。我怎样才能删除这个?

替代文字 http://www.refuctored.com/borders.png

替代文字 http://www.refuctored.com/fallingdirt.png

应用纹理的一些代码是:

    RenderTarget2D target;
    Texture2D texture;
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        texture = Content.Load<Texture2D>("grain");
        _width = this.Window.ClientBounds.Width - 1;
        _height = this.Window.ClientBounds.Height - 1;
        target = new RenderTarget2D(this.GraphicsDevice,_width, _height, 1, SurfaceFormat.Color,RenderTargetUsage.PreserveContents);
     }

 protected override void Draw(GameTime gameTime)
    {
        this.GraphicsDevice.SetRenderTarget(0, target);
        this.GraphicsDevice.SetRenderTarget(0, null);
        this.GraphicsDevice.Clear(Color.SkyBlue);
        this.spriteBatch.Begin(SpriteBlendMode.None,SpriteSortMode.Deferred,SaveStateMode.None);
        SetPixels(texture);
        this.spriteBatch.End();
    }

 private void SetPixels(Texture2D texture)
    {
        for (int y = _grains.Height -1; y > 0; y--)
        {
            for (int x = _grains.Width-1; x > 0; x--)
            {
                if (_grains.GetGrain(x, y) >0)
                {
                    this.spriteBatch.Draw(texture, new Vector2(x,y),null, _grains.GetGrainColor(x, y));
                }
            }
        }
    }
4

4 回答 4

2

此方法不使用像素着色器,但如果您希望使用 Texture2D 的 SetData 方法而不是为每个像素调用 SpriteBatch.Draw(),您可能会发现这很有用。我使用了一个 uint 数组而不是 bool 来表示你的颜色。如果您可以使用 8 位颜色纹理,则可以通过更改纹理格式来加快速度。

public class Game1 : Microsoft.Xna.Framework.Game
{
    // Set width, height
    const int WIDTH = 800;
    const int HEIGHT = 600;

    // Used to randomly fill in initial data, not necessary
    Random rand;

    // Graphics and spritebatch
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    // Texture you will regenerate each call to update
    Texture2D texture;

    // Data array you perform calculations on
    uint[] data;

    // Colors are represented in the texture as 0xAARRGGBB where:
    // AA = alpha
    // RR = red
    // GG = green
    // BB = blue

    // Set the first color to red
    const uint COLOR0 = 0xFFFF0000;

    // Set the second color to blue
    const uint COLOR1 = 0xFF0000FF;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        // Set width, height
        graphics.PreferredBackBufferWidth = WIDTH;
        graphics.PreferredBackBufferHeight = HEIGHT;
    }

    protected override void Initialize()
    {
        base.Initialize();

        // Seed random, initialize array with random picks of the 2 colors
        rand = new Random((int)DateTime.Now.Ticks);
        data = new uint[WIDTH * HEIGHT];
        loadInitialData();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // Create a new texture
        texture = new Texture2D(GraphicsDevice, WIDTH, HEIGHT);
    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // Run-time error without this
        // Complains you can't modify a texture that has been set on the device
        GraphicsDevice.Textures[0] = null;

        // Do the calculations
        updateData();

        // Update the texture for the next time it is drawn to the screen
        texture.SetData(data);

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        // Draw the texture once
        spriteBatch.Begin();
        spriteBatch.Draw(texture, Vector2.Zero, Color.Purple);
        spriteBatch.End();

        base.Draw(gameTime);
    }

    private void loadInitialData()
    {
        // Don't know where the initial data comes from
        // Just populate the array with a random selection of the two colors
        for (int i = 0; i < WIDTH; i++)
            for (int j = 0; j < HEIGHT; j++)
                data[i * HEIGHT + j] = rand.Next(2) == 0 ? COLOR0 : COLOR1;

    }

    private void updateData()
    {
        // Rough approximation of calculations
        for(int y = HEIGHT - 1; y >= 0; y--)
            for (int x = WIDTH - 1; x >= 0; x--)
                if (data[x * HEIGHT + y] == COLOR1)
                    if (y + 1 < HEIGHT && data[x * HEIGHT + (y + 1)] == COLOR0)
                    {
                        data[x * HEIGHT + (y + 1)] = data[x * HEIGHT + y];
                        data[x * HEIGHT + y] = COLOR0;
                    }
    }
}
于 2010-04-10T01:08:15.640 回答
0

这个怎么样...

创建两个纹理 (800x600)

将其中之一初始化为初始值。

对于每一帧,您在更新像素着色器中的值时将一个纹理渲染到另一个。

将生成的纹理渲染到屏幕后,交换它们,以便它们为下一帧做好准备。

编辑:

您将需要两个 RenderTarget2D 实例并使用 RenderTargetUsage.PreserveContents 创建它们。您可以从 SurfaceFormat.Color 开始,使用黑色作为 0,使用白色作为 1。(您也可以找到 8 位格式来节省视频内存。)

new RenderTarget2D(_device, 800, 600, 1, SurfaceFormat.Color, RenderTargetUsage.PreserveContents);

您可以像这样将它们分配给 rendertaget:

_device.SetRenderTarget(0, myRenderTarget);

您使用 RenderTarget2D 作为纹理,如下所示:

_device.Textures[0] = myRenderTarget.GetTexture();

希望对您有所帮助...我可以从我的引擎中挖掘出更多信息,所以请询问。

于 2010-04-07T08:58:02.967 回答
0

您正在尝试做的(逐个像素绘制)是 DirectX 所做的。XNA 是建立在 DirectX 之上的层,因此您不必逐个像素地绘制。如果这确实是您想要做的,那么您可能应该学习 DirectX 而不是 XNA。你可能会发现它更容易...

于 2010-04-12T19:38:32.490 回答
0

将广告牌添加到您的场景中(直接在相机前面,使其恰好占据 800x600 像素)。

将二进制数组绑定为纹理。

在片段(/像素)着色器中,使用片段的 2D 位置和纹理计算片段的颜色。

于 2010-04-12T21:56:23.560 回答