本文整理汇总了C#中Microsoft.Xna.Framework.Graphics.Viewport.Unproject方法的典型用法代码示例。如果您正苦于以下问题:C# Viewport.Unproject方法的具体用法?C# Viewport.Unproject怎么用?C# Viewport.Unproject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Xna.Framework.Graphics.Viewport
的用法示例。
在下文中一共展示了Viewport.Unproject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FromScreenPoint
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);
}
示例2: CreateWorldRayFromScreenPoint
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;
}
示例3: CalculateRay
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);
}
示例4: GetRayFromScreenPoint
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);
}
示例5: PickPlane
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 );
}
示例6: GetMouseRay
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);
}
示例7: Run
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);
}
示例8: GetMouseRay
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);
}
示例9: UnprojectTest
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)));
}
示例10: Intersects
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;
}
示例11: CalculateRay
/// <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);
}
示例12: MouseClickToRay
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);
}
示例13: GetMouseRay
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));
}
示例14: GetFirstVisibleBlockHitByScreenCoord
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;
}
示例15: ControlCamera
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;
}
}