feat: render programmable 3d scenes cameras and picking

This commit is contained in:
2026-11-10 23:19:26 +08:00
parent 040b779f5f
commit ad2c01bc29
33 changed files with 2463 additions and 42 deletions
+4 -2
View File
@@ -142,9 +142,11 @@ ScrollBlock.axis is a managed visual attribute: "both" (default), "vertical" or
ContainerBlock.mainAlign accepts "start" (default), "center", "end" or "space-between". Native arrangement uses allocated Bounds after padding/minimum gap, distributes positive remaining main-axis space and keeps child sizes unchanged. Overflow keeps start placement. Explicit source arrange placements remain authoritative; this is alignment, not flex sizing.
Finite box layout: Overlay(padding, gap, children...) stacks children (gap is unused); mainAlign aligns horizontally and crossAlign vertically. Row/Column crossAlign accepts "start", "center", "end" or "stretch". Block.alignSelf defaults to "auto" and can override crossAlign per occurrence. marginTop/Right/Bottom/Left participate in measurement, alignment and flex allocation, without collapsing. paddingTop/Right/Bottom/Left override a side of the container padding; "auto" restores its fallback. borderTop/Right/Bottom/Left reserve inner border space; borderColor controls native border paint. Source paint overrides remain authoritative. boxWidth/boxHeight accept "auto", a nonnegative border-box dimension, or "fill" (finite parent required). Main-axis fill shares remaining space with weight 1 unless flex is set; minWidth/minHeight/maxWidth/maxHeight bound each dimension. Stretch changes only auto cross-axis dimensions. overflow is "visible" or "clip"; clipping affects descendant paint and hits. ScrollBlock handles scrolling independently of its content layout. See examples/box-layout.endor and docs/scene-architecture.md. Independent 2D scenes are described below; Scene3D/Viewport3D are still pending. Page.mode remains a compatibility API.
Finite box layout: Overlay(padding, gap, children...) stacks children (gap is unused); mainAlign aligns horizontally and crossAlign vertically. Row/Column crossAlign accepts "start", "center", "end" or "stretch". Block.alignSelf defaults to "auto" and can override crossAlign per occurrence. marginTop/Right/Bottom/Left participate in measurement, alignment and flex allocation, without collapsing. paddingTop/Right/Bottom/Left override a side of the container padding; "auto" restores its fallback. borderTop/Right/Bottom/Left reserve inner border space; borderColor controls native border paint. Source paint overrides remain authoritative. boxWidth/boxHeight accept "auto", a nonnegative border-box dimension, or "fill" (finite parent required). Main-axis fill shares remaining space with weight 1 unless flex is set; minWidth/minHeight/maxWidth/maxHeight bound each dimension. Stretch changes only auto cross-axis dimensions. overflow is "visible" or "clip"; clipping affects descendant paint and hits. ScrollBlock handles scrolling independently of its content layout. See examples/box-layout.endor and docs/scene-architecture.md. Independent 2D scenes are described below; 3D support is described below. Page.mode remains a compatibility API.
Independent 2D scenes are now implemented: Scene2D(children...) stores world content at child x/y; CanvasViewport(width,height,scene) is a finite layout box with its own Camera2D(). Camera2D(x=0,y=0,zoom=1) fields are ordinary managed attributes; zoom is 0.01–100. Sharing a scene does not share its camera; assigning second.camera=first.camera does. Scene content may contain layout containers and nested finite viewports; Overlay siblings stay fixed. Mount.fitView() fits finite direct scene-child bounds using that occurrence's allocation. Native wheel/pinch/space or middle drag targets the innermost viewport; drag retains its initial target. viewport-pan/viewport-zoom carry the viewport Mount and current frame identities, with signed logical page coordinates. Pan delta is measured before the viewport projection; zoom delta is [factor,0] with an in-content pointer anchor. Worker validation rejects stale/invalid input before invoking the VM. Draw and hit transforms compose consistently while layout geometry and measurement caches remain in world coordinates. ScrollView is a nominal ScrollBlock subtype. The default page uses these APIs; old Page.mode/panX/panY/zoom remain for compatibility. Scene3D/Viewport3D are still pending. See examples/scene-viewports.endor.
Independent 2D scenes are now implemented: Scene2D(children...) stores world content at child x/y; CanvasViewport(width,height,scene) is a finite layout box with its own Camera2D(). Camera2D(x=0,y=0,zoom=1) fields are ordinary managed attributes; zoom is 0.01–100. Sharing a scene does not share its camera; assigning second.camera=first.camera does. Scene content may contain layout containers and nested finite viewports; Overlay siblings stay fixed. Mount.fitView() fits finite direct scene-child bounds using that occurrence's allocation. Native wheel/pinch/space or middle drag targets the innermost viewport; drag retains its initial target. viewport-pan/viewport-zoom carry the viewport Mount and current frame identities, with signed logical page coordinates. Pan delta is measured before the viewport projection; zoom delta is [factor,0] with an in-content pointer anchor. Worker validation rejects stale/invalid input before invoking the VM. Draw and hit transforms compose consistently while layout geometry and measurement caches remain in world coordinates. ScrollView is a nominal ScrollBlock subtype. The default page uses these APIs; old Page.mode/panX/panY/zoom remain for compatibility. 3D support is described below. See examples/scene-viewports.endor.
3D basics are now implemented: Scene3D/Group3D, Box3D, immutable Geometry3D made from Vec3/Triangle3D ObjectLists, Mesh3D, DirectionalLight, and finite Viewport3D. PerspectiveCamera and OrthographicCamera expose x/y/z, targetX/Y/Z, upX/Y/Z, near/far, fov or height; viewport aspect is derived separately. New viewports have independent cameras and OrbitController; explicit camera assignment shares a camera. A controller can be replaced with an Endor object defining update(camera,gesture), or disabled by assigning "none". Native scene-orbit/scene-pan/scene-zoom events carry viewport Mount/frame identities. Mesh/group translate and rotate use native arithmetic; camera.translate moves position and target. This avoids the fixed Aster version's unsupported numeric arithmetic expressions. Mount.pick3D(x,y) returns nullable RayHit3D with managed mesh and world intersection data; pointer click Event.hit carries the same result. Rendering uses frustum clipping and a depth buffer in a bounded cached surface. World-space interactive UI panels and complete nesting budgets remain pending. See examples/scene3d.endor and docs/scene-architecture.md.
Block.flex is a nonnegative weight (default 0). Native containers reserve fixed measured children, padding and gaps, then share remaining allocated main-axis space between positive weights with zero basis. Actual child allocation feeds remeasurement. It does not request parent expansion. Further controls are described below.
+91
View File
@@ -665,3 +665,94 @@ fn explicit_viewport_camera_crosses_http_and_rejects_bad_anchors_without_faultin
assert!(namespace.read().unwrap().document.frame.fault.is_none());
}
}
#[test]
fn scene3d_controllers_cross_http_and_can_be_replaced_by_endor_code() {
let server = Arc::new(Server::http("127.0.0.1:0").unwrap());
let namespace = Arc::new(RwLock::new(NamespaceState {
viewport: Some(crate::layout::Size {
width: 400.,
height: 300.,
}),
..NamespaceState::default()
}));
let api = start_server(server.clone(), namespace.clone(), Arc::new(|| {})).unwrap();
let client = TestServer {
address: server.server_addr().to_ip().unwrap(),
server,
api: Some(api),
};
let source = r##"
class PanOnly {
function update(self: PanOnly, camera: Object, gesture: CameraGesture): Unit { camera.translate(gesture.dx, gesture.dy, 0) }
}
view = Viewport3D(200,100,Scene3D(Box3D(2,2,2,"#CC4455")))
page = Page()
page.render(view)
"##;
let response = client.request("PUT", "/v1/namespaces/app.main/code", source);
assert_eq!(response.0, 200, "{}", response.2);
let gesture = |kind: &str, delta: [f32; 2], point: Option<[f32; 2]>| {
let current = namespace.read().unwrap();
let frame = &current.document.frame;
serde_json::to_string(&crate::events::Input {
window: 1,
session: frame.session,
layout_version: frame.layout_version,
mount: frame
.mounts
.iter()
.find(|mount| mount.scene3d_viewport)
.unwrap()
.id,
kind: kind.into(),
delta: Some(delta),
point,
press_point: None,
tab_drop: None,
screen_position: None,
})
.unwrap()
};
let event_path = "/v1/namespaces/app.main/events";
let zoom = gesture("scene-zoom", [2., 0.], Some([100., 50.]));
let response = client.request("POST", event_path, &zoom);
assert_eq!(response.0, 200, "{}", response.2);
assert_eq!(client.request("POST", event_path, &zoom).0, 409);
let value = |expression: &str| {
let response = client.request("POST", "/v1/namespaces/app.main/repl", expression);
assert_eq!(response.0, 200, "{}", response.2);
serde_json::from_str::<serde_json::Value>(&response.2).unwrap()["value"]
.as_str()
.unwrap()
.to_owned()
};
assert_eq!(value("view.camera.z"), "2.5");
let response = client.request("POST", event_path, &gesture("scene-orbit", [20., 0.], None));
assert_eq!(response.0, 200, "{}", response.2);
let x = value("view.camera.x").parse::<f32>().unwrap();
assert!((x - 2.5 * 0.1f32.sin()).abs() < 0.0001);
value("view.camera = OrthographicCamera()");
let response = client.request(
"POST",
event_path,
&gesture("scene-zoom", [2., 0.], Some([100., 50.])),
);
assert_eq!(response.0, 200, "{}", response.2);
assert_eq!(value("view.camera.height"), "2.5");
value("view.controller = PanOnly()");
let response = client.request("POST", event_path, &gesture("scene-pan", [7., 0.], None));
assert_eq!(response.0, 200, "{}", response.2);
assert_eq!(value("view.camera.x"), "7");
let response = client.request(
"POST",
event_path,
&gesture("scene-zoom", [2., 0.], Some([250., 50.])),
);
assert_eq!(response.0, 400);
assert!(namespace.read().unwrap().document.frame.fault.is_none());
value("view.controller = \"none\"");
let response = client.request("POST", event_path, &gesture("scene-pan", [7., 0.], None));
assert_eq!(response.0, 409);
assert!(namespace.read().unwrap().document.frame.fault.is_none());
}
+13 -4
View File
@@ -340,6 +340,9 @@ impl Worker {
| "page-zoom"
| "viewport-pan"
| "viewport-zoom"
| "scene-orbit"
| "scene-pan"
| "scene-zoom"
| "tab-activate"
| "tab-close"
| "tab-detach"
@@ -439,9 +442,15 @@ impl Worker {
} else {
!frame.mounts.iter().any(|mount| {
mount.id == input.mount
&& (mount.interactive
|| input.kind.starts_with("viewport-") && mount.canvas_viewport
|| input.kind == "scroll" && mount.scroll.is_some())
&& if input.kind.starts_with("viewport-") {
mount.canvas_viewport
} else if input.kind.starts_with("scene-") {
mount.scene3d_viewport && mount.camera_controls
} else if input.kind == "scroll" {
mount.scroll.is_some()
} else {
mount.interactive
}
})
}
|| self.repl.as_ref().is_none_or(|(context, runtime)| {
@@ -463,7 +472,7 @@ impl Worker {
"camera input is unavailable in this Page mode",
));
}
if input.kind == "viewport-zoom"
if matches!(input.kind.as_str(), "viewport-zoom" | "scene-zoom")
&& !frame.mounts.iter().any(|mount| {
mount.id == input.mount
&& input.point.is_some_and(|point| {
+219 -3
View File
@@ -4560,7 +4560,7 @@ fn nested_canvas_projects_pixels_and_hits_while_overlay_ui_stays_fixed() {
assert_eq!(button.clip, Some([40.0, 40.0, 160.0, 120.0]));
let point = crate::geometry::Point::new(55.0, 55.0);
assert_eq!(initial.frame.hit_test(point).unwrap().id, button.id);
let inner_id = initial.frame.canvas_at(point).unwrap().id;
let inner_id = initial.frame.camera_at(point).unwrap().id;
let input = crate::events::Input {
window: 1,
session: initial.frame.session,
@@ -4593,7 +4593,8 @@ fn nested_canvas_projects_pixels_and_hits_while_overlay_ui_stays_fixed() {
&mut pixels,
200,
200,
);
)
.unwrap();
assert_eq!(pixels[5 * 200 + 5], 0x0000FF);
assert_ne!(pixels[55 * 200 + 45], pixels[190 * 200 + 190]);
}
@@ -4623,7 +4624,8 @@ fn canvas_world_paint_is_zoomed_and_clipped_in_the_software_renderer() {
&mut pixels,
80,
80,
);
)
.unwrap();
assert_eq!(pixels[5 * 80 + 5], 0x0000FF);
assert_eq!(pixels[25 * 80 + 5], 0xFF0000);
assert_eq!(pixels[55 * 80 + 5], pixels[79 * 80 + 79]);
@@ -4708,6 +4710,220 @@ fn fit_view_uses_finite_scene_bounds_and_this_mounts_size() {
assert_eq!(screen, crate::geometry::Point::new(48.0, 48.0));
}
#[test]
fn real_scene3d_renders_depth_with_independent_perspective_and_orthographic_cameras() {
let (mut session, initial) = ReplSession::load(
r##"
back = Box3D(2, 2, 2, "#FF0000")
front = Box3D(1, 1, 1, "#0000FF")
front.z = 1.5
scene = Scene3D(front, back)
scene.ambient = 1
perspective = Viewport3D(120, 100, scene)
orthographic = Viewport3D(120, 100, scene)
orthographic.camera = OrthographicCamera()
orthographic.camera.height = 4
page = Page()
page.render(Row(0, 10, perspective, orthographic))
"##,
"app.main",
309,
1,
)
.unwrap();
initial.validate().unwrap();
let commands: Vec<_> = initial
.frame
.commands
.iter()
.filter_map(|command| {
if let RenderCommand::Scene3D { view, .. } = command {
Some(view)
} else {
None
}
})
.collect();
assert_eq!(commands.len(), 2);
let hit = commands[0].pick(0.5, 0.5, 1.2).unwrap();
assert!((hit.distance - 3.0).abs() < 1e-5);
assert_eq!(hit.point, crate::scene3d::math::Vec3::new(0., 0., 2.));
assert!(matches!(
commands[1].camera.projection,
crate::scene3d::math::Projection::Orthographic { height: 4.0 }
));
let mut pixels = vec![0; 250 * 100];
crate::software_renderer::SoftwareRenderer::render(
&initial.display_list(1.).unwrap(),
&mut pixels,
250,
100,
)
.unwrap();
assert_eq!(pixels[50 * 250 + 60], 0x0000FF);
assert_eq!(pixels[50 * 250 + 190], 0x0000FF);
let updated = session
.execute("front.x=10\nperspective.camera.z=8", "app.main")
.unwrap()
.document
.unwrap();
updated.validate().unwrap();
let cameras: Vec<_> = updated
.frame
.commands
.iter()
.filter_map(|command| {
if let RenderCommand::Scene3D { view, .. } = command {
Some(view.camera.position.z)
} else {
None
}
})
.collect();
assert_eq!(cameras, vec![8., 5.]);
crate::software_renderer::SoftwareRenderer::render(
&updated.display_list(1.).unwrap(),
&mut pixels,
250,
100,
)
.unwrap();
assert_eq!(pixels[50 * 250 + 60], 0xFF0000);
assert_eq!(pixels[50 * 250 + 190], 0xFF0000);
}
#[test]
fn ray_pick_returns_managed_mesh_and_native_pointer_event_updates_its_material() {
let (mut session, initial) = ReplSession::load(
r##"
class Actions {
function select(self: Actions, event: Event): Unit { event.hit.mesh.color = "#00FF00" }
}
mesh = Box3D(2,2,2,"#FF0000")
scene = Scene3D(mesh)
scene.ambient = 1
view = Viewport3D(100,100,scene)
actions = Actions()
view.onClick(actions.select)
page = Page()
page.render(view)
"##,
"app.main",
310,
1,
)
.unwrap();
initial.validate().unwrap();
assert_eq!(
session
.execute("view.mounts().at(0).pick3D(50,50).point.z", "app.main")
.unwrap()
.value,
"1"
);
assert_eq!(
session
.execute("view.mounts().at(0).pick3D(50,50).mesh.color", "app.main")
.unwrap()
.value,
"\"#FF0000\""
);
let frame = session
.execute("view.invalidate(\"paint\")", "app.main")
.unwrap()
.document
.unwrap()
.frame;
let mount = frame
.mounts
.iter()
.find(|mount| mount.scene3d_viewport)
.unwrap();
let updated = session
.dispatch(&crate::events::Input {
window: 1,
session: frame.session,
layout_version: frame.layout_version,
mount: mount.id,
kind: "click".into(),
point: Some([50., 50.]),
press_point: Some([50., 50.]),
delta: None,
tab_drop: None,
screen_position: None,
})
.unwrap()
.document
.unwrap();
assert_eq!(
session.execute("mesh.color", "app.main").unwrap().value,
"\"#00FF00\""
);
let mut pixels = vec![0; 100 * 100];
crate::software_renderer::SoftwareRenderer::render(
&updated.display_list(1.).unwrap(),
&mut pixels,
100,
100,
)
.unwrap();
assert_eq!(pixels[50 * 100 + 50], 0x00FF00);
}
#[test]
fn scene3d_example_custom_geometry_and_cycle_diagnostics_use_real_source() {
let (mut session, document) = ReplSession::load(
include_str!("../../../examples/scene3d.endor"),
"app.main",
311,
1,
)
.unwrap();
document.validate().unwrap();
session.execute("actions.rotate()", "app.main").unwrap();
assert_eq!(
session.execute("cube.rotationY", "app.main").unwrap().value,
"40"
);
session.execute(r##"
geometry=Geometry3D(ObjectList(Vec3(-1,-1,0),Vec3(1,-1,0),Vec3(0,1,0)),ObjectList(Triangle3D(0,1,2)))
custom=Mesh3D(geometry,"#112233")
scene.children=ObjectList(custom)
"##,"app.main").unwrap().document.unwrap().validate().unwrap();
let error = ReplSession::load(
r##"
group=Group3D()
scene=Scene3D(group)
group.children=ObjectList(group)
page=Page()
page.render(Viewport3D(100,100,scene))
"##,
"app.main",
312,
1,
)
.err()
.unwrap();
assert!(
format!("{error:#}").contains("cyclic 3D scene graph"),
"{error:#}"
);
let error = ReplSession::load(
r##"
geometry=Geometry3D(ObjectList(Vec3(0,0,0)),ObjectList(Triangle3D(0,1,2)))
"##,
"app.main",
313,
1,
)
.err()
.unwrap();
assert!(
format!("{error:#}").contains("invalid Geometry3D buffers"),
"{error:#}"
);
}
#[test]
fn scroll_axes_wrap_shared_text_and_clamp_only_the_enabled_offsets() {
let (mut session, initial) = ReplSession::load(
+1 -1
View File
@@ -223,7 +223,7 @@ pub fn new_event(
let event = cx.allocate(Event::default())?;
cx.attach_type(event, definition)?;
cx.set_attr(event, "kind", Value::Text(kind.into()))?;
for name in ["target", "mount", "currentTarget", "currentMount"] {
for name in ["target", "mount", "currentTarget", "currentMount", "hit"] {
cx.set_attr(event, name, Value::Nil)?;
}
cx.set_attr(event, "phase", Value::Text("none".into()))?;
+8 -2
View File
@@ -75,9 +75,15 @@ impl Input {
}
if matches!(
self.kind.as_str(),
"page-pan" | "page-zoom" | "viewport-pan" | "viewport-zoom"
"page-pan"
| "page-zoom"
| "viewport-pan"
| "viewport-zoom"
| "scene-orbit"
| "scene-pan"
| "scene-zoom"
) {
let viewport = self.kind.starts_with("viewport-");
let viewport = self.kind.starts_with("viewport-") || self.kind.starts_with("scene-");
let valid_delta = self.delta.is_some_and(|delta| {
delta
.iter()