gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:49:32 25 4
gpt4 key购买 nike

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

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

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

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 代码并放置我的像素后,我的像素上出现了不需要的边框,总是在左边。我该如何删除它?

alt text http://www.refuctored.com/borders.png

alt text 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));
}
}
}
}

最佳答案

此方法不使用像素着色器,但如果您希望使用 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;
}
}
}

关于c# - 我如何使用 XNA 中的着色器为单个像素着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2569996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com