Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

stage: the hold and wireframe are a crate of their own (raea-sys-hold); the stage's new calls share one send

shinyobjectz9f4990cd906c
diff --git a/Cargo.lock b/Cargo.lock
index 80bab8ea..df40d696 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -14767,6 +14767,7 @@ dependencies = [
"raea-sys-framepass",
"raea-sys-grass",
"raea-sys-grid",
+ "raea-sys-hold",
"raea-sys-look",
"raea-sys-matchmove",
"raea-sys-material",
@@ -15178,6 +15179,7 @@ dependencies = [
"raea-runtime-react",
"raea-runtime-sound",
"raea-sys-frame",
+ "raea-sys-hold",
"raea-sys-mix",
"serde_json",
"twox-hash",
@@ -15719,6 +15721,14 @@ dependencies = [
"raea-sys-frame",
]
+[[package]]
+name = "raea-sys-hold"
+version = "0.1.0"
+dependencies = [
+ "bevy",
+ "raea-sys-frame",
+]
+
[[package]]
name = "raea-sys-image"
version = "0.1.0"
diff --git a/Cargo.toml b/Cargo.toml
index 9caae175..2ebd2761 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -103,6 +103,7 @@ members = [
"systems/page-net",
"systems/page-ops",
"systems/frame",
+ "systems/hold",
"systems/layers",
"systems/splat",
"systems/sprite",
diff --git a/docs/scene-feed.feature b/docs/scene-feed.feature
index e8e5cfeb..55c1588e 100644
--- a/docs/scene-feed.feature
+++ b/docs/scene-feed.feature
@@ -29,7 +29,7 @@ Feature: Scene: a feed item that is raea 3D looked at from inside
Background:
Given the app's code is apps/app (raea-feed): src/scene, src/detail/views/Scene.tsx, host/scene, samples/scene
And the inspector is built on stage.show, stage.pick, stage.view, stage.locate, stage.seek and stage.play as
- docs/preview.feature PV1 left them, and on three calls added for it (raea-sys-frame hold.rs, raea-runtime-live
+ docs/preview.feature PV1 left them, and on three calls added for it (raea-sys-hold, raea-runtime-live
pick3d.rs, raea-page-stage): stage.part (where a part stands), stage.hold (a part drawn where a drag has it)
and stage.wire (wireframe). On a raea without them it still works: handles from the code's numbers, no live
drag, and Wireframe says it can't yet
diff --git a/engine/page/stage/src/calls.rs b/engine/page/stage/src/calls.rs
index f7b8119f..b08bc748 100644
--- a/engine/page/stage/src/calls.rs
+++ b/engine/page/stage/src/calls.rs
@@ -10,6 +10,21 @@ pub(super) fn settle(answer: raea_runtime_js::calls::Answer, r: Result<Value, St
}
}
+/// A call for the stage's thread to answer; `idle` is the answer while the stage shows nothing.
+fn ask(w: &World, answer: raea_runtime_js::calls::Answer, idle: Value, cmd: impl FnOnce(raea_runtime_js::calls::Answer) -> Cmd) {
+ let tx = w.resource::<Stage>().tx.lock().unwrap().clone();
+ if let Some(tx) = tx {
+ let _ = tx.send(cmd(answer));
+ return;
+ }
+ answer.ok(idle)
+}
+
+/// An argument that is exactly N numbers.
+fn numbers<const N: usize>(a: &Value, k: &str) -> Option<[f32; N]> {
+ serde_json::from_value::<Vec<f32>>(a.get(k)?.clone()).ok()?.try_into().ok()
+}
+
/// The page's host calls (the module's list).
pub(super) fn calls(mut calls: ResMut<HostCalls>) {
let num = |a: &Value, k: &str| a.get(k).and_then(Value::as_f64);
@@ -86,41 +101,19 @@ pub(super) fn calls(mut calls: ResMut<HostCalls>) {
None => answer.err("the stage shows nothing"),
}
})
- .on("stage.part", move |w, a, answer| {
- let key = a.get("key").and_then(Value::as_str).unwrap_or_default().to_string();
- let tx = w.resource::<Stage>().tx.lock().unwrap().clone();
- match tx {
- Some(tx) => {
- let _ = tx.send(Cmd::Part(key, answer));
- }
- None => answer.ok(Value::Null),
- }
- })
+ .on("stage.part", move |w, a, answer| ask(w, answer, Value::Null, |to| Cmd::Part(a["key"].as_str().unwrap_or_default().to_string(), to)))
.on("stage.hold", move |w, a, answer| {
- let key = a.get("key").and_then(Value::as_str).unwrap_or_default().to_string();
- let list = |k: &str, n: usize| a.get(k).and_then(Value::as_array).map(|v| v.iter().filter_map(Value::as_f64).map(|x| x as f32).collect::<Vec<_>>()).filter(|v| v.len() == n && v.iter().all(|x| x.is_finite()));
- let to = match (a.get("release").and_then(Value::as_bool), list("translation", 3), list("rotation", 4), list("scale", 3)) {
- (Some(true), ..) => None,
- (_, Some(t), Some(r), Some(s)) => Some(([t[0], t[1], t[2]], [r[0], r[1], r[2], r[3]], [s[0], s[1], s[2]])),
+ let place = match (a["release"] == true, numbers(&a, "translation"), numbers(&a, "rotation"), numbers(&a, "scale")) {
+ (true, ..) => None,
+ (_, Some(t), Some(r), Some(s)) => Some((t, r, s)),
_ => return answer.err("stage.hold takes a key with translation [x, y, z], rotation [x, y, z, w] and scale [x, y, z], or release: true"),
};
- let tx = w.resource::<Stage>().tx.lock().unwrap().clone();
- match tx {
- Some(tx) => {
- let _ = tx.send(Cmd::Hold(key, to, answer));
- }
- None => answer.ok(json!(false)),
- }
+ let key = a.get("key").and_then(Value::as_str).unwrap_or_default().to_string();
+ ask(w, answer, json!(false), |to| Cmd::Hold(key, place, to))
})
.on("stage.wire", move |w, a, answer| {
let on = a.get("on").and_then(Value::as_bool).unwrap_or(true);
- let tx = w.resource::<Stage>().tx.lock().unwrap().clone();
- match tx {
- Some(tx) => {
- let _ = tx.send(Cmd::Wire(on, answer));
- }
- None => answer.ok(json!(false)),
- }
+ ask(w, answer, json!(false), |to| Cmd::Wire(on, to))
})
.on("stage.inspect", move |w, a, answer| {
let id = a.get("id").and_then(Value::as_u64).unwrap_or(0);
diff --git a/engine/render/Cargo.toml b/engine/render/Cargo.toml
index bfad73f4..2288331e 100644
--- a/engine/render/Cargo.toml
+++ b/engine/render/Cargo.toml
@@ -9,6 +9,8 @@ description = "raea's render engine: an edit's rows drawn by Bevy (3D) or the di
[dependencies]
# a frame's rows as the passes read them (FrameRows, the edit, the clock, row helpers, bevy_api)
raea-sys-frame = { path = "../../systems/frame" }
+# a part held where a hand has it, and wireframe (the app's scene inspector)
+raea-sys-hold = { path = "../../systems/hold" }
# the passes, each a crate of its own (systems/)
raea-sys-splat = { path = "../../systems/splat" }
raea-sys-sprite = { path = "../../systems/sprite" }
diff --git a/engine/render/src/engine/open.rs b/engine/render/src/engine/open.rs
index 8b38156b..9cc33f37 100644
--- a/engine/render/src/engine/open.rs
+++ b/engine/render/src/engine/open.rs
@@ -189,10 +189,7 @@ impl Engine {
let backend = raea_io_media::backend(&media_backend())?;
app.insert_non_send(Media { backend, decoders: HashMap::new(), dir: dir.to_path_buf() });
}
- // the app's stage can draw a scene as wireframe (docs/scene-feed.feature SF3e): only where it asked
- if live {
- raea_sys_frame::hold::add_wire(&mut app);
- }
+ raea_sys_hold::install(&mut app, live); // a part held by a hand, and wireframe (docs/scene-feed.feature SF4c, SF3e)
mem_mark("engine: plugins built (DefaultPlugins, the render device)");
Ok(Opening { app, prog: prog.clone(), inserted: inserted.to_vec(), dir: dir.to_path_buf(), timeline, live })
}
@@ -227,8 +224,6 @@ impl Engine {
app.insert_resource(TagRowTarget(target.clone())).init_resource::<ViewBox>().add_systems(PostUpdate, tag_row_view.before(bevy::camera::CameraUpdateSystems));
// a free camera over the scene (docs/preview.feature PV1c): off until a preview asks for one
app.init_resource::<raea_sys_frame::view::FreeView>().add_systems(PostUpdate, raea_sys_frame::view::apply.before(bevy::transform::TransformSystems::Propagate));
- // a part held where a hand has it (docs/scene-feed.feature SF4c): nothing until an inspector holds one
- app.init_resource::<raea_sys_frame::hold::Held>().add_systems(PostUpdate, raea_sys_frame::hold::apply.before(bevy::transform::TransformSystems::Propagate));
// without native nothing ever comes back (frames are read back only to be encoded or analysed)
let nv12 = !STILLS_RGBA.load(std::sync::atomic::Ordering::Relaxed) && std::env::var("RAEA_READBACK").map(|v| v != "rgba").unwrap_or(true);
// stills through the GPU composite: its RGBA words and the tag word, unpadded
diff --git a/runtime/live/Cargo.toml b/runtime/live/Cargo.toml
index ce04d5d6..fdf083b2 100644
--- a/runtime/live/Cargo.toml
+++ b/runtime/live/Cargo.toml
@@ -19,6 +19,7 @@ raea-io-trace = { path = "../../io/trace" }
raea-core-program = { path = "../../core/program" }
raea-sys-mix = { path = "../../systems/mix" }
raea-sys-frame = { path = "../../systems/frame" }
+raea-sys-hold = { path = "../../systems/hold" }
bevy = { version = "=0.20.0", default-features = false, features = ["std", "default_app"] }
serde_json = "1"
wgpu = "=30.0.1"
diff --git a/runtime/live/src/pick3d.rs b/runtime/live/src/pick3d.rs
index 8d7125e1..36152d7f 100644
--- a/runtime/live/src/pick3d.rs
+++ b/runtime/live/src/pick3d.rs
@@ -36,7 +36,7 @@ impl Live {
out["entity"] = json!(hit.entity.to_bits());
// where it stands (docs/scene-feed.feature SF4c): what its handles are drawn from
if let Some(w) = self.scene.as_mut().map(|e| e.world_mut()) {
- out["placed"] = placed_json(raea_sys_frame::hold::placed(w, hit.entity));
+ out["placed"] = placed_json(raea_sys_hold::placed(w, hit.entity));
}
// how a part with no key is asked for again (locate): its entity, as text (its bits don't fit a page's number)
out["entity_key"] = json!(format!("entity:{}", hit.entity.to_bits()));
@@ -87,16 +87,16 @@ impl Live {
/// drawn has that key. A picked part asked for again after the scene's code changed (a new world, new entities).
pub fn part(&mut self, key: &str) -> Option<Value> {
let w = self.scene.as_mut()?.world_mut();
- let e = raea_sys_frame::hold::part_of(w, key)?;
- Some(json!({"entity_key": format!("entity:{}", e.to_bits()), "placed": placed_json(raea_sys_frame::hold::placed(w, e))}))
+ let e = raea_sys_hold::part_of(w, key)?;
+ Some(json!({"entity_key": format!("entity:{}", e.to_bits()), "placed": placed_json(raea_sys_hold::placed(w, e))}))
}
/// The part a key names held at a place in its parent's space (translation, rotation as a quaternion, scale), or
/// let go of (None): drawn there until then, its code untouched. False with no 3D or no such part.
pub fn hold(&mut self, key: &str, to: Option<([f32; 3], [f32; 4], [f32; 3])>) -> bool {
let Some(w) = self.scene.as_mut().map(|e| e.world_mut()) else { return false };
- let Some(e) = raea_sys_frame::hold::part_of(w, key) else { return false };
- let Some(mut held) = w.get_resource_mut::<raea_sys_frame::hold::Held>() else { return false };
+ let Some(e) = raea_sys_hold::part_of(w, key) else { return false };
+ let Some(mut held) = w.get_resource_mut::<raea_sys_hold::Held>() else { return false };
match to {
Some((t, r, s)) => {
held.parts.insert(e, Transform { translation: Vec3::from(t), rotation: Quat::from_array(r).normalize(), scale: Vec3::from(s) });
@@ -112,7 +112,7 @@ impl Live {
/// Every mesh drawn as its edges over its shading, or as it is. False with no 3D or no wireframe pass.
pub fn wire(&mut self, on: bool) -> bool {
let Some(w) = self.scene.as_mut().map(|e| e.world_mut()) else { return false };
- let ok = raea_sys_frame::hold::set_wire(w, on);
+ let ok = raea_sys_hold::set_wire(w, on);
self.dirty |= ok;
ok
}
@@ -172,7 +172,7 @@ impl Live {
/// Where a part stands, for the page: its place in its parent's space and in the world (translation, rotation as a
/// quaternion [x, y, z, w], scale), and its parent's matrix in the world (16 numbers, column by column; null at the top).
-fn placed_json(p: raea_sys_frame::hold::Placed) -> Value {
+fn placed_json(p: raea_sys_hold::Placed) -> Value {
let trs = |t: Transform| json!({"translation": t.translation.to_array(), "rotation": t.rotation.to_array(), "scale": t.scale.to_array()});
json!({
"local": trs(p.local),
@@ -183,5 +183,5 @@ fn placed_json(p: raea_sys_frame::hold::Placed) -> Value {
/// Scenes opened from here on can be drawn as wireframe (`Live::wire`): the app's stage asks once, before any opens.
pub fn want_wire() {
- raea_sys_frame::hold::want_wire();
+ raea_sys_hold::want_wire();
}
diff --git a/systems/frame/src/lib.rs b/systems/frame/src/lib.rs
index c2fc81dd..a8b00455 100644
--- a/systems/frame/src/lib.rs
+++ b/systems/frame/src/lib.rs
@@ -22,8 +22,6 @@ pub mod pick;
pub mod shadow;
/// a free camera over the scene (docs/preview.feature PV1c)
pub mod view;
-/// a part held where a hand has it, and the scene as wireframe (docs/scene-feed.feature SF4c, SF3e)
-pub mod hold;
/// pipelined updates: the render world drawing the last frame while the main world runs this one
pub mod pipe;
// the edit's rows as entities, each column a component of its own, found by kind and id
diff --git a/systems/hold/Cargo.toml b/systems/hold/Cargo.toml
new file mode 100644
index 00000000..f27ec37c
--- /dev/null
+++ b/systems/hold/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "raea-sys-hold"
+version = "0.1.0"
+edition = "2024"
+license = "FSL-1.1-ALv2"
+publish = false
+description = "A part of a 3D scene held where a hand has it while it is dragged, where a part stands, and the scene drawn as wireframe (docs/scene-feed.feature SF4c, SF4d, SF3e)"
+
+[dependencies]
+bevy = { version = "=0.20.0", default-features = false, features = ["std", "default_app", "3d_bevy_render"] }
+# a part's layer key: how the app names the part it holds
+raea-sys-frame = { path = "../frame" }
diff --git a/systems/frame/src/hold.rs b/systems/hold/src/lib.rs
similarity index 74%
rename from systems/frame/src/hold.rs
rename to systems/hold/src/lib.rs
index 6c587c53..cc42b23c 100644
--- a/systems/frame/src/hold.rs
+++ b/systems/hold/src/lib.rs
@@ -2,14 +2,14 @@
//! `Held` names a part, its Transform is put where the hold says after the composition's rows have placed it and
//! before transforms propagate, so the frame draws the part (and all under it) there. Let go, it goes back to where
//! the composition last put it. Nothing in the composition's rows changes: a hold is how a drag looks while it is
-//! dragged, and the release writes the scene's code. The same shape as the free camera (view.rs).
+//! dragged, and the release writes the scene's code. The same shape as the free camera (raea-sys-frame view.rs).
use std::collections::HashMap;
-use std::sync::atomic::{AtomicBool, Ordering};
+use std::sync::OnceLock;
+use bevy::pbr::wireframe::{WireframeConfig, WireframePlugin};
use bevy::prelude::*;
-
-use crate::layer::LayerKey;
+use raea_sys_frame::layer::LayerKey;
/// The parts held and where; and what the hold last wrote and what the composition had put there, to tell the
/// composition's own moves from the hold's and to put each part back.
@@ -20,6 +20,15 @@ pub struct Held {
own: HashMap<Entity, Transform>,
}
+/// What an engine being built needs for both: the hold (nothing until an inspector holds a part), and Bevy's
+/// wireframe pass where a live engine's host asked for it (`want_wire`), off until `set_wire`.
+pub fn install(app: &mut App, live: bool) {
+ app.init_resource::<Held>().add_systems(PostUpdate, apply.before(bevy::transform::TransformSystems::Propagate));
+ if live && WIRE.get().is_some() && !app.is_plugin_added::<WireframePlugin>() {
+ app.add_plugins(WireframePlugin::default());
+ }
+}
+
/// PostUpdate, before transforms propagate.
pub fn apply(mut held: ResMut<Held>, mut places: Query<&mut Transform>) {
let Held { parts, wrote, own } = &mut *held;
@@ -64,33 +73,30 @@ pub struct Placed {
}
pub fn placed(world: &World, e: Entity) -> Placed {
- Placed {
- // as the composition has it, not as a hold has moved it
- local: world.get_resource::<Held>().and_then(|h| h.own.get(&e).copied()).or_else(|| world.get::<Transform>(e).copied()).unwrap_or_default(),
- world: world.get::<GlobalTransform>(e).copied().unwrap_or_default(),
- parent: world.get::<ChildOf>(e).and_then(|p| world.get::<GlobalTransform>(p.parent()).copied()),
+ // as the composition has it, not as a hold has moved it
+ let mut local = world.get::<Transform>(e).copied().unwrap_or_default();
+ if let Some(own) = world.get_resource::<Held>() {
+ local = own.own.get(&e).copied().unwrap_or(local);
}
+ let mut parent = None;
+ if let Some(p) = world.get::<ChildOf>(e) {
+ parent = world.get::<GlobalTransform>(p.parent()).copied();
+ }
+ Placed { local, world: world.get::<GlobalTransform>(e).copied().unwrap_or_default(), parent }
}
-static WIRE: AtomicBool = AtomicBool::new(false);
+static WIRE: OnceLock<()> = OnceLock::new();
-/// Engines opened from here on can draw as wireframe (the app's stage asks; a render never does: Bevy's wireframe
-/// pass is one more pass in every frame's graph).
+/// Live engines opened from here on can draw as wireframe (the app's stage asks; a render never does: Bevy's
+/// wireframe pass is one more pass in every frame's graph).
pub fn want_wire() {
- WIRE.store(true, Ordering::Relaxed);
-}
-
-/// Bevy's wireframe pass added to an engine being built, when asked for (`want_wire`): off until `set_wire`.
-pub fn add_wire(app: &mut App) {
- if WIRE.load(Ordering::Relaxed) && !app.is_plugin_added::<bevy::pbr::wireframe::WireframePlugin>() {
- app.add_plugins(bevy::pbr::wireframe::WireframePlugin::default());
- }
+ WIRE.get_or_init(|| ());
}
/// Every mesh drawn as its edges over its shading (on), or as it is (off). False where the engine has no wireframe
/// pass (it wasn't asked for, or the GPU can't draw lines).
pub fn set_wire(world: &mut World, on: bool) -> bool {
- let Some(mut c) = world.get_resource_mut::<bevy::pbr::wireframe::WireframeConfig>() else { return false };
+ let Some(mut c) = world.get_resource_mut::<WireframeConfig>() else { return false };
if c.global != on {
c.global = on;
}
@@ -104,7 +110,7 @@ mod tests {
#[test]
fn a_hold_places_the_part_and_letting_go_gives_it_back() {
let mut app = App::new();
- app.init_resource::<Held>().add_systems(PostUpdate, apply);
+ install(&mut app, false);
let own = Transform::from_xyz(1.0, 0.0, 0.0);
let part = app.world_mut().spawn(own).id();
app.update();
diff --git a/tools/layers.toml b/tools/layers.toml
index d9a19f68..5651ea25 100644
--- a/tools/layers.toml
+++ b/tools/layers.toml
@@ -98,6 +98,7 @@ raea-sys-page = "systems"
raea-sys-page-net = "systems"
raea-sys-page-ops = "systems"
raea-sys-frame = "systems"
+raea-sys-hold = "systems"
raea-sys-splat = "systems"
raea-sys-sprite = "systems"
raea-sys-rt = "systems"