various bug fix, serialization, adding shapes

This commit is contained in:
Erris
2026-03-05 01:22:41 +01:00
parent f45091349e
commit ecf27eae73
8 changed files with 215 additions and 92 deletions

View File

@@ -19,7 +19,8 @@ namespace OpenEngine {
void MeshOnImGuiRender(entt::registry&registry, entt::entity entity);
void MaterialOnImGuiRender(entt::registry& registry, entt::entity entity);
void BodyOnImGuiRender(entt::registry& registry, entt::entity entity);
void ShapeOnImGuiRender(entt::registry& registry, entt::entity entity);
void SphereShapeOnImGuiRender(entt::registry& registry, entt::entity entity);
void BoxShapeOnImGuiRender(entt::registry& registry, entt::entity entity);
}
#endif // EDITOR_COMPONENT_HPP

View File

@@ -1,6 +1,7 @@
#include "imgui.h"
#include "open_engine/renderer/renderer3d.hpp"
#include "open_engine/scene/components.hpp"
#include <Jolt/Physics/Body/MotionType.h>
#include <editor_component.hpp>
#include <entt/entity/fwd.hpp>
#include <glm/gtc/type_ptr.hpp>
@@ -181,12 +182,11 @@ namespace OpenEngine {
{
auto& mesh_component = registry.get<MeshComponent>(entity);
const char* items[] = { "Quad", "Cube" };
static int item_selected_idx = 0;
const char* items[] = { "None", "Quad", "Cube" };
int item_selected_idx = (int)mesh_component.primitive_type;
if (ImGui::BeginCombo("Mesh", items[item_selected_idx])) {
for (int n = 0; n < 2; n++)
{
for (int n = 0; n < 3; n++) {
const bool is_selected = (item_selected_idx == n);
if (ImGui::Selectable(items[n], is_selected))
item_selected_idx = n;
@@ -196,25 +196,26 @@ namespace OpenEngine {
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
if ((int)mesh_component.primitive_type == (item_selected_idx + 1))
return;
if ((int)mesh_component.primitive_type == (item_selected_idx))
return;
mesh_component.primitive_type = (PrimitiveType)(item_selected_idx + 1);
switch (mesh_component.primitive_type) {
case OpenEngine::PrimitiveType::Quad:
{
mesh_component.mesh = CreateQuad((uint32_t)entity);
break;
}
case OpenEngine::PrimitiveType::Cube:
{
mesh_component.mesh = CreateCube((uint32_t)entity);
break;
}
default:
mesh_component.primitive_type = (PrimitiveType)(item_selected_idx);
switch (mesh_component.primitive_type) {
case OpenEngine::PrimitiveType::Quad:
{
mesh_component.mesh = CreateQuad((uint32_t)entity);
break;
}
}
case OpenEngine::PrimitiveType::Cube:
{
mesh_component.mesh = CreateCube((uint32_t)entity);
break;
}
default:
mesh_component.mesh = nullptr;
break;
}
}
@@ -233,16 +234,53 @@ namespace OpenEngine {
{
auto& body_comp = registry.get<PhysicsBodyComponent>(entity);
const char* items[] = { "Static", "Kinematic", "Dynamic" };
int item_selected_idx = body_comp.type;
if (ImGui::BeginCombo("Body type", items[item_selected_idx])) {
for (int n = 0; n < 3; n++)
{
const bool is_selected = (item_selected_idx == n);
if (ImGui::Selectable(items[n], is_selected))
item_selected_idx = n;
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
body_comp.type = item_selected_idx;
ImGui::SliderFloat("Linear damping", &body_comp.linear_damping, 0, 1);
ImGui::SliderFloat("Angular damping", &body_comp.angular_damping, 0, 1);
ImGui::SliderFloat("Gravity factor", &body_comp.gravity_factor, 0, 1);
ImGui::SliderFloat("Bounciness", &body_comp.restitution, 0, 1);
ImGui::SliderFloat("Friction", &body_comp.friction, 0, 1);
}
void ShapeOnImGuiRender(entt::registry &registry, entt::entity entity)
void SphereShapeOnImGuiRender(entt::registry &registry, entt::entity entity)
{
auto& shape_comp = registry.get<PhysicsShapeComponent>(entity);
auto& sphere_comp = registry.get<SphereShapeComponent>(entity);
ImGui::DragFloat("Radius",
&sphere_comp.radius,
0.1f,
0.11f, FLT_MAX,
"%.2f",
ImGuiSliderFlags_AlwaysClamp);
}
ImGui::SliderFloat("Bounciness", &shape_comp.restitution, 0, 1);
ImGui::SliderFloat("Friction", &shape_comp.friction, 0, 1);
void BoxShapeOnImGuiRender(entt::registry &registry, entt::entity entity)
{
auto& box_comp = registry.get<BoxShapeComponent>(entity);
ImGui::DragFloat3("Size",
glm::value_ptr(box_comp.size),
0.1f,
0.11f, FLT_MAX,
"%.2f",
ImGuiSliderFlags_AlwaysClamp);
}
}

View File

@@ -25,7 +25,8 @@ namespace OpenEngine {
RegisterDrawer<MeshComponent>("Mesh", &MeshOnImGuiRender);
RegisterDrawer<MaterialComponent>("Material", &MaterialOnImGuiRender);
RegisterDrawer<PhysicsBodyComponent>("Physics Body", &BodyOnImGuiRender);
RegisterDrawer<PhysicsShapeComponent>("Physics Shape", &ShapeOnImGuiRender);
RegisterDrawer<SphereShapeComponent>("Sphere Shape", &SphereShapeOnImGuiRender);
RegisterDrawer<BoxShapeComponent>("Box Shape", &BoxShapeOnImGuiRender);
scene = context;
selected_context = {};
@@ -64,9 +65,14 @@ namespace OpenEngine {
selected_context.AddComponent<PhysicsBodyComponent>();
ImGui::CloseCurrentPopup();
}
if (!selected_context.HasComponent<PhysicsShapeComponent>())
if (ImGui::MenuItem("Physics shape")) {
selected_context.AddComponent<PhysicsShapeComponent>();
if (!selected_context.HasComponent<SphereShapeComponent>())
if (ImGui::MenuItem("Sphere shape")) {
selected_context.AddComponent<SphereShapeComponent>();
ImGui::CloseCurrentPopup();
}
if (!selected_context.HasComponent<BoxShapeComponent>())
if (ImGui::MenuItem("Box shape")) {
selected_context.AddComponent<BoxShapeComponent>();
ImGui::CloseCurrentPopup();
}
}