From 93fc8a93563c399ec2fdbc60984b36caa0aae861 Mon Sep 17 00:00:00 2001 From: VTui22 <106135221+VTui22@users.noreply.github.com> Date: Fri, 8 Sep 2023 17:55:52 +0800 Subject: [PATCH] Camera point focus (#379) --- Engine/Source/Editor/UILayers/EntityList.cpp | 10 ++++++++++ .../Source/Runtime/Display/CameraController.cpp | 15 +++++++++++---- Engine/Source/Runtime/Display/CameraController.h | 7 ++++--- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Engine/Source/Editor/UILayers/EntityList.cpp b/Engine/Source/Editor/UILayers/EntityList.cpp index 71323d2a..03b10e8c 100644 --- a/Engine/Source/Editor/UILayers/EntityList.cpp +++ b/Engine/Source/Editor/UILayers/EntityList.cpp @@ -342,6 +342,16 @@ void EntityList::DrawEntity(engine::SceneWorld* pSceneWorld, engine::Entity enti } } } + else + { + if (engine::TransformComponent* pTransform = pSceneWorld->GetTransformComponent(entity)) + { + if (m_pCameraController) + { + m_pCameraController->CameraFocus(pTransform->GetTransform().GetTranslation()); + } + } + } } } diff --git a/Engine/Source/Runtime/Display/CameraController.cpp b/Engine/Source/Runtime/Display/CameraController.cpp index 97b3f983..933b32be 100644 --- a/Engine/Source/Runtime/Display/CameraController.cpp +++ b/Engine/Source/Runtime/Display/CameraController.cpp @@ -365,10 +365,21 @@ void CameraController::CameraFocus(const cd::AABB& aabb) m_movementSpeed = aabb.Size().Length() * 1.5f; } +void CameraController::CameraFocus(const cd::Vec3f& position) +{ + m_isMoving = true; + m_eyeDestination = position - m_lookAt * m_distanceFromLookAt; +} + void CameraController::Moving() { if (m_isMoving) { + if (cd::Math::IsSmallThan((m_eye - m_eyeDestination).Length(), 0.01f)) + { + m_isMoving = false; + return; + } cd::Direction eyeMove = (m_eye - m_eyeDestination).Normalize(); float stepDistance = (m_eye - m_eyeDestination).Length() / 5.0f; m_eye = m_eye - eyeMove * stepDistance; @@ -376,10 +387,6 @@ void CameraController::Moving() SynchronizeTrackingCamera(); ControllerToCamera(); - if (cd::Math::IsSmallThan((m_eye - m_eyeDestination).Length(), 0.01f)) - { - m_isMoving = false; - } } } diff --git a/Engine/Source/Runtime/Display/CameraController.h b/Engine/Source/Runtime/Display/CameraController.h index 1c0ae766..fdca2e55 100644 --- a/Engine/Source/Runtime/Display/CameraController.h +++ b/Engine/Source/Runtime/Display/CameraController.h @@ -55,6 +55,7 @@ class CameraController final // Double Click entity,camera will focus void CameraFocus(const cd::AABB& aabb); + void CameraFocus(const cd::Vec3f& position); // Implement the effect of a translation animation. void Moving(); @@ -94,9 +95,9 @@ class CameraController final float m_mouseScroll = 0.0f; cd::Vec3f m_lookAtPoint = cd::Vec3f::Zero(); - cd::Vec3f m_lookAt; - cd::Vec3f m_up; - cd::Vec3f m_eye; + cd::Vec3f m_lookAt = cd::Vec3f(0.0f, 1.0f, 0.0f); + cd::Vec3f m_up = cd::Vec3f(0.0f, 0.0f, 1.0f); + cd::Vec3f m_eye = cd::Vec3f::Zero(); cd::Vec3f m_eyeDestination; // This is for focusing animation cd::Vec3f m_lookAtDestination;