MonoGame/クラスライブラリ/RenderTarget2Dクラス

提供: MonoBook
ナビゲーションに移動 検索に移動

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

上記を実行するとこんな感じ。

RenderTarget2Dの使用例1.png

関連項目[編集 | ソースを編集]