public Unproject ( System.Vector3 source, System.Matrix projection, System.Matrix view, System.Matrix world ) : System.Vector3 | ||
source | System.Vector3 | The |
projection | System.Matrix | The projection |
view | System.Matrix | The view |
world | System.Matrix | The world |
Результат | System.Vector3 |
public static Microsoft.Xna.Framework.Ray FromScreenPoint(Viewport viewport, Vector2 mousePos, Matrix view, Matrix project) { Vector3 nearPoint = viewport.Unproject(new Vector3(mousePos, 0), project, view, Matrix.Identity); Vector3 farPoint = viewport.Unproject(new Vector3(mousePos, 1), project, view, Matrix.Identity); return new Microsoft.Xna.Framework.Ray(nearPoint, farPoint - nearPoint); }
public static Ray CreateWorldRayFromScreenPoint(Vector2 screenPosition, Viewport viewport, Vector3 cameraPosition, Matrix viewTransform, Matrix projectionTransform) { Vector3 nearScreen = new Vector3(screenPosition.X, screenPosition.Y, 0.0f); Vector3 farScreen = new Vector3(screenPosition.X, screenPosition.Y, 1.0f); Vector3 nearWorld = viewport.Unproject(nearScreen, projectionTransform, viewTransform, Matrix.Identity); Vector3 farWorld = viewport.Unproject(farScreen, projectionTransform, viewTransform, Matrix.Identity); Vector3 projectionDirection = (farWorld - nearWorld); projectionDirection.Normalize(); Ray ray = new Ray(cameraPosition, projectionDirection); return ray; }
public Ray CalculateRay(MouseState mouseState, Matrix view, Matrix projection, Viewport viewport) { Vector3 nearPoint = viewport.Unproject(new Vector3(mouseState.X, mouseState.Y, 0.0f), projection, view, Matrix.Identity); Vector3 farPoint = viewport.Unproject(new Vector3(mouseState.X, mouseState.Y, 1.0f), projection, view, Matrix.Identity); Vector3 direction = farPoint - nearPoint; direction.Normalize(); return new Ray(nearPoint, direction); }
public static Ray GetRayFromScreenPoint(Vector2 screenPosition, Viewport vp, Matrix pMatrix, Matrix vMatrix) { // unproject screen coordinates to world coordinates Vector3 near = vp.Unproject(new Vector3(screenPosition.X, screenPosition.Y, vp.MinDepth), pMatrix, vMatrix, Matrix.Identity); Vector3 far = vp.Unproject(new Vector3(screenPosition.X, screenPosition.Y, vp.MaxDepth), pMatrix, vMatrix, Matrix.Identity); // get the direction of the ray from near vector to far vector Vector3 direction = far - near; return new Ray(near, direction); }
public static Vector3 PickPlane( Vector2 screenCoord, Plane plane, Viewport viewport, Matrix projection, Matrix view ) { Vector3 near = viewport.Unproject( new Vector3( screenCoord, 0 ), projection, view, Matrix.Identity ); Vector3 far = viewport.Unproject( new Vector3( screenCoord, 1 ), projection, view, Matrix.Identity ); float dist1 = Vector3.Dot( plane.Normal, near ); float dist2 = Vector3.Dot( plane.Normal, far ); float u = dist1 / ( dist1 - dist2 ); return near + u * ( far - near ); }
public Ray GetMouseRay(Vector2 mousePosition, Viewport viewport) { Vector3 nearPoint = new Vector3(mousePosition.X, 0, mousePosition.Y); Vector3 farPoint = new Vector3(mousePosition.X, 1, mousePosition.Y); nearPoint = viewport.Unproject(nearPoint, Projection, View, Matrix.Identity); farPoint = viewport.Unproject(farPoint, Projection, View, Matrix.Identity); Vector3 direction = farPoint - nearPoint; //direction.Normalize(); return new Ray(nearPoint, direction); }
public static void Run() { var left = -1; var right = 1; var bottom = -1; var top = 1; var near = 5.0f; var far = near + 30.0f; var xdiv = (right - left) / (far - near); var ydiv = (top - bottom) / (far - near); var world = Matrix.Identity; var view = Matrix.Identity; var proj = Matrix.CreatePerspectiveOffCenter(left, right, bottom, top, near, far); var width = 2; var height = 2; var port = new Viewport(left, bottom, width, height); // From MSDN et. al. // // projection space refers to the space after applying projection transformation from // view space. After the projection transformation, visible content has x and y coordinates // ranging from -1 to 1, and z coordinates ranging from 0 to 1. // // Positive Z comes out of the screen, so negative near and far is into the screen. // // The viewport then mirrors Y so it goes down in screen space. Doing(v => port.Project(v, proj, view, world)).With(-1, -1, -near).ShouldBe(-1, 1, 0) .With( 1, -1, -near).ShouldBe(1, 1, 0) .With(-1, 1, -near).ShouldBe(-1, -1, 0) .With(1, 1, -near).ShouldBe(1, -1, 0) .With( 0, 0, -near).ShouldBe(0, 0, 0) .With(0, 0, -far).ShouldBe(0, 0, 1); // Test project is inverse of unproject Doing(v => port.Unproject(v, proj, view, world)).With(-1, -1, 1).ShouldBe(-1 / near * far, 1 / near * far, -far); Doing(v => port.Project(v, proj, view, world)).With(-1 / near * far, 1 / near * far, -far).ShouldBe(-1, -1, 1); Doing(v => port.Project(v, proj, view, world)).With(-1 / near * far, -1 / near * far, -far).ShouldBe(-1, 1, 1) .With(1 / near * far, 1 / near * far, -far).ShouldBe(1, -1, 1) .With(1 / near * far, -1 / near * far, -far).ShouldBe(1, 1, 1); // Similar verification but now with values that are typical of usage. top = 10; left = -5; bottom = 0; right = 5; near = 1.0f; far = near + 100.0f; world = Matrix.Identity; view = Matrix.Identity; proj = Matrix.CreatePerspectiveOffCenter(left, right, bottom, top, near, far); width = 640; height = 480; port = new Viewport(0, 0, width, height); Doing(v => port.Project(v, proj, view, world)) // Corners of the screen at near plane. .With(left, top, -near).ShouldBe(0, 0, 0) .With(right, top, -near).ShouldBe(width, 0, 0) .With(left, bottom, -near).ShouldBe(0, height, 0) .With(right, bottom, -near).ShouldBe(width, height, 0) // Center of the screen. .With(0, 0, -near).ShouldBe(width / 2, height, 0) .With(0, 0, -far).ShouldBe(width / 2, height, 1); Doing(v => port.Unproject(v, proj, view, world)).With(0, 0, 0).ShouldBe(left, top, -near) .With(width, 0, 0).ShouldBe(right, top, -near) .With(0, height, 0).ShouldBe(left, bottom, -near) .With(width, height, 0).ShouldBe(right, bottom, -near); }
public Ray GetMouseRay(Vector2 mousePosition, Viewport viewport) { Vector3 nearPoint = new Vector3(mousePosition, 0); Vector3 farPoint = new Vector3(mousePosition, 1); /*nearPoint = viewport.Unproject(nearPoint, _projection, RhsLevelViewMatrix, Matrix.Identity); farPoint = viewport.Unproject(farPoint, _projection, RhsLevelViewMatrix, Matrix.Identity);*/ nearPoint = viewport.Unproject(nearPoint, GetProjectionMatrix(), GetViewMatrix(), Matrix.Identity); farPoint = viewport.Unproject(farPoint, GetProjectionMatrix(), GetViewMatrix(), Matrix.Identity); Vector3 direction = farPoint - nearPoint; direction.Normalize(); return new Ray(nearPoint, direction); }
public void UnprojectTest() { Viewport viewport = new Viewport(0, 0, 640, 480); PerspectiveProjection projection = new PerspectiveProjection(); projection.SetFieldOfView(MathHelper.ToRadians(60), viewport.AspectRatio, 10, 1000); Matrix44F view = Matrix44F.CreateLookAt(new Vector3F(0, 0, 0), new Vector3F(0, 0, -1), Vector3F.Up); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 0, -10), viewport.Unproject(new Vector3F(320, 240, 0), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(projection.Left, projection.Top, -10), viewport.Unproject(new Vector3F(0, 0, 0), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(projection.Right, projection.Top, -10), viewport.Unproject(new Vector3F(640, 0, 0), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(projection.Left, projection.Bottom, -10), viewport.Unproject(new Vector3F(0, 480, 0), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(projection.Right, projection.Bottom, -10), viewport.Unproject(new Vector3F(640, 480, 0), projection, view))); Vector3[] farCorners = new Vector3[4]; GraphicsHelper.GetFrustumFarCorners(projection, farCorners); Assert.IsTrue(Vector3F.AreNumericallyEqual((Vector3F)farCorners[0], viewport.Unproject(new Vector3F(0, 0, 1), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual((Vector3F)farCorners[1], viewport.Unproject(new Vector3F(640, 0, 1), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual((Vector3F)farCorners[2], viewport.Unproject(new Vector3F(0, 480, 1), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual((Vector3F)farCorners[3], viewport.Unproject(new Vector3F(640, 480, 1), projection, view))); }
public static bool Intersects(Sprite3D s, Vector2 pointer, Viewport v, Camera cam) { Vector3 nearPoint = new Vector3(pointer, 0); Vector3 farPoint = new Vector3(pointer, 1); nearPoint = v.Unproject(nearPoint, cam.Projection, cam.View, Matrix.Identity); farPoint = v.Unproject(farPoint, cam.Projection, cam.View, Matrix.Identity); Vector3 direction = farPoint - nearPoint; direction.Normalize(); Ray r = new Ray(nearPoint, direction); BoundingSphere bs = s.GetBoundingSphere(); return (bs.Intersects(r) != null) ? true : false; }
/// <summary> /// Calculates the ray formed from the mouse to the world space. /// </summary> /// <param name="mouseLocation">Vector2D corresponding to the coordinate of the mouse on the screen.</param> /// <param name="view"> View matrix used</param> /// <param name="projection"> Projection matrix used</param> /// <param name="viewport"> Viewport of the game</param> /// <returns>The Ray pointing from the mouse to the world space</returns> public Ray CalculateRay(Vector2 mouseLocation, Matrix view, Matrix projection, Viewport viewport) { Vector3 nearPoint = viewport.Unproject(new Vector3(mouseLocation.X, mouseLocation.Y, 5), projection, view, Matrix.Identity); Vector3 farPoint = viewport.Unproject(new Vector3(mouseLocation.X, mouseLocation.Y, 5000), projection, view, Matrix.Identity); Vector3 direction = farPoint - nearPoint; direction = Vector3.Normalize(direction); return new Ray(nearPoint, direction); }
public static Ray MouseClickToRay(Vector2 mousePosition, Matrix projectionMatrix, Matrix viewMatrix, Viewport viewport) { // 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(mousePosition, 0f); Vector3 farSource = new Vector3(mousePosition, 1f); // 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. Vector3 nearPoint = viewport.Unproject(nearSource, projectionMatrix, viewMatrix, Matrix.Identity); Vector3 farPoint = viewport.Unproject(farSource, projectionMatrix, viewMatrix, Matrix.Identity); // find the direction vector that goes from the nearPoint to the farPoint // and normalize it.... Vector3 direction = farPoint - nearPoint; direction.Normalize(); // and then create a new ray using nearPoint as the source. return new Ray(nearPoint, direction); }
public Ray GetMouseRay(Vector2 mousePosition, Viewport viewport) { Vector3 near = new Vector3(mousePosition, 0); Vector3 far = new Vector3(mousePosition, 1); near = viewport.Unproject(near, projection, view, Matrix.Identity); far = viewport.Unproject(far, projection, view, Matrix.Identity); return new Ray(near, Vector3.Normalize(far - near)); }
public Voxel GetFirstVisibleBlockHitByScreenCoord(int x, int y, Camera camera, Viewport viewPort, float dist, bool draw = false, bool selectEmpty = false) { Vector3 pos1 = viewPort.Unproject(new Vector3(x, y, 0), camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); Vector3 pos2 = viewPort.Unproject(new Vector3(x, y, 1), camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); Vector3 dir = Vector3.Normalize(pos2 - pos1); Voxel vox = GetFirstVisibleBlockHitByRay(pos1, pos1 + dir * dist, draw, selectEmpty); return vox; }
public void ControlCamera(float dt, InputManager input, Viewport vp) { Point p = input.Mouse.Displacement; if(input.Keyboard.Current.IsKeyDown(Keys.LeftControl)) { if(input.Mouse.Current.MiddleButton == ButtonState.Pressed) { // Zoom By FOV if(p.Y != 0) FOV *= (float)Math.Pow(FOV_ZOOM_SPEED, dt * p.Y); } } else if(input.Keyboard.Current.IsKeyDown(Keys.LeftShift)) { if(input.Mouse.Current.MiddleButton == ButtonState.Pressed) { // Pan Vector3 eye = Eye; Ray r1 = new Ray(eye, vp.Unproject( new Vector3(input.Mouse.Previous.X, input.Mouse.Previous.Y, 1), Projection, View, Matrix.Identity )); Ray r2 = new Ray(eye, vp.Unproject( new Vector3(input.Mouse.Current.X, input.Mouse.Current.Y, 1), Projection, View, Matrix.Identity )); Matrix mVI = Matrix.Invert(mView); Plane hit = new Plane(Center, Center + mVI.Up, Center + mVI.Right); //new Plane(Vector3.Normalize(eye - Center), 0f); r1.Direction -= r1.Position; r1.Direction.Normalize(); float d1 = r1.Intersects(hit).Value; Vector3 p1 = r1.Position + d1 * r1.Direction; r2.Direction -= r2.Position; r2.Direction.Normalize(); float d2 = r2.Intersects(hit).Value; Vector3 p2 = r2.Position + d2 * r2.Direction; Center += p1 - p2; } } else if(input.Mouse.Current.MiddleButton == ButtonState.Pressed) { // Yaw if(p.X != 0) Yaw -= YAW_SPEED * (p.X * dt); // Pitch if(p.Y != 0) Pitch += PITCH_SPEED * (p.Y * dt); } // Zoom By Distance if(input.Mouse.ScrollDisplacement != 0) { float amt = (input.Mouse.ScrollDisplacement / 60) * dt; Distance = MathHelper.Clamp(Distance * (float)Math.Pow(DIST_ZOOM_SPEED, amt), 0.1f, 1000f); } if(input.Keyboard.IsKeyJustPressed(Keys.R)) { // Reset The Camera Center = Vector3.Zero; Distance = 4f; Pitch = MathHelper.PiOver4; Yaw = 0; FOV = MathHelper.PiOver4; } }
public static Ray GetMouseRay(Vector2 mousePosition, Viewport viewport, Camera camera) { Vector3 nearPoint = new Vector3(mousePosition, 0); Vector3 farPoint = new Vector3(mousePosition, 1); nearPoint = viewport.Unproject(nearPoint, camera.Projection, camera.View, Matrix.Identity); farPoint = viewport.Unproject(farPoint, camera.Projection, camera.View, Matrix.Identity); Vector3 direction = farPoint - nearPoint; direction.Normalize(); return new Ray(nearPoint, direction); }
void Minimap_OnClicked() { MouseState mouseState = Mouse.GetState(); Viewport viewPort = new Viewport(RenderTarget.Bounds); Rectangle imageBounds = GetImageBounds(); if (IsOverButtons(mouseState.X, mouseState.Y) || IsMinimized || SuppressClick || !imageBounds.Contains(mouseState.X, mouseState.Y)) { SuppressClick = false; return; } Vector3 forward = (PlayState.Camera.Target - PlayState.Camera.Position); forward.Normalize(); Vector3 pos = viewPort.Unproject(new Vector3(mouseState.X - imageBounds.X, mouseState.Y - imageBounds.Y, 0), Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity) - forward * 10; Vector3 target = new Vector3(pos.X, PlayState.Camera.Target.Y, pos.Z); float height = PlayState.ChunkManager.ChunkData.GetFilledVoxelGridHeightAt(target.X, target.Y, target.Z); target.Y = Math.Max(height + 15, target.Y); target = MathFunctions.Clamp(target, PlayState.ChunkManager.Bounds); PlayState.Camera.ZoomTargets.Clear(); PlayState.Camera.ZoomTargets.Add(target); }