本文整理汇总了C#中Viewport.Unproject方法的典型用法代码示例。如果您正苦于以下问题:C# Viewport.Unproject方法的具体用法?C# Viewport.Unproject怎么用?C# Viewport.Unproject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Viewport
的用法示例。
在下文中一共展示了Viewport.Unproject方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Raycast
private void Raycast(Vector2 screenPos)
{
var backBuffer = GraphicsDevice.Presenter.BackBuffer;
screenPos.X *= backBuffer.Width;
screenPos.Y *= backBuffer.Height;
var viewport = new Viewport(0, 0, backBuffer.Width, backBuffer.Height);
var unprojectedNear =
viewport.Unproject(
new Vector3(screenPos, 0.0f),
camera.ProjectionMatrix,
camera.ViewMatrix,
Matrix.Identity);
var unprojectedFar =
viewport.Unproject(
new Vector3(screenPos, 1.0f),
camera.ProjectionMatrix,
camera.ViewMatrix,
Matrix.Identity);
var result = simulation.Raycast(unprojectedNear, unprojectedFar);
if (!result.Succeeded || result.Collider == null) return;
var rigidBody = result.Collider as RigidbodyComponent;
if (rigidBody == null) return;
rigidBody.Activate();
rigidBody.ApplyImpulse(new Vector3(0, 5, 0));
}
示例2: GetWorldRay
/// <summary>
/// Creates a ray in object space based on a screen position and a previously rendered object's WorldViewProjection matrix
/// </summary>
/// <param name="viewport">The viewport in which the object was rendered</param>
/// <param name="screenPos">The click position on screen in normalized (0..1, 0..1) range</param>
/// <param name="worldViewProj">The WorldViewProjection matrix with which the object was last rendered in the view</param>
/// <returns></returns>
private Ray GetWorldRay(ref Viewport viewport, Vector2 screenPos, ref Matrix worldViewProj)
{
var graphicsDevice = graphicsDeviceService?.GraphicsDevice;
if (graphicsDevice == null)
return new Ray(new Vector3(float.NegativeInfinity), new Vector3(0, 1, 0));
screenPos.X *= graphicsDevice.Presenter.BackBuffer.Width;
screenPos.Y *= graphicsDevice.Presenter.BackBuffer.Height;
var unprojectedNear = viewport.Unproject(new Vector3(screenPos, 0.0f), ref worldViewProj);
var unprojectedFar = viewport.Unproject(new Vector3(screenPos, 1.0f), ref worldViewProj);
var rayDirection = Vector3.Normalize(unprojectedFar - unprojectedNear);
var clickRay = new Ray(unprojectedNear, rayDirection);
return clickRay;
}