885 lines
31 KiB
Rust
885 lines
31 KiB
Rust
//! 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,
|
|
Panel,
|
|
}
|
|
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",
|
|
Self::Panel => "Panel3D",
|
|
}
|
|
}
|
|
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",
|
|
Self::Panel => "panel3D",
|
|
}
|
|
}
|
|
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,
|
|
Self::Panel,
|
|
]
|
|
.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),
|
|
Self::Panel => (3, 3),
|
|
_ => (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"],
|
|
Self::Panel => &["Number", "Number", "Block"],
|
|
_ => &[],
|
|
}
|
|
}
|
|
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,
|
|
Panel,
|
|
}
|
|
#[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::Panel => NodeKind::Panel,
|
|
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,
|
|
uv: vec![],
|
|
};
|
|
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 | NodeKind::Panel
|
|
) {
|
|
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::Panel => {
|
|
let content = object_ref(&args[2])?;
|
|
crate::block::validate(cx.store(), content, None)?;
|
|
set_number(cx, instance, "width", number(&args[0])?)?;
|
|
set_number(cx, instance, "height", number(&args[1])?)?;
|
|
set_number(cx, instance, "uiWidth", 512.)?;
|
|
set_number(cx, instance, "uiHeight", 320.)?;
|
|
cx.set_attr(instance, "content", Value::Object(content))?;
|
|
cx.set_attr(instance, "background", Value::Text("#FFFFFF".into()))?;
|
|
}
|
|
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 | NodeKind::Panel) {
|
|
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 struct PanelMount {
|
|
pub key: u64,
|
|
pub content: ObjectRef,
|
|
pub size: crate::layout::Size,
|
|
}
|
|
pub type Snapshot = (
|
|
Arc<scene3d::View>,
|
|
Vec<(ObjectRef, String)>,
|
|
Vec<PanelMount>,
|
|
);
|
|
pub fn snapshot(
|
|
store: &ObjectStore,
|
|
scene: ObjectRef,
|
|
camera: ObjectRef,
|
|
) -> Result<Snapshot, VmError> {
|
|
snapshot_reusing(store, scene, camera, None)
|
|
}
|
|
pub fn snapshot_reusing(
|
|
store: &ObjectStore,
|
|
scene: ObjectRef,
|
|
camera: ObjectRef,
|
|
reuse: Option<Arc<scene3d::Scene>>,
|
|
) -> 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,
|
|
acceleration: Arc::default(),
|
|
};
|
|
let mut work = vec![(scene, Matrix::default(), false, 0u64)];
|
|
let mut panels = Vec::new();
|
|
let mut path = HashSet::new();
|
|
let mut visits = 0;
|
|
let mut triangles = 0;
|
|
let mut vertices = 0;
|
|
while let Some((object, parent, finish, key)) = 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, key));
|
|
let kind = store.payload::<Node>(object, "3D node")?.0;
|
|
let world = if matches!(kind, NodeKind::Mesh | NodeKind::Group | NodeKind::Panel) {
|
|
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 (index, child) in object_list::items(store, list)?.iter().enumerate().rev() {
|
|
use std::hash::{Hash, Hasher};
|
|
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
|
key.hash(&mut hasher);
|
|
index.hash(&mut hasher);
|
|
child.hash(&mut hasher);
|
|
work.push((*child, world, false, hasher.finish()));
|
|
}
|
|
}
|
|
NodeKind::Mesh => {
|
|
let geometry = object_ref(&reader.value(object, "geometry")?)?;
|
|
let geometry = store.payload::<Geometry>(geometry, "Geometry3D")?.0.clone();
|
|
triangles += geometry.triangles.len();
|
|
vertices += geometry.vertices.len();
|
|
if vertices > 262_144 {
|
|
return Err(VmError::ExecutionLimit("3D scene vertices"));
|
|
}
|
|
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,
|
|
panel: None,
|
|
});
|
|
}
|
|
NodeKind::Panel => {
|
|
// Arrangement slots cross Endor's signed integer API. A raw
|
|
// u64 hash can turn negative when native Bounds are created.
|
|
let key = key & i64::MAX as u64;
|
|
if panels.iter().any(|panel: &PanelMount| panel.key == key) {
|
|
return Err(invalid("colliding 3D UI occurrence identities"));
|
|
}
|
|
if panels.len() >= 64 {
|
|
return Err(VmError::ExecutionLimit("3D UI panels"));
|
|
}
|
|
let width = reader.number(object, "width")?;
|
|
let height = reader.number(object, "height")?;
|
|
let ui_width = reader.number(object, "uiWidth")?;
|
|
let ui_height = reader.number(object, "uiHeight")?;
|
|
if width <= 0.
|
|
|| height <= 0.
|
|
|| ![ui_width, ui_height]
|
|
.iter()
|
|
.all(|size| (1.0..=2048.0).contains(size) && size.fract() == 0.)
|
|
{
|
|
return Err(invalid(
|
|
"Panel3D requires positive world dimensions and integer UI dimensions from 1 through 2048",
|
|
));
|
|
}
|
|
let content = object_ref(&reader.value(object, "content")?)?;
|
|
crate::block::resolve(store, content)?;
|
|
let background = color(&text(&reader.value(object, "background")?)?)?;
|
|
let geometry = Arc::new(scene3d::Geometry {
|
|
vertices: vec![
|
|
Vec3::new(-width / 2., height / 2., 0.),
|
|
Vec3::new(width / 2., height / 2., 0.),
|
|
Vec3::new(width / 2., -height / 2., 0.),
|
|
Vec3::new(-width / 2., -height / 2., 0.),
|
|
],
|
|
triangles: vec![[0, 1, 2], [0, 2, 3]],
|
|
uv: vec![[0., 0.], [1., 0.], [1., 1.], [0., 1.]],
|
|
});
|
|
output.meshes.push(scene3d::Mesh {
|
|
identity: object.to_string(),
|
|
geometry,
|
|
world,
|
|
color: [1., 1., 1.],
|
|
panel: Some(scene3d::Panel {
|
|
key,
|
|
width: ui_width as u32,
|
|
height: ui_height as u32,
|
|
background,
|
|
surface: None,
|
|
}),
|
|
});
|
|
panels.push(PanelMount {
|
|
key,
|
|
content,
|
|
size: crate::layout::Size {
|
|
width: ui_width,
|
|
height: ui_height,
|
|
},
|
|
});
|
|
}
|
|
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 scene = if let Some(scene) = reuse.filter(|scene| **scene == output) {
|
|
scene
|
|
} else {
|
|
output.acceleration = Arc::new(scene3d::spatial::Index::build(&output.meshes));
|
|
Arc::new(output)
|
|
};
|
|
let view = Arc::new(scene3d::View { scene, camera });
|
|
if !view.valid() {
|
|
return Err(invalid("invalid 3D scene snapshot"));
|
|
}
|
|
Ok((view, reader.reads.into_iter().collect(), panels))
|
|
}
|
|
|
|
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 Some(point) = mount.geometry.logical_point(point) else {
|
|
return Ok(Value::Nil);
|
|
};
|
|
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, "node", Value::Object(mesh))?;
|
|
cx.set_attr(
|
|
result,
|
|
"instance",
|
|
Value::Number((hit.instance as i64).into()),
|
|
)?;
|
|
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::Panel
|
|
| 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)
|
|
}
|
|
}
|