Skip to content

Commit 79c3d96

Browse files
schellm0ppers
andcommitted
Rename nodes_in_scene to root_nodes_in_scene, add recursive_nodes_in_scene
Split the scene node query API into two explicit methods: - root_nodes_in_scene: returns only the top-level nodes directly referenced by the scene (the original behavior) - recursive_nodes_in_scene: returns all nodes including descendants in depth-first order This fixes a bug where animations targeting child (parented) nodes were silently skipped because only root nodes were collected. Update call sites: - Animator test now uses recursive_nodes_in_scene for correctness - Example crate simplified by removing manual get_children helper Co-authored-by: Andreas Streichardt <andreas@mop.koeln>
1 parent bc21fb2 commit 79c3d96

3 files changed

Lines changed: 32 additions & 27 deletions

File tree

crates/example/src/lib.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -286,29 +286,8 @@ impl App {
286286

287287
let scene = doc.default_scene.unwrap_or(0);
288288
log::info!("Displaying scene {scene}");
289-
fn get_children(doc: &GltfDocument, n: usize) -> Vec<usize> {
290-
let mut children = vec![];
291-
if let Some(parent) = doc.nodes.get(n) {
292-
children.extend(parent.children.iter().copied());
293-
let descendants = parent
294-
.children
295-
.iter()
296-
.copied()
297-
.flat_map(|n| get_children(doc, n));
298-
children.extend(descendants);
299-
}
300-
children
301-
}
302289

303-
let nodes = doc.nodes_in_scene(scene).flat_map(|n| {
304-
let mut all_nodes = vec![n];
305-
for child_index in get_children(&doc, n.index) {
306-
if let Some(child_node) = doc.nodes.get(child_index) {
307-
all_nodes.push(child_node);
308-
}
309-
}
310-
all_nodes
311-
});
290+
let nodes = doc.recursive_nodes_in_scene(scene);
312291
log::trace!(" nodes:");
313292
for node in nodes {
314293
let tfrm = Mat4::from(node.global_transform());

crates/renderling/src/gltf.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,21 +1237,47 @@ where
12371237
self.primitives.iter().flat_map(|(_, rs)| rs.iter())
12381238
}
12391239

1240-
fn nodes_in_scene_recursive<'a>(&'a self, node_index: usize, nodes: &mut Vec<&'a GltfNode>) {
1240+
fn collect_nodes_recursive<'a>(&'a self, node_index: usize, nodes: &mut Vec<&'a GltfNode>) {
12411241
if let Some(node) = self.nodes.get(node_index) {
12421242
nodes.push(node);
12431243
for child_index in node.children.iter() {
1244-
self.nodes_in_scene_recursive(*child_index, nodes);
1244+
self.collect_nodes_recursive(*child_index, nodes);
12451245
}
12461246
}
12471247
}
12481248

1249-
pub fn nodes_in_scene(&self, scene_index: usize) -> impl Iterator<Item = &GltfNode> {
1249+
/// Returns the root (top-level) nodes in the given scene.
1250+
///
1251+
/// This roughly follows [`gltf::Scene::nodes`](https://docs.rs/gltf/latest/gltf/scene/struct.Scene.html#method.nodes),
1252+
/// returning only the nodes directly referenced by the scene — not
1253+
/// their children.
1254+
///
1255+
/// Use [`recursive_nodes_in_scene`](Self::recursive_nodes_in_scene)
1256+
/// if you need all nodes (including descendants).
1257+
pub fn root_nodes_in_scene(&self, scene_index: usize) -> impl Iterator<Item = &GltfNode> {
1258+
let scene = self.scenes.get(scene_index);
1259+
let mut nodes = vec![];
1260+
if let Some(indices) = scene {
1261+
for node_index in indices {
1262+
if let Some(node) = self.nodes.get(*node_index) {
1263+
nodes.push(node);
1264+
}
1265+
}
1266+
}
1267+
nodes.into_iter()
1268+
}
1269+
1270+
/// Returns all nodes in the given scene, recursively including
1271+
/// children.
1272+
///
1273+
/// Root nodes are visited first, followed by their descendants in
1274+
/// depth-first order.
1275+
pub fn recursive_nodes_in_scene(&self, scene_index: usize) -> impl Iterator<Item = &GltfNode> {
12501276
let scene = self.scenes.get(scene_index);
12511277
let mut nodes = vec![];
12521278
if let Some(indices) = scene {
12531279
for node_index in indices {
1254-
self.nodes_in_scene_recursive(*node_index, &mut nodes);
1280+
self.collect_nodes_recursive(*node_index, &mut nodes);
12551281
}
12561282
}
12571283
nodes.into_iter()

crates/renderling/src/gltf/anime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ mod test {
787787
.unwrap();
788788

789789
let nodes = doc
790-
.nodes_in_scene(doc.default_scene.unwrap_or_default())
790+
.recursive_nodes_in_scene(doc.default_scene.unwrap_or_default())
791791
.collect::<Vec<_>>();
792792

793793
let mut animator = Animator::new(nodes, doc.animations.first().unwrap().clone());

0 commit comments

Comments
 (0)