Added Editor Camera Arm function

This commit is contained in:
maverickdgg 2022-11-13 05:19:46 +08:00
parent e4e4894610
commit 4d0598a7f5
2 changed files with 48 additions and 58 deletions

View File

@ -60,64 +60,49 @@ namespace SHADE
camera.dirtyView = true; camera.dirtyView = true;
} }
UpdateCameraComponent(editorCamera); UpdateCameraComponent(editorCamera);
if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LEFT_ALT))
{
UpdateEditorArm(dt, true, SHVec3{ 0.0f });
}
UpdateEditorArm(dt, false, SHVec3{ 0.0f });
} }
void SHCameraSystem::EditorCameraUpdate::Execute(double dt) noexcept
{
SHCameraSystem* system = static_cast<SHCameraSystem*>(GetSystem());
auto& camera = system->editorCamera;
SHVec3 view, right, UP;
system->GetCameraAxis(camera, view, right, UP);
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::A)) void SHCameraSystem::UpdateEditorArm(double dt,bool active ,SHVec3 const& targetPos) noexcept
{
if (active == false)
{ {
//std::cout << "Camera movement: "<<right.x<<", " << right.y << std::endl; editorCameraArm.offset = SHVec3{0.0f};
camera.position -= right * dt * camera.movementSpeed; return;
camera.dirtyView = true;
}
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::D))
{
camera.position += right * dt * camera.movementSpeed;
camera.dirtyView = true;
}
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::W))
{
camera.position += view * dt * camera.movementSpeed;
camera.dirtyView = true;
}
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::S))
{
camera.position -= view * dt * camera.movementSpeed;
camera.dirtyView = true;
}
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::Q))
{
camera.position += UP * dt * camera.movementSpeed;
camera.dirtyView = true;
}
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::E))
{
camera.position -= UP * dt * camera.movementSpeed;
camera.dirtyView = true;
}
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::RMB))
{
double mouseX, mouseY;
SHInputManager::GetMouseVelocity(&mouseX,&mouseY);
//std::cout << camera.yaw << std::endl;
camera.pitch -= mouseY * dt * camera.turnSpeed.x;
camera.yaw -= mouseX * dt * camera.turnSpeed.y;
camera.dirtyView = true;
} }
//std::cout << "Camera position: " << camera.position.x << " " << camera.position.y << std::endl;
system->UpdateCameraComponent(system->editorCamera);
system->DecomposeViewMatrix(camera.viewMatrix, camera.pitch, camera.yaw, camera.roll, camera.position); double mouseX, mouseY;
} SHInputManager::GetMouseVelocity(&mouseX, &mouseY);
editorCameraArm.pitch -= mouseY * dt * editorCamera.turnSpeed.x;
editorCameraArm.yaw -= mouseX * dt * editorCamera.turnSpeed.y;
editorCameraArm.armLength += SHInputManager::GetMouseWheelVerticalDelta() * dt;
if (editorCameraArm.armLength < 1.0f)
editorCameraArm.armLength = 1.0f;
UpdatePivotArmComponent(editorCameraArm);
editorCamera.offset = editorCameraArm.GetOffset();
CameraLookAt(editorCamera, targetPos);
}
void SHCameraSystem::Init(void) void SHCameraSystem::Init(void)
{ {
@ -164,6 +149,9 @@ namespace SHADE
void SHCameraSystem::UpdateCameraComponent(SHCameraComponent& camera) noexcept void SHCameraSystem::UpdateCameraComponent(SHCameraComponent& camera) noexcept
{ {
if (camera.isActive == false)
return;
if (SHComponentManager::HasComponent<SHTransformComponent>(camera.GetEID()) == true && &camera != &editorCamera) if (SHComponentManager::HasComponent<SHTransformComponent>(camera.GetEID()) == true && &camera != &editorCamera)
{ {
auto transform = SHComponentManager::GetComponent<SHTransformComponent>(camera.GetEID()); auto transform = SHComponentManager::GetComponent<SHTransformComponent>(camera.GetEID());
@ -183,11 +171,17 @@ namespace SHADE
if (SHComponentManager::HasComponent<SHCameraArmComponent>(camera.GetEID())) if (SHComponentManager::HasComponent<SHCameraArmComponent>(camera.GetEID()))
{ {
auto arm = SHComponentManager::GetComponent<SHCameraArmComponent>(camera.GetEID()); auto arm = SHComponentManager::GetComponent<SHCameraArmComponent>(camera.GetEID());
camera.offset = arm->GetOffset(); if (arm->isActive == true)
if(arm->lookAtCameraOrigin) {
CameraLookAt(camera, camera.position); camera.offset = arm->GetOffset();
if (arm->lookAtCameraOrigin)
CameraLookAt(camera, camera.position);
}
} }
SHVec3 view, right, UP; SHVec3 view, right, UP;

View File

@ -5,6 +5,7 @@
#include "ECS_Base/System/SHSystemRoutine.h" #include "ECS_Base/System/SHSystemRoutine.h"
#include "Resource/SHResourceLibrary.h" #include "Resource/SHResourceLibrary.h"
#include "SHCameraDirector.h" #include "SHCameraDirector.h"
#include "SHCameraArmComponent.h"
#include "SH_API.h" #include "SH_API.h"
namespace SHADE namespace SHADE
@ -18,6 +19,7 @@ namespace SHADE
//A camera component that represents editor camera. //A camera component that represents editor camera.
//This is not tied to any entity. Hence this EID should not be used. //This is not tied to any entity. Hence this EID should not be used.
SHCameraComponent editorCamera; SHCameraComponent editorCamera;
SHCameraArmComponent editorCameraArm;
SHResourceLibrary<SHCameraDirector> directorLibrary; SHResourceLibrary<SHCameraDirector> directorLibrary;
std::vector<DirectorHandle> directorHandleList; std::vector<DirectorHandle> directorHandleList;
@ -34,14 +36,7 @@ namespace SHADE
void Init (void); void Init (void);
void Exit (void); void Exit (void);
class SH_API EditorCameraUpdate final : public SHSystemRoutine
{
public:
EditorCameraUpdate() : SHSystemRoutine("Editor Camera Update", true) { };
virtual void Execute(double dt) noexcept override final;
};
friend class EditorCameraUpdate; friend class EditorCameraUpdate;
class SH_API CameraSystemUpdate final: public SHSystemRoutine class SH_API CameraSystemUpdate final: public SHSystemRoutine
@ -63,6 +58,7 @@ namespace SHADE
void DecomposeViewMatrix(SHMatrix const& matrix, float& pitch, float& yaw, float& roll, SHVec3& pos) noexcept; void DecomposeViewMatrix(SHMatrix const& matrix, float& pitch, float& yaw, float& roll, SHVec3& pos) noexcept;
void SetCameraViewMatrix(SHCameraComponent& camera, SHMatrix const& viewMatrix) noexcept; void SetCameraViewMatrix(SHCameraComponent& camera, SHMatrix const& viewMatrix) noexcept;
void CameraLookAt(SHCameraComponent& camera, SHVec3 target) noexcept; void CameraLookAt(SHCameraComponent& camera, SHVec3 target) noexcept;
void UpdateEditorArm(double dt,bool active ,SHVec3 const& targetPos) noexcept;
}; };