feat: compose interactive world ui across nested 3d viewports

This commit is contained in:
2026-11-11 00:08:23 +08:00
parent ad2c01bc29
commit 15fc01542c
27 changed files with 1412 additions and 84 deletions
+62 -6
View File
@@ -194,24 +194,46 @@ pub fn project(
) -> anyhow::Result<()> {
use crate::ui_bridge::UiKind;
use std::collections::HashMap;
use std::sync::Arc;
let mut nodes = HashMap::new();
let mut work = vec![root];
while let Some(node) = work.pop() {
nodes.insert(node.block_id.as_str(), node);
work.extend(node.children().iter().map(|node| node.as_ref()));
}
let mut contexts = HashMap::<u64, (Transform, Option<[f32; 4]>)>::new();
type Scope = (
Transform,
Option<[f32; 4]>,
Option<Arc<crate::scene3d::panel::Mapping>>,
);
let mut contexts = HashMap::<u64, Scope>::new();
let next: HashMap<_, _> = frame
.mounts
.iter()
.map(|mount| (mount.id, mount.next_sibling))
.collect();
let mut panels =
HashMap::<u64, (Arc<crate::scene3d::panel::Mapping>, crate::layout::Size)>::new();
for mount in &mut frame.mounts {
let node = nodes[mount.block_id.as_str()];
let (view, clip) = mount
.parent
.and_then(|parent| contexts.get(&parent).copied())
.unwrap_or_default();
let (view, clip, panel) = if let Some((panel, size)) = panels.get(&mount.id) {
(
Transform::default(),
Some([0., 0., size.width, size.height]),
Some(panel.clone()),
)
} else {
mount
.parent
.and_then(|parent| contexts.get(&parent).cloned())
.unwrap_or_default()
};
anyhow::ensure!(
view.valid(),
"nested viewport transform exceeds numeric budget"
);
mount.view = (view != Transform::default()).then_some(view);
mount.panel = panel.clone();
mount.clip = clip;
mount.viewport_clip = None;
let mut child_view = view;
@@ -260,7 +282,41 @@ pub fn project(
mount.viewport_clip = child_clip;
}
}
contexts.insert(mount.id, (child_view, child_clip));
if let UiKind::Viewport3D {
view: scene,
panels: sizes,
..
} = &node.kind
{
let mut depth = 0;
let mut ancestor = panel.clone();
while let Some(mapping) = ancestor {
depth += 1;
ancestor = mapping.parent.clone();
}
anyhow::ensure!(depth < 8, "3D UI viewport nesting exceeds 8 surfaces");
let mut child = mount.first_child;
for (key, size) in node.slots.iter().zip(sizes) {
let id = child.ok_or_else(|| anyhow::anyhow!("missing 3D UI root occurrence"))?;
let mapping = Arc::new(crate::scene3d::panel::Mapping {
owner: mount.id,
key: *key,
view: scene.clone(),
rect: [
mount.x + inset.left,
mount.y + inset.top,
(mount.width - inset.horizontal()).max(0.),
(mount.height - inset.vertical()).max(0.),
],
outer: view,
clip: mount.viewport_clip,
parent: panel.clone(),
});
panels.insert(id, (mapping, *size));
child = next[&id];
}
}
contexts.insert(mount.id, (child_view, child_clip, panel));
}
let mounts: HashMap<_, _> = frame.mounts.iter().map(|mount| (mount.id, mount)).collect();
frame.command_views.clear();