我的 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));
}
}
}
}