11

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));
                }
            }
        }
    }
4

4 に答える 4

2

このメソッドはピクセル シェーダーを使用しませんが、ピクセルごとに SpriteBatch.Draw() を呼び出す代わりに Texture2D の SetData メソッドを使用する場合は、これが役立つことがあります。色を表すために、bool の代わりに uint の配列を使用しました。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ピクセルを使用します)。

バイナリ配列をテクスチャとしてバインドします。

フラグメント(/ pixel)シェーダーで、フラグメントの2D位置とテクスチャを使用してフラグメントの色を計算します。

于 2010-04-12T21:56:23.560 に答える
0

あなたがしようとしていること (ピクセル単位で描画) は、DirectX が行うことです。XNA は、ピクセルごとに描画する必要がないように、DirectX の上に構築されたレイヤーです。それが本当にやりたいことなら、おそらく XNA ではなく DirectX を学ぶべきです。あなたはおそらくそれがはるかに簡単だと思うでしょう...

于 2010-04-12T19:38:32.490 に答える
0

これはどう...

テクスチャを 2 つ作成する (800x600)

そのうちの 1 つを初期値に初期化します。

フレームごとに、ピクセルシェーダーの値を更新しながら、1 つのテクスチャを別のテクスチャにレンダリングします。

結果のテクスチャを画面にレンダリングした後、それらを交換して、次のフレームの準備が整うようにします。

編集:

RenderTarget2D の 2 つのインスタンスが必要で、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 に答える