From 7b0afff07341f71422198dfe45bb91b49e0fa42d Mon Sep 17 00:00:00 2001 From: Evgeny Prikazchikov Date: Mon, 8 Jun 2026 18:16:50 +0300 Subject: [PATCH] Editor: Fallback camera is active at the start of the simulation #1343 --- engine/includes/components/camera.h | 5 +++ engine/src/components/camera.cpp | 44 +++++++++++++++++-------- engine/src/editor/viewport/viewport.cpp | 12 +++---- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/engine/includes/components/camera.h b/engine/includes/components/camera.h index f3e76991e..2723dbda6 100644 --- a/engine/includes/components/camera.h +++ b/engine/includes/components/camera.h @@ -30,6 +30,7 @@ class ENGINE_EXPORT Camera : public Component { public: Camera(); + ~Camera(); Matrix4 viewMatrix() const; Matrix4 projectionMatrix() const; @@ -66,6 +67,8 @@ class ENGINE_EXPORT Camera : public Component { static Camera *current(); static void setCurrent(Camera *current); + static Camera *findActiveCamera(World *world); + Vector3 project(const Vector3 &worldSpace) const; Vector3 unproject(const Vector3 &screenSpace) const; @@ -101,6 +104,8 @@ class ENGINE_EXPORT Camera : public Component { bool m_screen; + bool m_main; + mutable bool m_dirty; }; diff --git a/engine/src/components/camera.cpp b/engine/src/components/camera.cpp index 53557218a..3c1dd36f8 100644 --- a/engine/src/components/camera.cpp +++ b/engine/src/components/camera.cpp @@ -28,8 +28,15 @@ Camera::Camera() : m_screen(false), m_dirty(true) { + static uint32_t hash = Mathf::hashString("cameras"); + addTagByHash(hash); } +Camera::~Camera() { + if(s_currentCamera == this) { + s_currentCamera = nullptr; + } +} /*! Returns view matrix for the camera. */ @@ -259,21 +266,15 @@ void Camera::setScreenSpace(bool mode) { */ Camera *Camera::current() { if(s_currentCamera == nullptr || !s_currentCamera->isEnabled() || !s_currentCamera->actor()->isEnabled()) { - s_currentCamera = nullptr; - for(auto it : Engine::world()->findChildren()) { - if(it->isEnabled() && it->actor()->isEnabled()) { // Get first active Camera - s_currentCamera = it; - break; - } - } + s_currentCamera = findActiveCamera(Engine::world()); if(s_currentCamera == nullptr) { - static Camera *reserveCamera = nullptr; - if(reserveCamera == nullptr) { - Actor *cameraActor = Engine::composeActor("ReserveCamera", Engine::world()); - reserveCamera = cameraActor->getComponent(); + static Camera *fallbackCamera = nullptr; + if(fallbackCamera == nullptr) { + Actor *cameraActor = Engine::composeActor("FallbackCamera", nullptr); + fallbackCamera = cameraActor->getComponent(); } - s_currentCamera = reserveCamera; + s_currentCamera = fallbackCamera; s_currentCamera->actor()->setEnabled(true); s_currentCamera->setEnabled(true); } @@ -287,7 +288,24 @@ Camera *Camera::current() { void Camera::setCurrent(Camera *current) { s_currentCamera = current; } - +/*! + Returns a first active camera in the \a world +*/ +Camera *Camera::findActiveCamera(World *world) { + static uint32_t hash = Mathf::hashString("cameras"); + for(auto scene : world->scenes()) { + for(auto it : scene->getObjectsInGroupByHash(hash)) { + Camera *camera = static_cast(it); + if(camera->isEnabled() && camera->actor()->isEnabled()) { // Get first active Camera + return camera; + } + } + } + return nullptr; +} +/*! + Returns a set of frustum planes. +*/ Frustum Camera::frustum() const { Transform *t = transform(); diff --git a/engine/src/editor/viewport/viewport.cpp b/engine/src/editor/viewport/viewport.cpp index 536f7ced8..65c82f3f4 100644 --- a/engine/src/editor/viewport/viewport.cpp +++ b/engine/src/editor/viewport/viewport.cpp @@ -174,13 +174,7 @@ void Viewport::onDraw() { Engine::renderSystem()->setPipelineContext(m_pipelineContext); if(m_gameView) { - for(auto it : m_world->findChildren()) { - if(it->isEnabled() && it->actor()->isEnabled()) { // Get first active Camera - Camera::setCurrent(it); - break; - } - } - + Camera::setCurrent(Camera::findActiveCamera(Engine::world())); Engine::update(m_world); if(!m_gamePaused && isFocused()) { @@ -217,7 +211,9 @@ void Viewport::onDraw() { m_controller->update(); } onCursorSet(instance.mouseCursor()); - instance.update(); + if(!m_gameView) { + instance.update(); + } } if(m_screenInProgress && m_color) {