|
|
2D to 3D ビデオクリックした2D座標をy座標0で交わるの3D座標を取得して ビデオ
コード// variables defined elsewhere
Vector2 mousePosition;
GraphicsDeviceManager graphicsDevMgr;
// class that can access your ViewMatrix and ProjectionMatrix
ChaseCamera chaseCamera;
//----------------------------------------------------------------
// GetPickedPosition() - gets 3D position of mouse pointer
// - always on the the Y = 0 plane
//----------------------------------------------------------------
public static Vector3 GetPickedPosition()
{
// create 2 positions in screenspace using the cursor position. 0 is as
// close as possible to the camera, 10 is as far away as possible
Vector3 nearSource = new Vector3(mousePosition, 0f);
Vector3 farSource = new Vector3(mousePosition, chaseCamera.nearClip);
// find the two screen space positions in world space
Vector3 nearPoint = graphicsDevMgr.GraphicsDevice.Viewport.Unproject(nearSource,
chaseCamera.projectionMatrix,
chaseCamera.viewMatrix, Matrix.Identity);
Vector3 farPoint = graphicsDevMgr.GraphicsDevice.Viewport.Unproject(farSource,
chaseCamera.projectionMatrix,
chaseCamera.viewMatrix, Matrix.Identity);
// compute normalized direction vector from nearPoint to farPoint
Vector3 direction = farPoint - nearPoint;
direction.Normalize();
// create a ray using nearPoint as the source
Ray r = new Ray(nearPoint, direction);
// calculate the ray-plane intersection point
Vector3 n = new Vector3(0f, 1f, 0f);
Plane p = new Plane(n, 0f);
// calculate distance of intersection point from r.origin
float denominator = Vector3.Dot(p.Normal, r.Direction);
float numerator = Vector3.Dot(p.Normal, r.Position) + p.D;
float t = -(numerator / denominator);
// calculate the picked position on the y = 0 plane
Vector3 pickedPosition = nearPoint + direction * t;
return pickedPosition;
}
参考URLXNAに戻る |