private Vector3 screenToWorld(Vector3 coord, Matrix view, Matrix proj, Matrix world, ViewportF screen) { Vector3 near = screen.Unproject(new Vector3(coord.X * screen.Width, coord.Y * screen.Height, 0.0f), proj, view, world); Vector3 far = screen.Unproject(new Vector3(coord.X * screen.Width, coord.Y * screen.Height, 1.0f), proj, view, world); return(near + (far - near) * 0.1f); }
public Ray ScreenPointToRay(Vector2 screenCoords) { // expand coord to fit the view screenCoords.X *= _graphicsDevice.BackBuffer.Width; screenCoords.Y *= _graphicsDevice.BackBuffer.Height; // translate vectors into world space by unprojecting them. var customViewport = new ViewportF(0, 0, _graphicsDevice.BackBuffer.Width, _graphicsDevice.BackBuffer.Height, _nearZClip, _farZClip); var nearWorldCoord = customViewport.Unproject(new Vector3(screenCoords, _nearZClip), ProjectionMatrix, ViewMatrix, Matrix.Identity); var farWorldCoord = customViewport.Unproject(new Vector3(screenCoords, _farZClip), ProjectionMatrix, ViewMatrix, Matrix.Identity); var dir = (farWorldCoord - nearWorldCoord).ToNormal(); return(new Ray(nearWorldCoord, dir)); }
public Vector3 UnprojectScreenCoord(Vector3 screenCoords) { // expand coord to fit the view screenCoords.X *= _graphicsDevice.BackBuffer.Width; screenCoords.Y *= _graphicsDevice.BackBuffer.Height; // translate vectors into world space by unprojecting them. var customViewport = new ViewportF(0, 0, _graphicsDevice.BackBuffer.Width, _graphicsDevice.BackBuffer.Height, _nearZClip, _farZClip); var worldCoord = customViewport.Unproject(screenCoords, ProjectionMatrix, ViewMatrix, Matrix.Identity); return(worldCoord); }