Three-dimensional coordinates to screen coordinate conversion

Page update date :
Page creation date :

summary

It converts the coordinates in 3D space to the coordinates on the screen and displays the name at the location of the model. In the sample, the camera is rotated to make it easier to see that the name follows the model.

3次元座標からスクリーンへの座標変換

Operating environment

Prerequisites

Supported XNA Versions
  • 4.0
Supported Platforms
  • Windows (XP SP2 or later, Vista, 7)
  • Xbox 360
  • Windows Phone 7
Windows Required Vertex Shader Version 2.0
Windows Required Pixel Shader Version 2.0

Operating environment

platform
  • Windows 7
  • Xbox 360
  • Windows Phone 7 Emulator

substance

Converting 3D coordinates to screen coordinates

If you want to display the name above the character's head, such as in a 3D game, it is necessary to calculate the screen coordinates on which the character should be displayed from the character's three-dimensional spatial position.

It may seem like a difficult calculation, but in fact, the vertex data of a polygon is obtained in the same way as the formula used to convert the vertex data of a polygon from 3D coordinates to screen coordinates, so no new knowledge is required.

There are also methods that are easy to calculate, so you don't have to write any additional code.

field

In the sample, three models are displayed, so we define a Model and a Vector[3], which is the three position information. It also defines an automatic rotation angle for the camera so that the movement is easy to understand.

/// <summary>
/// モデル
/// </summary>
private Model model = null;

/// <summary>
/// 位置の配列
/// </summary>
private Vector3[] positions = new Vector3[3];

/// <summary>
/// カメラの水平回転角度
/// </summary>
private float theta = 0.0f;

Update process

The rotation angle for automatic rotation of the camera is obtained from the elapsed time of the game.

// カメラの水平角度を自動更新
this.theta = (float)gameTime.TotalGameTime.TotalSeconds / 2.0f;

Pre-Drawing

After drawing the sprite, the depth buffer may be disabled, so set "DepthStencilState.Default" to enable drawing judgment by depth value.

By multiplying the Y-axis rotation matrix by the view matrix that you have already created, you can rotate the position of the camera around the point of interest. Pay attention to the order in which the matrix is multiplied.

// Zバッファを有効にする
this.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

// ビューマトリックスに回転を合成算出
Matrix rotatedView = Matrix.CreateRotationY(this.theta) * this.view;

Get Viewport

The Viewport provides a method to convert from 3D spatial coordinates to screen coordinates, so get it from the GraphicsDevice.

// ビューポート取得
Viewport viewport = this.GraphicsDevice.Viewport;

Calculation of screen coordinates from 3D coordinates

Finding the screen coordinates from 3D space coordinates is as simple as using the Viewport.Project method.

Specify the 3D spatial position of the source as the first argument, the projection matrix as the second argument, and the view matrix as the third argument. Then, you can get the coordinates on the screen as a return value.

After that, if you extract the X and Y values from Vector3, you can use the display position of the text. (Z is the depth position)

// 3次元座標からスクリーンの座標算出
Vector3 v3 = viewport.Project(this.positions[i],
                                this.projection,
                                rotatedView,
                                Matrix.Identity);
Vector2 screenPosition = new Vector2(v3.X, v3.Y);

// テキスト描画
this.spriteBatch.DrawString(this.font, "Model " + (i + 1).ToString(),
    screenPosition, Color.White);

Viewport.Project method

Projects a 3D vector from object space into screen space.

source Vector3 3D spatial coordinate vector for projection into screen coordinates
projection Matrix Projective matrix
view Matrix View Matrix
world Matrix Specifies the first transformation to be performed
Return Values Vector3 Gets a vector on screen space

All Codes

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif

namespace TransformToScreen
{
    /// <summary>
    /// ゲームメインクラス
    /// </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
        /// <summary>
        /// グラフィックデバイス管理クラス
        /// </summary>
        private GraphicsDeviceManager graphics = null;

        /// <summary>
        /// スプライトのバッチ化クラス
        /// </summary>
        private SpriteBatch spriteBatch = null;

        /// <summary>
        /// スプライトでテキストを描画するためのフォント
        /// </summary>
        private SpriteFont font = null;

        /// <summary>
        /// モデル
        /// </summary>
        private Model model = null;

        /// <summary>
        /// 位置の配列
        /// </summary>
        private Vector3[] positions = new Vector3[3];

        /// <summary>
        /// カメラの水平回転角度
        /// </summary>
        private float theta = 0.0f;

        /// <summary>
        /// ビューマトリックス
        /// </summary>
        private Matrix view = Matrix.Identity;

        /// <summary>
        /// プロジェクションマトリックス
        /// </summary>
        private Matrix projection = Matrix.Identity;


        /// <summary>
        /// GameMain コンストラクタ
        /// </summary>
        public GameMain()
        {
            // グラフィックデバイス管理クラスの作成
            this.graphics = new GraphicsDeviceManager(this);

            // ゲームコンテンツのルートディレクトリを設定
            this.Content.RootDirectory = "Content";

#if WINDOWS_PHONE
            // Windows Phone のデフォルトのフレームレートは 30 FPS
            this.TargetElapsedTime = TimeSpan.FromTicks(333333);

            // バックバッファサイズの設定
            this.graphics.PreferredBackBufferWidth = 480;
            this.graphics.PreferredBackBufferHeight = 800;

            // フルスクリーン表示
            this.graphics.IsFullScreen = true;
#endif
        }

        /// <summary>
        /// ゲームが始まる前の初期化処理を行うメソッド
        /// グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
        /// </summary>
        protected override void Initialize()
        {
            // 位置をランダムに設定
            Random rand = new Random();
            for (int i = 0; i < this.positions.Length; i++)
            {
                this.positions[i] =
                    new Vector3((float)(rand.NextDouble() - 0.5) * 10.0f,
                                0.0f,
                                (float)(rand.NextDouble() - 0.5) * 10.0f);
            }

            // ビューマトリックス
            this.view = Matrix.CreateLookAt(new Vector3(0.0f, 10.0f, 20.0f),
                                            Vector3.Zero,
                                            Vector3.Up);

            // プロジェクションマトリックス
            this.projection = Matrix.CreatePerspectiveFieldOfView(
                        MathHelper.ToRadians(45.0f),
                        (float)this.GraphicsDevice.Viewport.Width /
                            (float)this.GraphicsDevice.Viewport.Height,
                        1.0f,
                        100.0f
                    );

            // コンポーネントの初期化などを行います
            base.Initialize();
        }

        /// <summary>
        /// ゲームが始まるときに一回だけ呼ばれ
        /// すべてのゲームコンテンツを読み込みます
        /// </summary>
        protected override void LoadContent()
        {
            // テクスチャーを描画するためのスプライトバッチクラスを作成します
            this.spriteBatch = new SpriteBatch(this.GraphicsDevice);

            // フォントをコンテンツパイプラインから読み込む
            this.font = this.Content.Load<SpriteFont>("Font");

            // モデルを作成
            this.model = this.Content.Load<Model>("Model");

            // ライトとプロジェクションはあらかじめ設定しておく
            foreach (ModelMesh mesh in this.model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    // デフォルトのライト適用
                    effect.EnableDefaultLighting();

                    // プロジェクションマトリックスをあらかじめ設定
                    effect.Projection = this.projection;
                }
            }
        }

        /// <summary>
        /// ゲームが終了するときに一回だけ呼ばれ
        /// すべてのゲームコンテンツをアンロードします
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: ContentManager で管理されていないコンテンツを
            //       ここでアンロードしてください
        }

        /// <summary>
        /// 描画以外のデータ更新等の処理を行うメソッド
        /// 主に入力処理、衝突判定などの物理計算、オーディオの再生など
        /// </summary>
        /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Update(GameTime gameTime)
        {
            // Xbox 360 コントローラ、Windows Phone の BACK ボタンを押したときに
            // ゲームを終了させます
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // カメラの水平角度を自動更新
            this.theta = (float)gameTime.TotalGameTime.TotalSeconds / 2.0f;

            // 登録された GameComponent を更新する
            base.Update(gameTime);
        }

        /// <summary>
        /// 描画処理を行うメソッド
        /// </summary>
        /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Draw(GameTime gameTime)
        {
            // 画面を指定した色でクリアします
            this.GraphicsDevice.Clear(Color.CornflowerBlue);

            // Zバッファを有効にする
            this.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            // ビューマトリックスに回転を合成算出
            Matrix rotatedView = Matrix.CreateRotationY(this.theta) * this.view;

            for (int i = 0; i < this.positions.Length; i++)
            {
                foreach (ModelMesh mesh in this.model.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        // ビューマトリックス
                        effect.View = rotatedView;

                        // モデルの位置設定
                        effect.World = Matrix.CreateTranslation(this.positions[i]);
                    }

                    // モデルを描画
                    mesh.Draw();
                }
            }

            // ビューポート取得
            Viewport viewport = this.GraphicsDevice.Viewport;

            // スプライトの描画準備
            this.spriteBatch.Begin();

            // 各モデルの位置にテキストを描画
            for (int i = 0; i < this.positions.Length; i++)
            {
                // 3次元座標からスクリーンの座標算出
                Vector3 v3 = viewport.Project(this.positions[i],
                                              this.projection,
                                              rotatedView,
                                              Matrix.Identity);
                Vector2 screenPosition = new Vector2(v3.X, v3.Y);

                // テキスト描画
                this.spriteBatch.DrawString(this.font, "Model " + (i + 1).ToString(),
                    screenPosition, Color.White);
            }

            // スプライトの一括描画
            this.spriteBatch.End();

            // 登録された DrawableGameComponent を描画する
            base.Draw(gameTime);
        }
    }
}