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.

An ImagePlane plays its sprite sheet and swaps its picture in an mp4 render: a video's 3D is drawn live, frame by frame, and a live frame sent a panel only its placement, visibility and screen values, so the cell `frame` showed, the `src` it showed and its sheet's grid stayed at frame 0's for the whole film (a folder of PNGs, made from a program, played them). A live frame now sends a panel's sheet_frame, sheet_cols, sheet_rows and src when they change (live.rs's columns, and the fast path, which leaves a panel whose src changed to the full path: it holds the raw src against the one it last made a row from, so an unchanged one costs nothing); the engine swaps a live panel's picture when its row's src names another (scene3d::Pictures' `live`, found as spawn finds it). A program keys a part's grid as it keys its cell (it was the first frame's), so a src that changes to a sheet of another grid shows that sheet's cells; a card whose height follows its picture takes the new picture's shape. Found making 0.1.1's app showreel, which worked around it by rendering frames. 2 tests: raea-runtime-look's sheet keys, and raea-runtime-live's live_sheet (raea-runtime-scene is at its 2,000-line limit), where RAEA_LIVE3D_CHECK holds the fast path against the full one; a 30-frame repro reads the right cell and grid at frame 20 in the mp4, the program render and the PNGs.

shinyobjectz281da9cc1075
diff --git a/engine/render/src/engine/spawn.rs b/engine/render/src/engine/spawn.rs
index 80a9bce2..68f891a4 100644
--- a/engine/render/src/engine/spawn.rs
+++ b/engine/render/src/engine/spawn.rs
@@ -341,7 +341,7 @@ pub(crate) fn spawn_3d(world: &mut World, e: Entity, t: &Tup, inserted: &[Tup],
let pictures = match vals.get("srcs") {
Some(V::S(list)) => {
let paths = list.lines().map(|s| if s.starts_with("data:") { Ok(PathBuf::from(s)) } else { resolve_src(inserted, s, dir) }).collect::<Result<Vec<_>, _>>()?;
- Some(scene3d::Pictures { paths, shown: g("src_k", 0.0).round().max(0.0) as usize })
+ Some(scene3d::Pictures { paths, shown: g("src_k", 0.0).round().max(0.0) as usize, live: None })
}
_ => None,
};
@@ -352,9 +352,8 @@ pub(crate) fn spawn_3d(world: &mut World, e: Entity, t: &Tup, inserted: &[Tup],
let pic = scene3d::panel_picture(&src, inserted, dir).map_err(|e| format!("panel {}: {e}", t.id))?;
let (pw, ph) = pic.dimensions();
let tex = world.resource_mut::<Assets<Image>>().add(scene3d::panel_image(pic));
- if let Some(p) = pictures {
- world.entity_mut(e).insert(p);
- }
+ // a picture listed up front swaps by `src_k`; any other by a live frame's `src`
+ world.entity_mut(e).insert(pictures.unwrap_or_else(|| scene3d::Pictures { paths: Vec::new(), shown: 0, live: Some((src.clone(), dir.to_path_buf())) }));
// a sprite sheet shows one cell: its shape is a cell's (sync_meshes picks the cell, scene3d::sheet_uv)
let (cols, rows) = (g("sheet_cols", 1.0).max(1.0), g("sheet_rows", 1.0).max(1.0));
let (w, h) = (g("w", 1.0), g("h", g("w", 1.0) * (ph as f32 / rows) / (pw.max(1) as f32 / cols)));
diff --git a/runtime/live/tests/live_sheet.rs b/runtime/live/tests/live_sheet.rs
new file mode 100644
index 00000000..b0cfa5eb
--- /dev/null
+++ b/runtime/live/tests/live_sheet.rs
@@ -0,0 +1,27 @@
+//! An ImagePlane in live 3D (an mp4 render draws a video's 3D live: raea-runtime-scene live.rs) plays its sheet and
+//! swaps its picture. Tested here: raea-runtime-scene is at its 2,000-line limit.
+use raea_core_program::ir::V;
+use raea_runtime_react::scene::{LiveScene, Node3, Shot};
+
+fn plane(src: &str, grid: [u32; 2], frame: u32) -> Vec<Node3> {
+ let props = serde_json::json!({"name": "film", "src": src, "width": 1, "grid": grid, "frame": frame});
+ vec![Node3 { kind: "ImagePlane".into(), props: std::sync::Arc::new(props.as_object().unwrap().clone()), shape: None, kids: Vec::new().into() }]
+}
+
+/// Each frame sends the cell, and a swap to a sheet of another grid sends its src and grid, on the fast path as on the
+/// full one (RAEA_LIVE3D_CHECK holds each fast frame against the full path's). 0.1.1's showreel held frame 0's cell and
+/// picture for a whole mp4 (11-10).
+#[test]
+fn a_live_plane_plays_its_sheet_and_swaps_its_picture() {
+ // SAFETY: this binary's only test, so no other thread reads the environment
+ unsafe { std::env::set_var("RAEA_LIVE3D_CHECK", "1") };
+ let mut ls = LiveScene::default();
+ ls.open(Shot { scene: &plane("data:a", [2, 1], 0), clear: None }, 64, 64, 30.0);
+ let mut frame = |scene: Vec<Node3>| ls.frame(Shot { scene: &scene, clear: None }).0;
+ let set = frame(plane("data:a", [2, 1], 1));
+ assert_eq!((set.len(), set[0].vals.get("sheet_frame")), (1, Some(&V::N(1.0))));
+ let set = frame(plane("data:b", [1, 2], 1));
+ let v = &set.iter().find(|t| t.kind == "panel").unwrap().vals;
+ assert_eq!((v.get("src"), v.get("sheet_cols"), v.get("sheet_rows")), (Some(&V::S("data:b".into())), Some(&V::N(1.0)), Some(&V::N(2.0))));
+ assert!(frame(plane("data:b", [1, 2], 1)).is_empty());
+}
diff --git a/runtime/look/src/parts.rs b/runtime/look/src/parts.rs
index b48d845a..d9f80589 100644
--- a/runtime/look/src/parts.rs
+++ b/runtime/look/src/parts.rs
@@ -10,16 +10,24 @@ pub fn visible(p: &mut Program, row: &mut BTreeMap<String, V>, id: &str, track:
}
}
-/// A sprite sheet (`grid` [columns, rows], from the first frame): the cell `frame` shows, keyed (an <ImagePlane>'s,
-/// a <Sprite>'s)
+/// A sprite sheet (`grid` [columns, rows]): the cell `frame` shows and the grid, both keyed, so a part whose `src`
+/// changes to a sheet of another grid shows that sheet's cells (an <ImagePlane>'s, a <Sprite>'s). A frame without a
+/// grid is one cell.
pub fn sheet(p: &mut Program, row: &mut BTreeMap<String, V>, id: &str, its: &[Item]) {
- if let Some(Value::Array(g)) = its[0].props.get("grid") {
- if let [c, r] = g.as_slice() {
- row.insert("sheet_cols".into(), V::N(c.as_f64().unwrap_or(1.0)));
- row.insert("sheet_rows".into(), V::N(r.as_f64().unwrap_or(1.0)));
- column(p, row, id, "sheet_frame", &its.iter().map(|i| n(&i.props, "frame", 0.0).floor()).collect::<Vec<_>>());
- }
+ let grid = |i: &Item| match i.props.get("grid") {
+ Some(Value::Array(g)) => match g.as_slice() {
+ [c, r] => Some([c.as_f64().unwrap_or(1.0), r.as_f64().unwrap_or(1.0)]),
+ _ => None,
+ },
+ _ => None,
+ };
+ if !its.iter().any(|i| grid(i).is_some()) {
+ return;
}
+ let grids: Vec<[f64; 2]> = its.iter().map(|i| grid(i).unwrap_or([1.0, 1.0])).collect();
+ column(p, row, id, "sheet_cols", &grids.iter().map(|g| g[0]).collect::<Vec<_>>());
+ column(p, row, id, "sheet_rows", &grids.iter().map(|g| g[1]).collect::<Vec<_>>());
+ column(p, row, id, "sheet_frame", &its.iter().map(|i| n(&i.props, "frame", 0.0).floor()).collect::<Vec<_>>());
}
/// `color` keyed as col_r, col_g, col_b (`default` where a frame has none); `srgb` ([r, g, b], a Bevy Srgba's
@@ -108,3 +116,21 @@ pub fn srcs_of(its: &[Item]) -> Vec<String> {
None => src_path(s),
}).collect()
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// A sheet's grid is keyed as its cell is, so a src that changes to a sheet of another grid shows that sheet's
+ /// cells (the grid was the first frame's: 0.1.1's showreel, 11-10).
+ #[test]
+ fn a_sheets_grid_is_keyed() {
+ let plane = |grid: [u32; 2], frame: u32| Item { kind: "panel", props: std::sync::Arc::new(serde_json::json!({"grid": grid, "frame": frame}).as_object().unwrap().clone()), shape: None, material: None, world: Default::default() };
+ let (mut p, mut row) = (Program::default(), BTreeMap::new());
+ sheet(&mut p, &mut row, "plane0", &[plane([2, 1], 0), plane([1, 2], 1)]);
+ let keyed = |c: &str| p.keys.get(&("plane0".to_string(), c.to_string())).map(|k| k.iter().map(|(f, v, _)| (*f, *v)).collect::<Vec<_>>());
+ assert_eq!(keyed("sheet_cols"), Some(vec![(0, 2.0), (1, 1.0)]));
+ assert_eq!(keyed("sheet_rows"), Some(vec![(0, 1.0), (1, 2.0)]));
+ assert_eq!(keyed("sheet_frame"), Some(vec![(0, 0.0), (1, 1.0)]));
+ }
+}
diff --git a/runtime/scene/src/fast.rs b/runtime/scene/src/fast.rs
index 26037c87..5fe39b5c 100644
--- a/runtime/scene/src/fast.rs
+++ b/runtime/scene/src/fast.rs
@@ -56,6 +56,8 @@ impl LiveScene {
if !self.rows.contains_key(id) {
return None;
}
+ // a panel showing another picture (`current` kept the last): the full path, which sends the new src
+ if it.kind == "panel" && self.panel_srcs.get(key.as_str()).map(String::as_str) != it.props.get("src").and_then(Value::as_str) { return None }
parts.push((i, id));
}
}
@@ -193,6 +195,11 @@ impl LiveScene {
"decal"
}
_ => {
+ // a sprite sheet's grid and cell, as parts::sheet keys them
+ if let Some([c, r]) = it.props.get("grid").and_then(Value::as_array).map(Vec::as_slice) {
+ let cell = [("sheet_cols", c.as_f64().unwrap_or(1.0)), ("sheet_rows", r.as_f64().unwrap_or(1.0)), ("sheet_frame", n(&it.props, "frame", 0.0).floor())];
+ cell.into_iter().for_each(|(k, v)| put(k, v));
+ }
if it.props.contains_key("screenPower") {
put("screen_power", n(&it.props, "screenPower", 1.0));
}
diff --git a/runtime/scene/src/live.rs b/runtime/scene/src/live.rs
index 44b8fb0a..5cbbdef4 100644
--- a/runtime/scene/src/live.rs
+++ b/runtime/scene/src/live.rs
@@ -25,6 +25,8 @@ pub struct LiveScene {
pub(crate) cam: BTreeMap<String, V>,
/// frame 0 was stepped when play opened (live.rs), and nothing since
pub warm: bool,
+ /// identity -> a panel's `src` as written when the full path last made its row (the fast path leaves it a new one)
+ pub(crate) panel_srcs: std::collections::HashMap<String, String>,
}
#[derive(Clone)]
@@ -58,7 +60,7 @@ pub(crate) fn live_dynamic(kind: &str) -> &'static [&'static str] {
"light" => &["lux", "lumens", "x", "y", "z", "dir_x", "dir_y", "dir_z", "rot_qx", "rot_qy", "rot_qz", "rot_qw", "inner", "outer"],
"mesh" => &["x", "y", "z", "q_x", "q_y", "q_z", "q_w", "scale_x", "scale_y", "scale_z", "visible", "col_r", "col_g", "col_b", "rough", "metal", "emissive_k", "opacity", "sh_r", "sh_g", "sh_b", "rim_k", "spec_k", "outline_w", "pass"],
"model" => &["x", "y", "z", "q_x", "q_y", "q_z", "q_w", "scale_x", "scale_y", "scale_z", "visible", "anim_t", "mix0_t", "mix0_w", "mix1_t", "mix1_w", "mix2_t", "mix2_w", "mix3_t", "mix3_w", "morph0", "morph1", "morph2", "morph3", "morph4", "morph5", "morph6", "morph7", "morph8", "morph9", "morph10", "morph11", "morph12", "morph13", "morph14", "morph15", "morph16", "morph17", "morph18", "morph19", "morph20", "morph21", "morph22", "morph23", "morph24", "morph25", "morph26", "morph27", "morph28", "morph29", "morph30", "morph31", "morph32", "morph33", "morph34", "morph35", "morph36", "morph37", "morph38", "morph39", "morph40", "morph41", "morph42", "morph43", "morph44", "morph45", "morph46", "morph47", "morph48", "morph49", "morph50", "morph51", "morph52", "morph53", "morph54", "morph55", "morph56", "morph57", "morph58", "morph59", "morph60", "morph61", "morph62", "morph63", "g0_w", "g0_t", "g1_w", "g1_t", "g2_w", "g2_t", "g3_w", "g3_t", "g4_w", "g4_t", "g5_w", "g5_t", "g6_w", "g6_t", "g7_w", "g7_t", "g8_w", "g8_t", "g9_w", "g9_t", "g10_w", "g10_t", "g11_w", "g11_t", "g12_w", "g12_t", "g13_w", "g13_t", "g14_w", "g14_t", "g15_w", "g15_t", "look", "glitch_explode", "glitch_scramble", "glitch_jitter", "glitch_snap", "glitch_smear_x", "glitch_smear_y", "glitch_smear_z", "glitch_seed", "cloth_frame", "screen_power", "pass"],
- "panel" => &["x", "y", "z", "q_x", "q_y", "q_z", "q_w", "scale_x", "scale_y", "scale_z", "visible", "screen_power", "screen_bright", "pass"],
+ "panel" => &["x", "y", "z", "q_x", "q_y", "q_z", "q_w", "scale_x", "scale_y", "scale_z", "visible", "screen_power", "screen_bright", "pass", "sheet_frame", "sheet_cols", "sheet_rows", "src"],
"decal" => &["x", "y", "z", "q_x", "q_y", "q_z", "q_w", "scale_x", "scale_y", "scale_z", "visible", "col_r", "col_g", "col_b", "opacity", "rough", "metal"],
_ => &[],
}
@@ -127,6 +129,9 @@ impl LiveScene {
let cam = self.camera_row(&all, f.clear, &mut p);
let tr = Tracks::of(&all);
rows_into_of(&all, &tr, &mut p);
+ for (key, src) in tr.panels.iter().filter_map(|(k, t)| Some((k, t[0].as_ref()?.props.get("src")?.as_str()?))) {
+ self.panel_srcs.insert(key.clone(), src.to_string());
+ }
// each local row (this frame's tracks, numbered as rows_into numbers them) by its identity
let mut local: std::collections::HashMap<String, &String> = std::collections::HashMap::new();
for (li, (key, track)) in tr.lights.iter().enumerate() {
@@ -345,6 +350,7 @@ impl LiveScene {
for (_, id) in &gone {
self.rows.remove(id);
self.ids.retain(|_, v| v != id);
+ self.panel_srcs.retain(|k, _| self.ids.contains_key(k));
}
(out, gone)
}
diff --git a/runtime/scene3d/src/sync.rs b/runtime/scene3d/src/sync.rs
index d80356b1..829fb0de 100644
--- a/runtime/scene3d/src/sync.rs
+++ b/runtime/scene3d/src/sync.rs
@@ -1,41 +1,47 @@
use super::*;
-/// A panel whose picture changes over the run (an ImagePlane playing frames): every picture it shows, and the one its
-/// texture holds now. Its row keys `src_k`, which of them a frame shows.
+/// A panel whose picture changes over the run (an ImagePlane playing frames, or swapping its src): a render's lists
+/// every picture it shows (`paths`; its row keys `src_k`, which one a frame shows); a live scene's has the `src` it shows
+/// and its folder (`live`), and a live frame's row sends another src when it changes (raea-runtime-scene live.rs).
#[derive(Component)]
pub struct Pictures {
pub paths: Vec<PathBuf>,
pub shown: usize,
+ pub live: Option<(String, PathBuf)>,
}
-/// Each changing panel's picture for this frame: read and uploaded when `src_k` moves to another (the one it held is
-/// dropped with its handle, so a played sequence holds one picture at a time).
-pub fn sync_pictures(
- frame: Res<FrameRows>,
- mut mats: ResMut<Assets<StandardMaterial>>,
- mut images: ResMut<Assets<Image>>,
- mut q: Query<(Entity, &mut Pictures, &MeshMaterial3d<StandardMaterial>)>,
-) {
- for (e, mut pics, mat) in &mut q {
+/// Each changing panel's picture for this frame: read and uploaded when `src_k` moves to another or a live row's `src`
+/// names another (the one it held is dropped with its handle, so a played sequence holds one picture at a time). A card
+/// whose height follows its picture (no `h`) takes the new one's shape: one cell of its sheet.
+pub fn sync_pictures(frame: Res<FrameRows>, edit: Option<Res<Edit>>, mut mats: ResMut<Assets<StandardMaterial>>, mut images: ResMut<Assets<Image>>, mut meshes: ResMut<Assets<Mesh>>, mut q: Query<(Entity, &mut Pictures, &MeshMaterial3d<StandardMaterial>, &mut Mesh3d)>) {
+ let media = edit.as_ref().map_or(&[][..], |e| &e.media[..]);
+ for (e, mut pics, mat, mut mesh) in &mut q {
let Some(v) = frame.vals(e) else { continue };
let k = num(&v, "src_k", pics.shown as f32).round().max(0.0) as usize;
- if k == pics.shown {
- continue;
- }
- let Some(path) = pics.paths.get(k) else { continue };
- let src = path.to_string_lossy();
- match super::panel_picture(&src, &[], Path::new("")) {
+ let (src, dir) = match (&pics.live, v.get("src")) {
+ (Some((shown, dir)), Some(V::S(s))) if s != shown => (s.clone(), dir.clone()),
+ (None, _) if k != pics.shown => match pics.paths.get(k) {
+ Some(p) => (p.to_string_lossy().to_string(), PathBuf::new()),
+ None => continue,
+ },
+ _ => continue,
+ };
+ match super::panel_picture(&src, media, &dir) {
Ok(pic) => {
- let tex = images.add(super::panel_image(pic));
+ let ((pw, ph), tex) = (pic.dimensions(), images.add(super::panel_image(pic)));
if let Some(mut m) = mats.get_mut(&mat.0) {
m.base_color_texture = Some(tex);
}
- pics.shown = k;
- }
- Err(err) => {
- eprintln!("panel picture {src}: {err}");
- pics.shown = k;
+ if !v.contains_key("h") {
+ let (c, r, w) = (num(&v, "sheet_cols", 1.0).max(1.0), num(&v, "sheet_rows", 1.0).max(1.0), num(&v, "w", 1.0));
+ mesh.0 = meshes.add(Rectangle::new(w, w * (ph as f32 / r) / (pw.max(1) as f32 / c)));
+ }
}
+ Err(err) => eprintln!("panel picture {src}: {err}"),
+ }
+ pics.shown = k;
+ if let Some((shown, _)) = &mut pics.live {
+ *shown = src;
}
}
}