feat: separate 2d scenes viewports and explicit cameras

This commit is contained in:
2026-11-10 22:42:56 +08:00
parent 32d7ada3ab
commit 040b779f5f
29 changed files with 1309 additions and 70 deletions
+62 -6
View File
@@ -156,6 +156,16 @@ impl RenderDocument {
for clip in self.frame.command_clips.iter().flatten() {
validate_rect(clip[0], clip[1], clip[2], clip[3], 0.0)?;
}
if !self.frame.command_views.is_empty()
&& self.frame.command_views.len() != self.frame.commands.len()
{
bail!("invalid command view table");
}
for view in self.frame.command_views.iter().flatten() {
if !view.valid() {
bail!("invalid command view transform");
}
}
for command in &self.frame.commands {
command.validate()?;
}
@@ -175,6 +185,9 @@ impl RenderDocument {
}
let mut mount_ids = HashSet::new();
for mount in &self.frame.mounts {
if mount.view.is_some_and(|view| !view.valid()) {
bail!("invalid Mount view transform");
}
if mount.id == 0 || mount.block_id.is_empty() || !mount_ids.insert(mount.id) {
bail!("invalid or duplicate mount identity");
}
@@ -182,6 +195,9 @@ impl RenderDocument {
if let Some(clip) = mount.clip {
validate_rect(clip[0], clip[1], clip[2], clip[3], 0.0)?;
}
if let Some(clip) = mount.viewport_clip {
validate_rect(clip[0], clip[1], clip[2], clip[3], 0.0)?;
}
if let Some(scroll) = mount.scroll
&& (!scroll.iter().all(|n| {
n.is_finite() && (0.0..=crate::ui_bridge::MAX_DIMENSION as f32).contains(n)
@@ -229,7 +245,17 @@ impl RenderDocument {
clip[0], clip[1], clip[2], clip[3], scale,
)));
}
list.push(command.to_draw_command(scale)?);
let view = frame
.command_views
.get(index)
.copied()
.flatten()
.unwrap_or_default();
list.push(
command
.to_draw_command(scale * view.zoom)?
.translated(Point::new(view.x * scale, view.y * scale)),
);
if clip.is_some() {
list.push(DrawCommand::PopClip);
}
@@ -245,10 +271,18 @@ impl RenderDocument {
}
list.push(
RenderCommand::StrokeRoundedRect {
x: focus.x,
y: focus.y,
width: focus.width,
height: focus.height,
x: focus
.view
.unwrap_or_default()
.rect(Rect::new(focus.x, focus.y, focus.width, focus.height))
.x,
y: focus
.view
.unwrap_or_default()
.rect(Rect::new(focus.x, focus.y, focus.width, focus.height))
.y,
width: focus.width * focus.view.unwrap_or_default().zoom,
height: focus.height * focus.view.unwrap_or_default().zoom,
radius: 7.0,
stroke_width: 2.0,
color: "#4F6FDB".into(),
@@ -345,6 +379,10 @@ pub struct Frame {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub command_clips: Vec<Option<[f32; 4]>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub command_views: Vec<Option<crate::viewport::Transform>>,
#[serde(skip)]
pub command_mounts: Vec<u64>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub mounts: Vec<MountGeometry>,
#[serde(default)]
pub layout_version: u64,
@@ -366,6 +404,8 @@ impl Default for Frame {
background: default_background(),
commands: Vec::new(),
command_clips: Vec::new(),
command_views: Vec::new(),
command_mounts: Vec::new(),
mounts: Vec::new(),
layout_version: 0,
session: 0,
@@ -379,6 +419,12 @@ impl Default for Frame {
/// A position in a published frame. Identifiers are opaque data, never VM handles.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct MountGeometry {
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub canvas_viewport: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub viewport_clip: Option<[f32; 4]>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub view: Option<crate::viewport::Transform>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub clip: Option<[f32; 4]>,
#[serde(default, skip_serializing_if = "Option::is_none")]
@@ -397,7 +443,8 @@ pub struct MountGeometry {
impl MountGeometry {
pub fn contains(&self, point: Point) -> bool {
Rect::new(self.x, self.y, self.width, self.height).contains(point)
let local = self.view.map_or(point, |view| view.inverse(point));
Rect::new(self.x, self.y, self.width, self.height).contains(local)
&& self.clip.is_none_or(|c| {
c[2] > 0.0 && c[3] > 0.0 && Rect::new(c[0], c[1], c[2], c[3]).contains(point)
})
@@ -405,6 +452,15 @@ impl MountGeometry {
}
impl Frame {
pub fn canvas_at(&self, point: Point) -> Option<&MountGeometry> {
self.mounts.iter().rev().find(|mount| {
mount.canvas_viewport
&& mount.contains(point)
&& mount.viewport_clip.is_some_and(|clip| {
Rect::new(clip[0], clip[1], clip[2], clip[3]).contains(point)
})
})
}
pub fn hit_test(&self, point: Point) -> Option<&MountGeometry> {
if self.fault.is_some() {
return None;