refactor: Transform 字段封装 + dirty 标记(对齐 UE USceneComponent 真实现)

- 修正上一版"全量暴力重算":UE 实为 dirty 标记 + 惰性计算,前提是
  字段私有(SetRelativeLocation 等 setter 标脏 + 子级传播)
- TransformComponent:SRT 字段私有化,Set/Get/World()/IsDirty 访问器;
  Set* 变更才标脏;AddRotationY 便捷接口
- Scene::UpdateTransforms 只重算脏实体——静止实体零成本;
  层级(父子)传播在脏标记基础上扩展
- 迁移 Camera/SceneRenderer/main.cpp 到封装 API
This commit is contained in:
2026-09-23 15:35:20 +08:00
parent 57ab8901a9
commit 7d137fa9b1
5 changed files with 63 additions and 47 deletions
+2 -2
View File
@@ -112,8 +112,8 @@ protected:
for (size_t i = 0; i < m_Spinners.size(); ++i) for (size_t i = 0; i < m_Spinners.size(); ++i)
{ {
auto& t = m_Spinners[i].GetComponent<TransformComponent>(); auto& t = m_Spinners[i].GetComponent<TransformComponent>();
t.rotation.y = m_Time * 60.0f + static_cast<f32>(i) * 40.0f; t.SetRotation(glm::vec3(0.0f, m_Time * 60.0f + static_cast<f32>(i) * 40.0f, 0.0f));
t.scale = glm::vec3(0.5f); // dirty → 帧循环统一 Compose t.SetScale(glm::vec3(0.5f));
} }
} }
+34 -19
View File
@@ -34,34 +34,49 @@ namespace Engine::ECS {
std::string name; std::string name;
}; };
// 世界变换:可编辑的 SRT 分量 + 派生的 world 矩阵。 // 世界变换:UE USceneComponent 的 encapsulation 模式——
// 分量可直接读写(含直接改字段);world 由帧循环的 // SRT 分量私有,所有写入口标脏;world 矩阵由帧循环的
// Scene::UpdateTransforms() 全量重算(UE transform-update 阶段 // Scene::UpdateTransforms() 只对脏的实体重算(transform-update
// 模式),调用方永远不需要手动 Compose。渲染热路径只读 world。 // 阶段),并自动向未来的子层级传播。渲染热路径只读 World()。
// 层级(父子变换)留给后续的层级同步系统。 // 层级(父子变换)留给后续的层级同步系统。
struct TransformComponent { struct TransformComponent {
glm::vec3 position{0.0f}; // ---- 读取 ----
glm::vec3 rotation{0.0f}; // 欧拉角(度),应用顺序 Y→X→Z const glm::vec3& GetPosition() const { return m_position; }
glm::vec3 scale{1.0f}; const glm::vec3& GetRotation() const { return m_rotation; } // 欧拉角(度)
glm::mat4 world{1.0f}; const glm::vec3& GetScale() const { return m_scale; }
const glm::mat4& World() const { return m_world; } // 要求已由 UpdateTransforms 计算
TransformComponent& SetPosition(const glm::vec3& p) { position = p; return *this; } // ---- 写入(标脏,脏实体由 UpdateTransforms 重算)----
TransformComponent& SetRotation(const glm::vec3& r) { rotation = r; return *this; } TransformComponent& SetPosition(const glm::vec3& p) { if (m_position != p) { m_position = p; m_dirty = true; } return *this; }
TransformComponent& SetScale(const glm::vec3& s) { scale = s; return *this; } TransformComponent& SetRotation(const glm::vec3& r) { if (m_rotation != r) { m_rotation = r; m_dirty = true; } return *this; }
TransformComponent& SetScale(const glm::vec3& s) { if (m_scale != s) { m_scale = s; m_dirty = true; } return *this; }
// SRT 分量 → world 矩阵(T * R * S)。 // 便捷:原地修改单个欧拉角分量(度)
// 由 UpdateTransforms 每帧全量调用;勿在用户代码手动调用 TransformComponent& AddRotationY(f32 degY) { m_rotation.y += degY; m_dirty = true; return *this; }
bool IsDirty() const { return m_dirty; }
// SRT 分量 → world 矩阵(T * R * S),清除脏标记。
// 由 Scene::UpdateTransforms 调用(或层级系统),勿手动
TransformComponent& Compose() TransformComponent& Compose()
{ {
const glm::mat4 t = glm::translate(glm::mat4(1.0f), position); const glm::mat4 t = glm::translate(glm::mat4(1.0f), m_position);
const glm::mat4 r = const glm::mat4 r =
glm::rotate(glm::mat4(1.0f), glm::radians(rotation.y), glm::vec3(0, 1, 0)) * glm::rotate(glm::mat4(1.0f), glm::radians(m_rotation.y), glm::vec3(0, 1, 0)) *
glm::rotate(glm::mat4(1.0f), glm::radians(rotation.x), glm::vec3(1, 0, 0)) * glm::rotate(glm::mat4(1.0f), glm::radians(m_rotation.x), glm::vec3(1, 0, 0)) *
glm::rotate(glm::mat4(1.0f), glm::radians(rotation.z), glm::vec3(0, 0, 1)); glm::rotate(glm::mat4(1.0f), glm::radians(m_rotation.z), glm::vec3(0, 0, 1));
const glm::mat4 s = glm::scale(glm::mat4(1.0f), scale); const glm::mat4 s = glm::scale(glm::mat4(1.0f), m_scale);
world = t * r * s; m_world = t * r * s;
m_dirty = false;
return *this; return *this;
} }
private:
glm::vec3 m_position{0.0f};
glm::vec3 m_rotation{0.0f};
glm::vec3 m_scale{1.0f};
glm::mat4 m_world{1.0f};
bool m_dirty = true; // 出生即脏:首次 UpdateTransforms 计算 world
}; };
// 网格渲染体:引用共享的 Model 资产。 // 网格渲染体:引用共享的 Model 资产。
+3 -4
View File
@@ -55,11 +55,10 @@ namespace Engine::ECS {
void Scene::UpdateTransforms() void Scene::UpdateTransforms()
{ {
// 全量重算:分量允许直接写字段(无法可靠 dirty 追踪), // 只重算脏的:UE transform-update 阶段模式。
// 无条件 Compose 保证任何写法下 world 都新鲜。 // 静止实体(绝大多数)零成本;层级传播在此扩展
// 实测万级实体内成本可忽略;更大规模再引入 dirty+封装优化
m_Registry.view<TransformComponent>().each([](TransformComponent& t) { m_Registry.view<TransformComponent>().each([](TransformComponent& t) {
t.Compose(); if (t.IsDirty()) t.Compose();
}); });
} }
+16 -14
View File
@@ -14,8 +14,7 @@ namespace Engine {
void Camera::Update(Window& window, ECS::TransformComponent& t, f32 deltaTime) void Camera::Update(Window& window, ECS::TransformComponent& t, f32 deltaTime)
{ {
// 鼠标旋转:直接写实体旋转(yaw → rotation.y,pitch → rotation.x)。 // 鼠标旋转:直接写实体旋转(yaw → rotation.y,pitch → rotation.x)
// Set*/成员写后由帧循环的 UpdateTransforms 统一 Compose
if (m_MouseLookEnabled) if (m_MouseLookEnabled)
{ {
f32 mouseDX = 0, mouseDY = 0; f32 mouseDX = 0, mouseDY = 0;
@@ -23,35 +22,38 @@ namespace Engine {
if (mouseDX != 0 || mouseDY != 0) if (mouseDX != 0 || mouseDY != 0)
{ {
t.rotation.y += mouseDX * m_MouseSensitivity; glm::vec3 rot = t.GetRotation();
t.rotation.x = glm::clamp( rot.y += mouseDX * m_MouseSensitivity;
t.rotation.x - mouseDY * m_MouseSensitivity, -89.0f, 89.0f); rot.x = glm::clamp(
rot.x - mouseDY * m_MouseSensitivity, -89.0f, 89.0f);
t.SetRotation(rot);
} }
} }
// 基向量由实体旋转决定(本地 -Z 为视线方向;应用顺序 Y→X, // 基向量由实体旋转决定(本地 -Z 为视线方向;应用顺序 Y→X,
// 与 TransformComponent::Compose 一致;roll 不参与 FPS 相机) // 与 TransformComponent::Compose 一致;roll 不参与 FPS 相机)
const glm::vec3& rot = t.GetRotation();
const glm::mat3 r = static_cast<glm::mat3>( const glm::mat3 r = static_cast<glm::mat3>(
glm::rotate(glm::mat4(1.0f), glm::radians(t.rotation.y), glm::rotate(glm::mat4(1.0f), glm::radians(rot.y),
glm::vec3(0, 1, 0)) * glm::vec3(0, 1, 0)) *
glm::rotate(glm::mat4(1.0f), glm::radians(t.rotation.x), glm::rotate(glm::mat4(1.0f), glm::radians(rot.x),
glm::vec3(1, 0, 0))); glm::vec3(1, 0, 0)));
const glm::vec3 forward = r * glm::vec3(0, 0, -1); const glm::vec3 forward = r * glm::vec3(0, 0, -1);
const glm::vec3 right = r * glm::vec3(1, 0, 0); const glm::vec3 right = r * glm::vec3(1, 0, 0);
f32 speed = m_MoveSpeed * deltaTime; f32 speed = m_MoveSpeed * deltaTime;
if (window.IsKeyPressed(GLFW_KEY_W)) { t.position += forward * speed; } if (window.IsKeyPressed(GLFW_KEY_W)) { t.SetPosition(t.GetPosition() + forward * speed); }
if (window.IsKeyPressed(GLFW_KEY_S)) { t.position -= forward * speed; } if (window.IsKeyPressed(GLFW_KEY_S)) { t.SetPosition(t.GetPosition() - forward * speed); }
if (window.IsKeyPressed(GLFW_KEY_A)) { t.position -= right * speed; } if (window.IsKeyPressed(GLFW_KEY_A)) { t.SetPosition(t.GetPosition() - right * speed); }
if (window.IsKeyPressed(GLFW_KEY_D)) { t.position += right * speed; } if (window.IsKeyPressed(GLFW_KEY_D)) { t.SetPosition(t.GetPosition() + right * speed); }
if (window.IsKeyPressed(GLFW_KEY_SPACE)) { t.position.y += speed; } if (window.IsKeyPressed(GLFW_KEY_SPACE)) { t.SetPosition(t.GetPosition() + glm::vec3(0, speed, 0)); }
if (window.IsKeyPressed(GLFW_KEY_LEFT_SHIFT)) { t.position.y -= speed; } if (window.IsKeyPressed(GLFW_KEY_LEFT_SHIFT)) { t.SetPosition(t.GetPosition() - glm::vec3(0, speed, 0)); }
} }
glm::mat4 Camera::GetView(const ECS::TransformComponent& t) const glm::mat4 Camera::GetView(const ECS::TransformComponent& t) const
{ {
// 相机作为实体:视图 = 世界矩阵的逆(无重复位姿状态) // 相机作为实体:视图 = 世界矩阵的逆(无重复位姿状态)
return glm::inverse(t.world); return glm::inverse(t.World());
} }
glm::mat4 Camera::GetProjection(f32 aspect, f32 nearZ, f32 farZ) const glm::mat4 Camera::GetProjection(f32 aspect, f32 nearZ, f32 farZ) const
+8 -8
View File
@@ -185,7 +185,7 @@ namespace Engine
ubo.lights[i].color = glm::vec4(0.0f); ubo.lights[i].color = glm::vec4(0.0f);
} }
ComputeCascades(shadowDir); ComputeCascades(shadowDir);
ubo.cameraPos = glm::vec4(camTransform->position, 1.0f); ubo.cameraPos = glm::vec4(camTransform->GetPosition(), 1.0f);
// ---- 3. GPUDriven:单趟收集 + 多视图剔除 ---- // ---- 3. GPUDriven:单趟收集 + 多视图剔除 ----
// 每个实例的包围球对 4 个视锥分别测试: // 每个实例的包围球对 4 个视锥分别测试:
@@ -218,22 +218,22 @@ namespace Engine
// TAA velocity:本实例的上一帧世界矩阵(首帧/新实体 = 当前 // TAA velocity:本实例的上一帧世界矩阵(首帧/新实体 = 当前
// 矩阵 → 速度为 0,不产生假运动) // 矩阵 → 速度为 0,不产生假运动)
const u32 entKey = static_cast<u32>(entt::to_integral(entity)); const u32 entKey = static_cast<u32>(entt::to_integral(entity));
glm::mat4 prevWorld = t.world; glm::mat4 prevWorld = t.World();
if (auto it = m_PrevWorlds.find(entKey); it != m_PrevWorlds.end()) { if (auto it = m_PrevWorlds.find(entKey); it != m_PrevWorlds.end()) {
prevWorld = it->second; prevWorld = it->second;
} }
m_CurWorlds.emplace(entKey, t.world); m_CurWorlds.emplace(entKey, t.World());
// 实例世界缩放(矩阵列向量最大长度,包围球半径随缩放) // 实例世界缩放(矩阵列向量最大长度,包围球半径随缩放)
const f32 worldScale = glm::max( const f32 worldScale = glm::max(
glm::length(glm::vec3(t.world[0])), glm::length(glm::vec3(t.World()[0])),
glm::max(glm::length(glm::vec3(t.world[1])), glm::max(glm::length(glm::vec3(t.World()[1])),
glm::length(glm::vec3(t.world[2])))); glm::length(glm::vec3(t.World()[2]))));
for (auto& mesh : model->GetMeshes()) { for (auto& mesh : model->GetMeshes()) {
const auto& bounds = mesh->GetBounds(); const auto& bounds = mesh->GetBounds();
const glm::vec3 worldCenter = const glm::vec3 worldCenter =
glm::vec3(t.world * glm::vec4(bounds.center, 1.0f)); glm::vec3(t.World() * glm::vec4(bounds.center, 1.0f));
const f32 worldRadius = bounds.radius * worldScale; const f32 worldRadius = bounds.radius * worldScale;
auto tryAppend = [&](ViewScratch& s, const Frustum& frustum) { auto tryAppend = [&](ViewScratch& s, const Frustum& frustum) {
@@ -252,7 +252,7 @@ namespace Engine
} }
s.result.batches[it->second].count++; s.result.batches[it->second].count++;
s.result.instanceCount++; s.result.instanceCount++;
s.worlds.push_back(t.world); s.worlds.push_back(t.World());
s.prevWorlds.push_back(prevWorld); s.prevWorlds.push_back(prevWorld);
++totalInstances; ++totalInstances;
}; };