scena 1.7.2

A Rust-native scene-graph renderer with typed scene state, glTF assets, and explicit prepare/render lifecycles.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use std::collections::BTreeMap;
use std::collections::BTreeSet;

use crate::assets::Assets;
use crate::diagnostics::LookupError;
use crate::geometry::{Aabb, GeometryDesc};
use crate::material::{Color, MaterialDesc};

use super::{CameraKey, NodeKey, Scene, Transform};

pub const INSPECTION_HELPER_TAG: &str = "scena:inspection:helper";

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SceneVisibilitySnapshot {
    entries: Vec<SceneVisibilitySnapshotEntry>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SceneVisibilitySnapshotEntry {
    pub node: NodeKey,
    pub visible: bool,
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct SceneTintSnapshot {
    entries: Vec<SceneTintSnapshotEntry>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SceneTintSnapshotEntry {
    pub node: NodeKey,
    pub tint: Option<Color>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InspectionHelperKind {
    BoundingBox,
    WorldAxesTriad,
    LocalAxesTriad,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct InspectionHelperReport {
    pub kind: InspectionHelperKind,
    pub node: NodeKey,
    pub target: Option<NodeKey>,
    pub bounds: Option<Aabb>,
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct InspectionToolkitReport {
    pub isolated_nodes: Vec<NodeKey>,
    pub hidden_by_isolate_count: usize,
    pub ghosted_nodes: Vec<NodeKey>,
    pub helper_nodes: Vec<InspectionHelperReport>,
}

#[derive(Debug, Default)]
pub(super) struct InspectionToolkitState {
    isolated_nodes: Vec<NodeKey>,
    hidden_by_isolate: BTreeSet<NodeKey>,
    helpers: BTreeMap<NodeKey, InspectionHelperRecord>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
struct InspectionHelperRecord {
    kind: InspectionHelperKind,
    target: Option<NodeKey>,
    bounds: Option<Aabb>,
}

struct InspectionHelperSpec {
    geometry: GeometryDesc,
    transform: Transform,
    parent: NodeKey,
    record: InspectionHelperRecord,
    color: Color,
}

impl SceneVisibilitySnapshot {
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    pub fn entries(&self) -> &[SceneVisibilitySnapshotEntry] {
        &self.entries
    }
}

impl SceneTintSnapshot {
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    pub fn entries(&self) -> &[SceneTintSnapshotEntry] {
        &self.entries
    }
}

impl Scene {
    pub fn hide(&mut self, node: NodeKey) -> Result<(), LookupError> {
        self.set_visible(node, false)
    }

    pub fn show(&mut self, node: NodeKey) -> Result<(), LookupError> {
        self.set_visible(node, true)
    }

    pub fn toggle_visibility(&mut self, node: NodeKey) -> Result<bool, LookupError> {
        let visible = self.visible(node).ok_or(LookupError::NodeNotFound(node))?;
        let next = !visible;
        self.set_visible(node, next)?;
        Ok(next)
    }

    pub fn show_only(
        &mut self,
        nodes: impl IntoIterator<Item = NodeKey>,
    ) -> Result<SceneVisibilitySnapshot, LookupError> {
        let selected_nodes = nodes.into_iter().collect::<Vec<_>>();
        let mut keep = BTreeSet::from([self.root()]);
        for node in selected_nodes.iter().copied() {
            self.collect_isolate_keep_set(node, &mut keep)?;
        }
        let snapshot = self.apply_visibility_keep_set(&keep)?;
        self.inspection_toolkit.isolated_nodes = selected_nodes;
        self.inspection_toolkit.hidden_by_isolate = snapshot
            .entries
            .iter()
            .filter_map(|entry| entry.visible.then_some(entry.node))
            .collect();
        Ok(snapshot)
    }

    pub fn isolate(
        &mut self,
        nodes: impl IntoIterator<Item = NodeKey>,
    ) -> Result<SceneVisibilitySnapshot, LookupError> {
        self.show_only(nodes)
    }

    pub fn restore_visibility(
        &mut self,
        snapshot: &SceneVisibilitySnapshot,
    ) -> Result<(), LookupError> {
        for entry in &snapshot.entries {
            self.set_visible(entry.node, entry.visible)?;
        }
        self.inspection_toolkit.isolated_nodes.clear();
        self.inspection_toolkit.hidden_by_isolate.clear();
        Ok(())
    }

    pub fn ghost(&mut self, node: NodeKey, alpha: f32) -> Result<SceneTintSnapshot, LookupError> {
        if !alpha.is_finite() || !(0.0..=1.0).contains(&alpha) {
            return Err(LookupError::InvalidFramingOption {
                field: "alpha",
                reason: "ghost alpha must be finite and between 0 and 1",
            });
        }
        self.ghost_with_tint(node, Color::from_linear_rgba(1.0, 1.0, 1.0, alpha))
    }

    pub fn ghost_with_tint(
        &mut self,
        node: NodeKey,
        tint: Color,
    ) -> Result<SceneTintSnapshot, LookupError> {
        if !color_is_finite(tint) {
            return Err(LookupError::InvalidFramingOption {
                field: "tint",
                reason: "ghost tint must contain finite color channels",
            });
        }
        let mut nodes = Vec::new();
        self.collect_subtree(node, &mut nodes)?;
        let mut entries = Vec::new();
        for node in nodes {
            let previous = self.node_tint(node)?;
            if previous != Some(tint) {
                entries.push(SceneTintSnapshotEntry {
                    node,
                    tint: previous,
                });
                self.set_node_tint(node, Some(tint))?;
            }
        }
        Ok(SceneTintSnapshot { entries })
    }

    pub fn restore_tints(&mut self, snapshot: &SceneTintSnapshot) -> Result<(), LookupError> {
        for entry in &snapshot.entries {
            self.set_node_tint(entry.node, entry.tint)?;
        }
        Ok(())
    }

    pub fn fit_selection_with_assets<F>(
        &mut self,
        camera: CameraKey,
        nodes: impl IntoIterator<Item = NodeKey>,
        assets: &Assets<F>,
    ) -> Result<Aabb, LookupError> {
        let bounds = self.selected_bounds_with_assets(nodes, assets)?;
        self.frame(camera, bounds)?;
        Ok(bounds)
    }

    pub fn add_bounding_box_overlay<F>(
        &mut self,
        assets: &Assets<F>,
        node: NodeKey,
    ) -> Result<InspectionHelperReport, LookupError> {
        let bounds = self
            .node_world_bounds(node, assets)?
            .ok_or(LookupError::ImportHasNoBounds)?;
        let helper = self.add_inspection_helper(
            assets,
            InspectionHelperSpec {
                geometry: GeometryDesc::bounding_box(bounds),
                transform: Transform::IDENTITY,
                parent: self.root(),
                record: InspectionHelperRecord {
                    kind: InspectionHelperKind::BoundingBox,
                    target: Some(node),
                    bounds: Some(bounds),
                },
                color: Color::YELLOW,
            },
        )?;
        Ok(helper)
    }

    pub fn add_world_axes_triad<F>(
        &mut self,
        assets: &Assets<F>,
        length: f32,
    ) -> Result<InspectionHelperReport, LookupError> {
        validate_helper_length(length)?;
        self.add_inspection_helper(
            assets,
            InspectionHelperSpec {
                geometry: GeometryDesc::axes(length),
                transform: Transform::IDENTITY,
                parent: self.root(),
                record: InspectionHelperRecord {
                    kind: InspectionHelperKind::WorldAxesTriad,
                    target: None,
                    bounds: None,
                },
                color: Color::WHITE,
            },
        )
    }

    pub fn add_local_axes_triad<F>(
        &mut self,
        assets: &Assets<F>,
        node: NodeKey,
        length: f32,
    ) -> Result<InspectionHelperReport, LookupError> {
        if !self.nodes.contains_key(node) {
            return Err(LookupError::NodeNotFound(node));
        }
        validate_helper_length(length)?;
        self.add_inspection_helper(
            assets,
            InspectionHelperSpec {
                geometry: GeometryDesc::axes(length),
                transform: Transform::IDENTITY,
                parent: node,
                record: InspectionHelperRecord {
                    kind: InspectionHelperKind::LocalAxesTriad,
                    target: Some(node),
                    bounds: None,
                },
                color: Color::CYAN,
            },
        )
    }

    pub fn inspection_toolkit_report(&self) -> InspectionToolkitReport {
        let isolated_nodes = self
            .inspection_toolkit
            .isolated_nodes
            .iter()
            .copied()
            .filter(|node| self.nodes.contains_key(*node))
            .collect::<Vec<_>>();
        let hidden_by_isolate_count = self
            .inspection_toolkit
            .hidden_by_isolate
            .iter()
            .filter(|node| {
                self.nodes
                    .get(**node)
                    .is_some_and(|node_ref| !node_ref.visible)
            })
            .count();
        let ghosted_nodes = self
            .nodes
            .iter()
            .filter_map(|(node, node_ref)| {
                node_ref
                    .tint
                    .is_some_and(|tint| tint.a.is_finite() && tint.a < 1.0)
                    .then_some(node)
            })
            .collect::<Vec<_>>();
        let helper_nodes = self
            .inspection_toolkit
            .helpers
            .iter()
            .filter_map(|(node, record)| {
                self.nodes
                    .contains_key(*node)
                    .then_some(InspectionHelperReport {
                        kind: record.kind,
                        node: *node,
                        target: record.target,
                        bounds: record.bounds,
                    })
            })
            .collect();
        InspectionToolkitReport {
            isolated_nodes,
            hidden_by_isolate_count,
            ghosted_nodes,
            helper_nodes,
        }
    }

    fn selected_bounds_with_assets<F>(
        &self,
        nodes: impl IntoIterator<Item = NodeKey>,
        assets: &Assets<F>,
    ) -> Result<Aabb, LookupError> {
        let mut bounds: Option<Aabb> = None;
        let mut any_node = false;
        for node in nodes {
            any_node = true;
            if let Some(node_bounds) = self.node_world_bounds(node, assets)? {
                bounds = Some(match bounds {
                    Some(bounds) => bounds.union(node_bounds),
                    None => node_bounds,
                });
            }
        }
        if any_node {
            bounds.ok_or(LookupError::ImportHasNoBounds)
        } else {
            Err(LookupError::ImportHasNoBounds)
        }
    }

    fn add_inspection_helper<F>(
        &mut self,
        assets: &Assets<F>,
        spec: InspectionHelperSpec,
    ) -> Result<InspectionHelperReport, LookupError> {
        let geometry = assets.create_geometry(spec.geometry);
        let material = assets.create_material(MaterialDesc::line(spec.color, 1.0));
        let node = self
            .mesh(geometry, material)
            .parent(spec.parent)
            .transform(spec.transform)
            .add()?;
        self.set_helper_on_top(node, true)?;
        self.add_tag(node, INSPECTION_HELPER_TAG)?;
        self.inspection_toolkit.helpers.insert(node, spec.record);
        Ok(InspectionHelperReport {
            kind: spec.record.kind,
            node,
            target: spec.record.target,
            bounds: spec.record.bounds,
        })
    }

    fn collect_isolate_keep_set(
        &self,
        node: NodeKey,
        keep: &mut BTreeSet<NodeKey>,
    ) -> Result<(), LookupError> {
        if !self.nodes.contains_key(node) {
            return Err(LookupError::NodeNotFound(node));
        }
        let mut current = Some(node);
        while let Some(node) = current {
            keep.insert(node);
            current = self.nodes.get(node).and_then(|node| node.parent);
        }
        self.collect_subtree_into_set(node, keep)
    }

    fn collect_subtree_into_set(
        &self,
        node: NodeKey,
        keep: &mut BTreeSet<NodeKey>,
    ) -> Result<(), LookupError> {
        let node_ref = self
            .nodes
            .get(node)
            .ok_or(LookupError::NodeNotFound(node))?;
        keep.insert(node);
        for child in &node_ref.children {
            self.collect_subtree_into_set(*child, keep)?;
        }
        Ok(())
    }

    fn collect_subtree(&self, node: NodeKey, nodes: &mut Vec<NodeKey>) -> Result<(), LookupError> {
        let node_ref = self
            .nodes
            .get(node)
            .ok_or(LookupError::NodeNotFound(node))?;
        nodes.push(node);
        for child in &node_ref.children {
            self.collect_subtree(*child, nodes)?;
        }
        Ok(())
    }

    fn apply_visibility_keep_set(
        &mut self,
        keep: &BTreeSet<NodeKey>,
    ) -> Result<SceneVisibilitySnapshot, LookupError> {
        let updates = self
            .nodes
            .iter()
            .map(|(node, node_ref)| {
                let desired = keep.contains(&node);
                (node, node_ref.visible, desired)
            })
            .filter(|(_, current, desired)| current != desired)
            .collect::<Vec<_>>();
        let mut entries = Vec::with_capacity(updates.len());
        for (node, current, desired) in updates {
            entries.push(SceneVisibilitySnapshotEntry {
                node,
                visible: current,
            });
            self.set_visible(node, desired)?;
        }
        Ok(SceneVisibilitySnapshot { entries })
    }
}

fn color_is_finite(color: Color) -> bool {
    color.r.is_finite() && color.g.is_finite() && color.b.is_finite() && color.a.is_finite()
}

fn validate_helper_length(length: f32) -> Result<(), LookupError> {
    if length.is_finite() && length > 0.0 {
        Ok(())
    } else {
        Err(LookupError::InvalidFramingOption {
            field: "length",
            reason: "inspection helper length must be finite and positive",
        })
    }
}