refactor: transform world 矩阵改由帧循环统一重算,删除手动 Compose
- Scene::UpdateTransforms():渲染前全量 Compose(UE transform-update 阶段模式),Application::Run 在 FlushDestroyQueue 后调用 - 用户代码永远不需要手动 Compose——Set*/直接写字段均安全; Compose 降级为引擎内部调用 - 选择全量而非 dirty 标记:字段直接写(t.rotation.y = x)无法可靠 追踪脏,无条件重算零脚枪;万级实体内成本可忽略,更大规模再引入 dirty+封装 - main.cpp/Camera 所有手动 Compose 调用点删除
This commit is contained in:
@@ -96,6 +96,7 @@ namespace Engine {
|
||||
|
||||
// 延迟销毁在渲染前统一 flush,保证渲染遍历时池结构不变
|
||||
m_Scene->FlushDestroyQueue();
|
||||
m_Scene->UpdateTransforms(); // UE transform-update 阶段:渲染前保证 world 新鲜
|
||||
|
||||
const auto uiStart = std::chrono::steady_clock::now();
|
||||
|
||||
|
||||
@@ -53,6 +53,16 @@ namespace Engine::ECS {
|
||||
m_PendingDestroy.clear();
|
||||
}
|
||||
|
||||
void Scene::UpdateTransforms()
|
||||
{
|
||||
// 全量重算:分量允许直接写字段(无法可靠 dirty 追踪),
|
||||
// 无条件 Compose 保证任何写法下 world 都新鲜。
|
||||
// 实测万级实体内成本可忽略;更大规模再引入 dirty+封装优化
|
||||
m_Registry.view<TransformComponent>().each([](TransformComponent& t) {
|
||||
t.Compose();
|
||||
});
|
||||
}
|
||||
|
||||
CameraComponent* Scene::GetPrimaryCamera(TransformComponent** outTransform)
|
||||
{
|
||||
CameraComponent* found = nullptr;
|
||||
|
||||
@@ -14,9 +14,8 @@ namespace Engine {
|
||||
|
||||
void Camera::Update(Window& window, ECS::TransformComponent& t, f32 deltaTime)
|
||||
{
|
||||
bool dirty = false;
|
||||
|
||||
// 鼠标旋转:直接写实体旋转(yaw → rotation.y,pitch → rotation.x)
|
||||
// 鼠标旋转:直接写实体旋转(yaw → rotation.y,pitch → rotation.x)。
|
||||
// Set*/成员写后由帧循环的 UpdateTransforms 统一 Compose
|
||||
if (m_MouseLookEnabled)
|
||||
{
|
||||
f32 mouseDX = 0, mouseDY = 0;
|
||||
@@ -27,7 +26,6 @@ namespace Engine {
|
||||
t.rotation.y += mouseDX * m_MouseSensitivity;
|
||||
t.rotation.x = glm::clamp(
|
||||
t.rotation.x - mouseDY * m_MouseSensitivity, -89.0f, 89.0f);
|
||||
dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,15 +40,12 @@ namespace Engine {
|
||||
const glm::vec3 right = r * glm::vec3(1, 0, 0);
|
||||
|
||||
f32 speed = m_MoveSpeed * deltaTime;
|
||||
if (window.IsKeyPressed(GLFW_KEY_W)) { t.position += forward * speed; dirty = true; }
|
||||
if (window.IsKeyPressed(GLFW_KEY_S)) { t.position -= forward * speed; dirty = true; }
|
||||
if (window.IsKeyPressed(GLFW_KEY_A)) { t.position -= right * speed; dirty = true; }
|
||||
if (window.IsKeyPressed(GLFW_KEY_D)) { t.position += right * speed; dirty = true; }
|
||||
if (window.IsKeyPressed(GLFW_KEY_SPACE)) { t.position.y += speed; dirty = true; }
|
||||
if (window.IsKeyPressed(GLFW_KEY_LEFT_SHIFT)) { t.position.y -= speed; dirty = true; }
|
||||
|
||||
// 世界矩阵供 GetView(inverse)与渲染消费
|
||||
if (dirty) t.Compose();
|
||||
if (window.IsKeyPressed(GLFW_KEY_W)) { t.position += forward * speed; }
|
||||
if (window.IsKeyPressed(GLFW_KEY_S)) { t.position -= forward * speed; }
|
||||
if (window.IsKeyPressed(GLFW_KEY_A)) { t.position -= right * speed; }
|
||||
if (window.IsKeyPressed(GLFW_KEY_D)) { t.position += right * speed; }
|
||||
if (window.IsKeyPressed(GLFW_KEY_SPACE)) { t.position.y += speed; }
|
||||
if (window.IsKeyPressed(GLFW_KEY_LEFT_SHIFT)) { t.position.y -= speed; }
|
||||
}
|
||||
|
||||
glm::mat4 Camera::GetView(const ECS::TransformComponent& t) const
|
||||
|
||||
Reference in New Issue
Block a user