# スプライトバッチ 上級 プログラマー スプライトバッチは、スプライト(2D のテクスチャ付き平面)の集合体です。 >[!Note] >カスタムコードをコンポジションに含めるには、すべてのカスタムコードを[カスタム シーン レンダラー](../graphics-compositor/custom-scene-renderers.md)に記述する必要があることを覚えておいてください。 ## スプライトバッチを作成する Stride では、@'Stride.Graphics.SpriteBatch' クラスを使って、スプライトのバッチを簡単に扱うことができます。このクラスで、スプライトの再編成、更新、表示を効率的に行うことができます。 **コード:** スプライトバッチを作成する ```cs var spriteBatch = new SpriteBatch(GraphicsDevice); ``` スプライトの作成時に、バッチの大きさを指定することができます。これは、スプライトバッチが表示することのできるスプライトの最大数ではなく、スプライトの一括描画までに蓄えておけるスプライトの最大数を表しています。 **コード:** バッチのサイズを設定する ```cs var spriteBatch = new SpriteBatch(GraphicsDevice, 2000); ``` また、[パイプライン ステート](pipeline-state.md)のページで説明されているように、ステートを設定することもできます。 ## スプライトバッチを描画する @'Stride.Graphics.SpriteBatch' クラスには、様々なパラメータを設定できる複数の描画メソッドがあります。機能の一覧については、@'Stride.Graphics.SpriteBatch' API のドキュメントを参照してください。 **コード:** スプライトバッチを描画する ```cs // スプライトバッチの操作を開始する // begin the sprite batch operations spriteBatch.Begin(GraphicsContext, SpriteSortMode.Immediate); // スプライトを即時描画する // draw the sprite immediately spriteBatch.Draw(myTexture, new Vector2(10, 20)); // スプライトバッチの操作を終了する // end the sprite batch operations spriteBatch.End(); ``` スプライトを一括して描画する5つのモードがあります。これらは @'Stride.Graphics.SpriteSortMode' 列挙子で定義されています。 - Deferred(既定): スプライトは最後にまとめて描画されます。描画呼び出しのオーバーヘッドが削減されます。 - Immediate: @'Stride.Graphics.SpriteBatch.Draw*' を呼び出すたびに、すぐにスプライトが描画されます。 - Texture: Deffered モードですが、エフェクトパラメータの更新を減らすために、スプライトはテクスチャーに基づいてソートされます。 - BackToFront: スプライトの Z オーダーに基づいてソートを行う Deferred モードです。 - FrontToBack: スプライトの Z オーダーに基づいてソートを行う Deferred モードです。 モードは、@'Stride.Graphics.SpriteBatch.Begin*' メソッドで指定します。 **コード:** スプライトバッチの遅延描画 ```cs // スプライトバッチの操作を開始する // begin the sprite batch operations spriteBatch.Begin(GraphicsContext); // same as spriteBatch.Begin(GraphicsContext, SpriteSortMode.Deferred); // スプライトの修正を格納する(ここではまだ描画されない) // store the modification of the sprite spriteBatch.Draw(myTexture, new Vector2(10, 20)); // スプライトバッチの操作を終了し、すべてのスプライトを描画する // end the sprite batch operations, draw all the sprites spriteBatch.End(); ``` スプライトには、いくつかのパラメータを設定することができます。例えば、以下のようなものです。 - position(位置) - rotation(回転) - scale(拡大縮小) - depth(深度) - center offset(中央オフセット) - color tint(色彩) 完全なリストについては、@'Stride.Graphics.SpriteBatch' APIドキュメント、特に **Draw** メソッドを参照してください。 **コード:** より複雑なスプライトバッチの描画 ```cs // スプライトバッチの操作を開始 // begin the sprite batch operations spriteBatch.Begin(GraphicsContext); const int gridCount = 10; var textureOffset = new Vector2((float)graphicsDevice.BackBuffer.Width/gridCount, (float)graphicsDevice.BackBuffer.Height/gridCount); var textureOrigin = new Vector2(myTexture.Width/2.0f, myTexture.Height/2.0f); // 10x10 の格子状になった 100 個のすプライトを、それぞれ1.2度回転&0.5倍スケールで描画 // draw 100 sprites on a 10x10 grid with a rotation of 1.2 rad and a scale of 0.5 for each of them for (int y = 0; y < gridCount; y++) { for (int x = 0; x < gridCount; x++) { spriteBatch.Draw(UVTexture, new Vector2(x * textureOffset.X + textureOffset.X / 2.0f, y * textureOffset.Y + textureOffset.Y / 2.0f), Color.White, 1.2f, textureOrigin, 0.5f); } } // スプライトバッチの操作を終了し、すべてのスプライトを描画 // end the sprite batch operations, draw all the sprites spriteBatch.End(); ``` ## 関連項目 * [スプライト フォント](spritefont.md)