Пример #1
0
        public static Ray GetPickRay(int x, int y, ViewportF viewport, Matrix worldViewProjection)
        {
            Vector3 vector1   = new Vector3((float)x, (float)y, 0.0f);
            Vector3 vector2   = new Vector3((float)x, (float)y, 1f);
            Vector3 position  = Vector3.Unproject(vector1, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinDepth, viewport.MaxDepth, worldViewProjection);
            Vector3 direction = Vector3.Unproject(vector2, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinDepth, viewport.MaxDepth, worldViewProjection) - position;

            direction.Normalize();
            return(new Ray(position, direction));
        }
Пример #2
0
        /// <summary>
        /// Calculates a world space <see cref="SharpDX.Ray"/> from 2d screen coordinates.
        /// </summary>
        /// <param name="x">X coordinate on 2d screen.</param>
        /// <param name="y">Y coordinate on 2d screen.</param>
        /// <param name="viewport"><see cref="SharpDX.ViewportF"/>.</param>
        /// <param name="worldViewProjection">Transformation <see cref="SharpDX.Matrix"/>.</param>
        /// <returns>Resulting <see cref="SharpDX.Ray"/>.</returns>
        public static Ray GetPickRay(int x, int y, ViewportF viewport, Matrix worldViewProjection)
        {
            var nearPoint = new Vector3(x, y, 0);
            var farPoint  = new Vector3(x, y, 1);

            nearPoint = Vector3.Unproject(nearPoint, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinDepth,
                                          viewport.MaxDepth, worldViewProjection);
            farPoint = Vector3.Unproject(farPoint, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinDepth,
                                         viewport.MaxDepth, worldViewProjection);

            Vector3 direction = farPoint - nearPoint;

            direction.Normalize();

            return(new Ray(nearPoint, direction));
        }