Beispiel #1
0
        public static Vector3 ProjectMousePosition(Vector2 mousePos, Microsoft.Xna.Framework.Graphics.Viewport viewport, Matrix world, Matrix view, Matrix projection, float depth)
        {
            Vector3 nearSource = new Vector3(mousePos.X, mousePos.Y, 0);
            Vector3 farSource  = new Vector3(mousePos.X, mousePos.Y, 1);

            Vector3 nearPoint = viewport.Unproject(nearSource, projection, view, world);
            Vector3 farPoint  = viewport.Unproject(farSource, projection, view, world);

            Ray   ray   = new Ray(nearPoint, Vector3.Normalize(farPoint - nearPoint));
            Plane plane = new Plane(new Vector3(0, 0, -1), depth);

            float denominator = Vector3.Dot(plane.Normal, ray.Direction);
            float numerator   = Vector3.Dot(plane.Normal, ray.Position) + plane.D;
            float t           = -(numerator / denominator);

            return(nearPoint + ray.Direction * t);
        }
Beispiel #2
0
        public void UnprojectTest1()
        {
            Vector3 vector = Utilities.GenerateVector3();
            Vector3 vectorExpected = vector;
            float x = (float)Math.Floor(Utilities.GenerateFloat());
            float y = (float)Math.Floor(Utilities.GenerateFloat());
            float width = (float)Math.Floor(Utilities.GenerateFloat() + x);
            float height = (float)Math.Floor(Utilities.GenerateFloat() + y);
            float minZ = Utilities.GenerateFloat();
            float maxZ = Utilities.GenerateFloat() + minZ + 1f;
            Matrix worldViewProjection = Utilities.GenerateMatrix();
            Matrix worldViewProjectionExpected = worldViewProjection;
            Vector3 result;

            Microsoft.Xna.Framework.Graphics.Viewport viewport = new Microsoft.Xna.Framework.Graphics.Viewport()
            {
                X = (int)x,
                Y = (int)y,
                Width = (int)width,
                Height = (int)height,
                MinDepth = minZ,
                MaxDepth = maxZ
            };
            Vector3 resultExpected = Utilities.ConvertFrom(viewport.Unproject(
                Utilities.ConvertToXna(vector),
                Utilities.ConvertToXna(worldViewProjection),
                Utilities.ConvertToXna(Matrix.Identity),
                Utilities.ConvertToXna(Matrix.Identity)));

            Vector3.Unproject(ref vector, x, y, width, height, minZ, maxZ, ref worldViewProjection, out result);
            Utilities.AreEqual(vectorExpected, vector);
            Utilities.AreEqual(worldViewProjectionExpected, worldViewProjection);
            Utilities.AreEqual(resultExpected, result);
        }
Beispiel #3
0
 private Vector3 Unproject(Vector3 vector)
 {
     //Projects the vector from screen coordinates into 3D space
     return(Viewport.Unproject(vector, CameraProjection, view, world * Attitude));
 }