// // Created by atdunbg on 2026/9/20. // // SandboxApp:第一个基于 Application 框架的程序。 // // main 的职责边界: // - 定义 Application 派生类并运行它 // - "程序逻辑":场景搭建(实体/组件)、逐帧更新、事件响应、 // 异步环境加载 // - UI 全部委托 App/editor/ 的 docking Editor(本文件不再出现 // 任何 ImGui 控件) // // 窗口、渲染器、事件泵、帧计时、场景容器全部由框架管理。 // #include #include #include // SetInputCapture(Application.h 只前置声明) #include #include #include #include #include #include #include #include #include #include #include #include #include "Editor.h" #include "EditorContext.h" // kEnvPaths / kDefaultEnv(与 StatsPanel 共用) using namespace Engine; using namespace Engine::ECS; namespace { constexpr const char* kTemplePath = "assets/models/SunTemple/SunTemple.fbx"; class SandboxApp final : public Application { public: SandboxApp() : Application("HelloVulkan", 1920, 1080) { } ~SandboxApp() override { } protected: // ---- 生命周期 ---- void OnInit() override { Scene& scene = GetScene(); Renderer& renderer = GetRenderer(); renderer.SetDisplayMode(kDisplayViewport); // ImGui 叠加层(框架只做管线桥接;UI 内容 = docking Editor) EnableImGui(); // Editor 注入:引擎门面 + 沙盒能力回调(环境异步切换等) InitEditor(); // 默认环境(IBL 四件套):经资产层加载(compute 预积分,毫秒级), // 其余环境在 Stats 面板里按需 lazy 加载后切换 const auto env = GetAssets().LoadEnvironment(kEnvPaths[0]); if (env.Valid()) { GetRenderer().SetEnvironment(GetAssets().GetEnvironment(env)); } /* const auto suntemple = GetAssets().LoadModel(kTemplePath); auto entityhandle = scene.CreateEntity("SunTemple"); entityhandle.AddComponent(MeshRendererComponent{suntemple}); */ scene.CreateEntity("test entity"); scene.CreateEntity("test entity"); scene.CreateEntity("test entity"); const auto gun = GetAssets().LoadModel("assets/models/m1911/M1911.fbx"); if (gun.Valid()) { m_Gun = scene.CreateEntity("M1911"); m_Gun.AddComponent(MeshRendererComponent{gun}); m_Gun.AddComponent(); auto& gt = m_Gun.GetComponent(); gt.SetPosition(glm::vec3(0.0f, 1.2f, -6.0f)); // 物理验证:Dynamic 刚体(默认 Box halfExtents 0.5), // 出生位置悬空 → 应下落至地面静止(Transform 由物理写回) m_Gun.AddComponent(); } else { ENGINE_WARN("m1911 model not loaded, animation test skipped"); } // 地面:程序化纯色平面(半边长 15,中心 y=-0.5)+ 静态盒刚体 const auto ground = GetAssets().CreatePlane(15.0f, 140, 140, 145); m_Ground = scene.CreateEntity("Ground"); m_Ground.GetComponent() .SetPosition(glm::vec3(0.0f, -0.5f, 0.0f)); // dirty → 帧循环统一 Compose m_Ground.AddComponent(MeshRendererComponent{ground}); auto& groundRb = m_Ground.AddComponent(); groundRb.type = RigidBodyComponent::BodyType::Static; groundRb.boxHalfExtents = glm::vec3(15.0f, 0.1f, 15.0f); // 平面顶点烘焙 ±15,不能用默认 0.5 /* const auto gun = GetAssets().LoadModel("assets/models/m1911/M1911.fbx"); if (gun.Valid()) { m_Gun = scene.CreateEntity("M1911"); m_Gun.AddComponent(MeshRendererComponent{gun}); m_Gun.AddComponent(); auto& gt = m_Gun.GetComponent(); gt.SetPosition(glm::vec3(0.0f, 1.2f, -6.0f)); } else { ENGINE_WARN("m1911 model not loaded, animation test skipped"); } int countSize = 0; if (countSize) { // 模型资产(PBR 验证场景:backpack 为旧式贴图资产, // 退化为电介质因子路径——albedo+normal,metallic=0/roughness=0.5) const auto model = GetAssets().LoadModel("assets/models/backpack/backpack.obj"); // const auto model = GetAssets().LoadModel("assets/models/pocket_watch_4k.gltf/pocket_watch_4k.gltf"); for (int i = 0; i < countSize; i ++) { for (int j = 0; j < countSize; j ++) { auto m = scene.CreateEntity(); m.AddComponent(MeshRendererComponent{model}); m.GetComponent().SetPosition(glm::vec3{(i - countSize * 0.5) * 2, 0, (j - countSize * 0.5) * 2}); m_Spinners.push_back(m); } } } // 地面:程序化纯色平面(半边长 15,中心 y=-2) const auto ground = GetAssets().CreatePlane(15.0f, 140, 140, 145); m_Ground = scene.CreateEntity("Ground"); m_Ground.GetComponent() .SetPosition(glm::vec3(0.0f, -0.5f, 0.0f)); // dirty → 帧循环统一 Compose m_Ground.AddComponent(MeshRendererComponent{ground}); */ // 相机实体(primary = 当前渲染视角;位姿存实体 Transform) m_Camera = scene.CreateEntity("MainCamera"); auto& camera = m_Camera.AddComponent(); camera.primary = true; m_Camera.GetComponent() .SetPosition(glm::vec3(0.0f, 12.0f, 22.0f)); m_Camera.GetComponent() .SetRotation(glm::vec3(-25.0f, 0.0f, 0.0f)); // 灯光实体(第一个方向光 = 阴影光源) m_KeyLight = scene.CreateEntity("KeyLight"); m_KeyLight.AddComponent( LightComponent{ .type = LightComponent::Type::Directional, .direction = glm::vec3(-0.5f, -0.6f, -0.35f), .color = glm::vec4(1.0f, 0.95f, 0.9f, 5.0f), .castShadow = true, }); /* scene.CreateEntity("FillLight") .AddComponent( LightComponent{ .type = LightComponent::Type::Directional, .direction = glm::vec3(0.5f, -0.5f, 0.3f), .color = glm::vec4(0.3f, 0.5f, 1.0f, 1.5f) }); */ /* // 点光源(位置 = 实体 Transform,出生即有;无阴影,距离衰减) auto fire = scene.CreateEntity("FirePitLight"); fire.GetComponent().SetPosition(glm::vec3(2.0f, 1.5f, 1.0f)); fire.AddComponent( LightComponent{ .type = LightComponent::Type::Point, .color = glm::vec4(1.0f, 0.6f, 0.3f, 1.0f), // 暖橙 .radius = 8.0f, }); auto cool = scene.CreateEntity("CoolFillLight"); cool.GetComponent().SetPosition(glm::vec3(-2.5f, 2.0f, -2.0f)); cool.AddComponent( LightComponent{ .type = LightComponent::Type::Point, .color = glm::vec4(0.3f, 0.5f, 1.0f, 1.2f), // 冷蓝 .radius = 10.0f, }); */ } void OnUpdate(const FrameTime& frameTime) override { // 相机(WASD/Space/Shift 移动;位姿读写实体 Transform)。 // 编辑器相机走真实轨:暂停(timeScale=0)时仍可飞行观察 m_Camera.GetComponent() .camera.Update(GetWindow(), m_Camera.GetComponent(), frameTime.UnscaledDt()); // 旋转动画:编辑 Transform 分量后 Compose 重算 world。 // 动画时钟 = Time::Elapsed()(scaled 累计,暂停自动冻结—— // 帧历史验证:暂停后 Temporal Diff 视图 2 帧内整屏变黑) for (size_t i = 0; i < m_Spinners.size(); ++i) { auto& t = m_Spinners[i].GetComponent(); t.SetRotation(glm::vec3( 0.0f, static_cast(Time::Elapsed()) * 60.0f + static_cast(i) * 40.0f, 0.0f)); } } // ---- UI(全部委托 docking Editor;本文件零 ImGui 控件)---- void OnImGuiRender() override { m_Editor->Draw(); } // ---- 事件 ---- void OnWindowClose(const Event& event) override { // 退出确认走 Editor 的模态框(ImGui 必须可见才有 UI) SetImGuiEnabled(true); m_Editor->RequestQuit(); } void OnKeyPress(const Event& event) override { if (event.key == GLFW_KEY_GRAVE_ACCENT) { SetImGuiEnabled(!GetImGuiEnabled()); } if (event.key == GLFW_KEY_F1) { STATS.PrintComparison(); } } void OnWindowResize(const Event& event) override { ENGINE_INFO("App: window resized to {}x{}", event.width, event.height); } private: // Editor 注入:门面指针 + 反向能力回调(面板 → 沙盒) void InitEditor() { EditorContext ctx; ctx.renderer = &GetRenderer(); ctx.scene = &GetScene(); ctx.assets = &GetAssets(); ctx.app = this; ctx.getEnvIndex = [this] { return m_EnvIndex; }; ctx.setEnvIndex = [this](int index) { m_EnvIndex = index; }; // 继承原沙盒默认:初始暂停(StatsPanel 的 Pause Simulation 开关恢复) Time::SetTimeScale(0.0f); m_Editor->Init(ctx); // 输入归属:覆盖 EnableImGui 的全局 WantCapture* 谓词 //(全屏 dock host 让它恒真,输入全死)。改为 Editor 按帧 // 判定——Viewport hover/focus 拥有输入(Hazel/UE 语义); // UI 隐藏(反引号)时 Draw 不再执行,直接全放行 GetWindow().SetInputCapture( [this] { return GetImGuiEnabled() && m_Editor->BlockingKeyboard(); }, [this] { return GetImGuiEnabled() && m_Editor->BlockingMouse(); }); } private: std::unique_ptr m_Editor = std::make_unique(); EntityHandle m_Camera{}; // Scene::CreateEntity 之前为空 EntityHandle m_KeyLight{}; EntityHandle m_Ground{}; EntityHandle m_Gun{}; // 骨骼动画验证实体(M1911) std::vector m_Spinners; int m_EnvIndex = kDefaultEnv; }; } int main() { Logger::Initialize(); { SandboxApp app; try { app.Run(); } catch (const std::exception& e) { ENGINE_FATAL("Exception: {}", e.what()); Logger::Shutdown(); return -1; } } // app 析构:成员(Device/Renderer/Window…)全部销毁完毕 Logger::Shutdown(); return 0; }