diff --git a/Source/Engine/Level/Actors/Camera.cpp b/Source/Engine/Level/Actors/Camera.cpp index 90e33217a..f2625ca3b 100644 --- a/Source/Engine/Level/Actors/Camera.cpp +++ b/Source/Engine/Level/Actors/Camera.cpp @@ -151,6 +151,20 @@ void Camera::ProjectPoint(const Vector3& worldSpaceLocation, Float2& cameraViewp cameraViewportSpaceLocation = Float2(clipSpaceLocation); } +void Camera::UnprojectPoint(const Float2& gameWindowSpaceLocation, float depth, Vector3& worldSpaceLocation) const +{ + UnprojectPoint(gameWindowSpaceLocation, depth, worldSpaceLocation, GetViewport()); +} + +void Camera::UnprojectPoint(const Float2& cameraViewportSpaceLocation, float depth, Vector3& worldSpaceLocation, const Viewport& viewport) const +{ + Matrix v, p, ivp; + GetMatrices(v, p, viewport); + Matrix::Multiply(v, p, ivp); + ivp.Invert(); + viewport.Unproject(Vector3(cameraViewportSpaceLocation, depth), ivp, worldSpaceLocation); +} + bool Camera::IsPointOnView(const Vector3& worldSpaceLocation) const { Vector3 cameraUp = GetTransform().GetUp(); diff --git a/Source/Engine/Level/Actors/Camera.h b/Source/Engine/Level/Actors/Camera.h index 05d505368..5a0bece33 100644 --- a/Source/Engine/Level/Actors/Camera.h +++ b/Source/Engine/Level/Actors/Camera.h @@ -150,6 +150,23 @@ public: /// The viewport. API_FUNCTION() void ProjectPoint(const Vector3& worldSpaceLocation, API_PARAM(Out) Float2& cameraViewportSpaceLocation, API_PARAM(Ref) const Viewport& viewport) const; + /// + /// Converts a game window-space point into a corresponding point in world space. + /// + /// The input game window coordinates (XY in screen pixels). + /// The input camera-relative depth position (eg. clipping plane). + /// The output world-space location (XYZ in world). + API_FUNCTION() void UnprojectPoint(const Float2& gameWindowSpaceLocation, float depth, API_PARAM(Out) Vector3& worldSpaceLocation) const; + + /// + /// Converts a camera viewport-space point into a corresponding point in world space. + /// + /// The input camera viewport-space location (XY in screen pixels). + /// The input camera-relative depth position (eg. clipping plane). + /// The output world-space location (XYZ in world). + /// The viewport. + API_FUNCTION() void UnprojectPoint(const Float2& cameraViewportSpaceLocation, float depth, API_PARAM(Out) Vector3& worldSpaceLocation, API_PARAM(Ref) const Viewport& viewport) const; + /// /// Checks if the 3d point of the world is in the camera's field of view. ///