Added preview window when selecting a camera in the scene

This commit is contained in:
Antoine Pilote
2024-09-04 20:31:43 -04:00
parent e1813870a2
commit e607b99191
9 changed files with 94 additions and 10 deletions

View File

@@ -68,6 +68,8 @@ namespace Nuake {
EditorSelection EditorInterface::Selection;
int SelectedViewport = 0;
bool displayVirtualCameraOverlay = false;
Ref<FrameBuffer> virtualCamera;
glm::vec3 DepthToWorldPosition(const glm::vec2& pixelPos, float depth, const glm::mat4& projectionMatrix, const glm::mat4& viewMatrix, const glm::vec2& viewportSize)
{
@@ -120,6 +122,8 @@ namespace Nuake {
using namespace Nuake::StaticResources;
ImGui::LoadIniSettingsFromMemory((const char*)StaticResources::Resources_default_layout_ini);
virtualCamera = CreateRef<FrameBuffer>(true, Vector2{ 1280, 720 });
virtualCamera->SetTexture(CreateRef<Texture>(Vector2{ 1280, 720 }, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE), GL_COLOR_ATTACHMENT0);
//ScriptingContext::Get().Initialize();
}
@@ -2608,6 +2612,60 @@ namespace Nuake {
ImGui::PopStyleVar();
ImGui::End();
corner = 2;
if (Selection.Type == EditorSelectionType::Entity && Selection.Entity.IsValid() && Selection.Entity.HasComponent<CameraComponent>())
{
window_flags |= ImGuiWindowFlags_NoMove;
viewport = ImGui::GetWindowViewport();
work_area_pos = ImGui::GetCurrentWindow()->Pos; // Instead of using viewport->Pos we use GetWorkPos() to avoid menu bars, if any!
work_area_size = ImGui::GetCurrentWindow()->Size;
window_pos = ImVec2((corner & 1) ? (work_area_pos.x + work_area_size.x - DISTANCE) : (work_area_pos.x + DISTANCE), (corner & 2) ? (work_area_pos.y + work_area_size.y - DISTANCE) : (work_area_pos.y + DISTANCE));
window_pos_pivot = ImVec2((corner & 1) ? 1.0f : 0.0f, (corner & 2) ? 1.0f : 0.0f);
ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot);
ImGui::SetNextWindowViewport(viewport->ID);
ImGui::SetNextWindowBgAlpha(0.35f); // Transparent background
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 32.0f);
if (ImGui::Begin("VirtualViewport", &m_ShowOverlay, window_flags))
{
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(2, 2));
ImGui::PushStyleColor(ImGuiCol_Button, IM_COL32(0, 0, 0, 0));
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 100);
ImGui::PushStyleColor(ImGuiCol_FrameBg, IM_COL32(20, 20, 20, 0));
ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, IM_COL32(20, 20, 20, 60));
ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, IM_COL32(33, 33, 33, 45));
CameraComponent& camera = Selection.Entity.GetComponent<CameraComponent>();
Ref<Camera> cam = camera.CameraInstance;
auto& transform = Selection.Entity.GetComponent<TransformComponent>();
const Quat& globalRotation = transform.GetGlobalRotation();
const Matrix4& translationMatrix = glm::translate(Matrix4(1.0f), transform.GetGlobalPosition());
const Matrix4& rotationMatrix = glm::mat4_cast(globalRotation);
const Vector4& forward = Vector4(0, 0, -1, 1);
const Vector4& globalForward = rotationMatrix * forward;
const Vector4& right = Vector4(1, 0, 0, 1);
const Vector4& globalRight = rotationMatrix * right;
cam->Direction = globalForward;
cam->Right = globalRight;
auto sceneRenderer = Engine::GetCurrentScene()->m_SceneRenderer;
sceneRenderer->BeginRenderScene(cam->GetPerspective(), cam->GetTransform(), transform.GlobalTranslation);
sceneRenderer->RenderScene(*Engine::GetCurrentScene().get(), *virtualCamera.get());
virtualCamera->Clear();
ImGui::Image((void*)virtualCamera->GetTexture()->GetID(), { 1280, 720 }, { 0, 1 }, {1, 0});
ImGui::PopStyleVar(2);
ImGui::PopStyleColor(4);
}
ImGui::PopStyleVar();
ImGui::End();
}
}
void NewProject()
@@ -3025,7 +3083,6 @@ namespace Nuake {
}
auto& editorCam = Engine::GetCurrentScene()->m_EditorCamera;
editorCam->Update(ts, m_IsHoveringViewport && m_IsViewportFocused);
const bool entityIsSelected = Selection.Type == EditorSelectionType::Entity && Selection.Entity.IsValid();
@@ -3035,6 +3092,15 @@ namespace Nuake {
editorCam->TargetPos = Selection.Entity.GetComponent<TransformComponent>().GetGlobalPosition();
}
if (entityIsSelected && Selection.Type == EditorSelectionType::Entity && Selection.Entity.IsValid() && Selection.Entity.HasComponent<CameraComponent>())
{
displayVirtualCameraOverlay = true;
}
else
{
displayVirtualCameraOverlay = false;
}
if (entityIsSelected && Input::IsKeyPressed(Key::ESCAPE))
{
Selection = EditorSelection();

View File

@@ -13,8 +13,13 @@
#include <Engine.h>
#include <src/Resource/Prefab.h>
using namespace Nuake;
EditorSelectionPanel::EditorSelectionPanel()
{
virtualScene = CreateRef<Scene>();
virtualScene->SetName("Virtual Scene");
virtualScene->CreateEntity("Camera").AddComponent<CameraComponent>();
}
void EditorSelectionPanel::ResolveFile(Ref<Nuake::File> file)
@@ -228,6 +233,11 @@ void EditorSelectionPanel::DrawResource(Nuake::Resource resource)
}
void EditorSelectionPanel::DrawPrefabPanel(Ref<Nuake::Prefab> prefab)
{
}
void EditorSelectionPanel::DrawMaterialPanel(Ref<Nuake::Material> material)
{
using namespace Nuake;

View File

@@ -27,8 +27,14 @@
#include "../ComponentsPanel/NavMeshVolumePanel.h"
#include <src/Scene/Components/WrenScriptComponent.h>
#include <src/Resource/Prefab.h>
namespace Nuake
{
class Scene;
}
class EditorSelectionPanel
{
private:
@@ -56,6 +62,7 @@ private:
Ref<Nuake::File> currentFile;
Ref<Nuake::Resource> selectedResource;
Ref<Nuake::Scene> virtualScene;
public:
EditorSelectionPanel();
@@ -67,7 +74,7 @@ public:
void DrawFile(Ref<Nuake::File> file);
void DrawResource(Nuake::Resource resource);
void DrawPrefabPanel(Ref<Nuake::Prefab> prefab);
private:
void ResolveFile(Ref<Nuake::File> file);
void DrawMaterialPanel(Ref<Nuake::Material> material);

View File

@@ -8,6 +8,8 @@
#include <src/Threading/JobSystem.h>
#include <src/UI/ImUI.h>
#include <src/FileSystem/FileDialog.h>
#include <src/Core/String.h>
#include <src/Resource/Project.h>
#include <imgui/imgui.h>
@@ -16,6 +18,7 @@
#include <sstream>
#include <regex>
using namespace Nuake;
void MapImporterWindow::Draw()
{

View File

@@ -122,7 +122,7 @@ ProjectSettingsCategoryWindowViewport::ProjectSettingsCategoryWindowViewport(Ref
void ProjectSettingsCategoryWindowViewport::Draw()
{
ImGui::DragFloat("Outline Radius", &m_Project->Settings.OutlineRadius, 0.1f, 1.0f, 10.0f);
ImGui::DragFloat("Outline Radius", &m_Project->Settings.OutlineRadius, 0.1f, 1.0f, 90.0f);
ImGui::DragFloat("Gizmo Size", &m_Project->Settings.GizmoSize, 0.01f, 0.05f, 0.5f);
ImGui::Separator();
ImGui::Checkbox("Smooth Camera", &m_Project->Settings.SmoothCamera);

View File

@@ -15,7 +15,7 @@ File::File(Ref<Directory> parentDir, const std::string& fullPath, const std::str
FileType File::GetFileType() const
{
const std::string_view ext = GetExtension();
const std::string ext = GetExtension();
if (ext == ".png" || ext == ".jpg")
{
return FileType::Image;

View File

@@ -73,7 +73,7 @@ namespace Nuake
return this->Name;
}
bool Scene::SetName(std::string& newName)
bool Scene::SetName(const std::string& newName)
{
if (newName == "")
return false;
@@ -317,7 +317,6 @@ namespace Nuake
cam = camera.CameraInstance;
cam->Translation = transform.GetGlobalPosition();
break;
}
if (!cam)

View File

@@ -56,7 +56,7 @@ namespace Nuake
void Draw(FrameBuffer& framebuffer, const Matrix4& projection, const Matrix4& view);
std::string GetName();
bool SetName(std::string& newName);
bool SetName(const std::string& newName);
Ref<Camera> GetCurrentCamera();

View File

@@ -149,9 +149,6 @@ void Window::Draw()
cam->AspectRatio = size.x / size.y;
float resolutionScale = glm::clamp(Engine::GetProject()->Settings.ResolutionScale, 0.5f, 2.0f);
this->scene->m_EditorCamera->OnWindowResize(size.x * resolutionScale, size.y * resolutionScale);
if (Engine::IsPlayMode())
{
ZoneScopedN("PIE Draw");
@@ -160,6 +157,8 @@ void Window::Draw()
else
{
ZoneScopedN("Non-playmode Draw");
float resolutionScale = glm::clamp(Engine::GetProject()->Settings.ResolutionScale, 0.5f, 2.0f);
this->scene->m_EditorCamera->OnWindowResize(size.x * resolutionScale, size.y * resolutionScale);
this->scene->Draw(*this->framebuffer.get(), this->scene->m_EditorCamera->GetPerspective(), this->scene->m_EditorCamera->GetTransform());
}