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.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
//! Row helpers a part of any kind shares: shown on some frames, a sprite sheet's cell, a keyed colour.
use super::*;
/// A part shown only on some frames: `visible` keyed (0 on the frames it's missing from)
pub fn visible(p: &mut Program, row: &mut BTreeMap<String, V>, id: &str, track: &[Option<Item>]) {
if track.iter().any(Option::is_none) {
let on: Vec<f64> = track.iter().map(|t| t.is_some() as u8 as f64).collect();
column(p, row, id, "visible", &on);
}
}
/// 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]) {
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
/// channels) over it, unrounded, as a ColorMaterial's colour reaches the GPU in Bevy
pub fn tint(p: &mut Program, row: &mut BTreeMap<String, V>, id: &str, its: &[Item], default: &str) {
let given = |i: &Item| i.props.get("srgb").and_then(Value::as_array).filter(|a| a.len() == 3).and_then(|a| Some([a[0].as_f64()?, a[1].as_f64()?, a[2].as_f64()?]));
let colors: Vec<[f64; 3]> = its.iter().map(|i| given(i).unwrap_or_else(|| rgb(&hex(i.props.get("color")).unwrap_or(default.into())))).collect();
for (c, name) in ["col_r", "col_g", "col_b"].iter().enumerate() {
exact(p, row, id, name, &colors.iter().map(|v| v[c]).collect::<Vec<_>>());
}
}
/// A vector prop keyed as one column per component (a camera's `up` as up_x/y/z, a 2D part's `quat` as q_x..q_w),
/// written only when some frame sets it; a frame without it (or with the wrong length) takes `d`
pub fn keyed_vec(p: &mut Program, row: &mut BTreeMap<String, V>, id: &str, its: &[Item], key: &str, names: &[&str], d: &[f64]) {
if !its.iter().any(|i| i.props.contains_key(key)) {
return;
}
for (k, name) in names.iter().enumerate() {
let at = |i: &Item| i.props.get(key).and_then(Value::as_array).filter(|a| a.len() == names.len()).and_then(|a| a[k].as_f64());
column(p, row, id, name, &its.iter().map(|i| at(i).unwrap_or(d[k])).collect::<Vec<_>>());
}
}
/// A <Material>'s uniforms and storage buffers over the frames (docs/control.feature FC2a), sent as keyed
/// `own:uniforms` component rows from each frame they change, which raea-sys-shader's own_live.rs packs into the
/// part's Material; none when they never change over a render's frames (the spec the part is made with holds them),
/// but one for a single frame (live play's, whose next frame may change them). `spec` finds an item's Material spec: a
/// mesh's material's `shader`, a 2D part's own. With `ext`, a ShaderExtension's spec (no `own`) is sent too: its
/// uniforms froze at the part's first frame without (docs/stage-guards.feature SG1). Called after the part's row is pushed.
pub fn own_uniforms(p: &mut Program, id: &str, its: &[Item], ext: bool, spec: impl Fn(&Item) -> Option<&Value>) {
let each: Vec<(usize, Value)> = its.iter().enumerate().filter_map(|(f, it)| {
let s = spec(it).filter(|s| ext || s.get("own").is_some())?;
let v = serde_json::json!({ "uniforms": s.get("uniforms").cloned().unwrap_or(Value::Null), "buffers": s.get("buffers").cloned().unwrap_or(Value::Null) });
Some((f, serde_json::json!({ "type": "own:uniforms", "value": v })))
}).collect();
if its.len() > 1 && each.iter().all(|(_, v)| Some(v) == each.first().map(|x| &x.1)) {
return;
}
// the part's spec carries its track's hash: parts share a Material (as Bevy's share one handle) only when their
// spec and every frame's values are the same (raea-sys-shader own.rs)
let mut h = std::collections::hash_map::DefaultHasher::new();
std::hash::Hash::hash(&each.iter().map(|(f, v)| format!("{f}{v}")).collect::<String>(), &mut h);
let track = format!("{:016x}", std::hash::Hasher::finish(&h));
let row = p.spawns.iter_mut().rev().flat_map(|(_, rows)| rows.iter_mut()).find(|r| r.get("id") == Some(&V::S(id.to_string())));
if let Some(V::S(s)) = row.and_then(|r| r.get_mut("shader")) {
if let Ok(Value::Object(mut o)) = serde_json::from_str::<Value>(s) {
o.insert("track".into(), Value::String(track));
*s = Value::Object(o).to_string();
}
}
keyed(p, "component", Some(id), "o", each.iter().map(|(f, v)| (*f, v)));
}
/// A string that may change frame by frame (a column is a number per frame), as column `col`: its first frame's value,
/// and when it changes, each value it takes (`{col}s`, one a line) and which one each frame has (`{col}_k`, keyed,
/// stepped); raea-sys-frame's `keyed` reads it back. A value with a line break in it is held from the first frame.
pub fn keyed_strings(p: &mut Program, row: &mut BTreeMap<String, V>, id: &str, col: &str, vals: &[String]) {
let Some(first) = vals.first() else { return };
row.insert(col.into(), V::S(first.clone()));
if vals.iter().all(|s| s == first) || vals.iter().any(|s| s.contains('\n')) {
return;
}
let mut list: Vec<&str> = Vec::new();
let ks: Vec<f64> = vals.iter().map(|s| match list.iter().position(|x| x == s) {
Some(k) => k as f64,
None => {
list.push(s);
(list.len() - 1) as f64
}
}).collect();
row.insert(format!("{col}s"), V::S(list.join("\n")));
row.insert(format!("{col}_k"), V::N(ks[0]));
p.keys.insert((id.to_string(), format!("{col}_k")), ks.iter().enumerate().map(|(f, k)| (f as i64, *k, "step".to_string())).collect());
}
/// Each frame's `src` (a picture's path, found as staticFile's /media/ is, or a "render:NAME"; an "atlas:" its
/// textures each found so), for keyed_strings.
pub fn srcs_of(its: &[Item]) -> Vec<String> {
its.iter().map(|i| i.props.get("src").and_then(Value::as_str).unwrap_or("")).map(|s| match s.strip_prefix("atlas:").and_then(|j| serde_json::from_str::<Value>(j).ok()) {
Some(mut a) => {
if let Some(Value::Array(t)) = a.get_mut("textures") {
t.iter_mut().for_each(|p| if let Some(s) = p.as_str() { *p = Value::String(src_path(s)) });
}
format!("atlas:{a}")
}
None => src_path(s),
}).collect()
}
/// A screen's columns (<Screen>, <Model screens>): its spec (a panel's `screen`) or a model's `screens` and `hide`,
/// JSON, from the first frame; the tube's power keyed when it changes.
pub fn screen_columns(p: &mut Program, row: &mut BTreeMap<String, V>, id: &str, its: &[Item]) {
let first = &its[0].props;
for (k, col) in [("screen", "screen"), ("screens", "screens"), ("hide", "hide")] {
if let Some(v) = first.get(k).filter(|v| !v.is_null()) {
row.insert(col.into(), V::S(v.to_string()));
}
}
if its.iter().any(|i| i.props.contains_key("screenPower")) {
column(p, row, id, "screen_power", &its.iter().map(|i| n(&i.props, "screenPower", 1.0)).collect::<Vec<_>>());
}
// a flat screen's brightness over the frames (the spec holds the first; engine/screen.rs's sync_screens reads this)
if its.iter().any(|i| screen_bright(&i.props).is_some()) {
column(p, row, id, "screen_bright", &its.iter().map(|i| screen_bright(&i.props).unwrap_or(1.0)).collect::<Vec<_>>());
}
}
/// A flat screen's `brightness`, from its spec (Screen's prop)
pub fn screen_bright(props: &Map<String, Value>) -> Option<f64> {
props.get("screen")?.get("brightness")?.as_f64()
}
#[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, at: 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)]));
}
}