C# 如何使用XNA中的着色器为单个像素着色?

C# 如何使用XNA中的着色器为单个像素着色?,c#,xna,shader,C#,Xna,Shader,我的XNA项目中有一个标准的800x600窗口。我的目标是根据保存布尔值的矩形数组为每个像素着色。目前我正在使用1x1纹理并绘制阵列中的每个精灵 我对XNA非常陌生,来自GDI背景,所以我正在做我在GDI中应该做的事情,但它的扩展性不太好。我在另一个问题中被告知使用着色器,但经过大量研究,我仍然无法找到如何实现这一目标 我的应用程序循环遍历矩形数组的X和Y坐标,根据每个值进行计算,并重新分配/移动数组。最后,我需要用新的值更新我的“画布”。我的阵列的较小示例如下所示: 0,0,0,0,0,0,0

我的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,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
首字母:

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代码并放置我的像素后,我的像素上出现了一个不需要的边框,总是在左侧。我怎样才能删除这个

应用纹理的一些代码是:

    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));
                }
            }
        }
    }
RenderTarget2D目标;
纹理2D纹理;
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
纹理=含量。载荷(“颗粒”);
_宽度=this.Window.ClientBounds.width-1;
_高度=this.Window.ClientBounds.height-1;
target=new RenderTarget2D(this.GraphicsDevice,_width,_height,1,SurfaceFormat.Color,RenderTargetUsage.PreserveContents);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
this.GraphicsDevice.SetRenderTarget(0,目标);
this.GraphicsDevice.SetRenderTarget(0,null);
此.GraphicsDevice.Clear(颜色:天蓝色);
this.spriteBatch.Begin(SpriteBlendMode.None,SpriteSortMode.Deferred,SaveStateMode.None);
设置像素(纹理);
this.spriteBatch.End();
}
私有空集像素(纹理2D纹理)
{
对于(int y=_grains.Height-1;y>0;y--)
{
对于(int x=_grains.Width-1;x>0;x--)
{
如果(_grains.GetGrain(x,y)>0)
{
this.spriteBatch.Draw(纹理,新向量2(x,y),null,_grains.GetGrainColor(x,y));
}
}
}
}
这个怎么样

创建两个纹理(800x600)

将其中一个初始化为初始值

对于每个帧,在更新pixelshader中的值时将一个纹理渲染为另一个纹理

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

编辑:

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

将它们指定给RenderGet,如下所示:

_device.SetRenderTarget(0, myRenderTarget);
_device.Textures[0] = myRenderTarget.GetTexture();
将RenderTarget2D用作纹理,如下所示:

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

希望这有助于。。。我可以从我的引擎中挖掘更多信息,所以请提问。

此方法不使用像素着色器,但如果您希望使用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;
                    }
    }
}
公共类游戏1:Microsoft.Xna.Framework.Game
{
//设置宽度、高度
常数int宽度=800;
const int HEIGHT=600;
//用于随机填写初始数据,不需要
随机兰德;
//图形和spritebatch
图形管理器图形;
SpriteBatch SpriteBatch;
//您将重新生成每个要更新的调用
纹理2D纹理;
//对其执行计算的数据数组
uint[]数据;
//颜色在纹理中表示为0xAARGGBB,其中:
//AA=α
//RR=红色
//GG=绿色
//BB=蓝色
//将第一种颜色设置为红色
常量颜色0=0xFFFF0000;
//将第二种颜色设置为蓝色
常数颜色1=0xFF0000FF;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
//设置宽度、高度
graphics.PreferredBackBufferWidth=宽度;
graphics.PreferredBackBufferHeight=高度;
}
受保护的覆盖无效初始化()
{
base.Initialize();
//种子随机,用随机选取的2种颜色初始化数组
rand=新随机((int)DateTime.Now.Ticks);
数据=新单元[宽度*高度];
loadInitialData();
}
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
//创建新纹理
纹理=新纹理2d(图形设备、宽度、高度);
}
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
//没有此选项的运行时错误
//抱怨无法修改设备上设置的纹理
GraphicsDevice.Textures[0]=null;
//计算
更新数据();
//下次将纹理绘制到屏幕时更新纹理
设置数据(数据);
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
//在上面绘制纹理