From 6af6649f70f9f48d470aa51dd20677dbd90b384d Mon Sep 17 00:00:00 2001 From: Ruan Lucas <79365912+RuanLucasGD@users.noreply.github.com> Date: Sun, 2 Oct 2022 00:02:56 -0400 Subject: [PATCH 1/5] implement method to check if world position is within camera view --- Source/Engine/Level/Actors/Camera.cpp | 19 +++++++++++++++++++ Source/Engine/Level/Actors/Camera.h | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/Source/Engine/Level/Actors/Camera.cpp b/Source/Engine/Level/Actors/Camera.cpp index a91074ab5..6a6e9edd8 100644 --- a/Source/Engine/Level/Actors/Camera.cpp +++ b/Source/Engine/Level/Actors/Camera.cpp @@ -5,6 +5,7 @@ #include "Engine/Core/Math/Viewport.h" #include "Engine/Content/Assets/Model.h" #include "Engine/Content/Content.h" +#include "Engine/Engine/Screen.h" #include "Engine/Serialization/Serialization.h" #if USE_EDITOR #include "Editor/Editor.h" @@ -120,6 +121,24 @@ void Camera::ProjectPoint(const Vector3& worldSpaceLocation, Float2& cameraViewp cameraViewportSpaceLocation = Float2(clipSpaceLocation); } +bool Camera::CheckPointIsOnView(const Vector3& worldSpaceLocation) const +{ + Camera* mainCamera = Camera::GetMainCamera(); + + if (!mainCamera) + { + return false; + } + + Float2 windowSpace = Float2(); + Float2 screenSize = Screen::GetSize(); + + mainCamera->ProjectPoint(worldSpaceLocation, windowSpace); + + return (windowSpace.X >= 0 && windowSpace.X <= screenSize.X) && + (windowSpace.Y >= 0 && windowSpace.Y <= screenSize.Y); +} + Ray Camera::ConvertMouseToRay(const Float2& mousePosition) const { return ConvertMouseToRay(mousePosition, GetViewport()); diff --git a/Source/Engine/Level/Actors/Camera.h b/Source/Engine/Level/Actors/Camera.h index 2b59ab323..1e38e5e04 100644 --- a/Source/Engine/Level/Actors/Camera.h +++ b/Source/Engine/Level/Actors/Camera.h @@ -168,6 +168,13 @@ public: /// The viewport. API_FUNCTION() void ProjectPoint(const Vector3& worldSpaceLocation, API_PARAM(Out) Float2& cameraViewportSpaceLocation, API_PARAM(Ref) const Viewport& viewport) const; + /// + /// Checks if the 3d point of the world is in the camera's field of view. + /// + /// World Position (XYZ) + /// Returns true if the point is within the field of view + API_FUNCTION() bool CheckPointIsOnView(const Vector3& worldSpaceLocation) const; + /// /// Converts the mouse position to 3D ray. /// From 61747bef8529e4a1e807b59f15fa86b8f8730cc2 Mon Sep 17 00:00:00 2001 From: Ruan Lucas <79365912+RuanLucasGD@users.noreply.github.com> Date: Sun, 2 Oct 2022 00:16:25 -0400 Subject: [PATCH 2/5] change method name --- Source/Engine/Level/Actors/Camera.cpp | 4 ++-- Source/Engine/Level/Actors/Camera.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Engine/Level/Actors/Camera.cpp b/Source/Engine/Level/Actors/Camera.cpp index 6a6e9edd8..cd5d13702 100644 --- a/Source/Engine/Level/Actors/Camera.cpp +++ b/Source/Engine/Level/Actors/Camera.cpp @@ -121,7 +121,7 @@ void Camera::ProjectPoint(const Vector3& worldSpaceLocation, Float2& cameraViewp cameraViewportSpaceLocation = Float2(clipSpaceLocation); } -bool Camera::CheckPointIsOnView(const Vector3& worldSpaceLocation) const +bool Camera::IsPointOnView(const Vector3& worldSpaceLocation) const { Camera* mainCamera = Camera::GetMainCamera(); @@ -136,7 +136,7 @@ bool Camera::CheckPointIsOnView(const Vector3& worldSpaceLocation) const mainCamera->ProjectPoint(worldSpaceLocation, windowSpace); return (windowSpace.X >= 0 && windowSpace.X <= screenSize.X) && - (windowSpace.Y >= 0 && windowSpace.Y <= screenSize.Y); + (windowSpace.Y >= 0 && windowSpace.Y <= screenSize.Y); } Ray Camera::ConvertMouseToRay(const Float2& mousePosition) const diff --git a/Source/Engine/Level/Actors/Camera.h b/Source/Engine/Level/Actors/Camera.h index 1e38e5e04..bc412542e 100644 --- a/Source/Engine/Level/Actors/Camera.h +++ b/Source/Engine/Level/Actors/Camera.h @@ -173,7 +173,7 @@ public: /// /// World Position (XYZ) /// Returns true if the point is within the field of view - API_FUNCTION() bool CheckPointIsOnView(const Vector3& worldSpaceLocation) const; + API_FUNCTION() bool IsPointOnView(const Vector3& worldSpaceLocation) const; /// /// Converts the mouse position to 3D ray. From 0a91b8b360b76e0d17e7ae23ca44e66d7c0e962f Mon Sep 17 00:00:00 2001 From: Ruan Lucas <79365912+RuanLucasGD@users.noreply.github.com> Date: Mon, 3 Oct 2022 18:51:27 -0400 Subject: [PATCH 3/5] fix: isPointOnView doesn't work for long distance positions --- Source/Engine/Level/Actors/Camera.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Source/Engine/Level/Actors/Camera.cpp b/Source/Engine/Level/Actors/Camera.cpp index cd5d13702..ab695de5c 100644 --- a/Source/Engine/Level/Actors/Camera.cpp +++ b/Source/Engine/Level/Actors/Camera.cpp @@ -130,10 +130,23 @@ bool Camera::IsPointOnView(const Vector3& worldSpaceLocation) const return false; } + Vector3 cameraUp = mainCamera->GetTransform().GetUp(); + Vector3 cameraForward = mainCamera->GetTransform().GetForward(); + Vector3 directionToPosition = (worldSpaceLocation- mainCamera->GetPosition()).GetNormalized(); + + if (Vector3::Dot(cameraForward, directionToPosition) < 0) + { + return false; + } + + Quaternion lookAt = Quaternion::LookRotation(directionToPosition, cameraUp); + Vector3 lookAtDirection = lookAt * Vector3::Forward; + Vector3 newWorldLocation = mainCamera->GetPosition() + lookAtDirection; + Float2 windowSpace = Float2(); Float2 screenSize = Screen::GetSize(); - mainCamera->ProjectPoint(worldSpaceLocation, windowSpace); + mainCamera->ProjectPoint(newWorldLocation, windowSpace); return (windowSpace.X >= 0 && windowSpace.X <= screenSize.X) && (windowSpace.Y >= 0 && windowSpace.Y <= screenSize.Y); From 67c963f60dca9b51c618a92e227fc19241cf77cc Mon Sep 17 00:00:00 2001 From: Ruan Lucas <79365912+RuanLucasGD@users.noreply.github.com> Date: Mon, 10 Oct 2022 22:43:00 -0400 Subject: [PATCH 4/5] Optimization Method --- Source/Engine/Level/Actors/Camera.cpp | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/Source/Engine/Level/Actors/Camera.cpp b/Source/Engine/Level/Actors/Camera.cpp index ab695de5c..6ba7281c7 100644 --- a/Source/Engine/Level/Actors/Camera.cpp +++ b/Source/Engine/Level/Actors/Camera.cpp @@ -123,16 +123,9 @@ void Camera::ProjectPoint(const Vector3& worldSpaceLocation, Float2& cameraViewp bool Camera::IsPointOnView(const Vector3& worldSpaceLocation) const { - Camera* mainCamera = Camera::GetMainCamera(); - - if (!mainCamera) - { - return false; - } - - Vector3 cameraUp = mainCamera->GetTransform().GetUp(); - Vector3 cameraForward = mainCamera->GetTransform().GetForward(); - Vector3 directionToPosition = (worldSpaceLocation- mainCamera->GetPosition()).GetNormalized(); + Vector3 cameraUp = GetTransform().GetUp(); + Vector3 cameraForward = GetTransform().GetForward(); + Vector3 directionToPosition = (worldSpaceLocation - GetPosition()).GetNormalized(); if (Vector3::Dot(cameraForward, directionToPosition) < 0) { @@ -141,12 +134,12 @@ bool Camera::IsPointOnView(const Vector3& worldSpaceLocation) const Quaternion lookAt = Quaternion::LookRotation(directionToPosition, cameraUp); Vector3 lookAtDirection = lookAt * Vector3::Forward; - Vector3 newWorldLocation = mainCamera->GetPosition() + lookAtDirection; + Vector3 newWorldLocation = GetPosition() + lookAtDirection; Float2 windowSpace = Float2(); - Float2 screenSize = Screen::GetSize(); + Float2 screenSize = GetViewport().Size; - mainCamera->ProjectPoint(newWorldLocation, windowSpace); + ProjectPoint(newWorldLocation, windowSpace); return (windowSpace.X >= 0 && windowSpace.X <= screenSize.X) && (windowSpace.Y >= 0 && windowSpace.Y <= screenSize.Y); From 93dc57c08f9ca68e327d24886a3e603a77d45938 Mon Sep 17 00:00:00 2001 From: Ruan Lucas <79365912+RuanLucasGD@users.noreply.github.com> Date: Mon, 10 Oct 2022 23:10:25 -0400 Subject: [PATCH 5/5] remove unused include --- Source/Engine/Level/Actors/Camera.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Engine/Level/Actors/Camera.cpp b/Source/Engine/Level/Actors/Camera.cpp index 6ba7281c7..81c0851a9 100644 --- a/Source/Engine/Level/Actors/Camera.cpp +++ b/Source/Engine/Level/Actors/Camera.cpp @@ -5,7 +5,6 @@ #include "Engine/Core/Math/Viewport.h" #include "Engine/Content/Assets/Model.h" #include "Engine/Content/Content.h" -#include "Engine/Engine/Screen.h" #include "Engine/Serialization/Serialization.h" #if USE_EDITOR #include "Editor/Editor.h"