feat: render programmable 3d scenes cameras and picking
This commit is contained in:
+68
-1
@@ -34,6 +34,10 @@ pub enum Kind {
|
||||
Column,
|
||||
Overlay,
|
||||
Scene2D,
|
||||
Viewport3D {
|
||||
width: f32,
|
||||
height: f32,
|
||||
},
|
||||
CanvasViewport {
|
||||
width: f32,
|
||||
height: f32,
|
||||
@@ -135,6 +139,8 @@ pub struct Data {
|
||||
pub border_color: String,
|
||||
pub clip_children: bool,
|
||||
pub camera: Option<ObjectRef>,
|
||||
pub scene3d: Option<ObjectRef>,
|
||||
pub controller: Option<ObjectRef>,
|
||||
pub scroll_axis: crate::scroll::Axis,
|
||||
pub position: [f32; 2],
|
||||
pub surface: crate::page::Surface,
|
||||
@@ -175,6 +181,8 @@ impl Data {
|
||||
border_color: "#D8DCE2".into(),
|
||||
clip_children: false,
|
||||
camera: None,
|
||||
scene3d: None,
|
||||
controller: None,
|
||||
scroll_axis: crate::scroll::Axis::Both,
|
||||
position: [0.0, 0.0],
|
||||
surface: crate::page::Surface::new(crate::page::Mode::Window),
|
||||
@@ -214,6 +222,12 @@ impl ObjectPayload for Data {
|
||||
"tessera-block-data"
|
||||
}
|
||||
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
if let Some(scene) = self.scene3d {
|
||||
visit(scene);
|
||||
}
|
||||
if let Some(controller) = self.controller {
|
||||
visit(controller);
|
||||
}
|
||||
if let Some(camera) = self.camera {
|
||||
visit(camera);
|
||||
}
|
||||
@@ -415,6 +429,17 @@ pub fn native_properties(data: &Data) -> Vec<(&'static str, Value)> {
|
||||
));
|
||||
}
|
||||
match &data.kind {
|
||||
Kind::Viewport3D { width, height } => fields.extend([
|
||||
("width", number(*width)),
|
||||
("height", number(*height)),
|
||||
("scene", data.scene3d.map_or(Value::Nil, Value::Object)),
|
||||
(
|
||||
"controller",
|
||||
data.controller
|
||||
.map_or_else(|| Value::Text("none".into()), Value::Object),
|
||||
),
|
||||
("camera", data.camera.map_or(Value::Nil, Value::Object)),
|
||||
]),
|
||||
Kind::CanvasViewport { width, height } => fields.extend([
|
||||
("width", number(*width)),
|
||||
("height", number(*height)),
|
||||
@@ -496,6 +521,26 @@ pub fn visual_data(store: &ObjectStore, data: &Data) -> Result<Data, VmError> {
|
||||
));
|
||||
}
|
||||
}
|
||||
if matches!(data.kind, Kind::Viewport3D { .. })
|
||||
&& let Some(controller) = data.controller
|
||||
{
|
||||
let definition = store.type_of(controller)?.ok_or(VmError::InvalidArgument(
|
||||
"3D controller requires a nominal update method",
|
||||
))?;
|
||||
let method = store
|
||||
.lookup_method(definition, "update")?
|
||||
.ok_or(VmError::InvalidArgument(
|
||||
"3D controller must define update(camera, gesture)",
|
||||
))?;
|
||||
if store
|
||||
.call_arity(method)?
|
||||
.is_none_or(|arity| !arity.accepts(3))
|
||||
{
|
||||
return Err(VmError::InvalidArgument(
|
||||
"3D controller update expects camera and CameraGesture",
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
@@ -536,13 +581,33 @@ pub fn apply_visual_property(data: &mut Data, name: &str, value: &Value) -> Resu
|
||||
return Ok(());
|
||||
}
|
||||
match name {
|
||||
"camera" if matches!(data.kind, Kind::CanvasViewport { .. }) => {
|
||||
"controller" if matches!(data.kind, Kind::Viewport3D { .. }) => {
|
||||
data.controller = match value {
|
||||
Value::Object(controller) => Some(*controller),
|
||||
Value::Text(text) if text == "none" => None,
|
||||
_ => return Err(bad()),
|
||||
};
|
||||
return Ok(());
|
||||
}
|
||||
"camera"
|
||||
if matches!(
|
||||
data.kind,
|
||||
Kind::CanvasViewport { .. } | Kind::Viewport3D { .. }
|
||||
) =>
|
||||
{
|
||||
let Value::Object(camera) = value else {
|
||||
return Err(bad());
|
||||
};
|
||||
data.camera = Some(*camera);
|
||||
return Ok(());
|
||||
}
|
||||
"scene" if matches!(data.kind, Kind::Viewport3D { .. }) => {
|
||||
let Value::Object(scene) = value else {
|
||||
return Err(bad());
|
||||
};
|
||||
data.scene3d = Some(*scene);
|
||||
return Ok(());
|
||||
}
|
||||
"paddingTop" | "paddingRight" | "paddingBottom" | "paddingLeft" => {
|
||||
let index = match name {
|
||||
"paddingTop" => 0,
|
||||
@@ -688,6 +753,8 @@ pub fn apply_visual_property(data: &mut Data, name: &str, value: &Value) -> Resu
|
||||
return Ok(());
|
||||
}
|
||||
match (&mut data.kind, name) {
|
||||
(Kind::Viewport3D { width, .. }, "width") => *width = number()?,
|
||||
(Kind::Viewport3D { height, .. }, "height") => *height = number()?,
|
||||
(Kind::CanvasViewport { width, .. }, "width") => *width = number()?,
|
||||
(Kind::CanvasViewport { height, .. }, "height") => *height = number()?,
|
||||
(Kind::Text { value: text, .. }, "text") => {
|
||||
|
||||
+35
-2
@@ -421,6 +421,10 @@ impl Default for Frame {
|
||||
pub struct MountGeometry {
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
pub canvas_viewport: bool,
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
pub scene3d_viewport: bool,
|
||||
#[serde(default)]
|
||||
pub camera_controls: bool,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub viewport_clip: Option<[f32; 4]>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
@@ -452,9 +456,9 @@ impl MountGeometry {
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
pub fn canvas_at(&self, point: Point) -> Option<&MountGeometry> {
|
||||
pub fn camera_at(&self, point: Point) -> Option<&MountGeometry> {
|
||||
self.mounts.iter().rev().find(|mount| {
|
||||
mount.canvas_viewport
|
||||
(mount.canvas_viewport || mount.scene3d_viewport && mount.camera_controls)
|
||||
&& mount.contains(point)
|
||||
&& mount.viewport_clip.is_some_and(|clip| {
|
||||
Rect::new(clip[0], clip[1], clip[2], clip[3]).contains(point)
|
||||
@@ -483,6 +487,13 @@ fn default_background() -> String {
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum RenderCommand {
|
||||
Scene3D {
|
||||
x: f32,
|
||||
y: f32,
|
||||
width: f32,
|
||||
height: f32,
|
||||
view: Arc<crate::scene3d::View>,
|
||||
},
|
||||
RoundedRect {
|
||||
x: f32,
|
||||
y: f32,
|
||||
@@ -518,6 +529,18 @@ pub enum RenderCommand {
|
||||
impl RenderCommand {
|
||||
pub(crate) fn validate(&self) -> Result<()> {
|
||||
match self {
|
||||
Self::Scene3D {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
view,
|
||||
} => {
|
||||
validate_rect(*x, *y, *width, *height, 0.0)?;
|
||||
if !view.valid() {
|
||||
bail!("invalid 3D view");
|
||||
}
|
||||
}
|
||||
Self::RoundedRect {
|
||||
x,
|
||||
y,
|
||||
@@ -578,6 +601,16 @@ impl RenderCommand {
|
||||
|
||||
fn to_draw_command(&self, scale: f32) -> Result<DrawCommand> {
|
||||
Ok(match self {
|
||||
Self::Scene3D {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
view,
|
||||
} => DrawCommand::Scene3D {
|
||||
rect: scaled_rect(*x, *y, *width, *height, scale),
|
||||
view: view.clone(),
|
||||
},
|
||||
Self::RoundedRect {
|
||||
x,
|
||||
y,
|
||||
|
||||
@@ -6,5 +6,6 @@ pub(crate) mod mount;
|
||||
pub(crate) mod object_list;
|
||||
pub(crate) mod presentation;
|
||||
pub(crate) mod protocols;
|
||||
pub(crate) mod scene3d;
|
||||
pub(crate) mod snapshot;
|
||||
pub(crate) mod window;
|
||||
|
||||
+24
-3
@@ -56,6 +56,7 @@ enum Query {
|
||||
ScrollPosition,
|
||||
ScrollIntoView,
|
||||
FitView,
|
||||
Pick3D,
|
||||
Parent,
|
||||
FirstChild,
|
||||
NextSibling,
|
||||
@@ -80,7 +81,7 @@ impl NativeCallable for Method {
|
||||
Some(endor_core::CallArity::exact(
|
||||
if matches!(
|
||||
self.query,
|
||||
Query::HitTest | Query::ScrollTo | Query::ScrollBy
|
||||
Query::HitTest | Query::Pick3D | Query::ScrollTo | Query::ScrollBy
|
||||
) {
|
||||
2
|
||||
} else {
|
||||
@@ -101,6 +102,7 @@ impl NativeCallable for Method {
|
||||
| Query::ScrollBy
|
||||
| Query::ScrollIntoView
|
||||
| Query::FitView
|
||||
| Query::Pick3D
|
||||
| Query::ScrollPosition
|
||||
)
|
||||
{
|
||||
@@ -108,7 +110,7 @@ impl NativeCallable for Method {
|
||||
}
|
||||
let arity = if matches!(
|
||||
self.query,
|
||||
Query::HitTest | Query::ScrollTo | Query::ScrollBy
|
||||
Query::HitTest | Query::Pick3D | Query::ScrollTo | Query::ScrollBy
|
||||
) {
|
||||
2
|
||||
} else {
|
||||
@@ -139,6 +141,20 @@ impl NativeCallable for Method {
|
||||
crate::viewport::fit(cx, &mount)?;
|
||||
Value::Nil
|
||||
}
|
||||
Query::Pick3D => {
|
||||
let desktop = cx
|
||||
.upgrade(self.desktop)?
|
||||
.ok_or(VmError::InvalidArgument("viewport desktop expired"))?;
|
||||
crate::scene3d_model::pick(
|
||||
cx,
|
||||
desktop,
|
||||
&mount,
|
||||
crate::geometry::Point::new(
|
||||
crate::scroll::coordinate(&args[0])?,
|
||||
crate::scroll::coordinate(&args[1])?,
|
||||
),
|
||||
)?
|
||||
}
|
||||
Query::ScrollPosition => {
|
||||
let p = mount
|
||||
.geometry
|
||||
@@ -264,6 +280,7 @@ fn mount_signature(
|
||||
}
|
||||
Query::Page => CallType::nullable(nominal("Page")?),
|
||||
Query::HitTest => T::Bool.into(),
|
||||
Query::Pick3D => CallType::nullable(nominal("RayHit3D")?),
|
||||
Query::ScrollPosition => T::Any.into(),
|
||||
_ => T::Unit.into(),
|
||||
};
|
||||
@@ -275,7 +292,10 @@ fn mount_signature(
|
||||
} else {
|
||||
CallEffect::ReadOnly
|
||||
};
|
||||
let parameters = if matches!(query, Query::HitTest | Query::ScrollTo | Query::ScrollBy) {
|
||||
let parameters = if matches!(
|
||||
query,
|
||||
Query::HitTest | Query::Pick3D | Query::ScrollTo | Query::ScrollBy
|
||||
) {
|
||||
vec![T::Number, T::Number]
|
||||
} else {
|
||||
vec![]
|
||||
@@ -412,6 +432,7 @@ impl NativeCallable for Sync {
|
||||
("scrollPosition", Query::ScrollPosition),
|
||||
("scrollIntoView", Query::ScrollIntoView),
|
||||
("fitView", Query::FitView),
|
||||
("pick3D", Query::Pick3D),
|
||||
("parent", Query::Parent),
|
||||
("firstChild", Query::FirstChild),
|
||||
("nextSibling", Query::NextSibling),
|
||||
|
||||
@@ -0,0 +1,761 @@
|
||||
//! Traced 3D objects and checked immutable snapshots. No managed handle enters
|
||||
//! the renderer; cameras and node transforms remain editable Endor attributes.
|
||||
use crate::{
|
||||
object_list,
|
||||
scene3d::{
|
||||
self,
|
||||
math::{Camera, Matrix, Projection, Vec3},
|
||||
},
|
||||
};
|
||||
use endor_core::{NativeContext, ObjectPayload, ObjectRef, ObjectStore, Value, VmError};
|
||||
use std::{collections::HashSet, sync::Arc};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Constructor {
|
||||
Vec3,
|
||||
Triangle,
|
||||
Geometry,
|
||||
Mesh,
|
||||
Box,
|
||||
Group,
|
||||
Scene,
|
||||
Light,
|
||||
Perspective,
|
||||
Orthographic,
|
||||
Orbit,
|
||||
}
|
||||
impl Constructor {
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
Self::Vec3 => "Vec3",
|
||||
Self::Triangle => "Triangle3D",
|
||||
Self::Geometry => "Geometry3D",
|
||||
Self::Mesh => "Mesh3D",
|
||||
Self::Box => "Box3D",
|
||||
Self::Group => "Group3D",
|
||||
Self::Scene => "Scene3D",
|
||||
Self::Light => "DirectionalLight",
|
||||
Self::Perspective => "PerspectiveCamera",
|
||||
Self::Orthographic => "OrthographicCamera",
|
||||
Self::Orbit => "OrbitController",
|
||||
}
|
||||
}
|
||||
pub fn function(self) -> &'static str {
|
||||
match self {
|
||||
Self::Vec3 => "vec3",
|
||||
Self::Triangle => "triangle3D",
|
||||
Self::Geometry => "geometry3D",
|
||||
Self::Mesh => "mesh3D",
|
||||
Self::Box => "box3D",
|
||||
Self::Group => "group3D",
|
||||
Self::Scene => "scene3D",
|
||||
Self::Light => "directionalLight",
|
||||
Self::Perspective => "perspectiveCamera",
|
||||
Self::Orthographic => "orthographicCamera",
|
||||
Self::Orbit => "orbitController",
|
||||
}
|
||||
}
|
||||
pub fn from_function(name: &str) -> Option<Self> {
|
||||
[
|
||||
Self::Vec3,
|
||||
Self::Triangle,
|
||||
Self::Geometry,
|
||||
Self::Mesh,
|
||||
Self::Box,
|
||||
Self::Group,
|
||||
Self::Scene,
|
||||
Self::Light,
|
||||
Self::Perspective,
|
||||
Self::Orthographic,
|
||||
Self::Orbit,
|
||||
]
|
||||
.into_iter()
|
||||
.find(|kind| kind.function() == name)
|
||||
}
|
||||
pub fn arity(self) -> (usize, usize) {
|
||||
match self {
|
||||
Self::Vec3 | Self::Triangle => (3, 3),
|
||||
Self::Geometry | Self::Mesh => (2, 2),
|
||||
Self::Box => (4, 4),
|
||||
Self::Group | Self::Scene => (0, 4096),
|
||||
Self::Light => (5, 5),
|
||||
_ => (0, 0),
|
||||
}
|
||||
}
|
||||
pub fn parameters(self) -> &'static [&'static str] {
|
||||
match self {
|
||||
Self::Vec3 | Self::Triangle => &["Number", "Number", "Number"],
|
||||
Self::Geometry => &["ObjectList", "ObjectList"],
|
||||
Self::Mesh => &["Geometry3D", "String"],
|
||||
Self::Box => &["Number", "Number", "Number", "String"],
|
||||
Self::Light => &["Number", "Number", "Number", "String", "Number"],
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
pub fn variadic(self) -> bool {
|
||||
matches!(self, Self::Group | Self::Scene)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum NodeKind {
|
||||
Vector,
|
||||
Triangle,
|
||||
Mesh,
|
||||
Group,
|
||||
Scene,
|
||||
Light,
|
||||
Perspective,
|
||||
Orthographic,
|
||||
Orbit,
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub struct Node(pub NodeKind);
|
||||
impl ObjectPayload for Node {
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"scene-3d-object"
|
||||
}
|
||||
fn reference_assignment_only(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn allows_attributes(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub struct Geometry(pub Arc<scene3d::Geometry>);
|
||||
impl ObjectPayload for Geometry {
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"geometry-3d"
|
||||
}
|
||||
fn reference_assignment_only(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn allows_attributes(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
fn invalid(message: &'static str) -> VmError {
|
||||
VmError::InvalidArgument(message)
|
||||
}
|
||||
fn object_ref(value: &Value) -> Result<ObjectRef, VmError> {
|
||||
if let Value::Object(object) = value {
|
||||
Ok(*object)
|
||||
} else {
|
||||
Err(invalid("3D field requires an object"))
|
||||
}
|
||||
}
|
||||
fn number(value: &Value) -> Result<f32, VmError> {
|
||||
crate::scroll::coordinate(value)
|
||||
}
|
||||
fn text(value: &Value) -> Result<String, VmError> {
|
||||
if let Value::Text(text) = value {
|
||||
Ok(text.clone())
|
||||
} else {
|
||||
Err(invalid("3D field requires text"))
|
||||
}
|
||||
}
|
||||
fn set_number(
|
||||
cx: &mut NativeContext<'_>,
|
||||
object: ObjectRef,
|
||||
name: &str,
|
||||
value: f32,
|
||||
) -> Result<(), VmError> {
|
||||
cx.set_attr(object, name, crate::native_visual::value(value))
|
||||
}
|
||||
fn color(value: &str) -> Result<[f32; 3], VmError> {
|
||||
let color = crate::document::parse_color(value).map_err(|_| invalid("invalid 3D color"))?;
|
||||
if color.alpha != 255 {
|
||||
return Err(invalid("3D opaque material color requires full alpha"));
|
||||
}
|
||||
Ok([
|
||||
color.red as f32 / 255.,
|
||||
color.green as f32 / 255.,
|
||||
color.blue as f32 / 255.,
|
||||
])
|
||||
}
|
||||
|
||||
pub fn allocate(
|
||||
cx: &mut NativeContext<'_>,
|
||||
desktop: ObjectRef,
|
||||
definition: ObjectRef,
|
||||
constructor: Constructor,
|
||||
args: &[Value],
|
||||
) -> Result<ObjectRef, VmError> {
|
||||
let kind = match constructor {
|
||||
Constructor::Vec3 => NodeKind::Vector,
|
||||
Constructor::Triangle => NodeKind::Triangle,
|
||||
Constructor::Mesh | Constructor::Box => NodeKind::Mesh,
|
||||
Constructor::Group => NodeKind::Group,
|
||||
Constructor::Scene => NodeKind::Scene,
|
||||
Constructor::Light => NodeKind::Light,
|
||||
Constructor::Perspective => NodeKind::Perspective,
|
||||
Constructor::Orthographic => NodeKind::Orthographic,
|
||||
Constructor::Orbit => NodeKind::Orbit,
|
||||
Constructor::Geometry => {
|
||||
let vertices = object_list::items(cx.store(), object_ref(&args[0])?)?
|
||||
.iter()
|
||||
.map(|&item| {
|
||||
let p = cx.store();
|
||||
if p.payload::<Node>(item, "Vec3")?.0 != NodeKind::Vector {
|
||||
return Err(invalid("geometry vertices require Vec3"));
|
||||
}
|
||||
Ok(Vec3::new(
|
||||
number(&p.get_attr(item, "x")?)?,
|
||||
number(&p.get_attr(item, "y")?)?,
|
||||
number(&p.get_attr(item, "z")?)?,
|
||||
))
|
||||
})
|
||||
.collect::<Result<Vec<_>, VmError>>()?;
|
||||
let triangles = object_list::items(cx.store(), object_ref(&args[1])?)?
|
||||
.iter()
|
||||
.map(|&item| {
|
||||
if cx.store().payload::<Node>(item, "Triangle3D")?.0 != NodeKind::Triangle {
|
||||
return Err(invalid("geometry indices require Triangle3D"));
|
||||
}
|
||||
let mut triangle = [0; 3];
|
||||
for (index, name) in ["a", "b", "c"].into_iter().enumerate() {
|
||||
let value = number(&cx.store().get_attr(item, name)?)?;
|
||||
if value < 0. || value.fract() != 0. {
|
||||
return Err(invalid("triangle indices must be nonnegative integers"));
|
||||
}
|
||||
triangle[index] = value as u32;
|
||||
}
|
||||
Ok(triangle)
|
||||
})
|
||||
.collect::<Result<Vec<_>, VmError>>()?;
|
||||
let geometry = scene3d::Geometry {
|
||||
vertices,
|
||||
triangles,
|
||||
};
|
||||
if !geometry.valid() {
|
||||
return Err(invalid(
|
||||
"invalid Geometry3D buffers or geometry budget exceeded",
|
||||
));
|
||||
}
|
||||
let object = cx.allocate(Geometry(Arc::new(geometry)))?;
|
||||
cx.attach_type(object, definition)?;
|
||||
return Ok(object);
|
||||
}
|
||||
};
|
||||
let instance = cx.allocate(Node(kind))?;
|
||||
cx.attach_type(instance, definition)?;
|
||||
match kind {
|
||||
NodeKind::Vector | NodeKind::Triangle => {
|
||||
for (name, value) in (if kind == NodeKind::Vector {
|
||||
["x", "y", "z"]
|
||||
} else {
|
||||
["a", "b", "c"]
|
||||
})
|
||||
.into_iter()
|
||||
.zip(args)
|
||||
{
|
||||
set_number(cx, instance, name, number(value)?)?;
|
||||
}
|
||||
}
|
||||
NodeKind::Mesh => {
|
||||
let (geometry, material) = if matches!(constructor, Constructor::Box) {
|
||||
let (w, h, d) = (number(&args[0])?, number(&args[1])?, number(&args[2])?);
|
||||
if w < 0. || h < 0. || d < 0. {
|
||||
return Err(invalid("Box3D dimensions must be nonnegative"));
|
||||
}
|
||||
let geometry =
|
||||
cx.allocate(Geometry(Arc::new(scene3d::Geometry::cuboid(w, h, d))))?;
|
||||
crate::ui_bridge::attach_desktop_type(cx, geometry, "Geometry3D", desktop)?;
|
||||
(geometry, text(&args[3])?)
|
||||
} else {
|
||||
(object_ref(&args[0])?, text(&args[1])?)
|
||||
};
|
||||
cx.store().payload::<Geometry>(geometry, "Geometry3D")?;
|
||||
color(&material)?;
|
||||
cx.set_attr(instance, "geometry", Value::Object(geometry))?;
|
||||
cx.set_attr(instance, "color", Value::Text(material))?;
|
||||
}
|
||||
NodeKind::Group | NodeKind::Scene => {
|
||||
let children = args.iter().map(object_ref).collect::<Result<Vec<_>, _>>()?;
|
||||
for child in &children {
|
||||
if !matches!(
|
||||
cx.store().payload::<Node>(*child, "3D node")?.0,
|
||||
NodeKind::Mesh | NodeKind::Group | NodeKind::Light
|
||||
) {
|
||||
return Err(invalid(
|
||||
"Scene3D/Group3D children require meshes, groups or lights",
|
||||
));
|
||||
}
|
||||
}
|
||||
let children = object_list::collection(cx, children, desktop)?;
|
||||
cx.set_attr(instance, "children", Value::Object(children))?;
|
||||
if kind == NodeKind::Scene {
|
||||
set_number(cx, instance, "ambient", 0.25)?;
|
||||
cx.set_attr(instance, "background", Value::Text("#F1F3F6".into()))?;
|
||||
}
|
||||
}
|
||||
NodeKind::Light => {
|
||||
for (name, value) in ["directionX", "directionY", "directionZ"]
|
||||
.into_iter()
|
||||
.zip(args)
|
||||
{
|
||||
set_number(cx, instance, name, number(value)?)?;
|
||||
}
|
||||
color(&text(&args[3])?)?;
|
||||
cx.set_attr(instance, "color", args[3].clone())?;
|
||||
set_number(cx, instance, "intensity", number(&args[4])?)?;
|
||||
}
|
||||
NodeKind::Perspective | NodeKind::Orthographic => {
|
||||
for (name, value) in [
|
||||
("x", 0.),
|
||||
("y", 0.),
|
||||
("z", 5.),
|
||||
("targetX", 0.),
|
||||
("targetY", 0.),
|
||||
("targetZ", 0.),
|
||||
("upX", 0.),
|
||||
("upY", 1.),
|
||||
("upZ", 0.),
|
||||
("near", 0.1),
|
||||
("far", 1000.),
|
||||
("fov", 50.),
|
||||
("height", 5.),
|
||||
] {
|
||||
set_number(cx, instance, name, value)?;
|
||||
}
|
||||
}
|
||||
NodeKind::Orbit => {
|
||||
set_number(cx, instance, "rotationSpeed", 0.005)?;
|
||||
set_number(cx, instance, "panSpeed", 1.)?;
|
||||
}
|
||||
}
|
||||
if matches!(kind, NodeKind::Mesh | NodeKind::Group) {
|
||||
for (name, value) in [
|
||||
("x", 0.),
|
||||
("y", 0.),
|
||||
("z", 0.),
|
||||
("rotationX", 0.),
|
||||
("rotationY", 0.),
|
||||
("rotationZ", 0.),
|
||||
("scaleX", 1.),
|
||||
("scaleY", 1.),
|
||||
("scaleZ", 1.),
|
||||
] {
|
||||
set_number(cx, instance, name, value)?;
|
||||
}
|
||||
}
|
||||
Ok(instance)
|
||||
}
|
||||
|
||||
struct Reader<'a> {
|
||||
store: &'a ObjectStore,
|
||||
reads: HashSet<(ObjectRef, String)>,
|
||||
}
|
||||
impl Reader<'_> {
|
||||
fn value(&mut self, object: ObjectRef, name: &str) -> Result<Value, VmError> {
|
||||
self.reads.insert((object, name.into()));
|
||||
self.store.get_attr(object, name)
|
||||
}
|
||||
fn number(&mut self, object: ObjectRef, name: &str) -> Result<f32, VmError> {
|
||||
number(&self.value(object, name)?)
|
||||
}
|
||||
fn vector(&mut self, object: ObjectRef, names: [&str; 3]) -> Result<Vec3, VmError> {
|
||||
Ok(Vec3::new(
|
||||
self.number(object, names[0])?,
|
||||
self.number(object, names[1])?,
|
||||
self.number(object, names[2])?,
|
||||
))
|
||||
}
|
||||
fn camera(&mut self, object: ObjectRef) -> Result<Camera, VmError> {
|
||||
let projection = match self.store.payload::<Node>(object, "3D camera")?.0 {
|
||||
NodeKind::Perspective => Projection::Perspective {
|
||||
fov: self.number(object, "fov")?,
|
||||
},
|
||||
NodeKind::Orthographic => Projection::Orthographic {
|
||||
height: self.number(object, "height")?,
|
||||
},
|
||||
_ => {
|
||||
return Err(invalid(
|
||||
"Viewport3D requires a PerspectiveCamera or OrthographicCamera",
|
||||
));
|
||||
}
|
||||
};
|
||||
let camera = Camera {
|
||||
position: self.vector(object, ["x", "y", "z"])?,
|
||||
target: self.vector(object, ["targetX", "targetY", "targetZ"])?,
|
||||
up: self.vector(object, ["upX", "upY", "upZ"])?,
|
||||
near: self.number(object, "near")?,
|
||||
far: self.number(object, "far")?,
|
||||
projection,
|
||||
};
|
||||
if !camera.valid() {
|
||||
return Err(invalid("invalid 3D camera basis or projection range"));
|
||||
}
|
||||
Ok(camera)
|
||||
}
|
||||
}
|
||||
|
||||
pub type Snapshot = (Arc<scene3d::View>, Vec<(ObjectRef, String)>);
|
||||
pub fn snapshot(
|
||||
store: &ObjectStore,
|
||||
scene: ObjectRef,
|
||||
camera: ObjectRef,
|
||||
) -> Result<Snapshot, VmError> {
|
||||
if store.payload::<Node>(scene, "Scene3D")?.0 != NodeKind::Scene {
|
||||
return Err(invalid("Viewport3D requires a Scene3D"));
|
||||
}
|
||||
let mut reader = Reader {
|
||||
store,
|
||||
reads: HashSet::new(),
|
||||
};
|
||||
let camera = reader.camera(camera)?;
|
||||
let ambient = reader.number(scene, "ambient")?;
|
||||
if !(0.0..=1.0).contains(&ambient) {
|
||||
return Err(invalid("Scene3D.ambient expects 0 through 1"));
|
||||
}
|
||||
let background = color(&text(&reader.value(scene, "background")?)?)?;
|
||||
let mut output = scene3d::Scene {
|
||||
meshes: Vec::new(),
|
||||
lights: Vec::new(),
|
||||
ambient,
|
||||
background,
|
||||
};
|
||||
let mut work = vec![(scene, Matrix::default(), false)];
|
||||
let mut path = HashSet::new();
|
||||
let mut visits = 0;
|
||||
let mut triangles = 0;
|
||||
while let Some((object, parent, finish)) = work.pop() {
|
||||
if finish {
|
||||
path.remove(&object);
|
||||
continue;
|
||||
}
|
||||
visits += 1;
|
||||
if visits > 4096 {
|
||||
return Err(VmError::ExecutionLimit("3D scene occurrences"));
|
||||
}
|
||||
if !path.insert(object) {
|
||||
return Err(invalid("cyclic 3D scene graph"));
|
||||
}
|
||||
work.push((object, parent, true));
|
||||
let kind = store.payload::<Node>(object, "3D node")?.0;
|
||||
let world = if matches!(kind, NodeKind::Mesh | NodeKind::Group) {
|
||||
parent.compose(Matrix::trs(
|
||||
reader.vector(object, ["x", "y", "z"])?,
|
||||
reader.vector(object, ["rotationX", "rotationY", "rotationZ"])?,
|
||||
reader.vector(object, ["scaleX", "scaleY", "scaleZ"])?,
|
||||
))
|
||||
} else {
|
||||
parent
|
||||
};
|
||||
match kind {
|
||||
NodeKind::Scene | NodeKind::Group => {
|
||||
let list = object_ref(&reader.value(object, "children")?)?;
|
||||
for child in object_list::items(store, list)?.iter().rev() {
|
||||
work.push((*child, world, false));
|
||||
}
|
||||
}
|
||||
NodeKind::Mesh => {
|
||||
let geometry = object_ref(&reader.value(object, "geometry")?)?;
|
||||
let geometry = store.payload::<Geometry>(geometry, "Geometry3D")?.0.clone();
|
||||
triangles += geometry.triangles.len();
|
||||
if triangles > scene3d::MAX_TRIANGLES {
|
||||
return Err(VmError::ExecutionLimit("3D scene triangles"));
|
||||
}
|
||||
let material = color(&text(&reader.value(object, "color")?)?)?;
|
||||
output.meshes.push(scene3d::Mesh {
|
||||
identity: object.to_string(),
|
||||
geometry,
|
||||
world,
|
||||
color: material,
|
||||
});
|
||||
}
|
||||
NodeKind::Light => {
|
||||
if output.lights.len() >= 32 {
|
||||
return Err(VmError::ExecutionLimit("3D scene lights"));
|
||||
}
|
||||
let direction =
|
||||
reader.vector(object, ["directionX", "directionY", "directionZ"])?;
|
||||
let direction = (world.point(direction) - world.point(Vec3::default()))
|
||||
.normalized()
|
||||
.ok_or(invalid("light direction cannot be zero"))?;
|
||||
let color = color(&text(&reader.value(object, "color")?)?)?;
|
||||
let intensity = reader.number(object, "intensity")?;
|
||||
if !(0.0..=32.0).contains(&intensity) {
|
||||
return Err(invalid("light intensity expects 0 through 32"));
|
||||
}
|
||||
output.lights.push(scene3d::Light {
|
||||
direction,
|
||||
color,
|
||||
intensity,
|
||||
});
|
||||
}
|
||||
_ => return Err(invalid("invalid node in 3D scene graph")),
|
||||
}
|
||||
}
|
||||
let view = Arc::new(scene3d::View {
|
||||
scene: Arc::new(output),
|
||||
camera,
|
||||
});
|
||||
if !view.valid() {
|
||||
return Err(invalid("invalid 3D scene snapshot"));
|
||||
}
|
||||
Ok((view, reader.reads.into_iter().collect()))
|
||||
}
|
||||
|
||||
pub fn pick(
|
||||
cx: &mut NativeContext<'_>,
|
||||
desktop: ObjectRef,
|
||||
mount: &crate::mount::Mount,
|
||||
point: crate::geometry::Point,
|
||||
) -> Result<Value, VmError> {
|
||||
if mount.layout.is_some() {
|
||||
return Err(invalid("pick3D requires a committed viewport"));
|
||||
}
|
||||
let (_, data) = crate::block::resolve(cx.store(), mount.block)?;
|
||||
let data = crate::block::visual_data(cx.store(), data)?;
|
||||
if !matches!(data.kind, crate::block::Kind::Viewport3D { .. }) {
|
||||
return Err(invalid("pick3D requires a Viewport3D"));
|
||||
}
|
||||
if !mount.geometry.contains(point) {
|
||||
return Ok(Value::Nil);
|
||||
}
|
||||
let node = crate::block::snapshot(cx.store(), mount.block)?;
|
||||
let inset = crate::layout::insets(&node);
|
||||
let point = mount.geometry.view.unwrap_or_default().inverse(point);
|
||||
let (width, height) = (
|
||||
(mount.geometry.width - inset.horizontal()).max(0.),
|
||||
(mount.geometry.height - inset.vertical()).max(0.),
|
||||
);
|
||||
if width <= 0. || height <= 0. {
|
||||
return Ok(Value::Nil);
|
||||
}
|
||||
let (view, reads) = snapshot(
|
||||
cx.store(),
|
||||
data.scene3d.ok_or(invalid("missing Scene3D"))?,
|
||||
data.camera.ok_or(invalid("missing 3D camera"))?,
|
||||
)?;
|
||||
let hit = view.pick(
|
||||
(point.x - mount.geometry.x - inset.left) / width,
|
||||
(point.y - mount.geometry.y - inset.top) / height,
|
||||
width / height,
|
||||
);
|
||||
let Some(hit) = hit else {
|
||||
return Ok(Value::Nil);
|
||||
};
|
||||
let mesh = reads
|
||||
.iter()
|
||||
.find_map(|(object, _)| (object.to_string() == hit.identity).then_some(*object))
|
||||
.ok_or(invalid("picked mesh is unavailable"))?;
|
||||
let result = cx.new_object()?;
|
||||
crate::ui_bridge::attach_desktop_type(cx, result, "RayHit3D", desktop)?;
|
||||
cx.set_attr(result, "mesh", Value::Object(mesh))?;
|
||||
cx.set_attr(
|
||||
result,
|
||||
"distance",
|
||||
crate::native_visual::value(hit.distance),
|
||||
)?;
|
||||
cx.set_attr(
|
||||
result,
|
||||
"triangle",
|
||||
Value::Number((hit.triangle as i64).into()),
|
||||
)?;
|
||||
for (name, vector) in [
|
||||
("point", hit.point),
|
||||
("normal", hit.normal),
|
||||
(
|
||||
"barycentric",
|
||||
Vec3::new(hit.barycentric[0], hit.barycentric[1], hit.barycentric[2]),
|
||||
),
|
||||
] {
|
||||
let definition = crate::ui_bridge::desktop_type(cx.store(), "Vec3", desktop)?;
|
||||
let vector = allocate(
|
||||
cx,
|
||||
desktop,
|
||||
definition,
|
||||
Constructor::Vec3,
|
||||
&[
|
||||
crate::native_visual::value(vector.x),
|
||||
crate::native_visual::value(vector.y),
|
||||
crate::native_visual::value(vector.z),
|
||||
],
|
||||
)?;
|
||||
cx.set_attr(result, name, Value::Object(vector))?;
|
||||
}
|
||||
Ok(Value::Object(result))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PointerPick {
|
||||
pub mount: ObjectRef,
|
||||
pub desktop: ObjectRef,
|
||||
pub point: crate::geometry::Point,
|
||||
}
|
||||
|
||||
/// The default controller is replaceable; camera data has no dependency on it.
|
||||
pub fn control(
|
||||
cx: &mut NativeContext<'_>,
|
||||
controller: ObjectRef,
|
||||
camera_ref: ObjectRef,
|
||||
gesture: ObjectRef,
|
||||
) -> Result<(), VmError> {
|
||||
if cx.store().payload::<Node>(controller, "OrbitController")?.0 != NodeKind::Orbit {
|
||||
return Err(invalid("invalid orbit controller"));
|
||||
}
|
||||
let mut reader = Reader {
|
||||
store: cx.store(),
|
||||
reads: HashSet::new(),
|
||||
};
|
||||
let camera = reader.camera(camera_ref)?;
|
||||
let dx = reader.number(gesture, "dx")?;
|
||||
let dy = reader.number(gesture, "dy")?;
|
||||
let kind = text(&reader.value(gesture, "kind")?)?;
|
||||
let offset = camera.position - camera.target;
|
||||
let radius = offset.length();
|
||||
let mut position = camera.position;
|
||||
let mut target = camera.target;
|
||||
match kind.as_str() {
|
||||
"scene-orbit" => {
|
||||
let speed = reader.number(controller, "rotationSpeed")?;
|
||||
if !(0.0..=1.0).contains(&speed) {
|
||||
return Err(invalid("rotationSpeed expects 0 through 1"));
|
||||
}
|
||||
let up = camera
|
||||
.up
|
||||
.normalized()
|
||||
.ok_or(invalid("camera up cannot be zero"))?;
|
||||
let vertical = (offset.dot(up) / radius).clamp(-1., 1.);
|
||||
let horizontal = (offset - up * offset.dot(up))
|
||||
.normalized()
|
||||
.ok_or(invalid("orbit at pole requires a different up vector"))?;
|
||||
let (sine, cosine) = (dx * speed).sin_cos();
|
||||
let horizontal = horizontal * cosine + up.cross(horizontal) * sine;
|
||||
let pitch = (vertical.asin() + dy * speed).clamp(
|
||||
-std::f32::consts::FRAC_PI_2 + 0.01,
|
||||
std::f32::consts::FRAC_PI_2 - 0.01,
|
||||
);
|
||||
position = target + horizontal * (radius * pitch.cos()) + up * (radius * pitch.sin());
|
||||
}
|
||||
"scene-pan" => {
|
||||
let speed = reader.number(controller, "panSpeed")?;
|
||||
if !(0.0..=100.0).contains(&speed) {
|
||||
return Err(invalid("panSpeed expects 0 through 100"));
|
||||
}
|
||||
let height = reader.number(gesture, "height")?.max(1.);
|
||||
let units = match camera.projection {
|
||||
Projection::Perspective { fov } => {
|
||||
2. * radius * (fov.to_radians() / 2.).tan() / height
|
||||
}
|
||||
Projection::Orthographic { height: extent } => extent / height,
|
||||
};
|
||||
let basis = camera.basis().ok_or(invalid("invalid camera basis"))?;
|
||||
let delta = basis.right * (dx * units * speed) + basis.up * (-dy * units * speed);
|
||||
position = position + delta;
|
||||
target = target + delta;
|
||||
}
|
||||
"scene-zoom" => {
|
||||
let factor = reader.number(gesture, "factor")?;
|
||||
if !(0.1..=10.0).contains(&factor) {
|
||||
return Err(invalid("zoom factor expects 0.1 through 10"));
|
||||
}
|
||||
if let Projection::Orthographic { height } = camera.projection {
|
||||
set_number(
|
||||
cx,
|
||||
camera_ref,
|
||||
"height",
|
||||
(height / factor).clamp(0.001, 1_000_000.),
|
||||
)?;
|
||||
} else {
|
||||
let next =
|
||||
(radius / factor).clamp(camera.near * 2., camera.far.max(camera.near * 2.));
|
||||
position = target + offset * (next / radius);
|
||||
}
|
||||
}
|
||||
_ => return Err(invalid("unsupported 3D camera gesture")),
|
||||
}
|
||||
if !position.valid() || !target.valid() {
|
||||
return Err(invalid("camera movement exceeds world budget"));
|
||||
}
|
||||
for (name, value) in [
|
||||
("x", position.x),
|
||||
("y", position.y),
|
||||
("z", position.z),
|
||||
("targetX", target.x),
|
||||
("targetY", target.y),
|
||||
("targetZ", target.z),
|
||||
] {
|
||||
set_number(cx, camera_ref, name, value)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn transform(
|
||||
cx: &mut NativeContext<'_>,
|
||||
owner: ObjectRef,
|
||||
args: &[Value],
|
||||
rotate: bool,
|
||||
) -> Result<(), VmError> {
|
||||
let kind = cx.store().payload::<Node>(owner, "3D transform target")?.0;
|
||||
let camera = matches!(kind, NodeKind::Perspective | NodeKind::Orthographic);
|
||||
if !matches!(
|
||||
kind,
|
||||
NodeKind::Mesh | NodeKind::Group | NodeKind::Perspective | NodeKind::Orthographic
|
||||
) || rotate && camera
|
||||
{
|
||||
return Err(invalid("unsupported 3D transform target"));
|
||||
}
|
||||
let names = if rotate {
|
||||
["rotationX", "rotationY", "rotationZ"]
|
||||
} else {
|
||||
["x", "y", "z"]
|
||||
};
|
||||
let mut updates = Vec::new();
|
||||
for (index, name) in names.into_iter().enumerate() {
|
||||
let delta = number(&args[index])?;
|
||||
let value = number(&cx.store().get_attr(owner, name)?)? + delta;
|
||||
if !value.is_finite() || !rotate && value.abs() > 1_000_000. {
|
||||
return Err(invalid("3D transform exceeds world budget"));
|
||||
}
|
||||
updates.push((
|
||||
name,
|
||||
if rotate {
|
||||
value.rem_euclid(360.)
|
||||
} else {
|
||||
value
|
||||
},
|
||||
));
|
||||
if camera {
|
||||
let name = ["targetX", "targetY", "targetZ"][index];
|
||||
let value = number(&cx.store().get_attr(owner, name)?)? + delta;
|
||||
if value.abs() > 1_000_000. {
|
||||
return Err(invalid("camera target exceeds world budget"));
|
||||
}
|
||||
updates.push((name, value));
|
||||
}
|
||||
}
|
||||
for (name, value) in updates {
|
||||
set_number(cx, owner, name, value)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
impl endor_core::NativeCallable for PointerPick {
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(endor_core::CallArity::exact(0))
|
||||
}
|
||||
fn invoke(
|
||||
&self,
|
||||
cx: &mut NativeContext<'_>,
|
||||
_: &[Value],
|
||||
) -> Result<endor_core::NativeResult, VmError> {
|
||||
let mount = cx
|
||||
.store()
|
||||
.payload::<crate::mount::Mount>(self.mount, "Mount")?
|
||||
.clone();
|
||||
let hit = pick(cx, self.desktop, &mount, self.point)?;
|
||||
let definition = crate::ui_bridge::desktop_type(cx.store(), "Event", self.desktop)?;
|
||||
let event = crate::event_dispatch::new_event(cx, definition, "click")?;
|
||||
cx.set_attr(event, "hit", hit)?;
|
||||
crate::event_dispatch::dispatch(cx, mount.block, event, Some(self.mount), self.desktop)
|
||||
}
|
||||
}
|
||||
+26
-2
@@ -154,7 +154,9 @@ impl RenderCache {
|
||||
});
|
||||
}
|
||||
}
|
||||
if let Some(camera) = data.camera {
|
||||
if matches!(data.kind, Kind::CanvasViewport { .. })
|
||||
&& let Some(camera) = data.camera
|
||||
{
|
||||
for name in ["x", "y", "zoom"] {
|
||||
dependencies.push(Dependency {
|
||||
object: camera,
|
||||
@@ -173,6 +175,22 @@ impl RenderCache {
|
||||
continue;
|
||||
}
|
||||
path.remove(&data_ref);
|
||||
let view3d = if matches!(data.kind, Kind::Viewport3D { .. }) {
|
||||
let (view, reads) = crate::scene3d_model::snapshot(
|
||||
store,
|
||||
data.scene3d
|
||||
.ok_or(VmError::InvalidArgument("missing Scene3D"))?,
|
||||
data.camera
|
||||
.ok_or(VmError::InvalidArgument("missing 3D camera"))?,
|
||||
)?;
|
||||
dependencies.extend(reads.into_iter().map(|(object, name)| Dependency {
|
||||
object,
|
||||
property: Some(name),
|
||||
}));
|
||||
Some(view)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut protocol_key = String::new();
|
||||
let mut custom_event = false;
|
||||
if let Some(definition) = store.type_of(block)? {
|
||||
@@ -267,6 +285,12 @@ impl RenderCache {
|
||||
children,
|
||||
},
|
||||
Kind::Spacer(size) => UiKind::Spacer(*size),
|
||||
Kind::Viewport3D { width, height } => UiKind::Viewport3D {
|
||||
width: *width,
|
||||
height: *height,
|
||||
view: view3d.ok_or(VmError::InvalidArgument("missing 3D view"))?,
|
||||
controls: data.controller.is_some(),
|
||||
},
|
||||
Kind::CanvasViewport { width, height } => UiKind::CanvasViewport {
|
||||
width: *width,
|
||||
height: *height,
|
||||
@@ -344,7 +368,7 @@ impl RenderCache {
|
||||
block_id: block.to_string(),
|
||||
protocol_key,
|
||||
slots: data.children.iter().map(|c| c.slot).collect(),
|
||||
interactive: matches!(&data.kind, Kind::Button)
|
||||
interactive: matches!(&data.kind, Kind::Button | Kind::Viewport3D { .. })
|
||||
|| data.click.is_some()
|
||||
|| click_connection
|
||||
|| custom_event,
|
||||
|
||||
Reference in New Issue
Block a user