XNAプロジェクトに標準の800x600ウィンドウがあります。私の目標は、ブール値を保持する長方形配列に基づいて、個々のピクセルに色を付けることです。現在、1x1テクスチャを使用して、配列内の各スプライトを描画しています。
私は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;
}
}
}
}
.. updateメソッドが呼び出されるたびに計算が実行され、上記のループの例では、更新は次のようになります。
イニシャル:
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
2番:
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));
}
}
}
}