2
0

textures-and-render-textures.md 13 KB

テクスチャーとレンダーテクスチャー

上級 プログラマー

Stride では、@'Stride.Graphics.Texture' クラスを使って、コード内でテクスチャーオブジェクトを操作することができます。

テクスチャーへのレンダリングの詳細については、レンダーテクスチャーを参照してください。

テクスチャーを読み込む

Stride のアセットからテクスチャーを読み込むには、次の関数を呼び出します。

// duck.dds(または.pngなど)というテクスチャーを読み込む
// loads the texture called duck.dds (or .png etc.)
var myTexture = Content.Load<Texture>("duck");

これによって、すべてのフィールドが適切に設定されたテクスチャーオブジェクトが自動的に生成されます。

テクスチャーを作成する

アセットを使わずにテクスチャーを作ることもできます(例:レンダーターゲットとして使用するため)。これを行うには、@'Stride.Graphics.Texture' クラスのコンストラクタを呼び出します。利用可能なオプションとパラメータの完全なリストについては、@'Stride.Graphics.Texture' クラスのリファレンスをご覧ください。一部、すべてのプラットフォームで利用できるわけではないテクスチャーフォーマットもあります。

コード: テクスチャーを作成する

var myTexture = Texture.New2D(GraphicsDevice, 512, 512, false, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource);

レンダーテクスチャー

レンダーテクスチャーを作成する

@'Stride.Graphics.GraphicsPresenter' クラスは、常に既定のレンダーターゲットと深度バッファを提供します。これらは、@'Stride.Graphics.GraphicsPresenter.BackBuffer' プロパティおよび @'Stride.Graphics.GraphicsPresenter.DepthStencilBuffer' プロパティでアクセスできます。プレゼンターは、@'Stride.Graphics.GraphicsDevice' の @'Stride.GraphicsDevice.Presenter' プロパティとして公開されています。しかし、オフスクリーンレンダリングやポストプロセスを実行するために、独自のバッファを使用したい場合があります。そのため、Stride は、レンダーテクスチャと深度バッファとして機能するテクスチャーを簡単に作成する方法を提供しています。

コード: カスタムのレンダーテクスチャーと深度バッファを作成する

// レンダーターゲット
// render target
var myRenderTarget = Texture.New2D(GraphicsDevice, 512, 512, false, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource | TextureFlags.RenderTarget);
 
 // 書込可能な深度バッファ
// writable depth buffer
var myDepthBuffer = Texture.New2D(GraphicsDevice, 512, 512, false, PixelFormat.D16_UNorm, TextureFlags.DepthStencil);

[!Note] レンダーターゲットの動作を有効にする @'Stride.Graphics.TextureFlags.RenderTarget' フラグも忘れないでください。

PixelFormat が正しいことを確認してください(特に深度バッファの場合)。ターゲットプラットフォームで利用可能なフォーマットであるかどうかに注意してください。

レンダーターゲットを使う

これらのバッファが作成されると、それらを、現在のレンダーテクスチャーとして簡単に設定することができます。

コード:レンダーターゲットを使う

// レンダーテクスチャーを設定する
// settings the render textures
CommandList.SetRenderTargetAndViewport(myDepthBuffer, myRenderTarget);

// 既定のレンダーターゲットを設定する 
// setting the default render target
CommandList.SetRenderTargetAndViewport(GraphicsDevice.Presenter.DepthStencilBuffer, GraphicsDevice.Presenter.BackBuffer);

[!Note] レンダーターゲットと深度バッファのサイズが同じであることを確認してください。そうでない場合、深度バッファは使用されません。

複数のレンダーテクスチャーを同時に設定することができます。@'Stride.Graphics.CommandList.SetRenderTargets(Stride.Graphics.Texture,Stride.Graphics.Texture[])'と@'Stride.Graphics.CommandList.SetRenderTargetsAndViewport(Stride.Graphics.Texture,Stride.Graphics.Texture[])' メソッドのオーバーロードを参照してください。

[!Note] 画面に表示されるのは @'Stride.Graphics.GraphicsPresenter.BackBuffer' だけなので、何かを表示するにはそれをレンダリングする必要があります。

レンダーターゲットをクリアする

レンダーテクスチャーをクリアするには、@'Stride.Graphics.CommandList.Clear(Stride.Graphics.Texture,Stride.Core.Mathematics.Color4)' メソッドと @'Stride.Graphics.CommandList.Clear(Stride.Graphics.Texture,Stride.Graphics.DepthStencilClearOptions,System.Single,System.Byte)' メソッドを呼び出します。

コード:ターゲットをクリアする

CommandList.Clear(GraphicsDevice.Presenter.BackBuffer, Color.Black);
CommandList.Clear(GraphicsDevice.Presenter.DepthStencilBuffer, DepthStencilClearOptions.DepthBuffer); // only clear the depth buffer 深度バッファのみクリアする

[!Note] フレームごとに @'Stride.Graphics.GraphicsPresenter.BackBuffer' と @'Stride.Graphics.GraphicsPresenter.DepthStencilBuffer' をクリアすることを忘れないでください。そうしないと、デバイスによっては予期せぬ動作をする可能性があります。フレームの内容をクリアせず維持したい場合は、中間レンダーターゲットを使用してください。

ビューポート

@'Stride.Graphics.CommandList.SetRenderTargetAndViewport(Stride.Graphics.Texture,Stride.Graphics.Texture)' は、現在の @'Stride.Graphics.Viewport' をレンダーターゲットの全体サイズに調整します。

テクスチャーのサブセットにのみレンダリングしたい場合は、@'Stride.Graphics.CommandList.SetRenderTarget(Stride.Graphics.Texture,Stride.Graphics.Texture)' と @'Stride.Graphics.CommandList.SetViewport(Stride.Graphics.Viewport)' を使って、レンダーターゲットとビューポートを別々に設定します。

@'Stride.Graphics.CommandList.SetViewports(Stride.Graphics.Viewport[])'と @'Stride.Graphics.CommandList.SetViewports(System.Int32,Stride.Graphics.Viewport[])' オーバーロードを使用して、ジオメトリシェーダーで使用する複数のビューポートをバインドすることができます。

コード:ビューポートを設定する

// フルHDバッファの例
// example of a full HD buffer
CommandList.SetRenderTarget(GraphicsDevice.Presenter.DepthStencilBuffer, GraphicsDevice.Presenter.BackBuffer); // no viewport set
 
// フルHDバッファ(1920x1080)の画像の周囲に10ピクセルの境界線があるようにビューポートを設定した例
// example of setting the viewport to have a 10-pixel border around the image in a full HD buffer (1920x1080)
var viewport = new Viewport(10, 10, 1900, 1060);
CommandList.SetViewport(viewport);

シザー

@'Stride.Graphics.CommandList.SetScissorRectangle(Stride.Core.Mathematics.Rectangle)' および @'Stride.Graphics.CommandList.SetScissorRectangles(Stride.Core.Mathematics.Rectangle[])' メソッドは、シザーを設定します。ビューポートとは異なり、サイズではなく、シザーを定義する頂点の位置の座標を指定する必要があります。

コード:シザーを設定する

// 
// フルHD(1920x1080)のバッファで、画像の周りを10ピクセルずつ切り取るようにシザーを設定した例
var rectangle = new Rectangle(10, 10, 1910, 1070);
CommandList.SetScissorRectangles(rectangle);
 
var rectangles = new[] { new Rectangle(10, 10, 1900, 1060), new Rectangle(0, 0, 256, 256) };
CommandList.SetScissorRectangles(rectangles);

関連項目