载入中...
搜索中...
未找到
NodeDesc.cpp
浏览该文件的文档.
1#include "scene/NodeDesc.h"
2
3#include "scene/SceneHost.h"
5
6#include <cmath>
7#include <functional>
8#include <stdexcept>
9#include <unordered_map>
10#include <unordered_set>
11
12namespace eve::scene {
13namespace {
14
15int appendDetachedNode(SceneHost::Tree& tree, NodeDesc&& desc, int parentIndex) {
16 const int index = int(tree.nodes.size());
17 SceneNode node;
18 node.id = std::move(desc.id);
19 node.persistentId = desc.persistentId;
20 node.key = desc.key.empty() ? node.id : std::move(desc.key);
21 node.name = desc.name.empty() ? node.id : std::move(desc.name);
22 node.space = std::move(desc.space);
23 if (node.space.empty()) node.space = "3d";
24 node.visible = desc.visible;
25 node.tags = std::move(desc.tags);
26 node.layer = desc.layer;
27 node.bminX = desc.bminX;
28 node.bminY = desc.bminY;
29 node.bminZ = desc.bminZ;
30 node.bmaxX = desc.bmaxX;
31 node.bmaxY = desc.bmaxY;
32 node.bmaxZ = desc.bmaxZ;
33 node.hasBounds = desc.hasBounds;
34 node.x = desc.x;
35 node.y = desc.y;
36 node.z = desc.z;
37 node.yaw = desc.yaw;
38 node.pitch = desc.pitch;
39 node.roll = desc.roll;
40 node.sx = desc.sx;
41 node.sy = desc.sy;
42 node.sz = desc.sz;
43 node.localDirty = true;
44 node.world = glm::mat4(1.f);
45 node.firstChild = -1;
46 node.nextSibling = -1;
47 node.parent = parentIndex;
48
49 tree.nodes.push_back(std::move(node));
50
51 int prevChild = -1;
52 int firstChild = -1;
53 for (auto &child : desc.children) {
54 int childIndex = appendDetachedNode(tree, std::move(child), index);
55 if (firstChild < 0) firstChild = childIndex;
56 if (prevChild >= 0) tree.nodes[size_t(prevChild)].nextSibling = childIndex;
57 prevChild = childIndex;
58 }
59 tree.nodes[size_t(index)].firstChild = firstChild;
60 return index;
61}
62
63bool structureMatches(const SceneHost::Tree &tree, int nodeIndex, const NodeDesc &desc) {
64 if (nodeIndex < 0 || nodeIndex >= int(tree.nodes.size())) return false;
65 const SceneNode &n = tree.nodes[size_t(nodeIndex)];
66 const std::string &dk = desc.reconcileKey();
67 if (!dk.empty() && !n.key.empty() && n.key != dk) return false;
68
69 std::vector<std::string> oldKeys;
70 for (int c = n.firstChild; c >= 0; c = tree.nodes[size_t(c)].nextSibling) {
71 oldKeys.push_back(tree.nodes[size_t(c)].key);
72 }
73 if (oldKeys.size() != desc.children.size()) return false;
74 for (size_t i = 0; i < desc.children.size(); ++i) {
75 const std::string &ck = desc.children[i].reconcileKey();
76 if (ck.empty() || oldKeys[i].empty()) {
77 if (!(ck.empty() && oldKeys[i].empty())) return false;
78 } else if (ck != oldKeys[i]) {
79 return false;
80 }
81 }
82
83 int child = n.firstChild;
84 for (const auto &ch : desc.children) {
85 if (!structureMatches(tree, child, ch)) return false;
86 child = tree.nodes[size_t(child)].nextSibling;
87 }
88 return true;
89}
90
95bool structureMatchesSet(const SceneHost::Tree &tree, int nodeIndex,
96 const NodeDesc &desc) {
97 if (nodeIndex < 0 || nodeIndex >= int(tree.nodes.size())) return false;
98 const SceneNode &n = tree.nodes[size_t(nodeIndex)];
99 const std::string &dk = desc.reconcileKey();
100 if (!dk.empty() && !n.key.empty() && n.key != dk) return false;
101
102 std::vector<std::string> oldKeys;
103 for (int c = n.firstChild; c >= 0; c = tree.nodes[size_t(c)].nextSibling) {
104 oldKeys.push_back(tree.nodes[size_t(c)].key);
105 }
106 if (oldKeys.size() != desc.children.size()) return false;
107
108 std::unordered_multiset<std::string> want;
109 for (const auto &ch : desc.children) want.insert(ch.reconcileKey());
110 for (const auto &k : oldKeys) {
111 auto it = want.find(k);
112 if (it == want.end()) return false;
113 want.erase(it);
114 }
115
116 // Pair every old child with a same-key desc child and recurse.
117 std::vector<bool> used(desc.children.size(), false);
118 for (int c = n.firstChild; c >= 0; c = tree.nodes[size_t(c)].nextSibling) {
119 bool found = false;
120 const std::string &ck = tree.nodes[size_t(c)].key;
121 for (size_t i = 0; i < desc.children.size(); ++i) {
122 if (used[i]) continue;
123 const std::string &dk2 = desc.children[i].reconcileKey();
124 if (ck == dk2 || (ck.empty() && dk2.empty())) {
125 if (structureMatchesSet(tree, c, desc.children[i])) {
126 used[i] = true;
127 found = true;
128 break;
129 }
130 }
131 }
132 if (!found) return false;
133 }
134 return true;
135}
136
138bool reorderChildrenRecursive(SceneHost::Tree &tree, int nodeIndex,
139 const NodeDesc &desc) {
140 SceneNode &n = tree.nodes[size_t(nodeIndex)];
141 std::unordered_map<std::string, std::vector<int>> byKey;
142 for (int c = n.firstChild; c >= 0; c = tree.nodes[size_t(c)].nextSibling) {
143 byKey[tree.nodes[size_t(c)].key].push_back(c);
144 }
145 std::vector<int> order;
146 order.reserve(desc.children.size());
147 for (const auto &ch : desc.children) {
148 auto it = byKey.find(ch.reconcileKey());
149 if (it == byKey.end() || it->second.empty()) return false;
150 order.push_back(it->second.back());
151 it->second.pop_back();
152 }
153 if (order.empty()) {
154 n.firstChild = -1;
155 } else {
156 n.firstChild = order[0];
157 for (size_t i = 0; i < order.size(); ++i) {
158 tree.nodes[size_t(order[i])].nextSibling =
159 (i + 1 < order.size()) ? order[i + 1] : -1;
160 }
161 }
162 for (size_t i = 0; i < order.size(); ++i) {
163 if (!reorderChildrenRecursive(tree, order[i], desc.children[i])) return false;
164 }
165 return true;
166}
167
168void patchProps(SceneHost *host, int nodeIndex, NodeDesc &&desc) {
169 SceneHost::Tree &tree = *host->tree();
170 SceneNode &n = tree.nodes[size_t(nodeIndex)];
171 n.visible = desc.visible;
172 n.tags = desc.tags;
173 n.layer = desc.layer;
174 n.bminX = desc.bminX;
175 n.bminY = desc.bminY;
176 n.bminZ = desc.bminZ;
177 n.bmaxX = desc.bmaxX;
178 n.bmaxY = desc.bmaxY;
179 n.bmaxZ = desc.bmaxZ;
180 n.hasBounds = desc.hasBounds;
181 n.x = desc.x;
182 n.y = desc.y;
183 n.z = desc.z;
184 n.yaw = desc.yaw;
185 n.pitch = desc.pitch;
186 n.roll = desc.roll;
187 n.sx = desc.sx;
188 n.sy = desc.sy;
189 n.sz = desc.sz;
190 if (!desc.persistentId.isNil()) n.persistentId = desc.persistentId;
191 if (!desc.space.empty()) n.space = desc.space;
192 if (!desc.id.empty()) n.id = desc.id;
193 if (!desc.name.empty()) n.name = desc.name;
194 host->markSubtreeDirty(nodeIndex);
195 host->fireEvent("node_changed", n.id,
196 n.parent >= 0 ? tree.nodes[size_t(n.parent)].id : "");
197
198 int child = n.firstChild;
199 for (auto &ch : desc.children) {
200 patchProps(host, child, std::move(ch));
201 child = tree.nodes[size_t(child)].nextSibling;
202 }
203}
204
205} // namespace
206
208 std::unordered_set<std::string> seen;
209 std::function<void(const NodeDesc &)> walk = [&](const NodeDesc &d) {
210 if (!d.id.empty() && !seen.insert(d.id).second) {
211 throw std::runtime_error("scene: duplicate node id '" + d.id + "'");
212 }
213 for (const auto &c : d.children) walk(c);
214 };
215 walk(root);
216}
217
218NodeDesc node(std::string id, std::vector<NodeDesc> children, std::string name) {
219 NodeDesc d;
220 d.id = std::move(id);
221 d.key = d.id;
222 d.name = name.empty() ? d.id : std::move(name);
223 d.children = std::move(children);
224 return d;
225}
226
227NodeDesc group(std::vector<NodeDesc> children, std::string id) {
228 NodeDesc d;
229 d.id = std::move(id);
230 d.key = d.id;
231 d.name = d.id.empty() ? "group" : d.id;
232 d.children = std::move(children);
233 return d;
234}
235
236NodeDesc when(bool cond, NodeDesc child) {
237 if (!cond) return group({}, "__when_empty");
238 return child;
239}
240
241NodeDesc whenElse(bool cond, NodeDesc ifTrue, NodeDesc ifFalse) {
242 return cond ? std::move(ifTrue) : std::move(ifFalse);
243}
244
246 if (!host) return;
248 auto t = host->tree();
249
250 std::unordered_set<std::string> oldIds;
251 for (const auto &n : t->nodes) {
252 if (!n.id.empty()) oldIds.insert(n.id);
253 }
254
255 std::unordered_map<std::string, std::vector<SceneLink>> saved;
256 for (const auto &n : t->nodes) {
257 if (!n.links.empty() && !n.id.empty()) saved[n.id] = n.links;
258 }
259
260 // Preserve lazy SceneObject bindings by node id (script entities stay alive
261 // across full rebuilds; orphaned SceneObjects are torn down by Scene::prune).
262 std::unordered_map<std::string, uint32_t> savedObjects;
263 for (const auto &n : t->nodes) {
264 if (n.objectId != 0 && !n.id.empty()) savedObjects[n.id] = n.objectId;
265 }
266
267 t->nodes.clear();
268 t->root = -1;
269 t->root = appendDetachedNode(*t, std::move(root), -1);
270 for (auto &n : t->nodes) {
271 auto it = saved.find(n.id);
272 if (it != saved.end()) {
273 n.links = it->second;
274 }
275 auto oit = savedObjects.find(n.id);
276 if (oit != savedObjects.end()) n.objectId = oit->second;
277 }
278 host->invalidateIndex();
279 t->dirty = true;
280 t->transformDirty = true;
281
282 // Node lifecycle events: ids that disappeared were removed; new ids added.
283 std::unordered_set<std::string> newIds;
284 for (const auto &n : t->nodes) {
285 if (!n.id.empty()) newIds.insert(n.id);
286 }
287 for (const auto &id : oldIds) {
288 if (!newIds.count(id)) host->fireEvent("node_removed", id);
289 }
290 for (const auto &n : t->nodes) {
291 if (!oldIds.count(n.id)) {
292 host->fireEvent("node_added", n.id,
293 n.parent >= 0 ? t->nodes[size_t(n.parent)].id : "");
294 }
295 }
296}
297
299 try {
301 Tree target;
302 target.root = appendDetachedNode(target, std::move(root), -1);
303 for (auto &node : target.nodes) {
304 const auto parentWorld = node.parent < 0 ? glm::mat4(1.f) : target.nodes[size_t(node.parent)].world;
305 node.world = parentWorld * detail::localMatrix(node);
306 for (unsigned column = 0; column < 4; ++column)
307 for (unsigned row = 0; row < 4; ++row)
308 if (!std::isfinite(node.world[column][row]))
311 "detached scene transform must produce a finite world matrix", node.id));
312 }
313 if (previous) {
314 std::unordered_map<std::string, const SceneNode *> preserved;
315 preserved.reserve(previous->nodes.size());
316 for (const auto& node : previous->nodes)
317 if (!node.id.empty()) preserved.emplace(node.id, &node);
318 for (auto& node : target.nodes) {
319 const auto found = preserved.find(node.id);
320 if (found == preserved.end()) continue;
321 node.links = found->second->links;
322 node.objectId = found->second->objectId;
323 }
324 }
325 target.dirty = true;
326 target.transformDirty = true;
327 target.indexValid = false;
328 return eve::Result<Tree>::success(std::move(target));
329 } catch (const std::exception& exception) {
331 eve::Diagnostic::error(eve::DiagnosticCode::Conflict, exception.what(), "root"));
332 }
333}
334
336 if (!host) return true;
337 auto t = host->tree();
338 if (t->root < 0 || t->nodes.empty() ||
339 !structureMatchesSet(*t, t->root, root)) {
340 applyTree(host, std::move(root));
341 return true;
342 }
343 // Same key set, maybe different order: relink siblings first so patching
344 // follows the new order without rebuilding the arena (links/objects keep
345 // their identity).
346 if (!reorderChildrenRecursive(*t, t->root, root)) {
347 applyTree(host, std::move(root));
348 return true;
349 }
350 patchProps(host, t->root, std::move(root));
351 t->dirty = false;
352 t->transformDirty = true;
353 return false;
354}
355
356} // namespace eve::scene
LogicalId target
int root
Definition AnimSmr.cpp:119
std::string name
Definition ECS.cpp:154
scene::NodeDesc desc
glm::vec3 n
Definition Grass.cpp:63
std::int32_t c
uint64_t used
std::vector< std::int32_t > order
std::shared_ptr< const OceanSprayFrame > previous
float d
float t
const RoadNode * node
bool found
unsigned column
AbilityActivationGroup group
int children
Definition TreeMesh.cpp:432
UIHostHandle host
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
ECS mount point for one scene graph (full scene or nested subtree root). Isomorphic to eve::ui::UIHos...
Definition SceneHost.h:89
static eve::Result< Tree > buildDetachedTree(const Tree *previous, NodeDesc root)
Builds a detached replacement tree without mutating a host or firing callbacks.
Definition NodeDesc.cpp:298
ShapeState * child(const JPH::Body &body, const JPH::SubShapeID &id)
Resolves a native child record for a synchronous callback or query. @ownership Borrowed from the worl...
glm::mat4 localMatrix(const SceneNode &node)
bool applyTreeReconcile(SceneHost *host, NodeDesc root)
Key-aware patch when structure matches; else full replace. Returns true if full rebuild.
Definition NodeDesc.cpp:335
void validateUniqueIds(const NodeDesc &root)
Definition NodeDesc.cpp:207
NodeDesc when(bool cond, NodeDesc child)
Conditional: include child only when cond is true (empty group otherwise).
Definition NodeDesc.cpp:236
void applyTree(SceneHost *host, NodeDesc root)
Applies tree.
Definition NodeDesc.cpp:245
NodeDesc whenElse(bool cond, NodeDesc ifTrue, NodeDesc ifFalse)
When else.
Definition NodeDesc.cpp:241
Declarative scene-node description (build once / on dirty → flatten into SceneHost::Tree)....
Definition NodeDesc.h:19
std::string id
Definition NodeDesc.h:20