1,384 バイト追加,
2019年12月9日 (月) 04:48 3D空間で物体を掴んだりするのに必要になる処理。
[[MonoGame]]での例だが、[[WebGL]]だろうと何だろうと同じだ。
* マウス座標をビュー座標に変換した場所から
* 画面の奥に向かって飛ぶベクトル(レイ)を求める。
<source lang="csharp">
// マウスの座標
var mouseState = Mouse.GetState();
var nearScreenPoint = new Vector3(mouseState.X, mouseState.Y, 0);
var farScreenPoint = new Vector3(mouseState.X, mouseState.Y, 1);
var viewport = GraphicsDevice.Viewport;
var nearWorldPoint = viewport.Unproject(nearScreenPoint, camera.Projection, camera.View, Matrix.Identity);
var farWorldPoint = viewport.Unproject(farScreenPoint , camera.Projection, camera.View, Matrix.Identity);
var direction = Vector3.Normalize( farWorldPoint - nearWorldPoint );
new Ray(nearWorldPoint, direction);
</source>
このレイは画面に描画しようとしても、ディスプレイのXY平面と垂直なZ方向(画面奥)に向かって飛ぶので「点」にしかならない。画面に「線」が表示されるようなら[[バグ]]ってる。
この計算後、かつ描画前にカメラを少しでも動かす処理が入ると表示がバグるよ。
== 関連項目 ==
* [[MonoGameで画面空間からモデル空間に投影する]]
[[category: MonoGame]]