MonoGame/クラスライブラリ/RenderTarget2Dクラス
< MonoGame | クラスライブラリ
ナビゲーションに移動
検索に移動
2017年11月8日 (水) 14:14時点におけるimported>Administratorによる版 (ページの作成:「RenderTarget2Dクラスは、Texture2Dの派生クラスであり、大雑把にいえば動的に絵を描くこ...」)
RenderTarget2Dクラスは、Texture2Dの派生クラスであり、大雑把にいえば動的に絵を描くことができるテクスチャである。
Texture2DはSetDataメソッドで画素配列をぶち込むことしかできないのに対して、RenderTarget2DはGraphicsDeviceにセットすることで画面に描画するのと同じ方法でテスクチャに絵を描ける。これを使えばアニメーションするテクスチャなんかを作ったりもできる。実のところGraphicsDeviceが最初から持っているプライマリサーフェイスもRenderTarget2Dである。
名前空間
Microsoft.Xna.Framework.Graphics名前空間
構文
public class RenderTarget2D : Texture2D
使用例
2個のカメラ
2個のRenderTarget2Dにそれぞれ視点を変えて描画して、プライマリサーフェイスにスプライトとして左右分割表示してみた。 画面分割して2プレイヤーで遊べたりするゲームでよくあるパターン。
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace TwoCamera
{
public class Game1 : Game
{
GraphicsDeviceManager _graphics;
SpriteBatch _spriteBatch;
BasicEffect _effect;
VertexPositionColor[] _vertices;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// マウスカーソルを表示する
this.IsMouseVisible = true;
// ウインドウサイズを変更する
_graphics.PreferredBackBufferWidth = 800;
_graphics.PreferredBackBufferHeight = 600;
_graphics.ApplyChanges();
// スプライトバッチ
_spriteBatch = new SpriteBatch(_graphics.GraphicsDevice);
// MonoGame標準搭載の固定シェーダーを生成する
_effect = new BasicEffect(_graphics.GraphicsDevice);
// 頂点色を有効にする
_effect.VertexColorEnabled = true;
// 頂点
_vertices = new VertexPositionColor[3];
_vertices[0] = new VertexPositionColor(new Vector3(-10, -10, 0), Color.Red);
_vertices[1] = new VertexPositionColor(new Vector3(0, +10, 0), Color.Green);
_vertices[2] = new VertexPositionColor(new Vector3(+10, -10, 0), Color.Blue);
base.Initialize();
}
protected override void LoadContent()
{
}
protected override void Update(GameTime gameTime)
{
#if !__IOS__ && !__TVOS__
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
#endif
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
var gd = _graphics.GraphicsDevice;
var w = _graphics.PreferredBackBufferWidth / 2;
var h = _graphics.PreferredBackBufferHeight;
// 1カメラのRenderTarget2Dに描画
var render1 = new RenderTarget2D(gd, w, h);
gd.SetRenderTarget(render1);
DrawCamera(new Vector3(+50, 0, 50), Color.CornflowerBlue);
// 2カメラのRenderTarget2Dに描画
var render2 = new RenderTarget2D(gd, w, h);
gd.SetRenderTarget(render2);
DrawCamera(new Vector3(-50, 0, 50), Color.SpringGreen);
// nullを渡すとプライマリサーフェイスに戻る
gd.SetRenderTarget(null);
// 1カメラと2カメラを結果をプライマリサーフェイスにスプライトとして左右分割表示
_spriteBatch.Begin();
_spriteBatch.Draw(render1, new Vector2(0, 0), Color.White);
_spriteBatch.Draw(render2, new Vector2(w, 0), Color.White);
_spriteBatch.End();
base.Draw(gameTime);
}
void DrawCamera(Vector3 cameraPosition, Color clearColor)
{
var gd = _graphics.GraphicsDevice;
// 塗りつぶす
gd.Clear(clearColor);
// カメラ
var cameraLookAtVector = Vector3.Zero;
var cameraUpVector = Vector3.UnitZ;
_effect.View = Matrix.CreateLookAt(cameraPosition, cameraLookAtVector, cameraUpVector);
var aspectRatio = gd.Viewport.AspectRatio;
var fieldOfView = MathHelper.PiOver4;
var nearClipPlane = 1;
var farClipPlane = 200;
_effect.Projection = Matrix.CreatePerspectiveFieldOfView(fieldOfView, aspectRatio, nearClipPlane, farClipPlane);
// 描画
foreach (var pass in _effect.CurrentTechnique.Passes)
{
pass.Apply();
gd.DrawUserPrimitives(PrimitiveType.TriangleList, _vertices, vertexOffset: 0, primitiveCount: _vertices.Length / 3);
};
}
}
}
上記を実行するとこんな感じ。