feat: add imguizmo + trigger buxfix

This commit is contained in:
2026-10-18 11:45:37 +02:00
parent 70c940c476
commit 4bd99b25d1
37 changed files with 700 additions and 58 deletions
+52 -3
View File
@@ -33,6 +33,7 @@ void LightKeeper::customInit()
const float aspectRatio = static_cast<float>(m_params.renderSize.x) / static_cast<float>(m_params.renderSize.y);
camera.setAspectRatio(aspectRatio);
camera.SetRotation(glm::radians(glm::vec2(90.f, 0.f)));
window.Maximize();
setupSkybox();
@@ -44,6 +45,7 @@ void LightKeeper::customInit()
hudFont = Font::LoadFromFile(AssetFS::GetInstance().GetFullPath("engine://fonts/Roboto-Medium.ttf"));
hudFont.Upload(gfxDevice, resources);
}
void LightKeeper::setupSkybox()
@@ -62,6 +64,8 @@ void LightKeeper::setupSkybox()
void LightKeeper::customUpdate(float dt)
{
updateViewMode();
// LightKeeper owns a camera that hides App::camera; update this instance
// after SDL events have been consumed.
if (!isPlayerViewActive() && getSceneCamera() == nullptr)
@@ -79,6 +83,45 @@ void LightKeeper::customUpdate(float dt)
updateDebugUi();
}
void LightKeeper::updateViewMode()
{
auto& input = InputManager::GetInstance();
FirstPersonController* player = mainScene.playerController;
if (player != nullptr && input.WasKeyPressed(SDL_SCANCODE_F5))
{
player->SetPossessed(!player->IsPossessed());
}
if (!isPlayerViewActive())
{
const bool looking = input.IsMouseDown(SDL_BUTTON_RIGHT);
if (looking != input.IsMouseCaptured())
{
input.SetMouseCaptured(looking);
}
}
}
void LightKeeper::drawViewModeHint()
{
if (isPlayerViewActive() || mainScene.playerController == nullptr) {
return;
}
constexpr std::string_view hint = "Free camera RMB look WASD / Q E move F5 play";
constexpr float hintSize = 18.0f;
const VkExtent3D extent = renderer.getDrawImage().extent;
const glm::vec2 hintExtent = hudFont.MeasureText(hint, hintSize);
Renderer2D::GetInstance().DrawText(
hudFont,
hint,
{16.0f, static_cast<float>(extent.height) - hintExtent.y - 12.0f},
hintSize,
TextStyle{.color = {1.0f, 1.0f, 1.0f, 0.85f}, .outline = 2.0f, .outlineColor = {0.0f, 0.0f, 0.0f, 0.6f}});
}
void LightKeeper::drawCrosshair()
{
if (!isPlayerViewActive()) {
@@ -114,7 +157,7 @@ void LightKeeper::drawCrosshair()
bool LightKeeper::isPlayerViewActive() const
{
return mainScene.playerController != nullptr && mainScene.playerController->IsMouseCaptured();
return mainScene.playerController != nullptr && mainScene.playerController->IsPossessed();
}
CameraComponent* LightKeeper::getSceneCamera() const
@@ -126,6 +169,12 @@ CameraComponent* LightKeeper::getSceneCamera() const
return activeScenes.front()->GetCurrentCamera();
}
Camera& LightKeeper::getViewCamera()
{
CameraComponent* sceneCamera = isPlayerViewActive() ? nullptr : getSceneCamera();
return sceneCamera != nullptr ? sceneCamera->GetCamera(camera.GetAspectRatio()) : camera;
}
void LightKeeper::customDraw()
{
const auto cmd = gfxDevice.beginFrame();
@@ -136,6 +185,7 @@ void LightKeeper::customDraw()
renderer.beginDrawing(gfxDevice);
submitDebugDraws();
drawCrosshair();
drawViewModeHint();
if (isPlayerViewActive())
{
@@ -145,8 +195,7 @@ void LightKeeper::customDraw()
camera.CalculateProjectionMatrix();
}
CameraComponent* sceneCamera = isPlayerViewActive() ? nullptr : getSceneCamera();
Camera& viewCamera = sceneCamera != nullptr ? sceneCamera->GetCamera(camera.GetAspectRatio()) : camera;
Camera& viewCamera = getViewCamera();
const SceneEnvironment& environment = SceneManager::GetInstance().GetCurrentScene().GetEnvironment();
const GameRenderer::SceneData sceneData{
+16
View File
@@ -51,6 +51,9 @@ void LightKeeper::updateDebugUi()
}
drawDebugMenuBar();
if (showTransformGizmo && !isPlayerViewActive()) {
drawSelectionGizmo();
}
if (showTestWindow) {
drawTestWindow();
}
@@ -129,6 +132,7 @@ void LightKeeper::drawDebugMenuBar()
ImGui::MenuItem("Colliders", nullptr, &renderColliders);
ImGui::MenuItem("2D Shapes", nullptr, &render2DShapes);
ImGui::MenuItem("Debug Lines", nullptr, &renderDebugLines);
ImGui::MenuItem("Transform Gizmo", nullptr, &showTransformGizmo);
bool lightHeatmap = renderer.getRenderLightHeatmap();
if (ImGui::MenuItem("Forward+ Light Heatmap", nullptr, &lightHeatmap)) {
@@ -163,6 +167,18 @@ void LightKeeper::drawDebugMenuBar()
ImGui::EndMainMenuBar();
}
void LightKeeper::drawSelectionGizmo()
{
GameObject* selected = SceneManager::GetInstance().GetCurrentScene().GetSelectedObject();
if (selected == nullptr) {
return;
}
transformGizmo.HandleShortcuts();
transformGizmo.DrawToolbar();
transformGizmo.Manipulate(*selected, getViewCamera());
}
void LightKeeper::drawWindowMenu()
{
if (!ImGui::BeginMenu("Window")) {
@@ -34,9 +34,12 @@ FirstPersonController::FirstPersonController(GameObject& parent)
{
}
void FirstPersonController::SetMouseCaptured(bool captured)
void FirstPersonController::SetPossessed(bool possessed)
{
InputManager::GetInstance().SetMouseCaptured(captured);
m_Possessed = possessed;
m_JumpHeld = false;
m_JumpBufferTimer = 0.0f;
InputManager::GetInstance().SetMouseCaptured(possessed);
}
void FirstPersonController::Start()
@@ -65,26 +68,16 @@ void FirstPersonController::Update(float dt)
{
const auto& input = InputManager::GetInstance();
if (input.WasKeyPressed(SDL_SCANCODE_F5))
{
SetMouseCaptured(!IsMouseCaptured());
}
m_FocusedInteractable = nullptr;
if (!IsMouseCaptured())
if (!m_Possessed)
{
m_JumpHeld = false;
if (input.WasMousePressed(SDL_BUTTON_LEFT) && !ImGui::GetIO().WantCaptureMouse)
{
SetMouseCaptured(true);
}
return;
}
if (input.WasKeyPressed(SDL_SCANCODE_ESCAPE))
{
SetMouseCaptured(false);
SetPossessed(false);
return;
}
@@ -143,7 +136,7 @@ void FirstPersonController::FixedUpdate(float fixedDt)
m_CoyoteTimer = m_Grounded ? m_CoyoteTime : std::max(m_CoyoteTimer - fixedDt, 0.0f);
const auto& input = InputManager::GetInstance();
const bool sprinting = IsMouseCaptured() && input.IsKeyDown(SDL_SCANCODE_LSHIFT);
const bool sprinting = m_Possessed && input.IsKeyDown(SDL_SCANCODE_LSHIFT);
const glm::vec3 wishVelocity = GetWishDirection() * (sprinting ? m_SprintSpeed : m_WalkSpeed);
const float acceleration = m_Grounded ? m_GroundAcceleration : m_AirAcceleration;
@@ -211,7 +204,7 @@ bool FirstPersonController::CheckGrounded() const
glm::vec3 FirstPersonController::GetWishDirection() const
{
if (!IsMouseCaptured())
if (!m_Possessed)
return glm::vec3{0.0f};
const auto& input = InputManager::GetInstance();
@@ -255,10 +248,8 @@ void FirstPersonController::ImGuiInspector()
ImGui::Text("Grounded: %s", m_Grounded ? "yes" : "no");
ImGui::Text("Yaw: %.1f deg", glm::degrees(m_Yaw));
ImGui::Text("Pitch: %.1f deg", glm::degrees(m_Pitch));
bool captured = IsMouseCaptured();
ImGui::Checkbox("Mouse Captured", &captured);
if (captured != IsMouseCaptured()) {
SetMouseCaptured(captured);
if (bool possessed = m_Possessed; ImGui::Checkbox("Possessed", &possessed)) {
SetPossessed(possessed);
}
}
@@ -33,14 +33,18 @@ InteractableButton::InteractableButton(GameObject& parent)
void InteractableButton::Start()
{
ApplyColor();
if (m_CycleColors) {
ApplyColor();
}
}
void InteractableButton::Interact(GameObject& interactor)
{
m_ColorIndex = (m_ColorIndex + 1) % ButtonColors.size();
++m_PressCount;
ApplyColor();
if (m_CycleColors) {
m_ColorIndex = (m_ColorIndex + 1) % ButtonColors.size();
ApplyColor();
}
spdlog::info("{} pressed {} by {}", GetGameObject()->GetName(), m_PressCount, interactor.GetName());
ActivateTargets(interactor);
@@ -107,6 +111,7 @@ nlohmann::json InteractableButton::Serialize() const
{
return {
{"colorIndex", m_ColorIndex},
{"cycleColors", m_CycleColors},
{"pressCount", m_PressCount},
{"targets", m_Targets},
};
@@ -115,6 +120,7 @@ nlohmann::json InteractableButton::Serialize() const
void InteractableButton::Deserialize(const nlohmann::json& data)
{
m_ColorIndex = data.value("colorIndex", m_ColorIndex) % ButtonColors.size();
m_CycleColors = data.value("cycleColors", m_CycleColors);
m_PressCount = data.value("pressCount", m_PressCount);
m_Targets = data.value("targets", std::vector<ObjectId>{});
}
@@ -125,6 +131,9 @@ void InteractableButton::ImGuiInspector()
if (ImGui::Button("Press")) {
Interact(*GetGameObject());
}
if (ImGui::Checkbox("Cycle Colors", &m_CycleColors) && m_CycleColors) {
ApplyColor();
}
DrawTargetsInspector();
}
@@ -0,0 +1,68 @@
#include <components/LightToggle.h>
#include <destrum/Components/LightComponent.h>
#include <destrum/Components/MeshRendererComponent.h>
#include <destrum/Graphics/Renderer.h>
#include <destrum/ObjectModel/GameObject.h>
#include <destrum/Util/GameState.h>
#include <imgui.h>
LightToggle::LightToggle(GameObject& parent)
: Activatable(parent, "LightToggle")
{
}
void LightToggle::Start()
{
Apply();
}
void LightToggle::SetOn(bool on)
{
m_On = on;
Apply();
}
void LightToggle::OnActivate(GameObject&)
{
SetOn(!m_On);
}
void LightToggle::Apply()
{
GameObject* object = GetGameObject();
auto* light = object->GetComponent<LightComponent>();
if (light != nullptr) {
light->SetEnabled(m_On);
}
// The bulb glows in the light's colour while it is on
const auto* meshRenderer = object->GetComponent<MeshRendererComponent>();
if (meshRenderer == nullptr || meshRenderer->GetMaterialID() == NULL_MATERIAL_ID ||
!GameState::GetInstance().HasRenderer()) {
return;
}
GameRenderer& renderer = GameState::GetInstance().Renderer();
Material& material = renderer.getMaterialMutable(meshRenderer->GetMaterialID());
material.emissiveColor = m_On && light != nullptr ? light->GetColor() : glm::vec3{0.0f};
renderer.updateMaterialGPU(meshRenderer->GetMaterialID());
}
nlohmann::json LightToggle::Serialize() const
{
return {{"on", m_On}};
}
void LightToggle::Deserialize(const nlohmann::json& data)
{
m_On = data.value("on", m_On);
}
void LightToggle::ImGuiInspector()
{
if (bool on = m_On; ImGui::Checkbox("On", &on)) {
SetOn(on);
}
}
+125 -3
View File
@@ -18,6 +18,7 @@
#include <components/FirstPersonController.h>
#include <components/Door.h>
#include <components/InteractableButton.h>
#include <components/LightToggle.h>
#include <components/TriggerLoggerComponent.h>
#include <glm/gtc/constants.hpp>
@@ -26,11 +27,13 @@
#include <spdlog/spdlog.h>
#include <memory>
#include <optional>
#include <string>
namespace {
// The visible ground plane is at y = -1
constexpr float GroundHeight = -1.0f;
constexpr float GroundHalfExtent = 25.0f;
ModelDoc::LoadOptions StaticModelOptions()
{
@@ -80,12 +83,12 @@ namespace {
planeMeshComp->SetMeshID(resources.meshes().addMesh(gfxDevice, planePrimitive.mesh));
planeMeshComp->SetMaterialID(planeMaterialID);
planeObj->GetTransform().SetWorldPosition(glm::vec3(0.f, GroundHeight, 0.f));
planeObj->GetTransform().SetWorldScale(glm::vec3(10.f, 1.f, 10.f));
planeObj->GetTransform().SetWorldScale(glm::vec3(GroundHalfExtent, 1.f, GroundHalfExtent));
// The plane mesh is flat, so the floor collider sits right below it
GameObject* groundObj = scene.CreateGameObject("GroundCollider");
groundObj->GetTransform().SetWorldPosition(glm::vec3(0.f, GroundHeight - 0.5f, 0.f));
groundObj->AddComponent<BoxCollider>(glm::vec3{10.0f, 0.5f, 10.0f});
groundObj->AddComponent<BoxCollider>(glm::vec3{GroundHalfExtent, 0.5f, GroundHalfExtent});
groundObj->AddComponent<Rigidbody>()->SetType(RigidbodyType::Static);
scene.GetPhysics().RegisterGameObject(*groundObj);
}
@@ -201,6 +204,124 @@ namespace {
button->AddComponent<InteractableButton>()->AddTarget(target);
}
struct WallOpening {
float offset;
float width;
float bottom;
float top;
};
void CreateWall(Scene& scene, GfxDevice& gfxDevice, RenderResources& resources, const std::string& name,
const glm::vec3& floorCenter, float length, float height, bool alongX, MaterialID materialID,
const std::optional<WallOpening>& opening = std::nullopt)
{
constexpr float thickness = 0.3f;
const auto createPiece = [&](const std::string& suffix, float from, float to, float bottom, float top)
{
if (to <= from || top <= bottom) {
return;
}
const float along = (from + to) * 0.5f;
const glm::vec3 alongOffset = alongX ? glm::vec3{along, 0.0f, 0.0f} : glm::vec3{0.0f, 0.0f, along};
const glm::vec3 center = floorCenter + alongOffset + glm::vec3{0.0f, (bottom + top) * 0.5f, 0.0f};
const glm::vec3 size = alongX
? glm::vec3{to - from, top - bottom, thickness}
: glm::vec3{thickness, top - bottom, to - from};
CreateBox(scene, gfxDevice, resources, name + suffix, center, size, materialID, RigidbodyType::Static);
};
const float halfLength = length * 0.5f;
if (!opening) {
createPiece("", -halfLength, halfLength, 0.0f, height);
return;
}
const float openingStart = opening->offset - opening->width * 0.5f;
const float openingEnd = opening->offset + opening->width * 0.5f;
createPiece("_Left", -halfLength, openingStart, 0.0f, height);
createPiece("_Right", openingEnd, halfLength, 0.0f, height);
createPiece("_Below", openingStart, openingEnd, 0.0f, opening->bottom);
createPiece("_Above", openingStart, openingEnd, opening->top, height);
}
GameObject* CreateCeilingLight(Scene& scene, GfxDevice& gfxDevice, RenderResources& resources,
const glm::vec3& position)
{
GameObject* lightObj = scene.CreateGameObject("CeilingLight");
lightObj->GetTransform().SetWorldPosition(position);
// Every bulb needs its own material, the toggle changes its glow
Material bulbMaterial = Material::SimpleColor(glm::vec3{1.0f, 0.95f, 0.85f}, "BulbMaterial");
auto* bulbMesh = lightObj->AddComponent<MeshRendererComponent>();
bulbMesh->SetMeshID(resources.meshes().addMesh(gfxDevice, PrimitiveMeshes::MakeBox({0.35f, 0.12f, 0.35f})));
bulbMesh->SetMaterialID(resources.materials().addMaterial(std::move(bulbMaterial)));
auto* light = lightObj->AddComponent<LightComponent>(LightType::Point);
light->SetColor(glm::vec3{1.0f, 0.82f, 0.55f});
light->SetIntensity(4.0f);
light->SetRange(7.0f);
lightObj->AddComponent<LightToggle>();
return lightObj;
}
void CreateHouse(Scene& scene, GfxDevice& gfxDevice, RenderResources& resources)
{
const MaterialID wallMaterial = AddPrototypeMaterial(gfxDevice, resources, "Light", 0.8f);
const MaterialID floorMaterial = AddPrototypeMaterial(gfxDevice, resources, "Orange", 0.7f);
const MaterialID roofMaterial = AddPrototypeMaterial(gfxDevice, resources, "Red", 0.8f);
const glm::vec3 center{0.0f, GroundHeight, -16.0f};
constexpr float width = 8.0f;
constexpr float depth = 6.0f;
constexpr float wallHeight = 3.0f;
constexpr float doorOffset = -1.5f;
constexpr float doorWidth = 1.2f;
CreateBox(scene, gfxDevice, resources, "House_Floor", center + glm::vec3{0.0f, 0.03f, 0.0f},
{width, 0.06f, depth}, floorMaterial, RigidbodyType::Static);
CreateWall(scene, gfxDevice, resources, "House_FrontWall", center + glm::vec3{0.0f, 0.0f, depth * 0.5f},
width + 0.3f, wallHeight, true, wallMaterial,
WallOpening{.offset = doorOffset, .width = doorWidth, .bottom = 0.0f, .top = 2.2f});
CreateWall(scene, gfxDevice, resources, "House_BackWall", center - glm::vec3{0.0f, 0.0f, depth * 0.5f},
width + 0.3f, wallHeight, true, wallMaterial,
WallOpening{.offset = 1.5f, .width = 1.4f, .bottom = 1.0f, .top = 2.0f});
CreateWall(scene, gfxDevice, resources, "House_LeftWall", center - glm::vec3{width * 0.5f, 0.0f, 0.0f},
depth - 0.3f, wallHeight, false, wallMaterial);
CreateWall(scene, gfxDevice, resources, "House_RightWall", center + glm::vec3{width * 0.5f, 0.0f, 0.0f},
depth - 0.3f, wallHeight, false, wallMaterial,
WallOpening{.offset = 0.0f, .width = 1.6f, .bottom = 1.0f, .top = 2.0f});
CreateBox(scene, gfxDevice, resources, "House_Roof", center + glm::vec3{0.0f, wallHeight + 0.15f, 0.0f},
{width + 0.8f, 0.3f, depth + 0.8f}, roofMaterial, RigidbodyType::Static);
const GameObject* ceilingLight =
CreateCeilingLight(scene, gfxDevice, resources, center + glm::vec3{0.0f, wallHeight - 0.06f, 0.0f});
// Inside, next to the door
Material switchMaterial = Material::SimpleColor(glm::vec3{0.9f}, "LightSwitchMaterial");
switchMaterial.roughnessFactor = 0.5f;
const glm::vec3 switchPosition =
center + glm::vec3{doorOffset + doorWidth * 0.5f + 0.4f, 1.3f, depth * 0.5f - 0.15f - 0.03f};
GameObject* lightSwitch = CreateBox(scene, gfxDevice, resources, "LightSwitch", switchPosition,
{0.12f, 0.2f, 0.06f},
resources.materials().addMaterial(std::move(switchMaterial)),
RigidbodyType::Static);
auto* switchButton = lightSwitch->AddComponent<InteractableButton>();
switchButton->SetCycleColors(false);
switchButton->AddTarget(*ceilingLight);
GameObject* sofa = ModelSpawner::SpawnStaticModel(
scene, gfxDevice, resources, "game://sofa.glb", StaticModelOptions(),
"Sofa"
);
sofa->GetTransform().SetWorldPosition({-3.7, -1, -17.5});
sofa->GetTransform().SetWorldRotation({180, 0, 180});
sofa->GetTransform().SetWorldScale({3, 3, 3});
}
void CreateFlightHelmet(Scene& scene, GfxDevice& gfxDevice, RenderResources& resources)
{
GameObject* helmetObj = ModelSpawner::SpawnStaticModel(
@@ -323,10 +444,11 @@ MainSceneObjects BuildMainScene(Scene& scene, GfxDevice& gfxDevice, RenderResour
CreateParkourCourse(scene, gfxDevice, resources);
CreateFlightHelmet(scene, gfxDevice, resources);
CreateTree(scene, gfxDevice, resources);
CreateCapybara(scene, gfxDevice, resources);
// CreateCapybara(scene, gfxDevice, resources);
CreateTriggerZone(scene);
const GameObject* door = CreateDoorway(scene, gfxDevice, resources);
CreateButton(scene, gfxDevice, resources, *door);
CreateHouse(scene, gfxDevice, resources);
CreatePlayer(scene, objects);
LoadSpawnBallResources(gfxDevice, resources, objects);