From f18be64eae972fd3d6fc7d0f2996c764fe4c4e4c Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Thu, 18 Jan 2024 16:44:52 -0500 Subject: [PATCH 01/26] Casting change --- Editor/src/Windows/FileSystemUI.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Editor/src/Windows/FileSystemUI.cpp b/Editor/src/Windows/FileSystemUI.cpp index 79064785..2791443e 100644 --- a/Editor/src/Windows/FileSystemUI.cpp +++ b/Editor/src/Windows/FileSystemUI.cpp @@ -355,7 +355,7 @@ namespace Nuake } ImGui::SetCursorPos(prevCursor); - ImGui::Image((ImTextureID)textureImage->GetID(), ImVec2(100, 100), ImVec2(0, 1), ImVec2(1, 0)); + ImGui::Image(reinterpret_cast(textureImage->GetID()), ImVec2(100, 100), ImVec2(0, 1), ImVec2(1, 0)); ImGui::PopStyleVar(); auto imguiStyle = ImGui::GetStyle(); From ccdb305b54b3a4e7fbcf6e3a261dc8decfc3a695 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sun, 10 Mar 2024 13:00:34 -0400 Subject: [PATCH 02/26] Added basic job system for async --- Editor/src/Windows/EditorInterface.cpp | 18 +++++++-- Nuake/src/Scripting/ScriptingEngineNet.cpp | 1 + Nuake/src/Threading/Job.cpp | 25 +++++++++++++ Nuake/src/Threading/Job.h | 25 +++++++++++++ Nuake/src/Threading/JobSystem.h | 43 ++++++++++++++++++++++ 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 Nuake/src/Threading/Job.cpp create mode 100644 Nuake/src/Threading/Job.h create mode 100644 Nuake/src/Threading/JobSystem.h diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 83e19b36..46d4e009 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -53,6 +53,7 @@ #include #include +#include namespace Nuake { @@ -127,10 +128,14 @@ namespace Nuake { { if (ImGui::Button(ICON_FA_PLAY, ImVec2(30, 30)) || (Input::IsKeyPressed(GLFW_KEY_F5))) { - SceneSnapshot = Engine::GetCurrentScene()->Copy(); + this->SceneSnapshot = Engine::GetCurrentScene()->Copy(); - ScriptingEngineNet::Get().BuildProjectAssembly(Engine::GetProject()); - Engine::EnterPlayMode(); + auto job = [this]() + { + ScriptingEngineNet::Get().BuildProjectAssembly(Engine::GetProject()); + }; + + JobSystem::Get().Dispatch(job, []() { Engine::EnterPlayMode(); }); } if (ImGui::BeginItemTooltip()) @@ -240,7 +245,12 @@ namespace Nuake { if (ImGui::Button(ICON_FA_HAMMER, ImVec2(30, 30))) { - Nuake::ScriptingEngineNet::Get().BuildProjectAssembly(Engine::GetProject()); + JobSystem::Get().Dispatch([]() + { + Nuake::ScriptingEngineNet::Get().BuildProjectAssembly(Engine::GetProject()); + }, + []() {} + ); } if (ImGui::BeginItemTooltip()) diff --git a/Nuake/src/Scripting/ScriptingEngineNet.cpp b/Nuake/src/Scripting/ScriptingEngineNet.cpp index 13bd272b..3544a1b0 100644 --- a/Nuake/src/Scripting/ScriptingEngineNet.cpp +++ b/Nuake/src/Scripting/ScriptingEngineNet.cpp @@ -3,6 +3,7 @@ #include "src/Core/Logger.h" #include "src/Core/FileSystem.h" #include "src/Core/OS.h" +#include "src/Threading/JobSystem.h" #include "src/Resource/Project.h" #include "src/Scene/Components/NetScriptComponent.h" diff --git a/Nuake/src/Threading/Job.cpp b/Nuake/src/Threading/Job.cpp new file mode 100644 index 00000000..36b18160 --- /dev/null +++ b/Nuake/src/Threading/Job.cpp @@ -0,0 +1,25 @@ +#include "Job.h" + +namespace Nuake { + + Job::Job(std::function job, std::function end) + : m_Job(job) + , m_End(end) + { + m_End = end; + + m_Thread = std::thread([this, job]() + { + job(); + m_IsDone = true; + }); + } + + void Job::End() + { + if (m_End) + { + m_End(); + } + } +} \ No newline at end of file diff --git a/Nuake/src/Threading/Job.h b/Nuake/src/Threading/Job.h new file mode 100644 index 00000000..c4f3bd0e --- /dev/null +++ b/Nuake/src/Threading/Job.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include + +namespace Nuake { + + class Job + { + public: + Job(std::function job, std::function end); + Job(const Job&) = delete; + Job& operator=(const Job&) = delete; + ~Job() { m_Thread.join(); } + bool IsDone() { return m_IsDone; } + + void End(); + private: + std::thread m_Thread; + std::atomic m_IsDone; + std::function m_Job; + std::function m_End; + }; +} \ No newline at end of file diff --git a/Nuake/src/Threading/JobSystem.h b/Nuake/src/Threading/JobSystem.h new file mode 100644 index 00000000..a1b39a29 --- /dev/null +++ b/Nuake/src/Threading/JobSystem.h @@ -0,0 +1,43 @@ +#pragma once +#include "Job.h" + +namespace Nuake { + + class JobSystem + { + private: + std::vector> m_Jobs; + + public: + + JobSystem() = default; + ~JobSystem() = default; + + static JobSystem& Get() + { + static JobSystem instance; + return instance; + } + + void Dispatch(std::function job, std::function end) + { + m_Jobs.push_back(std::make_unique(job, end)); + } + + void Update() + { + for (auto it = m_Jobs.begin(); it != m_Jobs.end();) + { + if (it->get()->IsDone()) + { + it->get()->End(); + it = m_Jobs.erase(it); + } + else + { + ++it; + } + } + } + }; +} \ No newline at end of file From 1fa0fd635fe426ab8252a1a45a1d32808bac14fb Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 16 Mar 2024 11:26:32 -0400 Subject: [PATCH 03/26] Made selection outline during play mode impossible and made outline outside of selected game object --- Editor/src/Windows/EditorInterface.cpp | 2 +- Editor/src/Windows/EditorSelectionPanel.cpp | 2 + Nuake/Engine.cpp | 4 +- Nuake/src/Resource/StaticResources.cpp | 135 +++++++++++--------- Resources/Images/project_icon.png | Bin 0 -> 719 bytes Resources/Shaders/outline.shader | 14 +- 6 files changed, 95 insertions(+), 62 deletions(-) create mode 100644 Resources/Images/project_icon.png diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 46d4e009..591493c9 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -370,7 +370,7 @@ namespace Nuake { m_IsViewportFocused = ImGui::IsWindowFocused(); - if (ImGui::GetIO().WantCaptureMouse && m_IsHoveringViewport && Input::IsMouseButtonPressed(GLFW_MOUSE_BUTTON_1) && !ImGuizmo::IsUsing() && m_IsViewportFocused) + if (!Engine::IsPlayMode() && ImGui::GetIO().WantCaptureMouse && m_IsHoveringViewport && Input::IsMouseButtonPressed(GLFW_MOUSE_BUTTON_1) && !ImGuizmo::IsUsing() && m_IsViewportFocused) { const auto windowPosNuake = Vector2(windowPos.x, windowPos.y); diff --git a/Editor/src/Windows/EditorSelectionPanel.cpp b/Editor/src/Windows/EditorSelectionPanel.cpp index ce15b92e..7856d7e1 100644 --- a/Editor/src/Windows/EditorSelectionPanel.cpp +++ b/Editor/src/Windows/EditorSelectionPanel.cpp @@ -170,6 +170,8 @@ void EditorSelectionPanel::DrawAddComponentMenu(Nuake::Entity entity) MenuItemComponent("Quake map", QuakeMapComponent); ImGui::Separator(); MenuItemComponent("Audio Emitter", AudioEmitterComponent); + ImGui::Separator(); + MenuItemComponent("Path", AudioEmitterComponent); ImGui::EndPopup(); } ImGui::Separator(); diff --git a/Nuake/Engine.cpp b/Nuake/Engine.cpp index f323daf7..2cbe34a5 100644 --- a/Nuake/Engine.cpp +++ b/Nuake/Engine.cpp @@ -6,7 +6,7 @@ #include "src/Core/FileSystem.h" #include "src/Scripting/ScriptingEngine.h" #include "src/Audio/AudioManager.h" - +#include "src/Threading/JobSystem.h" #include "src/Rendering/Renderer.h" #include "src/Rendering/Renderer2D.h" @@ -41,6 +41,8 @@ namespace Nuake void Engine::Tick() { + JobSystem::Get().Update(); + s_Time = static_cast(glfwGetTime()); s_TimeStep = s_Time - s_LastFrameTime; s_LastFrameTime = s_Time; diff --git a/Nuake/src/Resource/StaticResources.cpp b/Nuake/src/Resource/StaticResources.cpp index 92cb0f02..7da9197f 100644 --- a/Nuake/src/Resource/StaticResources.cpp +++ b/Nuake/src/Resource/StaticResources.cpp @@ -235391,66 +235391,85 @@ const std::string Resources_Shaders_outline_shader_path = R"(Resources/Shaders/o 0x61, 0x74, 0x20, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x20, 0x3d, 0x20, 0x34, 0x2e, 0x66, 0x3b, 0x0d, 0x0a, 0x09, 0x76, 0x65, 0x63, 0x32, 0x20, 0x75, 0x76, 0x20, 0x3d, 0x20, 0x61, 0x5f, 0x55, 0x56, 0x3b, 0x0d, 0x0a, - 0x20, 0x20, 0x20, 0x20, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x2f, 0x2f, - 0x20, 0x43, 0x6f, 0x72, 0x72, 0x65, 0x63, 0x74, 0x20, 0x61, 0x73, 0x70, - 0x65, 0x63, 0x74, 0x20, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x0d, 0x0a, 0x20, - 0x20, 0x20, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x61, 0x73, 0x70, 0x65, - 0x63, 0x74, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x20, 0x2f, 0x20, 0x76, - 0x65, 0x63, 0x32, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, - 0x69, 0x7a, 0x65, 0x28, 0x75, 0x5f, 0x45, 0x6e, 0x74, 0x69, 0x74, 0x79, - 0x54, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x30, 0x29, 0x29, - 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x0d, 0x0a, 0x09, 0x76, 0x65, - 0x63, 0x34, 0x20, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, - 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28, 0x30, 0x2e, 0x30, 0x2c, - 0x20, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x30, - 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0d, 0x0a, 0x09, 0x66, 0x6f, 0x72, 0x20, - 0x28, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x69, 0x20, 0x3d, 0x20, 0x30, - 0x2e, 0x30, 0x3b, 0x20, 0x69, 0x20, 0x3c, 0x20, 0x54, 0x41, 0x55, 0x3b, - 0x20, 0x69, 0x20, 0x2b, 0x3d, 0x20, 0x54, 0x41, 0x55, 0x20, 0x2f, 0x20, - 0x73, 0x74, 0x65, 0x70, 0x73, 0x29, 0x20, 0x0d, 0x0a, 0x20, 0x20, 0x20, - 0x20, 0x7b, 0x0d, 0x0a, 0x09, 0x09, 0x2f, 0x2f, 0x20, 0x53, 0x61, 0x6d, - 0x70, 0x6c, 0x65, 0x20, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, - 0x20, 0x61, 0x20, 0x63, 0x69, 0x72, 0x63, 0x75, 0x6c, 0x61, 0x72, 0x20, - 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x0d, 0x0a, 0x20, 0x20, 0x20, - 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x6f, 0x66, - 0x66, 0x73, 0x65, 0x74, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x32, 0x28, - 0x73, 0x69, 0x6e, 0x28, 0x69, 0x29, 0x2c, 0x20, 0x63, 0x6f, 0x73, 0x28, - 0x69, 0x29, 0x29, 0x20, 0x2a, 0x20, 0x61, 0x73, 0x70, 0x65, 0x63, 0x74, - 0x20, 0x2a, 0x20, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x3b, 0x0d, 0x0a, - 0x09, 0x09, 0x75, 0x69, 0x6e, 0x74, 0x20, 0x63, 0x6f, 0x6c, 0x20, 0x3d, - 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x28, 0x75, 0x5f, 0x45, - 0x6e, 0x74, 0x69, 0x74, 0x79, 0x54, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, - 0x2c, 0x20, 0x75, 0x76, 0x20, 0x2b, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, - 0x74, 0x29, 0x2e, 0x72, 0x3b, 0x0d, 0x0a, 0x09, 0x09, 0x0d, 0x0a, 0x09, - 0x09, 0x2f, 0x2f, 0x20, 0x4d, 0x69, 0x78, 0x20, 0x6f, 0x75, 0x74, 0x6c, - 0x69, 0x6e, 0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x62, 0x61, 0x63, - 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x0d, 0x0a, 0x09, 0x09, 0x66, - 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x20, 0x3d, - 0x20, 0x73, 0x6d, 0x6f, 0x6f, 0x74, 0x68, 0x73, 0x74, 0x65, 0x70, 0x28, - 0x30, 0x2e, 0x35, 0x2c, 0x20, 0x30, 0x2e, 0x37, 0x2c, 0x20, 0x69, 0x6e, - 0x74, 0x28, 0x63, 0x6f, 0x6c, 0x20, 0x21, 0x3d, 0x20, 0x74, 0x61, 0x72, - 0x67, 0x65, 0x74, 0x29, 0x20, 0x2a, 0x20, 0x31, 0x30, 0x2e, 0x30, 0x66, - 0x29, 0x3b, 0x0d, 0x0a, 0x09, 0x09, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, - 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, 0x28, 0x66, 0x72, - 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x75, 0x5f, 0x4f, - 0x75, 0x74, 0x6c, 0x69, 0x6e, 0x65, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, - 0x20, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x29, 0x3b, 0x0d, 0x0a, 0x09, 0x7d, - 0x0d, 0x0a, 0x09, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x28, - 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2e, 0x61, 0x20, - 0x3e, 0x20, 0x30, 0x2e, 0x31, 0x29, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, - 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, - 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2e, 0x61, 0x20, 0x3d, - 0x20, 0x31, 0x2e, 0x30, 0x66, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, - 0x7d, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x0d, 0x0a, 0x20, 0x20, 0x20, - 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, - 0x20, 0x6d, 0x69, 0x78, 0x28, 0x76, 0x65, 0x63, 0x34, 0x28, 0x30, 0x29, - 0x2c, 0x20, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, - 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x28, 0x75, 0x5f, 0x45, - 0x6e, 0x74, 0x69, 0x74, 0x79, 0x54, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, - 0x2c, 0x20, 0x75, 0x76, 0x29, 0x2e, 0x72, 0x20, 0x3d, 0x3d, 0x20, 0x74, + 0x20, 0x20, 0x20, 0x20, 0x0d, 0x0a, 0x09, 0x2f, 0x2f, 0x20, 0x73, 0x61, + 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x6d, 0x69, 0x64, 0x64, 0x6c, 0x65, 0x0d, + 0x0a, 0x09, 0x75, 0x69, 0x6e, 0x74, 0x20, 0x6d, 0x69, 0x64, 0x64, 0x6c, + 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0x74, 0x65, + 0x78, 0x74, 0x75, 0x72, 0x65, 0x28, 0x75, 0x5f, 0x45, 0x6e, 0x74, 0x69, + 0x74, 0x79, 0x54, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x75, + 0x76, 0x29, 0x2e, 0x72, 0x3b, 0x0d, 0x0a, 0x09, 0x66, 0x6c, 0x6f, 0x61, + 0x74, 0x20, 0x6f, 0x70, 0x61, 0x63, 0x69, 0x74, 0x79, 0x4d, 0x75, 0x6c, + 0x74, 0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, 0x28, 0x30, 0x2e, 0x30, 0x2c, + 0x20, 0x31, 0x2e, 0x30, 0x2c, 0x20, 0x69, 0x6e, 0x74, 0x28, 0x6d, 0x69, + 0x64, 0x64, 0x6c, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x29, 0x20, + 0x3d, 0x3d, 0x20, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x29, 0x3b, 0x0d, + 0x0a, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x2f, 0x2f, 0x20, 0x43, 0x6f, + 0x72, 0x72, 0x65, 0x63, 0x74, 0x20, 0x61, 0x73, 0x70, 0x65, 0x63, 0x74, + 0x20, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x76, 0x65, 0x63, 0x32, 0x20, 0x61, 0x73, 0x70, 0x65, 0x63, 0x74, 0x20, + 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x20, 0x2f, 0x20, 0x76, 0x65, 0x63, 0x32, + 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x69, 0x7a, 0x65, + 0x28, 0x75, 0x5f, 0x45, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x54, 0x65, 0x78, + 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x30, 0x29, 0x29, 0x3b, 0x0d, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x0d, 0x0a, 0x09, 0x66, 0x6c, 0x6f, 0x61, 0x74, + 0x20, 0x68, 0x61, 0x73, 0x4d, 0x69, 0x73, 0x73, 0x65, 0x64, 0x20, 0x3d, + 0x20, 0x30, 0x2e, 0x30, 0x66, 0x3b, 0x0d, 0x0a, 0x09, 0x76, 0x65, 0x63, + 0x34, 0x20, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, + 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28, 0x30, 0x2e, 0x30, 0x2c, 0x20, + 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x30, 0x2e, + 0x30, 0x66, 0x29, 0x3b, 0x0d, 0x0a, 0x09, 0x66, 0x6f, 0x72, 0x20, 0x28, + 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x69, 0x20, 0x3d, 0x20, 0x30, 0x2e, + 0x30, 0x3b, 0x20, 0x69, 0x20, 0x3c, 0x20, 0x54, 0x41, 0x55, 0x3b, 0x20, + 0x69, 0x20, 0x2b, 0x3d, 0x20, 0x54, 0x41, 0x55, 0x20, 0x2f, 0x20, 0x73, + 0x74, 0x65, 0x70, 0x73, 0x29, 0x20, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x7b, 0x0d, 0x0a, 0x09, 0x09, 0x2f, 0x2f, 0x20, 0x53, 0x61, 0x6d, 0x70, + 0x6c, 0x65, 0x20, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20, + 0x61, 0x20, 0x63, 0x69, 0x72, 0x63, 0x75, 0x6c, 0x61, 0x72, 0x20, 0x70, + 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x6f, 0x66, 0x66, + 0x73, 0x65, 0x74, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x32, 0x28, 0x73, + 0x69, 0x6e, 0x28, 0x69, 0x29, 0x2c, 0x20, 0x63, 0x6f, 0x73, 0x28, 0x69, + 0x29, 0x29, 0x20, 0x2a, 0x20, 0x61, 0x73, 0x70, 0x65, 0x63, 0x74, 0x20, + 0x2a, 0x20, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x3b, 0x0d, 0x0a, 0x09, + 0x09, 0x75, 0x69, 0x6e, 0x74, 0x20, 0x63, 0x6f, 0x6c, 0x20, 0x3d, 0x20, + 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x28, 0x75, 0x5f, 0x45, 0x6e, + 0x74, 0x69, 0x74, 0x79, 0x54, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x2c, + 0x20, 0x75, 0x76, 0x20, 0x2b, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, + 0x29, 0x2e, 0x72, 0x3b, 0x0d, 0x0a, 0x09, 0x09, 0x0d, 0x0a, 0x09, 0x09, + 0x69, 0x66, 0x28, 0x63, 0x6f, 0x6c, 0x20, 0x3d, 0x3d, 0x20, 0x74, 0x61, + 0x72, 0x67, 0x65, 0x74, 0x20, 0x7c, 0x7c, 0x20, 0x63, 0x6f, 0x6c, 0x20, + 0x3d, 0x3d, 0x20, 0x30, 0x29, 0x0d, 0x0a, 0x09, 0x09, 0x7b, 0x0d, 0x0a, + 0x09, 0x09, 0x09, 0x68, 0x61, 0x73, 0x4d, 0x69, 0x73, 0x73, 0x65, 0x64, + 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x3b, 0x0d, 0x0a, 0x09, 0x09, + 0x7d, 0x0d, 0x0a, 0x0d, 0x0a, 0x09, 0x09, 0x2f, 0x2f, 0x20, 0x4d, 0x69, + 0x78, 0x20, 0x6f, 0x75, 0x74, 0x6c, 0x69, 0x6e, 0x65, 0x20, 0x77, 0x69, + 0x74, 0x68, 0x20, 0x62, 0x61, 0x63, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x6e, + 0x64, 0x0d, 0x0a, 0x09, 0x09, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x61, + 0x6c, 0x70, 0x68, 0x61, 0x20, 0x3d, 0x20, 0x73, 0x6d, 0x6f, 0x6f, 0x74, + 0x68, 0x73, 0x74, 0x65, 0x70, 0x28, 0x30, 0x2e, 0x35, 0x2c, 0x20, 0x30, + 0x2e, 0x37, 0x2c, 0x20, 0x69, 0x6e, 0x74, 0x28, 0x63, 0x6f, 0x6c, 0x20, + 0x21, 0x3d, 0x20, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x29, 0x20, 0x2a, + 0x20, 0x68, 0x61, 0x73, 0x4d, 0x69, 0x73, 0x73, 0x65, 0x64, 0x20, 0x2a, + 0x20, 0x31, 0x30, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0d, 0x0a, 0x09, 0x09, + 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, + 0x6d, 0x69, 0x78, 0x28, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, + 0x72, 0x2c, 0x20, 0x75, 0x5f, 0x4f, 0x75, 0x74, 0x6c, 0x69, 0x6e, 0x65, + 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x61, 0x6c, 0x70, 0x68, 0x61, + 0x29, 0x3b, 0x0d, 0x0a, 0x09, 0x7d, 0x0d, 0x0a, 0x09, 0x0d, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x69, 0x66, 0x28, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, + 0x6c, 0x6f, 0x72, 0x2e, 0x61, 0x20, 0x3e, 0x20, 0x30, 0x2e, 0x31, 0x29, + 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, + 0x6f, 0x72, 0x2e, 0x61, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x3b, + 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0d, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, + 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, 0x28, 0x76, + 0x65, 0x63, 0x34, 0x28, 0x30, 0x29, 0x2c, 0x20, 0x66, 0x72, 0x61, 0x67, + 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x6d, 0x69, 0x64, 0x64, 0x6c, + 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x21, 0x3d, 0x20, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x29, 0x3b, 0x0d, 0x0a, 0x7d, 0x0d, 0x0a }; - unsigned int Resources_Shaders_outline_shader_len = 1260; + unsigned int Resources_Shaders_outline_shader_len = 1488; // Data for file: Resources_Shaders_pbr_shader_path const std::string Resources_Shaders_pbr_shader_path = R"(Resources/Shaders/pbr.shader)"; diff --git a/Resources/Images/project_icon.png b/Resources/Images/project_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..384eabf2cc0c3bff15b5849cc846f46a149ebf62 GIT binary patch literal 719 zcmeAS@N?(olHy`uVBq!ia0vp^DIm``W z$lZxy-8q?;Kn_c~qpu?a!^VE@KZ&di3`{AWE{-7;jBl?u7Tz)tX-K^In&BS9E=Fk+ z$Vwa2e({?s;nyFW{(tvn&gm-uz82NvmO!Ji zgYT!R?YUE>`}wwut1bGJUVVM>g!L0n&a>PTC$YhGPEUYbOn;Ky%#8PJpDyw${5_;L z`K0E*SBGBOObOl-mYYf@QVBp@%D)2Nu}pzKG|xx(`?)RLw|UE^^UJN zp*=sxY30?gep|JduH$;D@!Wgo_ZRv{f6w;GS@|j_{$lv&oVB@!3ZKT`+O^SGxTt6H z0=5ADs#U9({)ue5l5%C{4R+z^Q(v9DE`13KEI;zjH)qy_y)m!N3le{=F6p_FyX60r^bfXAnx(Jlm;Q6^Grs)n zeboE&ugfk9T9;R@tW#yK;dyR-Io?VxDB#6+!zum?YYNScPApq@<<*{y%*%TezujW1 zE_i-+pLYGjcPB37pU^7)cja2dzKU5sZytH&tm=<2bfR{?n8FQ`6xvibG2xi4>Tx4r#h2Iow#yH{50uQY!$ zYnjZly8DJRFK7SV`ZV-=_qyMUC(WKP@AGu6|39Wr`JdasX?ZGWb8*GoZ0|$%&-Sf8 gABH{p89wy=W%E>K)G@fSA^@bs)78&qol`;+0FjGF3jhEB literal 0 HcmV?d00001 diff --git a/Resources/Shaders/outline.shader b/Resources/Shaders/outline.shader index 8cc25404..bb621339 100644 --- a/Resources/Shaders/outline.shader +++ b/Resources/Shaders/outline.shader @@ -32,9 +32,14 @@ void main() float radius = 4.f; vec2 uv = a_UV; + // sample middle + uint middleSample = texture(u_EntityTexture, uv).r; + float opacityMult = mix(0.0, 1.0, int(middleSample) == target); + // Correct aspect ratio vec2 aspect = 1.0 / vec2(textureSize(u_EntityTexture, 0)); + float hasMissed = 0.0f; vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0f); for (float i = 0.0; i < TAU; i += TAU / steps) { @@ -42,8 +47,13 @@ void main() vec2 offset = vec2(sin(i), cos(i)) * aspect * radius; uint col = texture(u_EntityTexture, uv + offset).r; + if(col == target || col == 0) + { + hasMissed = 1.0f; + } + // Mix outline with background - float alpha = smoothstep(0.5, 0.7, int(col != target) * 10.0f); + float alpha = smoothstep(0.5, 0.7, int(col != target) * hasMissed * 10.0f); fragColor = mix(fragColor, u_OutlineColor, alpha); } @@ -52,5 +62,5 @@ void main() fragColor.a = 1.0f; } - FragColor = mix(vec4(0), fragColor, texture(u_EntityTexture, uv).r == target); + FragColor = mix(vec4(0), fragColor, middleSample != target); } From c49c0f75465bed5820ba2b5e260662fb0b78fc99 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 16 Mar 2024 11:45:13 -0400 Subject: [PATCH 04/26] Fixed outline --- Nuake/src/Resource/StaticResources.cpp | 121 ++++++++++++------------- Resources/Shaders/outline.shader | 11 +-- 2 files changed, 63 insertions(+), 69 deletions(-) diff --git a/Nuake/src/Resource/StaticResources.cpp b/Nuake/src/Resource/StaticResources.cpp index 7da9197f..65d842c2 100644 --- a/Nuake/src/Resource/StaticResources.cpp +++ b/Nuake/src/Resource/StaticResources.cpp @@ -235397,49 +235397,42 @@ const std::string Resources_Shaders_outline_shader_path = R"(Resources/Shaders/o 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x28, 0x75, 0x5f, 0x45, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x54, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x75, - 0x76, 0x29, 0x2e, 0x72, 0x3b, 0x0d, 0x0a, 0x09, 0x66, 0x6c, 0x6f, 0x61, - 0x74, 0x20, 0x6f, 0x70, 0x61, 0x63, 0x69, 0x74, 0x79, 0x4d, 0x75, 0x6c, - 0x74, 0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, 0x28, 0x30, 0x2e, 0x30, 0x2c, - 0x20, 0x31, 0x2e, 0x30, 0x2c, 0x20, 0x69, 0x6e, 0x74, 0x28, 0x6d, 0x69, - 0x64, 0x64, 0x6c, 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x29, 0x20, - 0x3d, 0x3d, 0x20, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x29, 0x3b, 0x0d, - 0x0a, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x2f, 0x2f, 0x20, 0x43, 0x6f, - 0x72, 0x72, 0x65, 0x63, 0x74, 0x20, 0x61, 0x73, 0x70, 0x65, 0x63, 0x74, - 0x20, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, - 0x76, 0x65, 0x63, 0x32, 0x20, 0x61, 0x73, 0x70, 0x65, 0x63, 0x74, 0x20, - 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x20, 0x2f, 0x20, 0x76, 0x65, 0x63, 0x32, - 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x53, 0x69, 0x7a, 0x65, - 0x28, 0x75, 0x5f, 0x45, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x54, 0x65, 0x78, - 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x30, 0x29, 0x29, 0x3b, 0x0d, 0x0a, - 0x20, 0x20, 0x20, 0x20, 0x0d, 0x0a, 0x09, 0x66, 0x6c, 0x6f, 0x61, 0x74, - 0x20, 0x68, 0x61, 0x73, 0x4d, 0x69, 0x73, 0x73, 0x65, 0x64, 0x20, 0x3d, - 0x20, 0x30, 0x2e, 0x30, 0x66, 0x3b, 0x0d, 0x0a, 0x09, 0x76, 0x65, 0x63, - 0x34, 0x20, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, - 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28, 0x30, 0x2e, 0x30, 0x2c, 0x20, - 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x30, 0x2e, - 0x30, 0x66, 0x29, 0x3b, 0x0d, 0x0a, 0x09, 0x66, 0x6f, 0x72, 0x20, 0x28, - 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x69, 0x20, 0x3d, 0x20, 0x30, 0x2e, - 0x30, 0x3b, 0x20, 0x69, 0x20, 0x3c, 0x20, 0x54, 0x41, 0x55, 0x3b, 0x20, - 0x69, 0x20, 0x2b, 0x3d, 0x20, 0x54, 0x41, 0x55, 0x20, 0x2f, 0x20, 0x73, - 0x74, 0x65, 0x70, 0x73, 0x29, 0x20, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, - 0x7b, 0x0d, 0x0a, 0x09, 0x09, 0x2f, 0x2f, 0x20, 0x53, 0x61, 0x6d, 0x70, - 0x6c, 0x65, 0x20, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20, - 0x61, 0x20, 0x63, 0x69, 0x72, 0x63, 0x75, 0x6c, 0x61, 0x72, 0x20, 0x70, - 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, - 0x20, 0x20, 0x20, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x6f, 0x66, 0x66, - 0x73, 0x65, 0x74, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x32, 0x28, 0x73, - 0x69, 0x6e, 0x28, 0x69, 0x29, 0x2c, 0x20, 0x63, 0x6f, 0x73, 0x28, 0x69, - 0x29, 0x29, 0x20, 0x2a, 0x20, 0x61, 0x73, 0x70, 0x65, 0x63, 0x74, 0x20, - 0x2a, 0x20, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x3b, 0x0d, 0x0a, 0x09, - 0x09, 0x75, 0x69, 0x6e, 0x74, 0x20, 0x63, 0x6f, 0x6c, 0x20, 0x3d, 0x20, - 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x28, 0x75, 0x5f, 0x45, 0x6e, - 0x74, 0x69, 0x74, 0x79, 0x54, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x2c, - 0x20, 0x75, 0x76, 0x20, 0x2b, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, - 0x29, 0x2e, 0x72, 0x3b, 0x0d, 0x0a, 0x09, 0x09, 0x0d, 0x0a, 0x09, 0x09, - 0x69, 0x66, 0x28, 0x63, 0x6f, 0x6c, 0x20, 0x3d, 0x3d, 0x20, 0x74, 0x61, - 0x72, 0x67, 0x65, 0x74, 0x20, 0x7c, 0x7c, 0x20, 0x63, 0x6f, 0x6c, 0x20, - 0x3d, 0x3d, 0x20, 0x30, 0x29, 0x0d, 0x0a, 0x09, 0x09, 0x7b, 0x0d, 0x0a, - 0x09, 0x09, 0x09, 0x68, 0x61, 0x73, 0x4d, 0x69, 0x73, 0x73, 0x65, 0x64, + 0x76, 0x29, 0x2e, 0x72, 0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x2f, 0x2f, 0x20, 0x43, 0x6f, 0x72, 0x72, 0x65, 0x63, 0x74, 0x20, + 0x61, 0x73, 0x70, 0x65, 0x63, 0x74, 0x20, 0x72, 0x61, 0x74, 0x69, 0x6f, + 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x61, + 0x73, 0x70, 0x65, 0x63, 0x74, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x20, + 0x2f, 0x20, 0x76, 0x65, 0x63, 0x32, 0x28, 0x74, 0x65, 0x78, 0x74, 0x75, + 0x72, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x28, 0x75, 0x5f, 0x45, 0x6e, 0x74, + 0x69, 0x74, 0x79, 0x54, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, + 0x30, 0x29, 0x29, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x0d, 0x0a, + 0x09, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x68, 0x61, 0x73, 0x48, 0x69, + 0x74, 0x20, 0x3d, 0x20, 0x30, 0x2e, 0x30, 0x66, 0x3b, 0x0d, 0x0a, 0x09, + 0x76, 0x65, 0x63, 0x34, 0x20, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, + 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28, 0x30, 0x2e, + 0x30, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x2c, + 0x20, 0x30, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0d, 0x0a, 0x09, 0x66, 0x6f, + 0x72, 0x20, 0x28, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x69, 0x20, 0x3d, + 0x20, 0x30, 0x2e, 0x30, 0x3b, 0x20, 0x69, 0x20, 0x3c, 0x20, 0x54, 0x41, + 0x55, 0x3b, 0x20, 0x69, 0x20, 0x2b, 0x3d, 0x20, 0x54, 0x41, 0x55, 0x20, + 0x2f, 0x20, 0x73, 0x74, 0x65, 0x70, 0x73, 0x29, 0x20, 0x0d, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x7b, 0x0d, 0x0a, 0x09, 0x09, 0x2f, 0x2f, 0x20, 0x53, + 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x20, + 0x69, 0x6e, 0x20, 0x61, 0x20, 0x63, 0x69, 0x72, 0x63, 0x75, 0x6c, 0x61, + 0x72, 0x20, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x0d, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, + 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, + 0x32, 0x28, 0x73, 0x69, 0x6e, 0x28, 0x69, 0x29, 0x2c, 0x20, 0x63, 0x6f, + 0x73, 0x28, 0x69, 0x29, 0x29, 0x20, 0x2a, 0x20, 0x61, 0x73, 0x70, 0x65, + 0x63, 0x74, 0x20, 0x2a, 0x20, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x3b, + 0x0d, 0x0a, 0x09, 0x09, 0x75, 0x69, 0x6e, 0x74, 0x20, 0x63, 0x6f, 0x6c, + 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x28, 0x75, + 0x5f, 0x45, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x54, 0x65, 0x78, 0x74, 0x75, + 0x72, 0x65, 0x2c, 0x20, 0x75, 0x76, 0x20, 0x2b, 0x20, 0x6f, 0x66, 0x66, + 0x73, 0x65, 0x74, 0x29, 0x2e, 0x72, 0x3b, 0x0d, 0x0a, 0x09, 0x09, 0x0d, + 0x0a, 0x09, 0x09, 0x69, 0x66, 0x28, 0x63, 0x6f, 0x6c, 0x20, 0x3d, 0x3d, + 0x20, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x29, 0x0d, 0x0a, 0x09, 0x09, + 0x7b, 0x0d, 0x0a, 0x09, 0x09, 0x09, 0x68, 0x61, 0x73, 0x48, 0x69, 0x74, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x3b, 0x0d, 0x0a, 0x09, 0x09, 0x7d, 0x0d, 0x0a, 0x0d, 0x0a, 0x09, 0x09, 0x2f, 0x2f, 0x20, 0x4d, 0x69, 0x78, 0x20, 0x6f, 0x75, 0x74, 0x6c, 0x69, 0x6e, 0x65, 0x20, 0x77, 0x69, @@ -235449,27 +235442,29 @@ const std::string Resources_Shaders_outline_shader_path = R"(Resources/Shaders/o 0x68, 0x73, 0x74, 0x65, 0x70, 0x28, 0x30, 0x2e, 0x35, 0x2c, 0x20, 0x30, 0x2e, 0x37, 0x2c, 0x20, 0x69, 0x6e, 0x74, 0x28, 0x63, 0x6f, 0x6c, 0x20, 0x21, 0x3d, 0x20, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x29, 0x20, 0x2a, - 0x20, 0x68, 0x61, 0x73, 0x4d, 0x69, 0x73, 0x73, 0x65, 0x64, 0x20, 0x2a, - 0x20, 0x31, 0x30, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0d, 0x0a, 0x09, 0x09, - 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, - 0x6d, 0x69, 0x78, 0x28, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, - 0x72, 0x2c, 0x20, 0x75, 0x5f, 0x4f, 0x75, 0x74, 0x6c, 0x69, 0x6e, 0x65, - 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x61, 0x6c, 0x70, 0x68, 0x61, - 0x29, 0x3b, 0x0d, 0x0a, 0x09, 0x7d, 0x0d, 0x0a, 0x09, 0x0d, 0x0a, 0x20, - 0x20, 0x20, 0x20, 0x69, 0x66, 0x28, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, - 0x6c, 0x6f, 0x72, 0x2e, 0x61, 0x20, 0x3e, 0x20, 0x30, 0x2e, 0x31, 0x29, - 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, - 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, - 0x6f, 0x72, 0x2e, 0x61, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x3b, - 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0d, 0x0a, 0x20, 0x20, 0x20, - 0x20, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, - 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, 0x28, 0x76, - 0x65, 0x63, 0x34, 0x28, 0x30, 0x29, 0x2c, 0x20, 0x66, 0x72, 0x61, 0x67, - 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x6d, 0x69, 0x64, 0x64, 0x6c, - 0x65, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x21, 0x3d, 0x20, 0x74, - 0x61, 0x72, 0x67, 0x65, 0x74, 0x29, 0x3b, 0x0d, 0x0a, 0x7d, 0x0d, 0x0a + 0x20, 0x68, 0x61, 0x73, 0x48, 0x69, 0x74, 0x20, 0x2a, 0x20, 0x31, 0x30, + 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0d, 0x0a, 0x09, 0x09, 0x66, 0x72, 0x61, + 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, + 0x28, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, + 0x75, 0x5f, 0x4f, 0x75, 0x74, 0x6c, 0x69, 0x6e, 0x65, 0x43, 0x6f, 0x6c, + 0x6f, 0x72, 0x2c, 0x20, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x29, 0x3b, 0x0d, + 0x0a, 0x09, 0x7d, 0x0d, 0x0a, 0x09, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, + 0x69, 0x66, 0x28, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, + 0x2e, 0x61, 0x20, 0x3e, 0x20, 0x30, 0x2e, 0x31, 0x29, 0x0d, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x2e, + 0x61, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x3b, 0x0d, 0x0a, 0x20, + 0x20, 0x20, 0x20, 0x7d, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x0d, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, + 0x72, 0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, 0x28, 0x76, 0x65, 0x63, 0x34, + 0x28, 0x30, 0x29, 0x2c, 0x20, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, + 0x6f, 0x72, 0x2c, 0x20, 0x6d, 0x69, 0x64, 0x64, 0x6c, 0x65, 0x53, 0x61, + 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x21, 0x3d, 0x20, 0x74, 0x61, 0x72, 0x67, + 0x65, 0x74, 0x20, 0x26, 0x26, 0x20, 0x68, 0x61, 0x73, 0x48, 0x69, 0x74, + 0x20, 0x3e, 0x20, 0x30, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0d, 0x0a, 0x7d, + 0x0d, 0x0a }; - unsigned int Resources_Shaders_outline_shader_len = 1488; + unsigned int Resources_Shaders_outline_shader_len = 1418; // Data for file: Resources_Shaders_pbr_shader_path const std::string Resources_Shaders_pbr_shader_path = R"(Resources/Shaders/pbr.shader)"; diff --git a/Resources/Shaders/outline.shader b/Resources/Shaders/outline.shader index bb621339..8bddf576 100644 --- a/Resources/Shaders/outline.shader +++ b/Resources/Shaders/outline.shader @@ -34,12 +34,11 @@ void main() // sample middle uint middleSample = texture(u_EntityTexture, uv).r; - float opacityMult = mix(0.0, 1.0, int(middleSample) == target); // Correct aspect ratio vec2 aspect = 1.0 / vec2(textureSize(u_EntityTexture, 0)); - float hasMissed = 0.0f; + float hasHit = 0.0f; vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0f); for (float i = 0.0; i < TAU; i += TAU / steps) { @@ -47,13 +46,13 @@ void main() vec2 offset = vec2(sin(i), cos(i)) * aspect * radius; uint col = texture(u_EntityTexture, uv + offset).r; - if(col == target || col == 0) + if(col == target) { - hasMissed = 1.0f; + hasHit = 1.0f; } // Mix outline with background - float alpha = smoothstep(0.5, 0.7, int(col != target) * hasMissed * 10.0f); + float alpha = smoothstep(0.5, 0.7, int(col != target) * hasHit * 10.0f); fragColor = mix(fragColor, u_OutlineColor, alpha); } @@ -62,5 +61,5 @@ void main() fragColor.a = 1.0f; } - FragColor = mix(vec4(0), fragColor, middleSample != target); + FragColor = mix(vec4(0), fragColor, middleSample != target && hasHit > 0.0f); } From d3382e22ff9377a968502d52187bf22733c9ed34 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 16 Mar 2024 11:51:32 -0400 Subject: [PATCH 05/26] Can no longer select occluded gizmos --- Nuake/src/Resource/StaticResources.cpp | 18 +++++++++++------- Resources/Shaders/gizmo.shader | 5 ++++- Resources/Shaders/outline.shader | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Nuake/src/Resource/StaticResources.cpp b/Nuake/src/Resource/StaticResources.cpp index 65d842c2..7c1751ff 100644 --- a/Nuake/src/Resource/StaticResources.cpp +++ b/Nuake/src/Resource/StaticResources.cpp @@ -235272,13 +235272,17 @@ const std::string Resources_Shaders_gizmo_shader_path = R"(Resources/Shaders/giz 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x2a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28, 0x31, 0x2c, 0x20, 0x31, 0x2c, 0x20, 0x31, 0x2c, 0x20, 0x75, 0x5f, 0x4f, 0x70, 0x61, 0x63, 0x69, 0x74, 0x79, 0x29, 0x3b, 0x0d, 0x0a, - 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x67, 0x45, 0x6e, 0x74, 0x69, 0x74, - 0x79, 0x49, 0x44, 0x20, 0x3d, 0x20, 0x75, 0x5f, 0x45, 0x6e, 0x74, 0x69, - 0x74, 0x79, 0x49, 0x44, 0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x20, 0x20, 0x20, - 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, - 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0d, 0x0a, 0x7d + 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x28, 0x75, 0x5f, 0x4f, + 0x70, 0x61, 0x63, 0x69, 0x74, 0x79, 0x20, 0x3e, 0x3d, 0x20, 0x30, 0x2e, + 0x35, 0x66, 0x29, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7b, 0x0d, 0x0a, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x67, 0x45, 0x6e, 0x74, + 0x69, 0x74, 0x79, 0x49, 0x44, 0x20, 0x3d, 0x20, 0x75, 0x5f, 0x45, 0x6e, + 0x74, 0x69, 0x74, 0x79, 0x49, 0x44, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, + 0x20, 0x7d, 0x0d, 0x0a, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x46, 0x72, + 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x63, 0x6f, + 0x6c, 0x6f, 0x72, 0x3b, 0x0d, 0x0a, 0x7d }; - unsigned int Resources_Shaders_gizmo_shader_len = 994; + unsigned int Resources_Shaders_gizmo_shader_len = 1039; // Data for file: Resources_Shaders_line_shader_path const std::string Resources_Shaders_line_shader_path = R"(Resources/Shaders/line.shader)"; @@ -235440,7 +235444,7 @@ const std::string Resources_Shaders_outline_shader_path = R"(Resources/Shaders/o 0x64, 0x0d, 0x0a, 0x09, 0x09, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x20, 0x3d, 0x20, 0x73, 0x6d, 0x6f, 0x6f, 0x74, 0x68, 0x73, 0x74, 0x65, 0x70, 0x28, 0x30, 0x2e, 0x35, 0x2c, 0x20, 0x30, - 0x2e, 0x37, 0x2c, 0x20, 0x69, 0x6e, 0x74, 0x28, 0x63, 0x6f, 0x6c, 0x20, + 0x2e, 0x39, 0x2c, 0x20, 0x69, 0x6e, 0x74, 0x28, 0x63, 0x6f, 0x6c, 0x20, 0x21, 0x3d, 0x20, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x29, 0x20, 0x2a, 0x20, 0x68, 0x61, 0x73, 0x48, 0x69, 0x74, 0x20, 0x2a, 0x20, 0x31, 0x30, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0d, 0x0a, 0x09, 0x09, 0x66, 0x72, 0x61, diff --git a/Resources/Shaders/gizmo.shader b/Resources/Shaders/gizmo.shader index d7239976..bd418f96 100644 --- a/Resources/Shaders/gizmo.shader +++ b/Resources/Shaders/gizmo.shader @@ -42,7 +42,10 @@ void main() vec4 px_color = texture(gizmo_texture, a_UV).rgba; color = px_color * vec4(1, 1, 1, u_Opacity); - gEntityID = u_EntityID; + if(u_Opacity >= 0.5f) + { + gEntityID = u_EntityID; + } FragColor = color; } \ No newline at end of file diff --git a/Resources/Shaders/outline.shader b/Resources/Shaders/outline.shader index 8bddf576..0219f089 100644 --- a/Resources/Shaders/outline.shader +++ b/Resources/Shaders/outline.shader @@ -52,7 +52,7 @@ void main() } // Mix outline with background - float alpha = smoothstep(0.5, 0.7, int(col != target) * hasHit * 10.0f); + float alpha = smoothstep(0.5, 0.9, int(col != target) * hasHit * 10.0f); fragColor = mix(fragColor, u_OutlineColor, alpha); } From 268de51af2e3fce2f3adbb12d9f61c64530fa9c6 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 16 Mar 2024 12:15:22 -0400 Subject: [PATCH 06/26] Fixed IsKeyPressed behaviour --- Nuake/src/Core/Input.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Nuake/src/Core/Input.cpp b/Nuake/src/Core/Input.cpp index a007acc5..81166c4f 100644 --- a/Nuake/src/Core/Input.cpp +++ b/Nuake/src/Core/Input.cpp @@ -41,7 +41,7 @@ namespace Nuake bool result = state == GLFW_PRESS; // First time pressed? - if (m_Keys.find(keycode) == m_Keys.end() || m_Keys[keycode] == true) + if (m_Keys.find(keycode) == m_Keys.end() || m_Keys[keycode] == false) { if (result) m_Keys[keycode] = true; From ea1339a8e82b6639a2efc0dcf337233d32df1c35 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 16 Mar 2024 12:15:40 -0400 Subject: [PATCH 07/26] Added isKeyPressed C# api --- Nuake/src/Scripting/NetModules/InputNetAPI.cpp | 7 +++++++ NuakeNet/src/Input.cs | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/Nuake/src/Scripting/NetModules/InputNetAPI.cpp b/Nuake/src/Scripting/NetModules/InputNetAPI.cpp index 44e0d214..71e34b3a 100644 --- a/Nuake/src/Scripting/NetModules/InputNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/InputNetAPI.cpp @@ -11,6 +11,11 @@ namespace Nuake { return Input::IsKeyDown(keyCode); } + bool IsKeyPressed(int keyCode) + { + return Input::IsKeyPressed(keyCode); + } + Coral::NativeArray GetMousePosition() { Vector2 mousePosition = Input::GetMousePosition(); @@ -21,6 +26,8 @@ namespace Nuake { void InputNetAPI::RegisterMethods() { RegisterMethod("Input.IsKeyDownIcall", &IsKeyDown); + RegisterMethod("Input.IsKeyPressedIcall", &IsKeyPressed); + RegisterMethod("Input.GetMousePositionIcall", &GetMousePosition); } diff --git a/NuakeNet/src/Input.cs b/NuakeNet/src/Input.cs index 4b672af1..b986cdcc 100644 --- a/NuakeNet/src/Input.cs +++ b/NuakeNet/src/Input.cs @@ -135,6 +135,7 @@ namespace Nuake.Net public class Input { internal static unsafe delegate* IsKeyDownIcall; + internal static unsafe delegate* IsKeyPressedIcall; internal static unsafe delegate*> GetMousePositionIcall; public static bool IsKeyDown(Key keys) @@ -142,6 +143,11 @@ namespace Nuake.Net unsafe { return IsKeyDownIcall((int)keys); } } + public static bool IsKeyPressed(Key key) + { + unsafe { return IsKeyPressedIcall((int)key); } + } + public static Vector2 GetMousePosition() { NativeArray result; From a80fb5241ed081f889b3b948815e49a864dcc898 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 16 Mar 2024 12:15:52 -0400 Subject: [PATCH 08/26] Added ability to play an animation from C# --- .../src/Scripting/NetModules/SceneNetAPI.cpp | 29 +++++++++++++++++++ NuakeNet/src/Components.cs | 12 +++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp index 1bbe5e9e..fadb5070 100644 --- a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp @@ -169,6 +169,33 @@ namespace Nuake { return false; } + void Play(int entityId, Coral::NativeString animation) + { + Entity entity = Entity((entt::entity)(entityId), Engine::GetCurrentScene().get()); + + if (entity.IsValid() && entity.HasComponent()) + { + auto& skinnedModel = entity.GetComponent(); + + if (skinnedModel.ModelResource) + { + auto& model = skinnedModel.ModelResource; + + // Find animation from name + int animIndex = 0; + for (const auto& anim : model->GetAnimations()) + { + if (anim->GetName() == animation.ToString()) + { + model->PlayAnimation(animIndex); + } + + animIndex++; + } + } + } + } + void Nuake::SceneNetAPI::RegisterMethods() { RegisterMethod("Entity.EntityHasComponentIcall", &EntityHasComponent); @@ -182,6 +209,8 @@ namespace Nuake { RegisterMethod("CharacterControllerComponent.MoveAndSlideIcall", &MoveAndSlide); RegisterMethod("CharacterControllerComponent.IsOnGroundIcall", &IsOnGround); + + RegisterMethod("SkinnedModelComponent.PlayIcall", &Play); } } diff --git a/NuakeNet/src/Components.cs b/NuakeNet/src/Components.cs index 73f82568..6e7fb641 100644 --- a/NuakeNet/src/Components.cs +++ b/NuakeNet/src/Components.cs @@ -152,10 +152,20 @@ namespace Nuake.Net public class SkinnedModelComponent : IComponent { - public SkinnedModelComponent(int entityId) { } + internal static unsafe delegate* PlayIcall; + + public SkinnedModelComponent(int entityId) + { + EntityID = entityId; + } public bool Playing { get; set; } public int CurrentAnimation { get; set; } + + public void Play(String name) + { + unsafe { PlayIcall(EntityID, name); } + } } public class BoneComponent : IComponent From 4b581750153af64582c9c077541cd29e44765efa Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 16 Mar 2024 16:36:31 -0400 Subject: [PATCH 09/26] Made lines gizmo more visible --- Editor/src/Misc/GizmoDrawer.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Editor/src/Misc/GizmoDrawer.cpp b/Editor/src/Misc/GizmoDrawer.cpp index e93b6f27..7be76346 100644 --- a/Editor/src/Misc/GizmoDrawer.cpp +++ b/Editor/src/Misc/GizmoDrawer.cpp @@ -196,6 +196,7 @@ void GizmoDrawer::DrawGizmos(Ref scene, bool occluded) const Matrix4& rotationMatrix = glm::mat4_cast(globalRotation); m_LineShader->Bind(); + m_LineShader->SetUniform1f("u_Opacity", 0.5f); m_LineShader->SetUniformMat4f("u_View", glm::scale(glm::translate(scene->m_EditorCamera->GetTransform(), Vector3(transform.GetGlobalTransform()[3])) * rotationMatrix, box.Size)); m_LineShader->SetUniformMat4f("u_Projection", scene->m_EditorCamera->GetPerspective()); @@ -213,6 +214,7 @@ void GizmoDrawer::DrawGizmos(Ref scene, bool occluded) auto [transform, sphere] = scene->m_Registry.get(e); m_LineShader->Bind(); + m_LineShader->SetUniform1f("u_Opacity", 0.5f); m_LineShader->SetUniformMat4f("u_View", glm::scale(glm::translate(scene->m_EditorCamera->GetTransform(), Vector3(transform.GetGlobalTransform()[3])), Vector3(sphere.Radius))); m_LineShader->SetUniformMat4f("u_Projection", scene->m_EditorCamera->GetPerspective()); @@ -238,6 +240,7 @@ void GizmoDrawer::DrawGizmos(Ref scene, bool occluded) Vector3 globalPosition = Vector3(transform.GetGlobalTransform()[3]); m_LineShader->Bind(); + m_LineShader->SetUniform1f("u_Opacity", 0.5f); m_LineShader->SetUniformMat4f("u_View", glm::scale(glm::translate(scene->m_EditorCamera->GetTransform(), globalPosition), Vector3(emitter.MaxDistance))); m_LineShader->SetUniformMat4f("u_Projection", scene->m_EditorCamera->GetPerspective()); @@ -270,6 +273,7 @@ void GizmoDrawer::DrawGizmos(Ref scene, bool occluded) const Matrix4& rotationMatrix = glm::mat4_cast(globalRotation); m_LineShader->Bind(); + m_LineShader->SetUniform1f("u_Opacity", 0.5f); m_LineShader->SetUniformMat4f("u_View", glm::translate(scene->m_EditorCamera->GetTransform(), Vector3(transform.GetGlobalTransform()[3])) * rotationMatrix); m_LineShader->SetUniformMat4f("u_Projection", scene->m_EditorCamera->GetPerspective()); @@ -299,6 +303,7 @@ void GizmoDrawer::DrawGizmos(Ref scene, bool occluded) const Matrix4& rotationMatrix = glm::mat4_cast(globalRotation); m_LineShader->Bind(); + m_LineShader->SetUniform1f("u_Opacity", 0.5f); m_LineShader->SetUniformMat4f("u_View", glm::translate(scene->m_EditorCamera->GetTransform(), Vector3(transform.GetGlobalTransform()[3])) * rotationMatrix); m_LineShader->SetUniformMat4f("u_Projection", scene->m_EditorCamera->GetPerspective()); @@ -316,6 +321,7 @@ void GizmoDrawer::DrawGizmos(Ref scene, bool occluded) auto [transform, particle] = scene->m_Registry.get(e); m_LineShader->Bind(); + m_LineShader->SetUniform1f("u_Opacity", 0.5f); m_LineShader->SetUniformMat4f("u_View", glm::scale(glm::translate(scene->m_EditorCamera->GetTransform(), Vector3(transform.GetGlobalTransform()[3])), Vector3(particle.Radius))); m_LineShader->SetUniformMat4f("u_Projection", scene->m_EditorCamera->GetPerspective()); From 16e12ff37d485d0972da1d64fc6610e0da32b31c Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 16 Mar 2024 16:36:45 -0400 Subject: [PATCH 10/26] unselect when entering playmode --- Editor/src/Windows/EditorInterface.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 591493c9..0c535e4e 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -119,14 +119,14 @@ namespace Nuake { if (ImGui::Button(ICON_FA_PAUSE, ImVec2(30, 30)) || (Input::IsKeyPressed(GLFW_KEY_F5))) { Engine::ExitPlayMode(); - + Engine::LoadScene(SceneSnapshot); Selection = EditorSelection(); } } else { - if (ImGui::Button(ICON_FA_PLAY, ImVec2(30, 30)) || (Input::IsKeyPressed(GLFW_KEY_F5))) + if (ImGui::Button(ICON_FA_PLAY, ImVec2(30, 30))) { this->SceneSnapshot = Engine::GetCurrentScene()->Copy(); @@ -135,6 +135,8 @@ namespace Nuake { ScriptingEngineNet::Get().BuildProjectAssembly(Engine::GetProject()); }; + Selection = EditorSelection(); + JobSystem::Get().Dispatch(job, []() { Engine::EnterPlayMode(); }); } From a5ca094c5f297a9296886cc7236455d21e7f7966 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 16 Mar 2024 16:37:06 -0400 Subject: [PATCH 11/26] Added trigger rigid bodies --- Nuake/src/Physics/DynamicWorld.cpp | 17 +++++++++++++---- Nuake/src/Physics/Rigibody.h | 10 +++++++--- Nuake/src/Scene/Systems/PhysicsSystem.cpp | 18 +++++++++++++++--- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Nuake/src/Physics/DynamicWorld.cpp b/Nuake/src/Physics/DynamicWorld.cpp index 2b65b61a..c07116b3 100644 --- a/Nuake/src/Physics/DynamicWorld.cpp +++ b/Nuake/src/Physics/DynamicWorld.cpp @@ -170,12 +170,21 @@ namespace Nuake //std::cout << "Contact validate callback" << std::endl; // Allows you to ignore a contact before it is created (using layers to not make objects collide is cheaper!) - return JPH::ValidateResult::AcceptAllContactsForThisBodyPair; + return JPH::ValidateResult::AcceptAllContactsForThisBodyPair; } virtual void OnContactAdded(const JPH::Body& inBody1, const JPH::Body& inBody2, const JPH::ContactManifold& inManifold, JPH::ContactSettings& ioSettings) override { - //std::cout << "A contact was added" << std::endl; + auto entId1 = static_cast(inBody1.GetUserData()); + Entity entity1 = Engine::GetCurrentScene()->GetEntityByID(entId1); + + auto entId2 = static_cast(inBody2.GetUserData()); + Entity entity2 = Engine::GetCurrentScene()->GetEntityByID(entId2); + + const std::string entity1Name = entity1.GetComponent().Name; + const std::string entity2Name = entity2.GetComponent().Name; + + Logger::Log("Collision detected between " + entity1Name + " and " + entity2Name); } virtual void OnContactPersisted(const JPH::Body& inBody1, const JPH::Body& inBody2, const JPH::ContactManifold& inManifold, JPH::ContactSettings& ioSettings) override @@ -314,8 +323,9 @@ namespace Nuake const auto& joltRotation = JPH::Quat(bodyRotation.x, bodyRotation.y, bodyRotation.z, bodyRotation.w); const auto& joltPos = JPH::Vec3(startPos.x, startPos.y, startPos.z); auto joltShape = GetJoltShape(rb->GetShape()); - JPH::BodyCreationSettings bodySettings(joltShape, joltPos, joltRotation, motionType, layer); + JPH::BodyCreationSettings bodySettings(joltShape, joltPos, joltRotation, motionType, layer); + bodySettings.mIsSensor = rb->IsTrigger(); bodySettings.mAllowedDOFs = (JPH::EAllowedDOFs::All); if (rb->GetLockXAxis()) @@ -372,7 +382,6 @@ namespace Nuake settings->mPenetrationRecoverySpeed = 1.0f; settings->mPredictiveContactDistance = 0.01f; settings->mShape = GetJoltShape(cc->Shape); - auto joltPosition = JPH::Vec3(cc->Position.x, cc->Position.y, cc->Position.z); const Quat& bodyRotation = cc->Rotation; diff --git a/Nuake/src/Physics/Rigibody.h b/Nuake/src/Physics/Rigibody.h index 8344a9b0..a6282569 100644 --- a/Nuake/src/Physics/Rigibody.h +++ b/Nuake/src/Physics/Rigibody.h @@ -20,6 +20,7 @@ namespace Nuake Quat _rotation; Entity _entity; + bool _isTrigger = false; bool m_LockXAxis = false; bool m_LockYAxis = false; bool m_LockZAxis = false; @@ -33,13 +34,16 @@ namespace Nuake void UpdateTransform(); + void SetIsTrigger(bool isTrigger) { _isTrigger = isTrigger; } + bool IsTrigger() const { return _isTrigger; } + bool GetLockXAxis() const { return m_LockXAxis; } bool GetLockYAxis() const { return m_LockYAxis; } bool GetLockZAxis() const { return m_LockZAxis; } - void setLockXAxis(bool lock) { m_LockXAxis = lock; } - void setLockYAxis(bool lock) { m_LockYAxis = lock; } - void setLockZAxis(bool lock) { m_LockZAxis = lock; } + void SetLockXAxis(bool lock) { m_LockXAxis = lock; } + void SetLockYAxis(bool lock) { m_LockYAxis = lock; } + void SetLockZAxis(bool lock) { m_LockZAxis = lock; } void SetEntityID(Entity ent); Vector3 GetPosition() const { return _position; } diff --git a/Nuake/src/Scene/Systems/PhysicsSystem.cpp b/Nuake/src/Scene/Systems/PhysicsSystem.cpp index e0bd94c7..bb54d874 100644 --- a/Nuake/src/Scene/Systems/PhysicsSystem.cpp +++ b/Nuake/src/Scene/Systems/PhysicsSystem.cpp @@ -219,6 +219,8 @@ namespace Nuake Entity ent = Entity({ e, m_Scene }); Ref rigidBody; Ref shape; + + bool isTrigger = false; if (rigidBodyComponent.GetRigidBody()) { continue; @@ -227,6 +229,7 @@ namespace Nuake if (ent.HasComponent()) { BoxColliderComponent& boxComponent = ent.GetComponent(); + isTrigger = boxComponent.IsTrigger; shape = CreateRef(boxComponent.Size); } @@ -235,6 +238,7 @@ namespace Nuake auto& capsuleComponent = ent.GetComponent(); float radius = capsuleComponent.Radius; float height = capsuleComponent.Height; + isTrigger = capsuleComponent.IsTrigger; shape = CreateRef(radius, height); } @@ -243,12 +247,14 @@ namespace Nuake auto& cylinderComponent = ent.GetComponent(); float radius = cylinderComponent.Radius; float height = cylinderComponent.Height; + isTrigger = cylinderComponent.IsTrigger; shape = CreateRef(radius, height); } if (ent.HasComponent()) { const auto& component = ent.GetComponent(); + isTrigger = component.IsTrigger; shape = CreateRef(component.Radius); } @@ -262,6 +268,8 @@ namespace Nuake const auto& modelComponent = ent.GetComponent(); const auto& component = ent.GetComponent(); + isTrigger = component.IsTrigger; + if (modelComponent.ModelResource) { uint32_t subMeshId = component.SubMesh; @@ -282,10 +290,14 @@ namespace Nuake } rigidBody = CreateRef(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalRotation(), transform.GetGlobalTransform(), shape, ent); - rigidBody->setLockXAxis(rigidBodyComponent.LockX); - rigidBody->setLockYAxis(rigidBodyComponent.LockY); - rigidBody->setLockZAxis(rigidBodyComponent.LockZ); + rigidBody->SetLockXAxis(rigidBodyComponent.LockX); + rigidBody->SetLockYAxis(rigidBodyComponent.LockY); + rigidBody->SetLockZAxis(rigidBodyComponent.LockZ); + + rigidBody->SetIsTrigger(isTrigger); + PhysicsManager::Get().RegisterBody(rigidBody); + rigidBodyComponent.Rigidbody = rigidBody; } } From 69205ec4fafba0d68ddf84a23d6876671e604787 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 16 Mar 2024 16:37:38 -0400 Subject: [PATCH 12/26] Added ShowMouse and get global position to .net api, deferred onInit after all entities have been initialized --- Nuake/src/Scene/Systems/ScriptingSystem.cpp | 19 ++++++++++++++----- .../src/Scripting/NetModules/InputNetAPI.cpp | 13 +++++++++++++ .../src/Scripting/NetModules/SceneNetAPI.cpp | 14 ++++++++++++++ Nuake/src/Scripting/ScriptingEngineNet.cpp | 2 +- NuakeNet/src/Components.cs | 14 +++++++++++++- NuakeNet/src/Input.cs | 6 ++++++ 6 files changed, 61 insertions(+), 7 deletions(-) diff --git a/Nuake/src/Scene/Systems/ScriptingSystem.cpp b/Nuake/src/Scene/Systems/ScriptingSystem.cpp index cb0b9487..708d8a27 100644 --- a/Nuake/src/Scene/Systems/ScriptingSystem.cpp +++ b/Nuake/src/Scene/Systems/ScriptingSystem.cpp @@ -52,13 +52,22 @@ namespace Nuake if (netScriptComponent.ScriptPath.empty()) continue; - // Creates an instance of the entity script in C# auto entity = Entity{ e, m_Scene }; - scriptingEngineNet.RegisterEntityScript(entity); - // We can now call on init on it. - auto scriptInstance = scriptingEngineNet.GetEntityScript(entity); - scriptInstance.InvokeMethod("OnInit"); + // Creates an instance of the entity script in C# + scriptingEngineNet.RegisterEntityScript(entity); + } + + for (auto& e : netEntities) + { + auto entity = Entity{ e, m_Scene }; + + if (entity.IsValid() && scriptingEngineNet.HasEntityScriptInstance(entity)) + { + // We can now call on init on it. + auto scriptInstance = scriptingEngineNet.GetEntityScript(entity); + scriptInstance.InvokeMethod("OnInit"); + } } return true; diff --git a/Nuake/src/Scripting/NetModules/InputNetAPI.cpp b/Nuake/src/Scripting/NetModules/InputNetAPI.cpp index 71e34b3a..e3fc1cdf 100644 --- a/Nuake/src/Scripting/NetModules/InputNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/InputNetAPI.cpp @@ -6,6 +6,18 @@ namespace Nuake { + void ShowMouse(bool visible) + { + if (visible) + { + Input::ShowMouse(); + } + else + { + Input::HideMouse(); + } + } + bool IsKeyDown(int keyCode) { return Input::IsKeyDown(keyCode); @@ -25,6 +37,7 @@ namespace Nuake { void InputNetAPI::RegisterMethods() { + RegisterMethod("Input.ShowMouseIcall", &ShowMouse); RegisterMethod("Input.IsKeyDownIcall", &IsKeyDown); RegisterMethod("Input.IsKeyPressedIcall", &IsKeyPressed); diff --git a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp index fadb5070..2e3a7fe8 100644 --- a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp @@ -115,6 +115,19 @@ namespace Nuake { } } + Coral::NativeArray TransformGetGlobalPosition(int entityId) + { + Entity entity = { (entt::entity)(entityId), Engine::GetCurrentScene().get() }; + + if (entity.IsValid() && entity.HasComponent()) + { + auto& component = entity.GetComponent(); + const auto& globalPosition = component.GetGlobalPosition(); + Coral::NativeArray result = { globalPosition.x, globalPosition.y, globalPosition.z }; + return result; + } + } + void TransformRotate(int entityId, float x, float y, float z) { Entity entity = { (entt::entity)(entityId), Engine::GetCurrentScene().get() }; @@ -203,6 +216,7 @@ namespace Nuake { // Components RegisterMethod("TransformComponent.SetPositionIcall", &TransformSetPosition); + RegisterMethod("TransformComponent.GetGlobalPositionIcall", &TransformGetGlobalPosition); RegisterMethod("TransformComponent.RotateIcall", &TransformRotate); RegisterMethod("CameraComponent.GetDirectionIcall", &CameraGetDirection); diff --git a/Nuake/src/Scripting/ScriptingEngineNet.cpp b/Nuake/src/Scripting/ScriptingEngineNet.cpp index 3544a1b0..ea4fcecf 100644 --- a/Nuake/src/Scripting/ScriptingEngineNet.cpp +++ b/Nuake/src/Scripting/ScriptingEngineNet.cpp @@ -223,7 +223,7 @@ namespace Nuake if (!HasEntityScriptInstance(entity)) { Logger::Log("Failed to get entity .Net script instance, doesn't exist", ".net", CRITICAL); - throw std::exception("Failed to get entity .Net script instance, doesn't exist"); + return Coral::ManagedObject(); } return m_EntityToManagedObjects[entity.GetID()]; diff --git a/NuakeNet/src/Components.cs b/NuakeNet/src/Components.cs index 6e7fb641..5297edcd 100644 --- a/NuakeNet/src/Components.cs +++ b/NuakeNet/src/Components.cs @@ -39,6 +39,7 @@ namespace Nuake.Net public class TransformComponent : IComponent { + internal static unsafe delegate*> GetGlobalPositionIcall; internal static unsafe delegate* SetPositionIcall; internal static unsafe delegate* RotateIcall; @@ -76,7 +77,18 @@ namespace Nuake.Net unsafe { SetPositionIcall(EntityID, value.X, value.Y, value.Z); } } } - public Vector3 GlobalPosition { get; set; } + public Vector3 GlobalPosition + { + get + { + unsafe + { + NativeArray result = GetGlobalPositionIcall(EntityID); + return new Vector3(result[0], result[1], result[2]); + } + } + set { } + } } public class LightComponent : IComponent diff --git a/NuakeNet/src/Input.cs b/NuakeNet/src/Input.cs index b986cdcc..23183e59 100644 --- a/NuakeNet/src/Input.cs +++ b/NuakeNet/src/Input.cs @@ -134,10 +134,16 @@ namespace Nuake.Net public class Input { + internal static unsafe delegate* ShowMouseIcall; internal static unsafe delegate* IsKeyDownIcall; internal static unsafe delegate* IsKeyPressedIcall; internal static unsafe delegate*> GetMousePositionIcall; + public static void ShowMouse(bool visible) + { + unsafe { ShowMouseIcall(visible); } + } + public static bool IsKeyDown(Key keys) { unsafe { return IsKeyDownIcall((int)keys); } From 48f2c69c383352d4a39f2b2f517d6157f631633b Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 23 Mar 2024 15:08:43 -0400 Subject: [PATCH 13/26] Fixed physics and added ghost body to virtual characters --- Nuake/src/Physics/DynamicWorld.cpp | 95 +++++++++++++++++++++++++----- Nuake/src/Physics/DynamicWorld.h | 8 ++- 2 files changed, 86 insertions(+), 17 deletions(-) diff --git a/Nuake/src/Physics/DynamicWorld.cpp b/Nuake/src/Physics/DynamicWorld.cpp index c07116b3..77ee13b6 100644 --- a/Nuake/src/Physics/DynamicWorld.cpp +++ b/Nuake/src/Physics/DynamicWorld.cpp @@ -72,7 +72,9 @@ namespace Nuake static constexpr uint8_t NON_MOVING = 0; static constexpr uint8_t MOVING = 1; static constexpr uint8_t KINEMATIC = 2; - static constexpr uint8_t NUM_LAYERS = 3; + static constexpr uint8_t CHARACTER_GHOST = 3; + static constexpr uint8_t CHARACTER = 4; + static constexpr uint8_t NUM_LAYERS = 5; }; // Function that determines if two object layers can collide @@ -81,11 +83,13 @@ namespace Nuake switch (inObject1) { case Layers::NON_MOVING: - return inObject2 == Layers::MOVING || inObject2 == Layers::KINEMATIC; // Non moving only collides with moving + return inObject2 == Layers::MOVING || inObject2 == Layers::KINEMATIC || inObject2 == Layers::CHARACTER; // Non moving only collides with moving case Layers::MOVING: return true; // Moving collides with everything case Layers::KINEMATIC: - return inObject2 == Layers::NON_MOVING || inObject2 == Layers::MOVING; // Only collides with non moving + return inObject2 == Layers::NON_MOVING || inObject2 == Layers::MOVING || inObject2 == Layers::CHARACTER; // Only collides with non moving + case Layers::CHARACTER: + return true; default: //JPH_ASSERT(false); return false; @@ -114,6 +118,8 @@ namespace Nuake // Create a mapping table from object to broad phase layer mObjectToBroadPhase[Layers::NON_MOVING] = BroadPhaseLayers::NON_MOVING; mObjectToBroadPhase[Layers::MOVING] = BroadPhaseLayers::MOVING; + mObjectToBroadPhase[Layers::CHARACTER] = BroadPhaseLayers::MOVING; + mObjectToBroadPhase[Layers::CHARACTER_GHOST] = BroadPhaseLayers::MOVING; } virtual JPH::uint GetNumBroadPhaseLayers() const override @@ -239,9 +245,13 @@ namespace Nuake switch (inObject1) { case Layers::NON_MOVING: - return inObject2 == Layers::MOVING; // Non moving only collides with moving + return inObject2 == Layers::MOVING || Layers::CHARACTER_GHOST; // Non moving only collides with moving case Layers::MOVING: return true; // Moving collides with everything + case Layers::CHARACTER_GHOST: + return true;// inObject2 != Layers::CHARACTER; + case Layers::CHARACTER: + return inObject2 != Layers::CHARACTER_GHOST; default: return false; @@ -258,7 +268,7 @@ namespace Nuake { DynamicWorld::DynamicWorld() : _stepCount(0) { - _registeredCharacters = std::map>(); + _registeredCharacters = std::map(); // Initialize Jolt Physics const uint32_t MaxBodies = 4096; @@ -364,7 +374,8 @@ namespace Nuake bodySettings.mUserData = entityId; // Create the actual rigid body JPH::BodyID body = _JoltBodyInterface->CreateAndAddBody(bodySettings, JPH::EActivation::Activate); // Note that if we run out of bodies this can return nullptr - uint32_t bodyIndex = (uint32_t)body.GetIndex(); + uint32_t bodyIndex = (uint32_t)body.GetIndexAndSequenceNumber(); + auto userData = _JoltBodyInterface->GetUserData(body); _registeredBodies.push_back(bodyIndex); } @@ -386,10 +397,34 @@ namespace Nuake const Quat& bodyRotation = cc->Rotation; const auto& joltRotation = JPH::Quat(bodyRotation.x, bodyRotation.y, bodyRotation.z, bodyRotation.w); - auto character = CreateRef(settings, std::move(joltPosition), std::move(joltRotation), _JoltPhysicsSystem.get()); + auto character = CreateRef(settings, std::move(joltPosition), joltRotation, _JoltPhysicsSystem.get()); + + // add ghost kinematic body to respond to hit test as the virtual char are not present in the world. + JPH::BodyInterface& bodyInterface = _JoltPhysicsSystem->GetBodyInterface(); + + const float mass = 0.1f; + JPH::EMotionType motionType = JPH::EMotionType::Kinematic; + JPH::ObjectLayer layer = Layers::CHARACTER_GHOST; + + const auto& startPos = joltPosition; + auto joltShape = GetJoltShape(cc->Shape); + JPH::BodyCreationSettings bodySettings(joltShape, startPos, joltRotation, motionType, layer); + + int entityId = cc->GetEntity().GetID(); + if (entityId == 0) + { + Logger::Log("ERROR"); + } + + //bodySettings.mUserData = entityId; + + // Create the actual rigid body + JPH::BodyID body = _JoltBodyInterface->CreateAndAddBody(bodySettings, JPH::EActivation::Activate); // Note that if we run out of bodies this can return nullptr + uint32_t bodyIndex = body.GetIndexAndSequenceNumber(); + _registeredBodies.push_back(bodyIndex); // To get the jolt character control from a scene entity. - _registeredCharacters[cc->Owner.GetHandle()] = character; + _registeredCharacters[cc->Owner.GetHandle()] = CharacterGhostPair{ character, bodyIndex }; } bool DynamicWorld::IsCharacterGrounded(const Entity& entity) @@ -397,7 +432,7 @@ namespace Nuake const uint32_t entityHandle = entity.GetHandle(); if (_registeredCharacters.find(entityHandle) != _registeredCharacters.end()) { - auto& characterController = _registeredCharacters[entityHandle]; + auto& characterController = _registeredCharacters[entityHandle].Character; const auto groundState = characterController->GetGroundState(); return groundState == JPH::CharacterBase::EGroundState::OnGround; @@ -493,7 +528,7 @@ namespace Nuake { Entity entity { (entt::entity)e.first, Engine::GetCurrentScene().get()}; - Ref characterController = e.second; + Ref characterController = e.second.Character; JPH::Mat44 joltTransform = characterController->GetWorldTransform(); const auto bodyRotation = characterController->GetRotation(); @@ -571,7 +606,7 @@ namespace Nuake auto characterController = characterControllerComponent.GetCharacterController(); const auto& broadPhaseLayerFilter = _JoltPhysicsSystem->GetDefaultBroadPhaseLayerFilter(Layers::NON_MOVING); - const auto& LayerFilter = _JoltPhysicsSystem->GetDefaultLayerFilter(Layers::MOVING); + const auto& LayerFilter = _JoltPhysicsSystem->GetDefaultLayerFilter(Layers::CHARACTER); const auto& joltGravity = _JoltPhysicsSystem->GetGravity(); auto& tempAllocatorPtr = *(joltTempAllocator); if (characterController->AutoStepping) @@ -583,11 +618,11 @@ namespace Nuake joltUpdateSettings.mWalkStairsStepForwardTest = characterController->StepDistance; joltUpdateSettings.mWalkStairsMinStepForward = characterController->StepMinDistance; - c.second->ExtendedUpdate(ts, joltGravity, joltUpdateSettings, broadPhaseLayerFilter, LayerFilter, { }, { }, tempAllocatorPtr); + c.second.Character->ExtendedUpdate(ts, joltGravity, joltUpdateSettings, broadPhaseLayerFilter, LayerFilter, { }, { }, tempAllocatorPtr); } else { - c.second->Update(ts, joltGravity, broadPhaseLayerFilter, LayerFilter, {}, {}, tempAllocatorPtr); + c.second.Character->Update(ts, joltGravity, broadPhaseLayerFilter, LayerFilter, {}, {}, tempAllocatorPtr); } } } @@ -599,6 +634,30 @@ namespace Nuake Logger::Log("Failed to run simulation update", "physics", CRITICAL); } + for (auto& c : _registeredCharacters) + { + uint32_t ghostId = c.second.Ghost; + + JPH::Mat44 joltTransform = c.second.Character->GetWorldTransform(); + const auto bodyRotation = c.second.Character->GetRotation(); + Matrix4 transform = glm::mat4( + joltTransform(0, 0), joltTransform(1, 0), joltTransform(2, 0), joltTransform(3, 0), + joltTransform(0, 1), joltTransform(1, 1), joltTransform(2, 1), joltTransform(3, 1), + joltTransform(0, 2), joltTransform(1, 2), joltTransform(2, 2), joltTransform(3, 2), + joltTransform(0, 3), joltTransform(1, 3), joltTransform(2, 3), joltTransform(3, 3) + ); + + Vector3 scale = Vector3(); + Quat rotation = Quat(); + Vector3 pos = Vector3(); + Vector3 skew = Vector3(); + Vector4 pesp = Vector4(); + glm::decompose(transform, scale, rotation, pos, skew, pesp); + + //auto& bodyInterface = _JoltPhysicsSystem->GetBodyInterfaceNoLock(); + _JoltBodyInterface->MoveKinematic(static_cast(ghostId), JPH::Vec3{ pos.x, pos.y, pos.z }, { rotation.x, rotation.y, rotation.z, rotation.w }, 1.0); + } + SyncEntitiesTranforms(); SyncCharactersTransforms(); } @@ -629,8 +688,12 @@ namespace Nuake const uint32_t entityHandle = entity.GetHandle(); if (_registeredCharacters.find(entityHandle) != _registeredCharacters.end()) { - auto& characterController = _registeredCharacters[entityHandle]; - characterController->SetLinearVelocity(JPH::Vec3(velocity.x, velocity.y, velocity.z)); + auto& characterController = _registeredCharacters[entityHandle].Character; + const auto& joltVelocity = JPH::Vec3(velocity.x, velocity.y, velocity.z); + characterController->SetLinearVelocity(joltVelocity); + + auto& ghost = _registeredCharacters[entityHandle].Ghost; + //_JoltBodyInterface->SetLinearVelocity(static_cast(ghost), joltVelocity); } } @@ -640,7 +703,7 @@ namespace Nuake for (const auto& body : _registeredBodies) { auto bodyId = static_cast(body); - auto entityId = static_cast(bodyInterface.GetUserData(bodyId)); + auto entityId = bodyInterface.GetUserData(bodyId); if (entityId == entity.GetID()) { bodyInterface.AddForce(bodyId, JPH::Vec3(force.x, force.y, force.z)); diff --git a/Nuake/src/Physics/DynamicWorld.h b/Nuake/src/Physics/DynamicWorld.h index 5569b799..78226b9e 100644 --- a/Nuake/src/Physics/DynamicWorld.h +++ b/Nuake/src/Physics/DynamicWorld.h @@ -35,6 +35,12 @@ namespace Nuake namespace Physics { + struct CharacterGhostPair + { + Ref Character; + uint32_t Ghost; + }; + class DynamicWorld { private: @@ -48,7 +54,7 @@ namespace Nuake BPLayerInterfaceImpl* _JoltBroadphaseLayerInterface; std::vector _registeredBodies; - std::map> _registeredCharacters; + std::map _registeredCharacters; public: DynamicWorld(); From 7b53e7baaa9d3270d96d368cd7241ac34c3c23b2 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 1 Apr 2024 12:54:10 -0400 Subject: [PATCH 14/26] Fixed scene loading through menu bar --- Editor/src/Windows/EditorInterface.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 0c535e4e..c9e93a00 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -1964,7 +1964,8 @@ namespace Nuake { std::string projectPath = FileDialog::OpenFile(".scene"); Ref scene = Scene::New(); - if (!scene->Deserialize(FileSystem::ReadFile(projectPath, true))) + const std::string& fileContent = FileSystem::ReadFile(projectPath, true); + if (!scene->Deserialize(json::parse(fileContent))) { Logger::Log("Error failed loading scene: " + projectPath, "editor", CRITICAL); return; From 72e2b6984e07a6802ca6d683055ca3deb3558f10 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 1 Apr 2024 13:05:02 -0400 Subject: [PATCH 15/26] Added ghost body on character controllers to trigger sensors bodies --- Nuake/src/Physics/DynamicWorld.cpp | 151 ++++++++++++++--------------- 1 file changed, 74 insertions(+), 77 deletions(-) diff --git a/Nuake/src/Physics/DynamicWorld.cpp b/Nuake/src/Physics/DynamicWorld.cpp index 77ee13b6..596c634e 100644 --- a/Nuake/src/Physics/DynamicWorld.cpp +++ b/Nuake/src/Physics/DynamicWorld.cpp @@ -74,26 +74,8 @@ namespace Nuake static constexpr uint8_t KINEMATIC = 2; static constexpr uint8_t CHARACTER_GHOST = 3; static constexpr uint8_t CHARACTER = 4; - static constexpr uint8_t NUM_LAYERS = 5; - }; - - // Function that determines if two object layers can collide - static bool MyObjectCanCollide(JPH::ObjectLayer inObject1, JPH::ObjectLayer inObject2) - { - switch (inObject1) - { - case Layers::NON_MOVING: - return inObject2 == Layers::MOVING || inObject2 == Layers::KINEMATIC || inObject2 == Layers::CHARACTER; // Non moving only collides with moving - case Layers::MOVING: - return true; // Moving collides with everything - case Layers::KINEMATIC: - return inObject2 == Layers::NON_MOVING || inObject2 == Layers::MOVING || inObject2 == Layers::CHARACTER; // Only collides with non moving - case Layers::CHARACTER: - return true; - default: - //JPH_ASSERT(false); - return false; - } + static constexpr uint8_t SENSORS = 5; + static constexpr uint8_t NUM_LAYERS = 6; }; // Each broadphase layer results in a separate bounding volume tree in the broad phase. You at least want to have @@ -120,6 +102,7 @@ namespace Nuake mObjectToBroadPhase[Layers::MOVING] = BroadPhaseLayers::MOVING; mObjectToBroadPhase[Layers::CHARACTER] = BroadPhaseLayers::MOVING; mObjectToBroadPhase[Layers::CHARACTER_GHOST] = BroadPhaseLayers::MOVING; + mObjectToBroadPhase[Layers::SENSORS] = BroadPhaseLayers::MOVING; } virtual JPH::uint GetNumBroadPhaseLayers() const override @@ -134,38 +117,10 @@ namespace Nuake return mObjectToBroadPhase[inLayer]; } -#if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED) - virtual const char* GetBroadPhaseLayerName(BroadPhaseLayer inLayer) const override - { - switch ((BroadPhaseLayer::Type)inLayer) - { - case (BroadPhaseLayer::Type)BroadPhaseLayers::NON_MOVING: return "NON_MOVING"; - case (BroadPhaseLayer::Type)BroadPhaseLayers::MOVING: return "MOVING"; - default: JPH_ASSERT(false); return "INVALID"; - } - } -#endif // JPH_EXTERNAL_PROFILE || JPH_PROFILE_ENABLED - private: JPH::BroadPhaseLayer mObjectToBroadPhase[Layers::NUM_LAYERS]; }; - // Function that determines if two broadphase layers can collide - static bool MyBroadPhaseCanCollide(JPH::ObjectLayer inLayer1, JPH::BroadPhaseLayer inLayer2) - { - using namespace JPH; - switch (inLayer1) - { - case Layers::NON_MOVING: - return inLayer2 == BroadPhaseLayers::MOVING; - case Layers::MOVING: - return true; - default: - JPH_ASSERT(false); - return false; - } - } - // An example contact listener class MyContactListener : public JPH::ContactListener { @@ -230,8 +185,9 @@ namespace Nuake return inLayer2 == BroadPhaseLayers::MOVING; case Layers::MOVING: return true; + case Layers::SENSORS: + return inLayer2 == BroadPhaseLayers::MOVING;; default: - return false; } } @@ -245,13 +201,15 @@ namespace Nuake switch (inObject1) { case Layers::NON_MOVING: - return inObject2 == Layers::MOVING || Layers::CHARACTER_GHOST; // Non moving only collides with moving + return inObject2 == Layers::MOVING || inObject2 == Layers::CHARACTER_GHOST || inObject2 == Layers::CHARACTER; // Non moving only collides with moving case Layers::MOVING: return true; // Moving collides with everything case Layers::CHARACTER_GHOST: - return true;// inObject2 != Layers::CHARACTER; + return inObject2 != Layers::CHARACTER; case Layers::CHARACTER: return inObject2 != Layers::CHARACTER_GHOST; + case Layers::SENSORS: + return inObject2 == Layers::MOVING || inObject2 == Layers::CHARACTER_GHOST; default: return false; @@ -328,14 +286,30 @@ namespace Nuake layer = Layers::MOVING; } + if (rb->IsTrigger()) + { + layer = Layers::SENSORS; + motionType = JPH::EMotionType::Kinematic; + } + const auto& startPos = rb->GetPosition(); const Quat& bodyRotation = rb->GetRotation(); const auto& joltRotation = JPH::Quat(bodyRotation.x, bodyRotation.y, bodyRotation.z, bodyRotation.w); const auto& joltPos = JPH::Vec3(startPos.x, startPos.y, startPos.z); - auto joltShape = GetJoltShape(rb->GetShape()); + JPH::Ref joltShape = GetJoltShape(rb->GetShape()); + + if (!joltShape) + { + return; + } JPH::BodyCreationSettings bodySettings(joltShape, joltPos, joltRotation, motionType, layer); bodySettings.mIsSensor = rb->IsTrigger(); + if (bodySettings.mIsSensor) + { + bodySettings.mCollideKinematicVsNonDynamic = true; + } + bodySettings.mAllowedDOFs = (JPH::EAllowedDOFs::All); if (rb->GetLockXAxis()) @@ -375,7 +349,6 @@ namespace Nuake // Create the actual rigid body JPH::BodyID body = _JoltBodyInterface->CreateAndAddBody(bodySettings, JPH::EActivation::Activate); // Note that if we run out of bodies this can return nullptr uint32_t bodyIndex = (uint32_t)body.GetIndexAndSequenceNumber(); - auto userData = _JoltBodyInterface->GetUserData(body); _registeredBodies.push_back(bodyIndex); } @@ -402,8 +375,8 @@ namespace Nuake // add ghost kinematic body to respond to hit test as the virtual char are not present in the world. JPH::BodyInterface& bodyInterface = _JoltPhysicsSystem->GetBodyInterface(); - const float mass = 0.1f; - JPH::EMotionType motionType = JPH::EMotionType::Kinematic; + const float mass = 0.0f; + JPH::EMotionType motionType = JPH::EMotionType::Dynamic; JPH::ObjectLayer layer = Layers::CHARACTER_GHOST; const auto& startPos = joltPosition; @@ -416,7 +389,7 @@ namespace Nuake Logger::Log("ERROR"); } - //bodySettings.mUserData = entityId; + bodySettings.mUserData = 1337; // Create the actual rigid body JPH::BodyID body = _JoltBodyInterface->CreateAndAddBody(bodySettings, JPH::EActivation::Activate); // Note that if we run out of bodies this can return nullptr @@ -506,6 +479,15 @@ namespace Nuake glm::decompose(transform, scale, rotation, pos, skew, pesp); auto entId = static_cast(bodyInterface.GetUserData(bodyId)); + if (entId == 1337) + { + Entity entity = Engine::GetCurrentScene()->GetEntity("Gizmo"); + auto& transformComponent = entity.GetComponent(); + transformComponent.SetLocalPosition(pos); + transformComponent.SetLocalRotation(Quat(bodyRotation.GetW(), bodyRotation.GetX(), bodyRotation.GetY(), bodyRotation.GetZ())); + transformComponent.SetLocalTransform(transform); + transformComponent.Dirty = true; + } if (entId != 0) { Entity entity = Engine::GetCurrentScene()->GetEntityByID(entId); @@ -573,7 +555,7 @@ namespace Nuake if(ts > minStepDuration) { #ifdef NK_DEBUG - Logger::Log("Large step detected: " + std::to_string(ts), "physics", WARNING); + //Logger::Log("Large step detected: " + std::to_string(ts), "physics", WARNING); #endif collisionSteps = static_cast(ts) / minStepDuration; } @@ -624,6 +606,27 @@ namespace Nuake { c.second.Character->Update(ts, joltGravity, broadPhaseLayerFilter, LayerFilter, {}, {}, tempAllocatorPtr); } + + uint32_t ghostId = c.second.Ghost; + + JPH::Mat44 joltTransform = c.second.Character->GetWorldTransform(); + const auto bodyRotation = c.second.Character->GetRotation(); + Matrix4 transform = glm::mat4( + joltTransform(0, 0), joltTransform(1, 0), joltTransform(2, 0), joltTransform(3, 0), + joltTransform(0, 1), joltTransform(1, 1), joltTransform(2, 1), joltTransform(3, 1), + joltTransform(0, 2), joltTransform(1, 2), joltTransform(2, 2), joltTransform(3, 2), + joltTransform(0, 3), joltTransform(1, 3), joltTransform(2, 3), joltTransform(3, 3) + ); + + Vector3 scale = Vector3(); + Quat rotation = Quat(); + Vector3 pos = Vector3(); + Vector3 skew = Vector3(); + Vector4 pesp = Vector4(); + glm::decompose(transform, scale, rotation, pos, skew, pesp); + + //auto& bodyInterface = _JoltPhysicsSystem->GetBodyInterfaceNoLock(); + _JoltBodyInterface->MoveKinematic(static_cast(ghostId), JPH::Vec3{ pos.x, pos.y, pos.z }, { rotation.x, rotation.y, rotation.z, rotation.w }, ts); } } @@ -636,26 +639,7 @@ namespace Nuake for (auto& c : _registeredCharacters) { - uint32_t ghostId = c.second.Ghost; - - JPH::Mat44 joltTransform = c.second.Character->GetWorldTransform(); - const auto bodyRotation = c.second.Character->GetRotation(); - Matrix4 transform = glm::mat4( - joltTransform(0, 0), joltTransform(1, 0), joltTransform(2, 0), joltTransform(3, 0), - joltTransform(0, 1), joltTransform(1, 1), joltTransform(2, 1), joltTransform(3, 1), - joltTransform(0, 2), joltTransform(1, 2), joltTransform(2, 2), joltTransform(3, 2), - joltTransform(0, 3), joltTransform(1, 3), joltTransform(2, 3), joltTransform(3, 3) - ); - - Vector3 scale = Vector3(); - Quat rotation = Quat(); - Vector3 pos = Vector3(); - Vector3 skew = Vector3(); - Vector4 pesp = Vector4(); - glm::decompose(transform, scale, rotation, pos, skew, pesp); - - //auto& bodyInterface = _JoltPhysicsSystem->GetBodyInterfaceNoLock(); - _JoltBodyInterface->MoveKinematic(static_cast(ghostId), JPH::Vec3{ pos.x, pos.y, pos.z }, { rotation.x, rotation.y, rotation.z, rotation.w }, 1.0); + } SyncEntitiesTranforms(); @@ -693,6 +677,11 @@ namespace Nuake characterController->SetLinearVelocity(joltVelocity); auto& ghost = _registeredCharacters[entityHandle].Ghost; + auto ghostPos = _JoltBodyInterface->GetPosition(static_cast(ghost)); + //std::cout << "Ghost pos: " << ghostPos.GetX() << ", " << ghostPos.GetY() << ", " << ghostPos.GetZ() << std::endl; + + auto charPos = characterController->GetPosition(); + //std::cout << "Char pos: " << charPos.GetX() << ", " << charPos.GetY() << ", " << charPos.GetZ() << std::endl; //_JoltBodyInterface->SetLinearVelocity(static_cast(ghost), joltVelocity); } } @@ -800,6 +789,14 @@ namespace Nuake break; } + if (!result.IsValid()) + { + const std::string errorMessage = std::string("Failed to create physics shape: ") + result.GetError().c_str(); + Logger::Log(errorMessage, "physics", WARNING); + + return nullptr; + } + return result.Get(); } } From 0693ac1ad0ab2be1a26617c2bd24549a6fc37863 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 1 Apr 2024 13:19:33 -0400 Subject: [PATCH 16/26] Added collision callback thread safe queue --- Nuake/src/Physics/DynamicWorld.cpp | 22 ++++++++++++-- Nuake/src/Physics/DynamicWorld.h | 12 ++++++++ Nuake/src/Scene/Systems/PhysicsSystem.cpp | 37 ----------------------- 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/Nuake/src/Physics/DynamicWorld.cpp b/Nuake/src/Physics/DynamicWorld.cpp index 596c634e..24336592 100644 --- a/Nuake/src/Physics/DynamicWorld.cpp +++ b/Nuake/src/Physics/DynamicWorld.cpp @@ -124,7 +124,15 @@ namespace Nuake // An example contact listener class MyContactListener : public JPH::ContactListener { + private: + Physics::DynamicWorld* _World; + public: + MyContactListener(Physics::DynamicWorld* world) + : _World(world) + { + } + // See: ContactListener virtual JPH::ValidateResult OnContactValidate(const JPH::Body& inBody1, const JPH::Body& inBody2, JPH::RVec3Arg inBaseOffset, const JPH::CollideShapeResult& inCollisionResult) override { @@ -146,6 +154,9 @@ namespace Nuake const std::string entity2Name = entity2.GetComponent().Name; Logger::Log("Collision detected between " + entity1Name + " and " + entity2Name); + + Physics::CollisionCallbackData data; + _World->RegisterCollisionCallback(std::move(data)); } virtual void OnContactPersisted(const JPH::Body& inBody1, const JPH::Body& inBody2, const JPH::ContactManifold& inManifold, JPH::ContactSettings& ioSettings) override @@ -217,7 +228,6 @@ namespace Nuake } }; - BPLayerInterfaceImpl JoltBroadphaseLayerInterface = BPLayerInterfaceImpl(); ObjectVsBroadPhaseLayerFilterImpl JoltObjectVSBroadphaseLayerFilter = ObjectVsBroadPhaseLayerFilterImpl(); ObjectLayerPairFilterImpl JoltObjectVSObjectLayerFilter; @@ -246,7 +256,7 @@ namespace Nuake // A contact listener gets notified when bodies (are about to) collide, and when they separate again. // Note that this is called from a job so whatever you do here needs to be thread safe. // Registering one is entirely optional. - _contactListener = CreateScope(); + _contactListener = CreateScope(this); _JoltPhysicsSystem->SetContactListener(_contactListener.get()); // The main way to interact with the bodies in the physics system is through the body interface. There is a locking and a non-locking @@ -667,6 +677,14 @@ namespace Nuake } } + void DynamicWorld::RegisterCollisionCallback(const CollisionCallbackData& data) + { + // This will be called from multiple threads + std::scoped_lock lock(_CollisionCallbackMutex); + + _CollisionCallbacks.push_back(std::move(data)); + } + void DynamicWorld::MoveAndSlideCharacterController(const Entity& entity, const Vector3& velocity) { const uint32_t entityHandle = entity.GetHandle(); diff --git a/Nuake/src/Physics/DynamicWorld.h b/Nuake/src/Physics/DynamicWorld.h index 78226b9e..5a7b13ae 100644 --- a/Nuake/src/Physics/DynamicWorld.h +++ b/Nuake/src/Physics/DynamicWorld.h @@ -41,6 +41,14 @@ namespace Nuake uint32_t Ghost; }; + struct CollisionCallbackData + { + uint32_t Entity1; + uint32_t Entity2; + Vector3 Normal; + Vector3 Position; + }; + class DynamicWorld { private: @@ -56,6 +64,8 @@ namespace Nuake std::vector _registeredBodies; std::map _registeredCharacters; + std::mutex _CollisionCallbackMutex; + std::vector _CollisionCallbacks; public: DynamicWorld(); @@ -75,6 +85,8 @@ namespace Nuake void StepSimulation(Timestep ts); void Clear(); + void RegisterCollisionCallback(const CollisionCallbackData& data); + private: JPH::Ref GetJoltShape(const Ref shape); void SyncEntitiesTranforms(); diff --git a/Nuake/src/Scene/Systems/PhysicsSystem.cpp b/Nuake/src/Scene/Systems/PhysicsSystem.cpp index bb54d874..3988b325 100644 --- a/Nuake/src/Scene/Systems/PhysicsSystem.cpp +++ b/Nuake/src/Scene/Systems/PhysicsSystem.cpp @@ -52,12 +52,6 @@ namespace Nuake { auto [transform, brush] = brushes.get(e); - for (auto& r : brush.Rigidbody) - { - //r->m_Transform->setOrigin(btVector3(transform.GlobalTranslation.x, transform.GlobalTranslation.y, transform.GlobalTranslation.z)); - //r->UpdateTransform(*r->m_Transform); - } - if (!brush.IsFunc) continue; @@ -73,37 +67,6 @@ namespace Nuake } } } - - //auto bspTriggerView = m_Scene->m_Registry.view(); - //for (auto e : bspTriggerView) - //{ - // auto [transform, brush, trigger] = bspTriggerView.get(e); - // trigger.GhostObject->ScanOverlap(); - - // brush.Targets.clear(); - // auto targetnameView = m_Scene->m_Registry.view(); - // for (auto e2 : targetnameView) - // { - // auto [ttransform, name] = targetnameView.get(e2); - - // if (name.Name == brush.target) { - // brush.Targets.push_back(Entity{ e2, m_Scene }); - // } - // } - //} - - - /*auto physicGroup = m_Scene->m_Registry.view(); - for (auto e : physicGroup) { - auto [transform, rb] = physicGroup.get(e); - rb.SyncTransformComponent(&m_Scene->m_Registry.get(e)); - }*/ - - //auto ccGroup = m_Scene->m_Registry.view(); - //for (auto e : ccGroup) { - // auto [transform, rb] = ccGroup.get(e); - // rb.SyncWithTransform(m_Scene->m_Registry.get(e)); - //} } void PhysicsSystem::FixedUpdate(Timestep ts) From 5217a8639b04ebc2f14ec5828f8c846a38421faa Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 1 Apr 2024 14:28:59 -0400 Subject: [PATCH 17/26] Started collision callbacks for .Net --- Nuake/src/Physics/CollisionData.h | 16 +++++++++ Nuake/src/Physics/DynamicWorld.cpp | 39 +++++++++++++++------ Nuake/src/Physics/DynamicWorld.h | 15 +++----- Nuake/src/Physics/PhysicsManager.cpp | 5 +++ Nuake/src/Physics/PhysicsManager.h | 3 ++ Nuake/src/Scene/Systems/ScriptingSystem.cpp | 23 +++++++++++- Nuake/src/Scene/Systems/ScriptingSystem.h | 3 ++ 7 files changed, 82 insertions(+), 22 deletions(-) create mode 100644 Nuake/src/Physics/CollisionData.h diff --git a/Nuake/src/Physics/CollisionData.h b/Nuake/src/Physics/CollisionData.h new file mode 100644 index 00000000..d6f6d95e --- /dev/null +++ b/Nuake/src/Physics/CollisionData.h @@ -0,0 +1,16 @@ +#pragma once +#include + +namespace Nuake { + + namespace Physics { + + struct CollisionData + { + uint32_t Entity1; + uint32_t Entity2; + Vector3 Normal; + Vector3 Position; + }; + } +} diff --git a/Nuake/src/Physics/DynamicWorld.cpp b/Nuake/src/Physics/DynamicWorld.cpp index 24336592..527c9c18 100644 --- a/Nuake/src/Physics/DynamicWorld.cpp +++ b/Nuake/src/Physics/DynamicWorld.cpp @@ -144,18 +144,23 @@ namespace Nuake virtual void OnContactAdded(const JPH::Body& inBody1, const JPH::Body& inBody2, const JPH::ContactManifold& inManifold, JPH::ContactSettings& ioSettings) override { - auto entId1 = static_cast(inBody1.GetUserData()); - Entity entity1 = Engine::GetCurrentScene()->GetEntityByID(entId1); + int entity1 = static_cast(inBody1.GetUserData()); + int entity2 = static_cast(inBody2.GetUserData()); - auto entId2 = static_cast(inBody2.GetUserData()); - Entity entity2 = Engine::GetCurrentScene()->GetEntityByID(entId2); + JPH::Vec3 joltNormal = inManifold.mWorldSpaceNormal; + Vector3 normal = Vector3(joltNormal.GetX(), joltNormal.GetY(), joltNormal.GetZ()); - const std::string entity1Name = entity1.GetComponent().Name; - const std::string entity2Name = entity2.GetComponent().Name; + JPH::Vec3 joltPos = inManifold.GetWorldSpaceContactPointOn1(0); + Vector3 position = Vector3(joltPos.GetX(), joltPos.GetY(), joltPos.GetZ()); - Logger::Log("Collision detected between " + entity1Name + " and " + entity2Name); + Physics::CollisionData data + { + entity1, + entity2, + normal, + position + }; - Physics::CollisionCallbackData data; _World->RegisterCollisionCallback(std::move(data)); } @@ -399,12 +404,12 @@ namespace Nuake Logger::Log("ERROR"); } - bodySettings.mUserData = 1337; + bodySettings.mUserData = cc->Owner.GetHandle(); // Create the actual rigid body JPH::BodyID body = _JoltBodyInterface->CreateAndAddBody(bodySettings, JPH::EActivation::Activate); // Note that if we run out of bodies this can return nullptr uint32_t bodyIndex = body.GetIndexAndSequenceNumber(); - _registeredBodies.push_back(bodyIndex); + //_registeredBodies.push_back(bodyIndex); // To get the jolt character control from a scene entity. _registeredCharacters[cc->Owner.GetHandle()] = CharacterGhostPair{ character, bodyIndex }; @@ -548,6 +553,12 @@ namespace Nuake void DynamicWorld::StepSimulation(Timestep ts) { + // Clear collisions, before very step + { + std::scoped_lock lock(_CollisionCallbackMutex); + _CollisionCallbacks.clear(); + } + if (ts > 0.1f) { ts = 0.08f; @@ -677,7 +688,7 @@ namespace Nuake } } - void DynamicWorld::RegisterCollisionCallback(const CollisionCallbackData& data) + void DynamicWorld::RegisterCollisionCallback(const CollisionData& data) { // This will be called from multiple threads std::scoped_lock lock(_CollisionCallbackMutex); @@ -685,6 +696,12 @@ namespace Nuake _CollisionCallbacks.push_back(std::move(data)); } + const std::vector& DynamicWorld::GetCollisionsData() + { + std::scoped_lock lock(_CollisionCallbackMutex); + return _CollisionCallbacks; + } + void DynamicWorld::MoveAndSlideCharacterController(const Entity& entity, const Vector3& velocity) { const uint32_t entityHandle = entity.GetHandle(); diff --git a/Nuake/src/Physics/DynamicWorld.h b/Nuake/src/Physics/DynamicWorld.h index 5a7b13ae..97555abb 100644 --- a/Nuake/src/Physics/DynamicWorld.h +++ b/Nuake/src/Physics/DynamicWorld.h @@ -9,6 +9,7 @@ #include #include "CharacterController.h" +#include "CollisionData.h" #include "Jolt/Jolt.h" @@ -41,13 +42,7 @@ namespace Nuake uint32_t Ghost; }; - struct CollisionCallbackData - { - uint32_t Entity1; - uint32_t Entity2; - Vector3 Normal; - Vector3 Position; - }; + class DynamicWorld { @@ -65,7 +60,7 @@ namespace Nuake std::map _registeredCharacters; std::mutex _CollisionCallbackMutex; - std::vector _CollisionCallbacks; + std::vector _CollisionCallbacks; public: DynamicWorld(); @@ -85,8 +80,8 @@ namespace Nuake void StepSimulation(Timestep ts); void Clear(); - void RegisterCollisionCallback(const CollisionCallbackData& data); - + void RegisterCollisionCallback(const CollisionData& data); + const std::vector& GetCollisionsData(); private: JPH::Ref GetJoltShape(const Ref shape); void SyncEntitiesTranforms(); diff --git a/Nuake/src/Physics/PhysicsManager.cpp b/Nuake/src/Physics/PhysicsManager.cpp index 620c5b77..06120b7e 100644 --- a/Nuake/src/Physics/PhysicsManager.cpp +++ b/Nuake/src/Physics/PhysicsManager.cpp @@ -42,6 +42,11 @@ namespace Nuake return m_World->Raycast(from, to); } + const std::vector& PhysicsManager::GetCollisions() + { + return m_World->GetCollisionsData(); + } + void PhysicsManager::DrawDebug() { if (m_DrawDebug) diff --git a/Nuake/src/Physics/PhysicsManager.h b/Nuake/src/Physics/PhysicsManager.h index f4074f07..9c310b12 100644 --- a/Nuake/src/Physics/PhysicsManager.h +++ b/Nuake/src/Physics/PhysicsManager.h @@ -3,6 +3,7 @@ #include "../Scene/Entities/Entity.h" #include "DynamicWorld.h" #include "Rigibody.h" +#include "CollisionData.h" #include "RaycastResult.h" @@ -49,6 +50,8 @@ namespace Nuake std::vector Raycast(const Vector3& from, const Vector3& to); + const std::vector& GetCollisions(); + void RegisterBody(Ref rb); void RegisterGhostBody(Ref rb); void RegisterCharacterController(Ref c); diff --git a/Nuake/src/Scene/Systems/ScriptingSystem.cpp b/Nuake/src/Scene/Systems/ScriptingSystem.cpp index 708d8a27..1e6d5be3 100644 --- a/Nuake/src/Scene/Systems/ScriptingSystem.cpp +++ b/Nuake/src/Scene/Systems/ScriptingSystem.cpp @@ -5,7 +5,7 @@ #include "Engine.h" #include "src/Scripting/ScriptingEngineNet.h" - +#include "src/Physics/PhysicsManager.h" namespace Nuake { @@ -129,6 +129,8 @@ namespace Nuake auto scriptInstance = scriptingEngineNet.GetEntityScript(entity); scriptInstance.InvokeMethod("OnFixedUpdate", ts.GetSeconds()); } + + DispatchPhysicCallbacks(); } void ScriptingSystem::Exit() @@ -166,4 +168,23 @@ namespace Nuake ScriptingEngine::Close(); ScriptingEngineNet::Get().Uninitialize(); } + + void ScriptingSystem::DispatchPhysicCallbacks() + { + auto& scriptingEngineNet = ScriptingEngineNet::Get(); + + auto& physicsManager = PhysicsManager::Get(); + const auto& collisions = physicsManager.GetCollisions(); + for (const auto& col : collisions) + { + Entity entity1 = m_Scene->GetEntityByID(col.Entity1); + Entity entity2 = m_Scene->GetEntityByID(col.Entity2); + + if (entity1.IsValid() && scriptingEngineNet.HasEntityScriptInstance(entity1)) + { + auto scriptInstance = scriptingEngineNet.GetEntityScript(entity1); + scriptInstance.InvokeMethod("OnCollision", col.Entity1, col.Entity2); + } + } + } } diff --git a/Nuake/src/Scene/Systems/ScriptingSystem.h b/Nuake/src/Scene/Systems/ScriptingSystem.h index 6ab630c2..106e7ba8 100644 --- a/Nuake/src/Scene/Systems/ScriptingSystem.h +++ b/Nuake/src/Scene/Systems/ScriptingSystem.h @@ -13,5 +13,8 @@ namespace Nuake { void Draw() override {} void FixedUpdate(Timestep ts) override; void Exit() override; + + private: + void DispatchPhysicCallbacks(); }; } From caa968feafed53b0ead5ed4000b46284fbb68591 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 1 Apr 2024 14:35:12 -0400 Subject: [PATCH 18/26] Fixed callbacks --- Nuake/src/Scene/Systems/ScriptingSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Nuake/src/Scene/Systems/ScriptingSystem.cpp b/Nuake/src/Scene/Systems/ScriptingSystem.cpp index 1e6d5be3..786dcd72 100644 --- a/Nuake/src/Scene/Systems/ScriptingSystem.cpp +++ b/Nuake/src/Scene/Systems/ScriptingSystem.cpp @@ -183,7 +183,7 @@ namespace Nuake if (entity1.IsValid() && scriptingEngineNet.HasEntityScriptInstance(entity1)) { auto scriptInstance = scriptingEngineNet.GetEntityScript(entity1); - scriptInstance.InvokeMethod("OnCollision", col.Entity1, col.Entity2); + scriptInstance.InvokeMethod("OnCollision", (int)col.Entity1, (int)col.Entity2); } } } From 96fa2294225b2ad1ba272a59f870f7dc85f32388 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 1 Apr 2024 14:54:20 -0400 Subject: [PATCH 19/26] Fixed callback --- NuakeNet/src/Components.cs | 14 +++++++++++++- NuakeNet/src/Entity.cs | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/NuakeNet/src/Components.cs b/NuakeNet/src/Components.cs index 5297edcd..5bb6be0b 100644 --- a/NuakeNet/src/Components.cs +++ b/NuakeNet/src/Components.cs @@ -22,12 +22,24 @@ namespace Nuake.Net } public class NameComponent : IComponent { + internal static unsafe delegate* GetNameIcall; + public NameComponent(int entityId) { EntityID = entityId; } - public string Name { get; set; } + public string Name + { + get + { + unsafe { return GetNameIcall(EntityID).ToString(); } + } + set + { + + } + } } public class PrefabComponent : IComponent diff --git a/NuakeNet/src/Entity.cs b/NuakeNet/src/Entity.cs index e5b2747f..12f13ffa 100644 --- a/NuakeNet/src/Entity.cs +++ b/NuakeNet/src/Entity.cs @@ -1,11 +1,20 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Text; using System.Threading.Tasks; namespace Nuake.Net { + public struct CollisionData + { + Entity Entity1; + Entity Entity2; + Vector3 Normal; + Vector3 Position; + } + public class Entity { internal static unsafe delegate* EntityHasComponentIcall; @@ -43,6 +52,17 @@ namespace Nuake.Net public virtual void OnFixedUpdate(float dt) { } public virtual void OnDestroy() { } + public virtual void OnCollision(int entity1, int entity2) + { + Engine.Log("penis"); + } + + // Physics + public void OnCollisionInternal(int entity1, int entity2) + { + //OnCollision(new Entity { ECSHandle = entity1 }, new Entity { ECSHandle = entity2 }); + } + protected static Dictionary MappingTypeEnum = new Dictionary() { { typeof(ParentComponent), ComponentTypes.PARENT }, From 1bc6bad5bea817eeabbdb33df8aff41ae1b1b3b4 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 1 Apr 2024 14:56:08 -0400 Subject: [PATCH 20/26] Reroutine physics callbacks to casts to Entity objects --- Nuake/src/Scene/Systems/ScriptingSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Nuake/src/Scene/Systems/ScriptingSystem.cpp b/Nuake/src/Scene/Systems/ScriptingSystem.cpp index 786dcd72..ad9aa71a 100644 --- a/Nuake/src/Scene/Systems/ScriptingSystem.cpp +++ b/Nuake/src/Scene/Systems/ScriptingSystem.cpp @@ -183,7 +183,7 @@ namespace Nuake if (entity1.IsValid() && scriptingEngineNet.HasEntityScriptInstance(entity1)) { auto scriptInstance = scriptingEngineNet.GetEntityScript(entity1); - scriptInstance.InvokeMethod("OnCollision", (int)col.Entity1, (int)col.Entity2); + scriptInstance.InvokeMethod("OnCollisionInternal", (int)col.Entity1, (int)col.Entity2); } } } From 46bcec62b7cf1cd776ff362854744ed701aa394f Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 1 Apr 2024 14:58:36 -0400 Subject: [PATCH 21/26] Moved callbacks to Update instead of FixedUpdate --- Nuake/src/Scene/Systems/ScriptingSystem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Nuake/src/Scene/Systems/ScriptingSystem.cpp b/Nuake/src/Scene/Systems/ScriptingSystem.cpp index ad9aa71a..f7586ce0 100644 --- a/Nuake/src/Scene/Systems/ScriptingSystem.cpp +++ b/Nuake/src/Scene/Systems/ScriptingSystem.cpp @@ -100,6 +100,8 @@ namespace Nuake auto scriptInstance = scriptingEngineNet.GetEntityScript(entity); scriptInstance.InvokeMethod("OnUpdate", ts.GetSeconds()); } + + DispatchPhysicCallbacks(); } void ScriptingSystem::FixedUpdate(Timestep ts) @@ -129,8 +131,6 @@ namespace Nuake auto scriptInstance = scriptingEngineNet.GetEntityScript(entity); scriptInstance.InvokeMethod("OnFixedUpdate", ts.GetSeconds()); } - - DispatchPhysicCallbacks(); } void ScriptingSystem::Exit() From aad15899fde6b305b211db5b83cafc6c9e819029 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 1 Apr 2024 19:21:58 -0400 Subject: [PATCH 22/26] Fixed F5 not stopping game --- Editor/src/Windows/EditorInterface.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index c9e93a00..564592ae 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -116,7 +116,7 @@ namespace Nuake { if (Engine::IsPlayMode()) { - if (ImGui::Button(ICON_FA_PAUSE, ImVec2(30, 30)) || (Input::IsKeyPressed(GLFW_KEY_F5))) + if (ImGui::Button(ICON_FA_PAUSE, ImVec2(30, 30)) || (Input::IsKeyDown(GLFW_KEY_F5))) { Engine::ExitPlayMode(); @@ -126,7 +126,7 @@ namespace Nuake { } else { - if (ImGui::Button(ICON_FA_PLAY, ImVec2(30, 30))) + if (ImGui::Button(ICON_FA_PLAY, ImVec2(30, 30)) ) { this->SceneSnapshot = Engine::GetCurrentScene()->Copy(); @@ -155,7 +155,7 @@ namespace Nuake { ImGui::BeginDisabled(); } - if ((ImGui::Button(ICON_FA_STOP, ImVec2(30, 30)) || Input::IsKeyPressed(GLFW_KEY_F8)) && Engine::IsPlayMode()) + if ((ImGui::Button(ICON_FA_STOP, ImVec2(30, 30)) || Input::IsKeyPressed(GLFW_KEY_F5)) && Engine::IsPlayMode()) { Engine::ExitPlayMode(); From f86e26a4903befa227bfc3eb7bc16b5b77a78e71 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 1 Apr 2024 19:22:20 -0400 Subject: [PATCH 23/26] Character controller now responds to setting localPosition on the entity --- Nuake/src/Physics/DynamicWorld.cpp | 64 +++++++++---------- Nuake/src/Physics/DynamicWorld.h | 2 + Nuake/src/Physics/PhysicsManager.cpp | 5 ++ Nuake/src/Physics/PhysicsManager.h | 2 + .../src/Scripting/NetModules/SceneNetAPI.cpp | 5 ++ NuakeNet/src/Entity.cs | 5 +- NuakeNet/src/Scene.cs | 1 + 7 files changed, 47 insertions(+), 37 deletions(-) diff --git a/Nuake/src/Physics/DynamicWorld.cpp b/Nuake/src/Physics/DynamicWorld.cpp index 527c9c18..c1308fad 100644 --- a/Nuake/src/Physics/DynamicWorld.cpp +++ b/Nuake/src/Physics/DynamicWorld.cpp @@ -430,6 +430,16 @@ namespace Nuake return false; } + void DynamicWorld::SetCharacterControllerPosition(const Entity & entity, const Vector3 & position) + { + const uint32_t entityHandle = entity.GetHandle(); + if (_registeredCharacters.find(entityHandle) != _registeredCharacters.end()) + { + auto& characterController = _registeredCharacters[entityHandle].Character; + characterController->SetPosition({ position.x, position.y, position.z }); + } + } + std::vector DynamicWorld::Raycast(const Vector3& from, const Vector3& to) { // Create jolt ray @@ -474,37 +484,27 @@ namespace Nuake for (const auto& body : _registeredBodies) { auto bodyId = static_cast(body); - JPH::Vec3 position = bodyInterface.GetCenterOfMassPosition(bodyId); - JPH::Vec3 velocity = bodyInterface.GetLinearVelocity(bodyId); - JPH::Mat44 joltTransform = bodyInterface.GetWorldTransform(bodyId); - const auto bodyRotation = bodyInterface.GetRotation(bodyId); - - Matrix4 transform = glm::mat4( - joltTransform(0, 0), joltTransform(1, 0), joltTransform(2, 0), joltTransform(3, 0), - joltTransform(0, 1), joltTransform(1, 1), joltTransform(2, 1), joltTransform(3, 1), - joltTransform(0, 2), joltTransform(1, 2), joltTransform(2, 2), joltTransform(3, 2), - joltTransform(0, 3), joltTransform(1, 3), joltTransform(2, 3), joltTransform(3, 3) - ); - - Vector3 scale = Vector3(); - Quat rotation = Quat(); - Vector3 pos = Vector3(); - Vector3 skew = Vector3(); - Vector4 pesp = Vector4(); - glm::decompose(transform, scale, rotation, pos, skew, pesp); - - auto entId = static_cast(bodyInterface.GetUserData(bodyId)); - if (entId == 1337) - { - Entity entity = Engine::GetCurrentScene()->GetEntity("Gizmo"); - auto& transformComponent = entity.GetComponent(); - transformComponent.SetLocalPosition(pos); - transformComponent.SetLocalRotation(Quat(bodyRotation.GetW(), bodyRotation.GetX(), bodyRotation.GetY(), bodyRotation.GetZ())); - transformComponent.SetLocalTransform(transform); - transformComponent.Dirty = true; - } - if (entId != 0) + if (auto entId = static_cast(bodyInterface.GetUserData(bodyId)); entId != 0) { + JPH::Vec3 position = bodyInterface.GetCenterOfMassPosition(bodyId); + JPH::Vec3 velocity = bodyInterface.GetLinearVelocity(bodyId); + JPH::Mat44 joltTransform = bodyInterface.GetWorldTransform(bodyId); + const auto bodyRotation = bodyInterface.GetRotation(bodyId); + + Matrix4 transform = glm::mat4( + joltTransform(0, 0), joltTransform(1, 0), joltTransform(2, 0), joltTransform(3, 0), + joltTransform(0, 1), joltTransform(1, 1), joltTransform(2, 1), joltTransform(3, 1), + joltTransform(0, 2), joltTransform(1, 2), joltTransform(2, 2), joltTransform(3, 2), + joltTransform(0, 3), joltTransform(1, 3), joltTransform(2, 3), joltTransform(3, 3) + ); + + Vector3 scale = Vector3(); + Quat rotation = Quat(); + Vector3 pos = Vector3(); + Vector3 skew = Vector3(); + Vector4 pesp = Vector4(); + glm::decompose(transform, scale, rotation, pos, skew, pesp); + Entity entity = Engine::GetCurrentScene()->GetEntityByID(entId); auto& transformComponent = entity.GetComponent(); transformComponent.SetLocalPosition(pos); @@ -517,10 +517,6 @@ namespace Nuake void DynamicWorld::SyncCharactersTransforms() { - // TODO(ANTO): Finish this to connect updated jolt transforms back to the entity. - // The problem was that I dont know yet how to go from jolt body ptr to the entity - // Combinations of find and iterators etc. I do not have the brain power rn zzz. - // const auto& bodyInterface = _JoltPhysicsSystem->GetBodyInterface(); for (const auto& e : _registeredCharacters) { Entity entity { (entt::entity)e.first, Engine::GetCurrentScene().get()}; diff --git a/Nuake/src/Physics/DynamicWorld.h b/Nuake/src/Physics/DynamicWorld.h index 97555abb..5b7f9b4b 100644 --- a/Nuake/src/Physics/DynamicWorld.h +++ b/Nuake/src/Physics/DynamicWorld.h @@ -72,6 +72,8 @@ namespace Nuake void AddGhostbody(Ref gb); void AddCharacterController(Ref cc); bool IsCharacterGrounded(const Entity& entity); + void SetCharacterControllerPosition(const Entity& entity, const Vector3& position); + // This is going to be ugly. TODO: Find a better way that passing itself as a parameter void MoveAndSlideCharacterController(const Entity& entity, const Vector3& velocity); void AddForceToRigidBody(Entity& entity, const Vector3& force); diff --git a/Nuake/src/Physics/PhysicsManager.cpp b/Nuake/src/Physics/PhysicsManager.cpp index 06120b7e..d48b0b98 100644 --- a/Nuake/src/Physics/PhysicsManager.cpp +++ b/Nuake/src/Physics/PhysicsManager.cpp @@ -27,6 +27,11 @@ namespace Nuake m_World->AddCharacterController(cc); } + void PhysicsManager::SetCharacterControllerPosition(const Entity& entity, const Vector3& position) + { + m_World->SetCharacterControllerPosition(entity, position); + } + void PhysicsManager::Step(Timestep ts) { m_World->StepSimulation(ts); diff --git a/Nuake/src/Physics/PhysicsManager.h b/Nuake/src/Physics/PhysicsManager.h index 9c310b12..418f60ce 100644 --- a/Nuake/src/Physics/PhysicsManager.h +++ b/Nuake/src/Physics/PhysicsManager.h @@ -55,5 +55,7 @@ namespace Nuake void RegisterBody(Ref rb); void RegisterGhostBody(Ref rb); void RegisterCharacterController(Ref c); + + void SetCharacterControllerPosition(const Entity& entity, const Vector3& position); }; } diff --git a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp index 2e3a7fe8..f8320002 100644 --- a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp @@ -112,6 +112,11 @@ namespace Nuake { { auto& component = entity.GetComponent(); component.SetLocalPosition({ x, y, z }); + + if (entity.HasComponent()) + { + PhysicsManager::Get().SetCharacterControllerPosition(entity, { x, y, z }); + } } } diff --git a/NuakeNet/src/Entity.cs b/NuakeNet/src/Entity.cs index 12f13ffa..4118778e 100644 --- a/NuakeNet/src/Entity.cs +++ b/NuakeNet/src/Entity.cs @@ -52,15 +52,14 @@ namespace Nuake.Net public virtual void OnFixedUpdate(float dt) { } public virtual void OnDestroy() { } - public virtual void OnCollision(int entity1, int entity2) + public virtual void OnCollision(Entity entity1, Entity entity2) { - Engine.Log("penis"); } // Physics public void OnCollisionInternal(int entity1, int entity2) { - //OnCollision(new Entity { ECSHandle = entity1 }, new Entity { ECSHandle = entity2 }); + OnCollision(new Entity { ECSHandle = entity1 }, new Entity { ECSHandle = entity2 }); } protected static Dictionary MappingTypeEnum = new Dictionary() diff --git a/NuakeNet/src/Scene.cs b/NuakeNet/src/Scene.cs index 1010f094..65edaf11 100644 --- a/NuakeNet/src/Scene.cs +++ b/NuakeNet/src/Scene.cs @@ -21,6 +21,7 @@ namespace Nuake.Net { ECSHandle = handle }; + return entity; } } From 22d8b35373214b6cd9581ce50ccb79cf76410f6f Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 1 Apr 2024 20:16:48 -0400 Subject: [PATCH 24/26] Updated Coral version to .Net 8 --- Nuake/src/Physics/DynamicWorld.h | 1 + .../src/Scripting/NetModules/EngineNetAPI.cpp | 4 ++-- .../src/Scripting/NetModules/InputNetAPI.cpp | 6 ++--- Nuake/src/Scripting/NetModules/NetAPIModule.h | 2 +- .../src/Scripting/NetModules/SceneNetAPI.cpp | 22 +++++++++---------- Nuake/src/Scripting/ScriptingEngineNet.cpp | 15 +++++++------ NuakeNet/premake5.lua | 2 +- premake5.lua | 22 +++++-------------- 8 files changed, 31 insertions(+), 43 deletions(-) diff --git a/Nuake/src/Physics/DynamicWorld.h b/Nuake/src/Physics/DynamicWorld.h index 5b7f9b4b..698b725b 100644 --- a/Nuake/src/Physics/DynamicWorld.h +++ b/Nuake/src/Physics/DynamicWorld.h @@ -13,6 +13,7 @@ #include "Jolt/Jolt.h" +#include namespace JPH { diff --git a/Nuake/src/Scripting/NetModules/EngineNetAPI.cpp b/Nuake/src/Scripting/NetModules/EngineNetAPI.cpp index 4b9725aa..34bcb0c8 100644 --- a/Nuake/src/Scripting/NetModules/EngineNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/EngineNetAPI.cpp @@ -2,9 +2,9 @@ namespace Nuake { - void Log(Coral::NativeString string) + void Log(Coral::String string) { - Logger::Log(string.ToString(), ".net", VERBOSE); + Logger::Log(string, ".net", VERBOSE); } void EngineNetAPI::RegisterMethods() diff --git a/Nuake/src/Scripting/NetModules/InputNetAPI.cpp b/Nuake/src/Scripting/NetModules/InputNetAPI.cpp index e3fc1cdf..32b41358 100644 --- a/Nuake/src/Scripting/NetModules/InputNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/InputNetAPI.cpp @@ -2,7 +2,7 @@ #include "src/Core/Input.h" -#include +#include namespace Nuake { @@ -28,10 +28,10 @@ namespace Nuake { return Input::IsKeyPressed(keyCode); } - Coral::NativeArray GetMousePosition() + Coral::Array GetMousePosition() { Vector2 mousePosition = Input::GetMousePosition(); - return { mousePosition.x, mousePosition.y}; + return Coral::Array::New({ mousePosition.x, mousePosition.y }); } diff --git a/Nuake/src/Scripting/NetModules/NetAPIModule.h b/Nuake/src/Scripting/NetModules/NetAPIModule.h index b388c69b..10af937b 100644 --- a/Nuake/src/Scripting/NetModules/NetAPIModule.h +++ b/Nuake/src/Scripting/NetModules/NetAPIModule.h @@ -2,7 +2,7 @@ #include "src/Core/Core.h" #include "src/Core/Logger.h" -#include +#include namespace Nuake { diff --git a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp index f8320002..9f86644a 100644 --- a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp @@ -22,22 +22,20 @@ #include "src/Physics/PhysicsManager.h" -#include +#include namespace Nuake { - uint32_t GetEntity(Coral::NativeString entityName) + uint32_t GetEntity(Coral::String entityName) { auto scene = Engine::GetCurrentScene(); - - std::string entityNameString = entityName.ToString(); - if (!scene->EntityExists(entityNameString)) + if (!scene->EntityExists(entityName)) { return UINT32_MAX; // Error code: entity not found. } - return scene->GetEntity(entityNameString).GetHandle(); + return scene->GetEntity(entityName).GetHandle(); } static enum ComponentTypes @@ -120,7 +118,7 @@ namespace Nuake { } } - Coral::NativeArray TransformGetGlobalPosition(int entityId) + Coral::Array TransformGetGlobalPosition(int entityId) { Entity entity = { (entt::entity)(entityId), Engine::GetCurrentScene().get() }; @@ -128,7 +126,7 @@ namespace Nuake { { auto& component = entity.GetComponent(); const auto& globalPosition = component.GetGlobalPosition(); - Coral::NativeArray result = { globalPosition.x, globalPosition.y, globalPosition.z }; + Coral::Array result = Coral::Array::New({ globalPosition.x, globalPosition.y, globalPosition.z }); return result; } } @@ -145,7 +143,7 @@ namespace Nuake { } } - Coral::NativeArray CameraGetDirection(int entityId) + Coral::Array CameraGetDirection(int entityId) { Entity entity = { (entt::entity)(entityId), Engine::GetCurrentScene().get() }; @@ -153,7 +151,7 @@ namespace Nuake { { auto& component = entity.GetComponent(); const Vector3 camDirection = component.CameraInstance->GetDirection(); - return { camDirection.x, camDirection.y, camDirection.z }; + return Coral::Array::New({ camDirection.x, camDirection.y, camDirection.z }); } } @@ -187,7 +185,7 @@ namespace Nuake { return false; } - void Play(int entityId, Coral::NativeString animation) + void Play(int entityId, Coral::String animation) { Entity entity = Entity((entt::entity)(entityId), Engine::GetCurrentScene().get()); @@ -203,7 +201,7 @@ namespace Nuake { int animIndex = 0; for (const auto& anim : model->GetAnimations()) { - if (anim->GetName() == animation.ToString()) + if (anim->GetName() == animation) { model->PlayAnimation(animIndex); } diff --git a/Nuake/src/Scripting/ScriptingEngineNet.cpp b/Nuake/src/Scripting/ScriptingEngineNet.cpp index ea4fcecf..d04321ea 100644 --- a/Nuake/src/Scripting/ScriptingEngineNet.cpp +++ b/Nuake/src/Scripting/ScriptingEngineNet.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include #include @@ -130,13 +130,15 @@ namespace Nuake for (auto& type : m_GameAssembly.GetTypes()) { - Logger::Log(std::string("Detected type: ") + std::string(type->GetName()), ".net"); - Logger::Log(std::string("Detected base type: ") + std::string(type->GetBaseType().GetName()), ".net"); + Logger::Log(std::string("Detected type: ") + std::string(type->GetFullName()), ".net"); + Logger::Log(std::string("Detected base type: ") + std::string(type->GetBaseType().GetFullName()), ".net"); - const std::string baseTypeName = std::string(type->GetBaseType().GetName()); - if (baseTypeName == "Entity") + const std::string baseTypeName = std::string(type->GetBaseType().GetFullName()); + if (baseTypeName == "Nuake.Net.Entity") { - m_GameEntityTypes[std::string(type->GetName())] = type; // We have found an entity script. + auto typeSplits = String::Split(type->GetFullName(), '.'); + std::string shortenedTypeName = typeSplits[typeSplits.size() - 1]; + m_GameEntityTypes[shortenedTypeName] = type; // We have found an entity script. } } } @@ -198,7 +200,6 @@ namespace Nuake size_t classNameLength = semiColonPos - classNameStartIndex; const std::string className = fileContent.substr(classNameStartIndex, classNameLength); - if(m_GameEntityTypes.find(className) == m_GameEntityTypes.end()) { // The class name parsed in the file was not found in the game's DLL. diff --git a/NuakeNet/premake5.lua b/NuakeNet/premake5.lua index 567d0b61..cef4d968 100644 --- a/NuakeNet/premake5.lua +++ b/NuakeNet/premake5.lua @@ -1,6 +1,6 @@ project "NuakeNet" language "C#" - dotnetframework "net7.0" + dotnetframework "net8.0" kind "SharedLib" clr "Unsafe" diff --git a/premake5.lua b/premake5.lua index 6994ebce..40230a7b 100644 --- a/premake5.lua +++ b/premake5.lua @@ -180,7 +180,7 @@ project "NuakeRuntime" "%{prj.name}/../Nuake/src/Vendors/wren/src/include", "%{prj.name}/../Nuake/dependencies/JoltPhysics/bin/%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}/JoltPhysics/", "%{prj.name}/../Nuake/dependencies/soloud/bin/%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}", - "%{prj.name}/../Nuake/dependencies/Coral/NetCore/7.0.7/" + "%{prj.name}/../Nuake/dependencies/Coral/NetCore/" } links @@ -193,8 +193,6 @@ project "NuakeRuntime" "JoltPhysics", "soloud", "Coral.Native", - "nethost", - "libnethost" } filter "system:windows" @@ -211,11 +209,7 @@ project "NuakeRuntime" externalincludedirs { "%{prj.name}/../Nuake/dependencies/Coral/Coral.Native/Include/" } postbuildcommands { - '{ECHO} Copying "%{wks.location}/NetCore/7.0.7/nethost.dll" to "%{cfg.targetdir}"', - '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/NetCore/7.0.7/nethost.dll" "%{cfg.targetdir}"', - '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/Coral.Managed/Coral.Managed.runtimeconfig.json" "%{wks.location}/%{prj.name}"', - '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/Coral.Managed/Build/%{cfg.buildcfg}-%{cfg.system}/Coral.Managed.dll" "%{wks.location}/%{prj.name}"', - '{COPYFILE} "%{wks.location}/NuakeNet/bin/%{cfg.buildcfg}/NuakeNet.dll" "%{wks.location}/%{prj.name}"' + '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/Coral.Managed/Coral.Managed.runtimeconfig.json" "%{wks.location}/%{prj.name}"' } filter "system:linux" @@ -318,7 +312,7 @@ project "Editor" "%{prj.name}/../Nuake/src/Vendors/wren/src/include", "%{prj.name}/../Nuake/dependencies/JoltPhysics/bin/%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}/JoltPhysics/", "%{prj.name}/../Nuake/dependencies/soloud/bin/%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}", - "%{prj.name}/../Nuake/dependencies/Coral/NetCore/7.0.7/" + "%{prj.name}/../Nuake/dependencies/Coral/NetCore/" } links @@ -330,9 +324,7 @@ project "Editor" "Freetype", "JoltPhysics", "soloud", - "Coral.Native", - "nethost", - "libnethost" + "Coral.Native" } filter "system:Windows" @@ -353,11 +345,7 @@ project "Editor" externalincludedirs { "%{prj.name}/../Nuake/dependencies/Coral/Coral.Native/Include/" } postbuildcommands { - '{ECHO} Copying "%{wks.location}/NetCore/7.0.7/nethost.dll" to "%{cfg.targetdir}"', - '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/NetCore/7.0.7/nethost.dll" "%{cfg.targetdir}"', - '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/Coral.Managed/Coral.Managed.runtimeconfig.json" "%{wks.location}/%{prj.name}"', - '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/Coral.Managed/Build/%{cfg.buildcfg}-%{cfg.system}/Coral.Managed.dll" "%{wks.location}/%{prj.name}"', - '{COPYFILE} "%{wks.location}/NuakeNet/bin/%{cfg.buildcfg}/NuakeNet.dll" "%{wks.location}/%{prj.name}"' + '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/Coral.Managed/Coral.Managed.runtimeconfig.json" "%{wks.location}/%{prj.name}"' } From 3d60ab1608531df31b57c2ce2ea7d8d68b0574b6 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Mon, 1 Apr 2024 20:26:54 -0400 Subject: [PATCH 25/26] Copying managed.coral.dll to work directory --- premake5.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/premake5.lua b/premake5.lua index 40230a7b..399131cb 100644 --- a/premake5.lua +++ b/premake5.lua @@ -345,7 +345,8 @@ project "Editor" externalincludedirs { "%{prj.name}/../Nuake/dependencies/Coral/Coral.Native/Include/" } postbuildcommands { - '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/Coral.Managed/Coral.Managed.runtimeconfig.json" "%{wks.location}/%{prj.name}"' + '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/Coral.Managed/Coral.Managed.runtimeconfig.json" "%{wks.location}/%{prj.name}"', + '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/Coral.Managed/bin/%{cfg.buildcfg}/Coral.Managed.dll" "%{wks.location}/%{prj.name}"' } From 33b4f75461b90bc07dd01924e4e803e4e13780f4 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Tue, 2 Apr 2024 00:12:04 -0400 Subject: [PATCH 26/26] Added ability to get other instances of .net scripts using GetEntity --- .../src/Scripting/NetModules/SceneNetAPI.cpp | 20 ++++++ Nuake/src/Scripting/ScriptingEngineNet.cpp | 8 ++- NuakeNet/src/Scene.cs | 15 ++++ premake5.lua | 72 ++++++------------- 4 files changed, 63 insertions(+), 52 deletions(-) diff --git a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp index 9f86644a..cc19cf58 100644 --- a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp @@ -21,6 +21,7 @@ #include "src/Scene/Components/QuakeMap.h" #include "src/Physics/PhysicsManager.h" +#include "src/Scripting/ScriptingEngineNet.h" #include @@ -38,6 +39,24 @@ namespace Nuake { return scene->GetEntity(entityName).GetHandle(); } + Coral::ManagedObject GetEntityScript(Coral::String entityName) + { + auto scene = Engine::GetCurrentScene(); + if (!scene->EntityExists(entityName)) + { + return Coral::ManagedObject(); // Error code: entity not found. + } + + Entity entity = scene->GetEntity(entityName); + + auto& scriptingEngine = ScriptingEngineNet::Get(); + if (scriptingEngine.HasEntityScriptInstance(entity)) + { + auto instance = scriptingEngine.GetEntityScript(entity); + return instance; + } + } + static enum ComponentTypes { Unknown = -1, @@ -216,6 +235,7 @@ namespace Nuake { { RegisterMethod("Entity.EntityHasComponentIcall", &EntityHasComponent); RegisterMethod("Scene.GetEntityIcall", &GetEntity); + RegisterMethod("Scene.GetEntityScriptIcall", &GetEntityScript); // Components RegisterMethod("TransformComponent.SetPositionIcall", &TransformSetPosition); diff --git a/Nuake/src/Scripting/ScriptingEngineNet.cpp b/Nuake/src/Scripting/ScriptingEngineNet.cpp index d04321ea..7fde9cd5 100644 --- a/Nuake/src/Scripting/ScriptingEngineNet.cpp +++ b/Nuake/src/Scripting/ScriptingEngineNet.cpp @@ -117,7 +117,7 @@ namespace Nuake void ScriptingEngineNet::LoadProjectAssembly(Ref project) { const std::string sanitizedProjectName = String::Sanitize(project->Name); - const std::string assemblyPath = "/bin/Debug/net7.0/" + sanitizedProjectName + ".dll"; + const std::string assemblyPath = "/bin/Debug/net8.0/" + sanitizedProjectName + ".dll"; if (!FileSystem::FileExists(assemblyPath)) { @@ -223,6 +223,8 @@ namespace Nuake { if (!HasEntityScriptInstance(entity)) { + std::string name = entity.GetComponent().Name; + Logger::Log(name); Logger::Log("Failed to get entity .Net script instance, doesn't exist", ".net", CRITICAL); return Coral::ManagedObject(); } @@ -266,10 +268,10 @@ namespace Nuake const std::string cleanProjectName = String::Sanitize(projectName); const std::string premakeScript = R"( workspace ")" + cleanProjectName + R"(" +configurations { "Debug", "Release" } project ")" + cleanProjectName + R"(" language "C#" - dotnetframework "net7.0" - + dotnetframework "net8.0" kind "SharedLib" clr "Unsafe" diff --git a/NuakeNet/src/Scene.cs b/NuakeNet/src/Scene.cs index 65edaf11..b8306b32 100644 --- a/NuakeNet/src/Scene.cs +++ b/NuakeNet/src/Scene.cs @@ -6,6 +6,21 @@ namespace Nuake.Net public class Scene { internal static unsafe delegate* GetEntityIcall; + internal static unsafe delegate*> GetEntityScriptIcall; + + public static T? GetEntity(string entityName) where T : class + { + NativeInstance handle; + unsafe { handle = GetEntityScriptIcall(entityName); } + + Entity? entity = handle.Get(); + if (entity != null && entity is T) + { + return entity as T; + } + + return null; + } public static Entity GetEntity(string entityName) { diff --git a/premake5.lua b/premake5.lua index 399131cb..d8807e7e 100644 --- a/premake5.lua +++ b/premake5.lua @@ -1,5 +1,6 @@ workspace "Nuake" - conformancemode "On" + architecture "x64" + configurations { "Debug", @@ -19,12 +20,6 @@ workspace "Nuake" "NK_DEBUG" } - filter { "language:C++" } - architecture "x64" - - filter { "language:C" } - architecture "x64" - outputdir = "%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}" include "Nuake/dependencies/glfw_p5.lua" @@ -34,9 +29,6 @@ include "Nuake/dependencies/freetype_p5.lua" include "Nuake/dependencies/jolt_p5.lua" include "Nuake/dependencies/soloud_p5.lua" include "Nuake/dependencies/optick_p5.lua" -include "Nuake/dependencies/coral_p5.lua" - -include "NuakeNet/premake5.lua" project "Nuake" location "Nuake" @@ -88,8 +80,7 @@ project "Nuake" "%{prj.name}/src/Vendors/wren/src/include", "%{prj.name}/src/Vendors/incbin", "%{prj.name}/dependencies/build", - "%{prj.name}/dependencies/soloud/include", - "%{prj.name}/dependencies/Coral/Coral.Native/Include" + "%{prj.name}/dependencies/soloud/include" } links @@ -99,16 +90,15 @@ project "Nuake" } filter "system:linux" - defines - { + defines { "GLFW_STATIC", "NK_LINUX" } links - { - "glib-2.0" - } + { + "glib-2.0" + } buildoptions { "`pkg-config --cflags glib-2.0 pango gdk-pixbuf-2.0 atk`" } linkoptions { "`pkg-config --libs glib-2.0 pango gdk-pixbuf-2.0`" } @@ -117,7 +107,7 @@ project "Nuake" { "/usr/include/gtk-3.0/", "/usr/lib/glib-2.0/include", - "/usr/include/glib-2.0", + "/usr/include/glib-2.0", } filter "system:windows" @@ -127,7 +117,7 @@ project "Nuake" "NK_WIN" } - + buildoptions { "-permissive", "-cxxflags", "gtk+-3.0"} filter "configurations:Debug" runtime "Debug" symbols "on" @@ -179,8 +169,7 @@ project "NuakeRuntime" "%{prj.name}/../Nuake/src/Vendors/msdfgen", "%{prj.name}/../Nuake/src/Vendors/wren/src/include", "%{prj.name}/../Nuake/dependencies/JoltPhysics/bin/%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}/JoltPhysics/", - "%{prj.name}/../Nuake/dependencies/soloud/bin/%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}", - "%{prj.name}/../Nuake/dependencies/Coral/NetCore/" + "%{prj.name}/../Nuake/dependencies/soloud/bin/%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}" } links @@ -191,8 +180,7 @@ project "NuakeRuntime" "assimp", "Freetype", "JoltPhysics", - "soloud", - "Coral.Native", + "soloud" } filter "system:windows" @@ -206,12 +194,6 @@ project "NuakeRuntime" "opengl32.lib" } - externalincludedirs { "%{prj.name}/../Nuake/dependencies/Coral/Coral.Native/Include/" } - - postbuildcommands { - '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/Coral.Managed/Coral.Managed.runtimeconfig.json" "%{wks.location}/%{prj.name}"' - } - filter "system:linux" links { @@ -230,7 +212,7 @@ project "NuakeRuntime" { "/usr/include/gtk-3.0/", "/usr/lib/glib-2.0/include", - "/usr/include/glib-2.0", + "/usr/include/glib-2.0", } buildoptions { "`pkg-config --cflags glib-2.0 pango gdk-pixbuf-2.0 gtk-3 atk tk-3.0 glib-2.0`" } @@ -297,7 +279,7 @@ project "Editor" "%{prj.name}/../Nuake/dependencies/JoltPhysics", "%{prj.name}/../Nuake/dependencies/build", "%{prj.name}/../Nuake/dependencies/soloud/include", - "/usr/include/gtk-3.0/", + "/usr/include/gtk-3.0/" } libdirs @@ -311,8 +293,7 @@ project "Editor" "%{prj.name}/../Nuake/src/Vendors/msdfgen", "%{prj.name}/../Nuake/src/Vendors/wren/src/include", "%{prj.name}/../Nuake/dependencies/JoltPhysics/bin/%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}/JoltPhysics/", - "%{prj.name}/../Nuake/dependencies/soloud/bin/%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}", - "%{prj.name}/../Nuake/dependencies/Coral/NetCore/" + "%{prj.name}/../Nuake/dependencies/soloud/bin/%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}" } links @@ -321,10 +302,9 @@ project "Editor" "glad", "GLFW", "assimp", - "Freetype", - "JoltPhysics", - "soloud", - "Coral.Native" + "Freetype", + "JoltPhysics", + "soloud" } filter "system:Windows" @@ -341,13 +321,6 @@ project "Editor" { "NK_WIN" } - - externalincludedirs { "%{prj.name}/../Nuake/dependencies/Coral/Coral.Native/Include/" } - - postbuildcommands { - '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/Coral.Managed/Coral.Managed.runtimeconfig.json" "%{wks.location}/%{prj.name}"', - '{COPYFILE} "%{wks.location}/Nuake/dependencies/Coral/Coral.Managed/bin/%{cfg.buildcfg}/Coral.Managed.dll" "%{wks.location}/%{prj.name}"' - } filter "system:linux" @@ -360,8 +333,8 @@ project "Editor" "asound", "glib-2.0", "gtk-3", - "gobject-2.0", - "asound" + "gobject-2.0", + "asound" } buildoptions { "`pkg-config --cflags glib-2.0 pango gdk-pixbuf-2.0 gtk-3 atk tk-3.0 glib-2.0`" } @@ -372,8 +345,10 @@ project "Editor" { "/usr/include/gtk-3.0/", "/usr/lib/glib-2.0/include", - "/usr/include/glib-2.0", + "/usr/include/glib-2.0", } + + defines { @@ -409,5 +384,4 @@ project "Editor" -- copy a file from the objects directory to the target directory postbuildcommands { --"{COPY} "Nuake/dependencies/GLFW/lib-vc2019/glfw3.dll" " .. "./bin/" .. outputdir .. "/%{prj.name}/glfw3.dll" - } - + } \ No newline at end of file