载入中...
搜索中...
未找到
TransformSystem.cpp
浏览该文件的文档.
1// eve-hot-path
4
5#include "common/Profile.h"
6#include "scene/SceneHost.h"
7#include "scene/SceneLink.h"
9
10#include <cmath>
11#include <glm/gtc/matrix_transform.hpp>
12#include <glm/gtc/quaternion.hpp>
13
14namespace eve::scene {
15namespace detail {
16
17glm::mat4 localMatrix(const SceneNode &n) {
18 glm::mat4 m(1.f);
19 m = glm::translate(m, glm::vec3(n.x, n.y, n.z));
20 if (n.space == "2d") {
21 m = glm::rotate(m, n.roll, glm::vec3(0.f, 0.f, 1.f));
22 } else {
23 m = glm::rotate(m, n.yaw, glm::vec3(0.f, 1.f, 0.f));
24 m = glm::rotate(m, n.pitch, glm::vec3(1.f, 0.f, 0.f));
25 m = glm::rotate(m, n.roll, glm::vec3(0.f, 0.f, 1.f));
26 }
27 m = glm::scale(m, glm::vec3(n.sx, n.sy, n.sz));
28 return m;
29}
30} // namespace detail
31
32namespace {
33
34void updateNode(SceneHost::Tree &tree, int nodeIndex, const glm::mat4 &parentWorld) {
35 SceneNode &n = tree.nodes[size_t(nodeIndex)];
36 n.world = parentWorld * detail::localMatrix(n);
37 n.localDirty = false;
38 for (int c = n.firstChild; c >= 0; c = tree.nodes[size_t(c)].nextSibling) {
39 updateNode(tree, c, n.world);
40 }
41}
42
44void pullTargets(SceneHost *host) {
45 if (!host) return;
46 auto t = host->tree();
47 for (size_t i = 0; i < t->nodes.size(); ++i) {
48 SceneNode &n = t->nodes[i];
49 for (auto &l : n.links) {
50 if (l.syncMode != 1 || !l.target) continue;
51 const LinkOps *ops = linkOps(l.kind);
52 if (!ops || !ops->pullWorld) continue;
53 ops->pullWorld(n, l.target);
54 host->markSubtreeDirty(int(i));
55 }
56 }
57}
58
60void pushTarget(const SceneNode &n, const SceneLink &l);
61void updateNodeIncremental(SceneHost::Tree &tree, int nodeIndex,
62 const glm::mat4 &parentWorld, bool force) {
63 SceneNode &n = tree.nodes[size_t(nodeIndex)];
64 const bool need = force || n.subtreeDirty;
65 if (need) {
66 n.world = parentWorld * detail::localMatrix(n);
67 n.localDirty = false;
68 n.subtreeDirty = false;
69 for (auto &l : n.links) {
70 if (l.syncMode == 0) pushTarget(n, l);
71 }
72 }
73 for (int c = n.firstChild; c >= 0; c = tree.nodes[size_t(c)].nextSibling) {
74 updateNodeIncremental(tree, c, n.world, need);
75 }
76}
77
79void pushTarget(const SceneNode &n, const SceneLink &l) {
80 if (!l.target) return;
81 const LinkOps *ops = linkOps(l.kind);
82 if (ops) ops->pushWorld(n, l.target);
83}
84
85void syncLinks(SceneHost *host) {
86 if (!host) return;
87 auto t = host->tree();
88 for (auto &n : t->nodes) {
89 for (auto &l : n.links) {
90 if (l.syncMode == 0) pushTarget(n, l);
91 }
92 }
93}
94
100bool linkTargetAlive(const SceneLink &l) {
101 if (!l.target) return false;
102 const LinkOps *ops = linkOps(l.kind);
103 if (!ops || !ops->alive) return true;
104 return ops->alive(l.target);
105}
106
108void purgeDeadLinks(SceneHost::Tree &tree) {
109 for (auto &n : tree.nodes) {
110 for (auto it = n.links.begin(); it != n.links.end();) {
111 if (linkTargetAlive(*it)) {
112 ++it;
113 } else {
114 it = n.links.erase(it);
115 }
116 }
117 }
118}
119
120} // namespace
121
123 if (!host) return;
124 auto t = host->tree();
125 if (t->root < 0 || t->nodes.empty()) {
126 t->transformDirty = false;
127 return;
128 }
129 purgeDeadLinks(*t);
130 pullTargets(host);
131
132 // Legacy coarse path: tree-level transformDirty or direct localDirty writes
133 // without subtree marking → full recompute + full link sync.
134 // Gather both flags after pull callbacks, preserving their ordering and
135 // detecting legacy edits even after a marked subtree has been found.
136 bool legacy = t->transformDirty;
137 bool anySubtree = false;
138 if (!legacy) {
139 for (const auto &n : t->nodes) {
140 if (n.localDirty && !n.subtreeDirty) {
141 legacy = true;
142 break;
143 }
144 anySubtree = anySubtree || n.subtreeDirty;
145 }
146 }
147 if (legacy) {
148 for (size_t index = 0; index < t->nodes.size(); ++index)
149 if (t->nodes[index].parent < 0) updateNode(*t, static_cast<int>(index), glm::mat4(1.f));
150 syncLinks(host);
151 for (auto &n : t->nodes) {
152 n.localDirty = false;
153 n.subtreeDirty = false;
154 }
155 t->transformDirty = false;
156 return;
157 }
158
159 // Incremental: only dirty subtrees recomputed, only their links synced.
160 if (!anySubtree) {
161 t->transformDirty = false;
162 return; // fully clean tree: zero work
163 }
164 for (size_t index = 0; index < t->nodes.size(); ++index)
165 if (t->nodes[index].parent < 0) updateNodeIncremental(*t, static_cast<int>(index), glm::mat4(1.f), false);
166 t->transformDirty = false;
167}
168
171 EV_PROFILE_MODULE("scene", "TransformSystem::updateAll");
172 if (ecs::current()->getManager<SceneHost>() == nullptr) return;
173 auto view = ecs::View<SceneHost, SceneHost::Meta, SceneHost::Tree>();
174 for (auto it = view.begin(); it != view.end(); ++it) {
175 auto [meta, tree] = *it;
176 (void)tree;
177 if (!meta->entity) continue;
178 updateHost(meta->entity);
179 }
180}
181
182} // namespace eve::scene
double m[16]
ForceVector force
glm::vec3 n
Definition Grass.cpp:63
std::int32_t c
std::map< std::string, Node > nodes
OceanSprayView view
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
Definition Profile.h:158
int detail
float t
LinkOps ops
UIHostHandle host
uint32_t index
ECS mount point for one scene graph (full scene or nested subtree root). Isomorphic to eve::ui::UIHos...
Definition SceneHost.h:89
static void updateHost(SceneHost *host)
Updates host.
static void updateAll()
Updates all.
glm::mat4 localMatrix(const SceneNode &node)
void flushPendingComponents()
Flush one batch of live dirty components before host traversal. @thread Owner thread; newly queued ed...
const LinkOps * linkOps(int kindId)
Link ops.
void(* pushWorld)(const SceneNode &node, void *target)
Void.
Definition SceneLink.h:34
void(* pullWorld)(SceneNode &node, void *target)
Void.
Definition SceneLink.h:41
bool(* alive)(const void *target)
Bool.
Definition SceneLink.h:48
Retained scene node (arena). Conceptual GameObject; isomorphic to eve::ui::UINode.
Definition SceneHost.h:46