add 2D physics (use box2d), fixed some little bugs

This commit is contained in:
2025-12-12 16:47:04 +08:00
parent 804c1d84ce
commit 4140a5b4be
27 changed files with 805 additions and 64 deletions

View File

@ -4,6 +4,10 @@
#include "EditorLayer.h"
#include "ImGuizmo.h"
#define GLM_ENABLE_EXPERIMENTAL
#include <filesystem>
#include "glm/gtx/matrix_decompose.hpp"
#include "Prism/Core/Input.h"
#include "Prism/Renderer/Renderer2D.h"
#include "Prism/Script/ScriptEngine.h"
@ -12,97 +16,131 @@ namespace Prism
{
enum class PropertyFlag
{
None = 0, ColorProperty = 1
None = 0, ColorProperty = 1, SliderProperty = 2, DragProperty = 4
};
static std::tuple<glm::vec3, glm::quat, glm::vec3> GetTransformDecomposition(const glm::mat4& transform)
{
glm::vec3 scale, translation, skew;
glm::vec4 perspective;
glm::quat orientation;
glm::decompose(transform, scale, orientation, translation, skew, perspective);
return { translation, orientation, scale };
}
bool Property(const std::string& name, bool& value)
{
ImGui::Text(name.c_str());
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
const std::string id = "##" + name;
const bool result = ImGui::Checkbox(id.c_str(), &value);
const bool isChanged = ImGui::Checkbox(id.c_str(), &value);
ImGui::PopItemWidth();
ImGui::NextColumn();
return result;
return isChanged;
}
void Property(const std::string& name, float& value, const float min = -1.0f, const float max = 1.0f, PropertyFlag flags = PropertyFlag::None)
bool Property(const std::string& name, float& value, const float min = -1.0f, const float max = 1.0f, PropertyFlag flags = PropertyFlag::None)
{
ImGui::Text(name.c_str());
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
const std::string id = "##" + name;
ImGui::SliderFloat(id.c_str(), &value, min, max);
bool isChanged = false;
if (flags == PropertyFlag::SliderProperty)
isChanged = ImGui::SliderFloat(id.c_str(), &value, min, max);
else
isChanged = ImGui::DragFloat(id.c_str(), &value, 1.0f, min, max);
ImGui::PopItemWidth();
ImGui::NextColumn();
return isChanged;
}
void Property(const std::string& name, glm::vec3& value, const float min = -1.0f, const float max = 1.0f, PropertyFlag flags = PropertyFlag::None)
bool Property(const std::string& name, glm::vec3& value, const float min = -1.0f, const float max = 1.0f, PropertyFlag flags = PropertyFlag::None)
{
ImGui::Text(name.c_str());
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
std::string id = "##" + name;
if ((int)flags & (int)PropertyFlag::ColorProperty)
ImGui::ColorEdit3(id.c_str(), glm::value_ptr(value), ImGuiColorEditFlags_NoInputs);
bool isChanged = false;
if (flags == PropertyFlag::ColorProperty)
isChanged = ImGui::ColorEdit3(id.c_str(), glm::value_ptr(value), ImGuiColorEditFlags_NoInputs);
else if (flags == PropertyFlag::SliderProperty)
isChanged = ImGui::SliderFloat3(id.c_str(), glm::value_ptr(value), min, max);
else
ImGui::SliderFloat3(id.c_str(), glm::value_ptr(value), min, max);
isChanged = ImGui::DragFloat3(id.c_str(), glm::value_ptr(value), 1.0f, min, max);
ImGui::PopItemWidth();
ImGui::NextColumn();
return isChanged;
}
void Property(const std::string& name, glm::vec3& value, PropertyFlag flags)
bool Property(const std::string& name, glm::vec3& value, PropertyFlag flags)
{
Property(name, value, -1.0f, 1.0f, flags);
return Property(name, value, -1.0f, 1.0f, flags);
}
void Property(const std::string& name, glm::vec4& value, const float min = -1.0f, const float max = 1.0f, PropertyFlag flags = PropertyFlag::None)
bool Property(const std::string& name, glm::vec4& value, const float min = -1.0f, const float max = 1.0f, PropertyFlag flags = PropertyFlag::None)
{
ImGui::Text(name.c_str());
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
std::string id = "##" + name;
if ((int)flags & (int)PropertyFlag::ColorProperty)
ImGui::ColorEdit4(id.c_str(), glm::value_ptr(value), ImGuiColorEditFlags_NoInputs);
bool isChanged = false;
if (flags == PropertyFlag::ColorProperty)
isChanged = ImGui::ColorEdit4(id.c_str(), glm::value_ptr(value), ImGuiColorEditFlags_NoInputs);
else if (flags == PropertyFlag::SliderProperty)
isChanged = ImGui::SliderFloat4(id.c_str(), glm::value_ptr(value), min, max);
else
ImGui::SliderFloat4(id.c_str(), glm::value_ptr(value), min, max);
isChanged = ImGui::DragFloat4(id.c_str(), glm::value_ptr(value), 1.0f, min, max);
ImGui::PopItemWidth();
ImGui::NextColumn();
return isChanged;
}
void Property(const std::string& name, glm::vec4& value, const PropertyFlag flags)
bool Property(const std::string& name, glm::vec4& value, const PropertyFlag flags)
{
Property(name, value, -1.0f, 1.0f, flags);
return Property(name, value, -1.0f, 1.0f, flags);
}
void Property(const std::string& name, glm::vec2& value, const float min, const float max, PropertyFlag flags)
bool Property(const std::string& name, glm::vec2& value, const float min, const float max, PropertyFlag flags)
{
ImGui::Text(name.c_str());
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
const std::string id = "##" + name;
ImGui::SliderFloat2(id.c_str(), glm::value_ptr(value), min, max);
bool isChanged = false;
if (flags == PropertyFlag::SliderProperty)
isChanged = ImGui::SliderFloat2(id.c_str(), glm::value_ptr(value), min, max);
else
isChanged = ImGui::DragFloat2(id.c_str(), glm::value_ptr(value), 1.0f, min, max);
ImGui::PopItemWidth();
ImGui::NextColumn();
return isChanged;
}
void Property(const std::string& name, glm::vec2& value, const PropertyFlag flags)
bool Property(const std::string& name, glm::vec2& value, const PropertyFlag flags)
{
Property(name, value, -1.0f, 1.0f, flags);
return Property(name, value, -1.0f, 1.0f, flags);
}
@ -139,7 +177,7 @@ namespace Prism
m_SceneHierarchyPanel = CreateScope<SceneHierarchyPanel>(m_EditorScene);
m_SceneHierarchyPanel->SetSelectionChangedCallback(std::bind(&EditorLayer::SelectEntity, this, std::placeholders::_1));
m_SceneHierarchyPanel->SetEntityDeletedCallback(std::bind(&EditorLayer::OnEntityDeleted, this, std::placeholders::_1));
UpdateWindowTitle("untitled Scene");
}
void EditorLayer::OnDetach()
@ -152,7 +190,7 @@ namespace Prism
{
case SceneState::Edit:
{
if (m_ViewportPanelFocused)
// if (m_ViewportPanelFocused)
m_EditorCamera.OnUpdate(deltaTime);
m_EditorScene->OnRenderEditor(deltaTime, m_EditorCamera);
@ -167,7 +205,7 @@ namespace Prism
Renderer::EndRenderPass();
}
if (m_SelectionContext.size() && false)
if (!m_SelectionContext.empty() && false)
{
auto& selection = m_SelectionContext[0];
@ -182,7 +220,26 @@ namespace Prism
Renderer::EndRenderPass();
}
}
break;
if (!m_SelectionContext.empty())
{
auto& selection = m_SelectionContext[0];
if (selection.Entity.HasComponent<BoxCollider2DComponent>())
{
const auto& size = selection.Entity.GetComponent<BoxCollider2DComponent>().Size;
auto [translation, rotationQuat, scale] = GetTransformDecomposition(selection.Entity.GetComponent<TransformComponent>().Transform);
const glm::vec3 rotation = glm::eulerAngles(rotationQuat);
Renderer::BeginRenderPass(SceneRenderer::GetFinalRenderPass(), false);
auto viewProj = m_EditorCamera.GetViewProjection();
Renderer2D::BeginScene(viewProj, false);
Renderer2D::DrawRotatedQuad({ translation.x, translation.y }, size * 2.0f, glm::degrees(rotation.z), { 1.0f, 0.0f, 1.0f, 1.0f });
Renderer2D::EndScene();
Renderer::EndRenderPass();
}
}
break;
}
case SceneState::Play:
@ -209,7 +266,6 @@ namespace Prism
void EditorLayer::OnImGuiRender()
{
#if ENABLE_DOCKSPACE
static bool p_open = true;
static bool opt_fullscreen = true;
@ -284,6 +340,9 @@ namespace Prism
SceneSerializer serializer(newScene);
serializer.Deserialize(filepath);
m_EditorScene = newScene;
std::filesystem::path path = filepath;
UpdateWindowTitle(path.filename().string());
m_SceneHierarchyPanel->SetContext(m_EditorScene);
ScriptEngine::SetSceneContext(m_EditorScene);
@ -297,28 +356,82 @@ namespace Prism
std::string filepath = app.SaveFile(R"(Scene file (*.hsc)*.hsc)");
SceneSerializer serializer(m_EditorScene);
serializer.Serialize(filepath);
std::filesystem::path path = filepath;
UpdateWindowTitle(path.filename().string());
}
ImGui::Separator();
/*
if (ImGui::MenuItem("Reload C# Assembly"))
ScriptEngine::ReloadAssembly("assets/scripts/ExampleApp.dll");
*/
ImGui::Separator();
if (ImGui::MenuItem("Exit"))
p_open = false;
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Script"))
{
if (ImGui::MenuItem("Reload C# Assembly"))
ScriptEngine::ReloadAssembly("assets/scripts/ExampleApp.dll");
ImGui::MenuItem("Reload assembly on play", nullptr, &m_ReloadScriptOnPlay);
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
m_SceneHierarchyPanel->OnImGuiRender();
ImGui::Begin("Materials");
if (!m_SelectionContext.empty())
{
Entity selectedEntity = m_SelectionContext.front().Entity;
if (selectedEntity.HasComponent<MeshComponent>())
{
Ref<Mesh> mesh = selectedEntity.GetComponent<MeshComponent>().Mesh;
if (mesh)
{
const auto& materials = mesh->GetMaterials();
static uint32_t selectedMaterialIndex = 0;
for (uint32_t i = 0; i < materials.size(); i++)
{
const auto& materialInstance = materials[i];
ImGuiTreeNodeFlags node_flags = (selectedMaterialIndex == i ? ImGuiTreeNodeFlags_Selected : 0) | ImGuiTreeNodeFlags_Leaf;
bool opened = ImGui::TreeNodeEx((void*)(&materialInstance), node_flags, "%s", materialInstance->GetName().c_str());
if (ImGui::IsItemClicked())
{
selectedMaterialIndex = i;
}
if (opened)
ImGui::TreePop();
}
ImGui::Separator();
if (selectedMaterialIndex < materials.size())
{
ImGui::Text("Shader: %s", materials[selectedMaterialIndex]->GetShader()->GetName().c_str());
}
}
}
}
ImGui::End();
ScriptEngine::OnImGuiRender();
ImGui::End();
#endif
// Editor Panel ------------------------------------------------------------------------------
ImGui::Begin("Model");
@ -338,13 +451,30 @@ namespace Prism
ImGui::AlignTextToFramePadding();
auto& light = m_EditorScene->GetLight();
Property("Light Direction", light.Direction);
Property("Light Direction", light.Direction, PropertyFlag::SliderProperty);
Property("Light Radiance", light.Radiance, PropertyFlag::ColorProperty);
Property("Light Multiplier", light.Multiplier, 0.0f, 5.0f);
Property("Exposure", m_EditorCamera.GetExposure(), 0.0f, 5.0f);
Property("Light Multiplier", light.Multiplier, 0.0f, 5.0f, PropertyFlag::SliderProperty);
Property("Exposure", m_EditorCamera.GetExposure(), 0.0f, 5.0f, PropertyFlag::SliderProperty);
Property("Radiance Prefiltering", m_RadiancePrefilter);
Property("Env Map Rotation", m_EnvMapRotation, -360.0f, 360.0f);
Property("Env Map Rotation", m_EnvMapRotation, -360.0f, 360.0f, PropertyFlag::SliderProperty);
if (m_SceneState == SceneState::Edit)
{
float physics2DGravity = m_EditorScene->GetPhysics2DGravity();
if (Property("Gravity", physics2DGravity, -10000.0f, 10000.0f, PropertyFlag::DragProperty))
{
m_EditorScene->SetPhysics2DGravity(physics2DGravity);
}
}
else if (m_SceneState == SceneState::Play)
{
float physics2DGravity = m_RuntimeScene->GetPhysics2DGravity();
if (Property("Gravity", physics2DGravity, -10000.0f, 10000.0f, PropertyFlag::DragProperty))
{
m_RuntimeScene->SetPhysics2DGravity(physics2DGravity);
}
}
if (Property("Show Bounding Boxes", m_UIShowBoundingBoxes))
ShowBoundingBoxes(m_UIShowBoundingBoxes, m_UIShowBoundingBoxesOnTop);
@ -732,7 +862,8 @@ namespace Prism
bool EditorLayer::OnMouseButtonPressedEvent(MouseButtonPressedEvent& e)
{
auto [mx, my] = Input::GetMousePosition();
if (e.GetMouseButton() == PM_MOUSE_BUTTON_LEFT && !Input::IsKeyPressed(KeyCode::LEFT_ALT) && !ImGuizmo::IsOver())
if (e.GetMouseButton() == PM_MOUSE_BUTTON_LEFT && !Input::IsKeyPressed(KeyCode::LEFT_ALT) && !ImGuizmo::IsOver() && m_SceneState != SceneState::Play)
{
auto [mouseX, mouseY] = GetMouseViewportSpace();
if (mouseX > -1.0f && mouseX < 1.0f && mouseY > -1.0f && mouseY < 1.0f)
@ -809,6 +940,13 @@ namespace Prism
m_EditorScene->SetSelectedEntity(entity);
}
void EditorLayer::UpdateWindowTitle(const std::string &sceneName)
{
const std::string title = sceneName + " - Prism - " + Application::GetPlatformName() + " (" + Application::GetConfigurationName() + ")";
Application::Get().GetWindow().SetTitle(title);
}
std::pair<float, float> EditorLayer::GetMouseViewportSpace() const
{
auto [mx, my] = ImGui::GetMousePos(); // Input::GetMousePosition();
@ -866,6 +1004,9 @@ namespace Prism
m_SceneState = SceneState::Play;
if (m_ReloadScriptOnPlay)
ScriptEngine::ReloadAssembly("assets/scripts/ExampleApp.dll");
m_RuntimeScene = Ref<Scene>::Create();
m_EditorScene->CopyTo(m_RuntimeScene);

View File

@ -28,6 +28,8 @@ namespace Prism
void ShowBoundingBoxes(bool show, bool onTop = false);
void SelectEntity(Entity entity);
void UpdateWindowTitle(const std::string& sceneName);
private:
std::pair<float, float> GetMouseViewportSpace() const;
std::pair<glm::vec3, glm::vec3> CastRay(float mx, float my);
@ -49,6 +51,7 @@ namespace Prism
Ref<Scene> m_ActiveScene;
Ref<Scene> m_RuntimeScene, m_EditorScene;
bool m_ReloadScriptOnPlay = false;
EditorCamera m_EditorCamera;