Skip to main content

re_core/
scene.rs

1use crate::id::{MaterialId, MeshId, NodeId, SkeletonId, TileMapId};
2use crate::transform::Transform;
3use serde::{Deserialize, Serialize};
4use std::collections::BTreeMap;
5
6#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
7pub enum NodeKind {
8    Empty,
9    MeshInstance {
10        mesh: MeshId,
11        #[serde(default, skip_serializing_if = "Option::is_none")]
12        material: Option<MaterialId>,
13        /// Skeleton-instance node this mesh is skinned to (for display/export).
14        #[serde(default, skip_serializing_if = "Option::is_none")]
15        skeleton_node: Option<NodeId>,
16    },
17    /// Instantiates a skeleton asset; its joints form a virtual hierarchy.
18    SkeletonInstance {
19        skeleton: SkeletonId,
20    },
21    /// Painted grid of tiles; expands to one mesh placement per cell.
22    TileMapInstance {
23        tilemap: TileMapId,
24    },
25    Camera {
26        fov_deg: f32,
27    },
28    Light {
29        intensity: f32,
30    },
31}
32
33#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
34pub struct Node {
35    pub name: String,
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    pub parent: Option<NodeId>,
38    pub children: Vec<NodeId>,
39    pub transform: Transform,
40    pub visible: bool,
41    pub kind: NodeKind,
42    /// Procedural motion evaluated at draw time (viewer only; glTF export skips it).
43    #[serde(default, skip_serializing_if = "crate::motion::Motion::is_none")]
44    pub motion: crate::motion::Motion,
45}
46
47impl Default for Node {
48    fn default() -> Self {
49        Self {
50            name: String::new(),
51            parent: None,
52            children: Vec::new(),
53            transform: Transform::default(),
54            visible: true,
55            kind: NodeKind::Empty,
56            motion: crate::motion::Motion::None,
57        }
58    }
59}
60
61/// Flat node map. Roots have `parent == None`; `roots` keeps stable ordering.
62#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
63pub struct Scene {
64    pub nodes: BTreeMap<NodeId, Node>,
65    pub roots: Vec<NodeId>,
66    pub(crate) next_node: u32,
67}
68
69impl Scene {
70    pub fn new() -> Self {
71        Self::default()
72    }
73
74    pub fn alloc_node_id(&mut self) -> NodeId {
75        let id = NodeId(self.next_node);
76        self.next_node += 1;
77        id
78    }
79
80    /// Insert a node under `parent` (`None` = scene root). Returns the id.
81    pub fn insert(&mut self, parent: Option<NodeId>, node: Node) -> NodeId {
82        let id = self.alloc_node_id();
83        let mut node = node;
84        node.parent = parent;
85        if let Some(p) = parent {
86            if let Some(pn) = self.nodes.get_mut(&p) {
87                pn.children.push(id);
88            }
89        } else {
90            self.roots.push(id);
91        }
92        self.nodes.insert(id, node);
93        id
94    }
95
96    pub fn get(&self, id: NodeId) -> Option<&Node> {
97        self.nodes.get(&id)
98    }
99
100    pub fn get_mut(&mut self, id: NodeId) -> Option<&mut Node> {
101        self.nodes.get_mut(&id)
102    }
103
104    pub fn remove(&mut self, id: NodeId) -> Option<Node> {
105        let node = self.nodes.remove(&id)?;
106        // detach from parent
107        match node.parent {
108            Some(p) => {
109                if let Some(pn) = self.nodes.get_mut(&p) {
110                    pn.children.retain(|c| *c != id);
111                }
112            }
113            None => self.roots.retain(|r| *r != id),
114        }
115        // orphan children become roots
116        for c in &node.children {
117            if let Some(cn) = self.nodes.get_mut(c) {
118                cn.parent = None;
119            }
120            self.roots.push(*c);
121        }
122        Some(node)
123    }
124
125    /// Remove a node and its whole subtree.
126    pub fn remove_subtree(&mut self, id: NodeId) {
127        let children: Vec<NodeId> = self.get(id).map(|n| n.children.clone()).unwrap_or_default();
128        for c in children {
129            self.remove_subtree(c);
130        }
131        self.remove(id);
132    }
133
134    /// World transform = product of TRS from root down to this node.
135    pub fn world_transform(&self, id: NodeId) -> Transform {
136        let Some(node) = self.get(id) else {
137            return Transform::default();
138        };
139        match node.parent {
140            Some(p) => self.world_transform(p).then(&node.transform),
141            None => node.transform,
142        }
143    }
144
145    /// Depth-first pre-order walk from roots.
146    pub fn walk(&self, mut f: impl FnMut(NodeId, &Node)) {
147        fn rec(scene: &Scene, id: NodeId, f: &mut impl FnMut(NodeId, &Node)) {
148            if let Some(n) = scene.get(id) {
149                f(id, n);
150                for c in n.children.clone() {
151                    rec(scene, c, f);
152                }
153            }
154        }
155        for r in self.roots.clone() {
156            rec(self, r, &mut f);
157        }
158    }
159}