「MonoGame/クラスライブラリ/RenderTargetUsage列挙体」を編集中
ナビゲーションに移動
検索に移動
この編集を取り消せます。 下記の差分を確認して、本当に取り消していいか検証してください。よろしければ変更を保存して取り消しを完了してください。
最新版 | 編集中の文章 | ||
30行目: | 30行目: | ||
=== DiscardContentsの注意点 === | === DiscardContentsの注意点 === | ||
− | + | GraphicsDeviceが最初から持っているレンダリングターゲット([[DirectX]]でいう[[プライマリサーフェイス]])に相当するものはDiscardContentsに設定されている。 | |
− | + | DiscardContentsの欠点はGraphicsDevice.SetRenderTargetするたびに破棄される点であり、レンダリング系APIの呼出順序を間違えると残念なことになる。 | |
− | |||
− | |||
− | + | たとば以下のコードは正常に動作しない。 | |
− | |||
− | |||
<source lang="csharp"> | <source lang="csharp"> | ||
− | protected override void Draw(GameTime gameTime) | + | protected override void Draw(GameTime gameTime) |
− | { | + | { |
− | + | // (1)プライマリサーフェイスを塗りつぶす | |
+ | _graphics.GraphicsDevice.Clear(Color.CornflowerBlue); | ||
− | + | // (2)レンダリングターゲットを切り替える | |
− | + | _graphics.GraphicsDevice.SetRenderTarget(new RenderTarget2D(GraphicsDevice, 128, 128)); | |
− | + | // (3)レンダリングターゲットをプライマリサーフェイスに戻す | |
− | + | // このときプライマリサーフェイスはDiscardContents設定なので内容がクリアされ(1)の塗りつぶしが無かったことにされる。 | |
+ | _graphics.GraphicsDevice.SetRenderTarget(null); | ||
− | + | base.Draw(gameTime); | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | } | ||
</source> | </source> | ||
DiscardContentsの場合は以下のような呼出順序で書かなければならない。 | DiscardContentsの場合は以下のような呼出順序で書かなければならない。 | ||
<source lang="csharp"> | <source lang="csharp"> | ||
− | protected override void Draw(GameTime gameTime) | + | protected override void Draw(GameTime gameTime) |
− | { | + | { |
− | + | // (1)レンダリングターゲットを切り替える | |
− | + | _graphics.GraphicsDevice.SetRenderTarget(new RenderTarget2D(GraphicsDevice, 128, 128)); | |
− | |||
− | |||
− | + | // (2)レンダリングターゲットをプライマリサーフェイスに戻す(と同時にまっさらになる) | |
− | + | _graphics.GraphicsDevice.SetRenderTarget(null); | |
− | |||
− | + | // (3)プライマリサーフェイスを塗りつぶす | |
− | + | _graphics.GraphicsDevice.Clear(Color.CornflowerBlue); | |
− | + | base.Draw(gameTime); | |
− | } | + | } |
</source> | </source> | ||