当前位置: 首页>>代码示例>>C#>>正文


C# Viewport.Unproject方法代码示例

本文整理汇总了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));
        }
开发者ID:Kryptos-FR,项目名称:xenko-reloaded,代码行数:31,代码来源:RaycastingScript.cs

示例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;
        }
开发者ID:Kryptos-FR,项目名称:xenko-reloaded,代码行数:25,代码来源:UIRenderFeature.Picking.cs


注:本文中的Viewport.Unproject方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。