Esempio n. 1
0
        /// <summary>
        /// Calculates a LineSegment from the camera's position to the
        /// 3D position of the cursor, as if it were on the camera's farplane.
        /// </summary>
        public void CalculateCursorLineSegment(out LineSegment segment)
        {
            segment.start = Vector3.Zero;
            segment.end   = Vector3.Zero;

            if (null == this.renderCamera)
            {
                return;
            }

            // Get position from Input Interface
            InputInterface input = this.game.SceneManager.GetInterface(InterfaceType.Input) as InputInterface;

            if (null == input)
            {
                throw new Exception("Input interface was not initialized before call to CalculateCursorRay()");
            }

            CursorInfo info = input.GetCursorInfo();

            if (!info.IsVisible)
            {
                // Cursor is not visible, so we just bail
                return;
            }

            // create 2 positions in screenspace using the cursor position. 0 is as
            // close as possible to the camera, 1 is as far away as possible.
            Vector3 nearSource = new Vector3(info.Position, 0f);
            Vector3 farSource  = new Vector3(info.Position, 1f);

            MsgGetViewport msgGetPort = ObjectPool.Aquire <MsgGetViewport>();

            this.game.SendInterfaceMessage(msgGetPort, InterfaceType.Graphics);

            MsgGetProjectionMatrix msgGetProj = ObjectPool.Aquire <MsgGetProjectionMatrix>();

            msgGetProj.UniqueTarget = renderCamera.UniqueID;
            this.game.SendMessage(msgGetProj);

            MsgGetViewMatrix msgGetView = ObjectPool.Aquire <MsgGetViewMatrix>();

            msgGetView.UniqueTarget = renderCamera.UniqueID;
            this.game.SendMessage(msgGetView);

            Viewport vPort   = msgGetPort.Data;
            Matrix   projMat = msgGetProj.Data;
            Matrix   viewMat = msgGetView.Data;

            // use Viewport.Unproject to tell what those two screen space positions
            // would be in world space. we'll need the projection matrix and view
            // matrix, which we have saved as member variables. We also need a world
            // matrix, which can just be identity.
            segment.start = vPort.Unproject(nearSource, projMat, viewMat, Matrix.Identity);
            segment.end   = vPort.Unproject(farSource, projMat, viewMat, Matrix.Identity);
        }