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.

Layer pass oracle (docs/review.feature R2e, work in progress, parked with the source stamps): bench/layers/conformance.tsx (two 3D meshes, a component used twice, a page element, text, an image, a rotated element and a .map() list, overlapping) and oracle.sh (each layer hidden in turn at three moments, the pixels that change as quarter-size masks, sampled as NAME.layers' are); all 14 layers' hides change their pixels. Stamps: raea's page parts (Img, Video, AbsoluteFill) stamped as 3D parts are; the work folder's motion/prelude read as raea; data-raea-idx kept through the page bridge.

shinyobjectzc03edea29491
diff --git a/bench/layers/badge.tsx b/bench/layers/badge.tsx
new file mode 100644
index 00000000..31210402
--- /dev/null
+++ b/bench/layers/badge.tsx
@@ -0,0 +1,8 @@
+// The badge: the conformance composition's React component, used twice (two uses, two keys).
+export function Badge({ x, y, color }: { x: number; y: number; color: string }) {
+ return (
+ <div style={{ position: "absolute", left: x, top: y, width: 90, height: 90, borderRadius: 18, background: color }}>
+ <span style={{ position: "absolute", left: 12, top: 30, color: "#fff", fontSize: 22, fontWeight: 700 }}>badge</span>
+ </div>
+ );
+}
diff --git a/bench/layers/conformance.tsx b/bench/layers/conformance.tsx
new file mode 100644
index 00000000..cf4717f0
--- /dev/null
+++ b/bench/layers/conformance.tsx
@@ -0,0 +1,41 @@
+// docs/review.feature R2e: every layer kind in one frame, overlapping, so the layer pass is checked against the oracle
+// (hide one layer, render again, the pixels that change) where layers meet: a square drawn as a 3D mesh, as a React
+// component (used twice) and as a page element, text, an image, a rotated element and a .map() list. The page is over
+// the 3D, as a composition's always is. bench/layers/oracle.sh renders it.
+import { AbsoluteFill, Composition, Img, registerRoot, useCurrentFrame } from "raea";
+import { StandardMaterial } from "raea/pbr";
+import { Cuboid, Mesh3d } from "raea/mesh";
+import { DirectionalLight } from "raea/light";
+import { Quat, Transform } from "raea/transform";
+import { Camera3d } from "raea/camera";
+import { Badge } from "./badge";
+
+const Shot = () => {
+ const f = useCurrentFrame();
+ return (
+ <AbsoluteFill style={{ background: "#16202e" }}>
+ <Camera3d transform={Transform.fromXyz(0, 0, 6).lookingAt([0, 0, 0])} />
+ <DirectionalLight illuminance={9000} transform={Transform.fromXyz(2, 4, 6).lookingAt([0, 0, 0])} />
+ <Mesh3d transform={Transform.fromXyz(-0.6 + f * 0.01, 0.3, 0).withRotation(Quat.fromEuler("XYZ", 0.4, f * 0.02, 0))}>
+ <Cuboid half_size={[0.7, 0.7, 0.7]} />
+ <StandardMaterial base_color="#2fbf71" />
+ </Mesh3d>
+ <Mesh3d transform={Transform.fromXyz(1.4, -1.2, -1)}>
+ <Cuboid half_size={[0.5, 0.5, 0.5]} />
+ <StandardMaterial base_color="#e0a82e" />
+ </Mesh3d>
+ <Badge x={150 + f} y={150} color="#3366ff" />
+ <Badge x={300} y={60} color="#cc3377" />
+ <div style={{ position: "absolute", left: 210, top: 210, width: 90, height: 90, background: "#ff6a00" }} />
+ <h1 style={{ position: "absolute", left: 24, top: 8, margin: 0, color: "#f4f4f4", fontSize: 30 }}>Layers</h1>
+ <Img src="pic.png" style={{ position: "absolute", left: 20, top: 300, width: 96, height: 64 }} />
+ <div style={{ position: "absolute", left: 330, top: 300, width: 80, height: 40, background: "#8a5cf6", transform: `rotate(${20 + f}deg)` }} />
+ <ul style={{ position: "absolute", left: 20, top: 70, margin: 0, padding: 0, listStyle: "none" }}>
+ {["one", "two", "three"].map((t) => (
+ <li key={t} style={{ width: 70, height: 22, marginBottom: 6, background: "#2a3a52", color: "#cfe", fontSize: 14 }}>{t}</li>
+ ))}
+ </ul>
+ </AbsoluteFill>
+ );
+};
+registerRoot(() => <Composition id="layers" component={Shot} width={480} height={400} fps={30} durationInFrames={90} />);
diff --git a/bench/layers/oracle.sh b/bench/layers/oracle.sh
new file mode 100755
index 00000000..3a5b7c1e
--- /dev/null
+++ b/bench/layers/oracle.sh
@@ -0,0 +1,57 @@
+#!/bin/bash
+# The layer pass's oracle (docs/review.feature R2e): where each layer of bench/layers/conformance.tsx shows, found
+# without the layer pass. Each layer is hidden in turn (RAEA_HIDE_LAYERS: its element and all inside it, or its 3D part)
+# and the frame rendered again at three moments; the pixels that change are where it shows. Written as quarter-size
+# masks, sampled as NAME.layers' masks are (mask px (x, y) is frame px (4x+2, 4y+2)): OUT/<moment>/<n>.pgm, 255 where
+# it changed, with OUT/keys.txt (n, then the key) and OUT/<moment>/base.png. Exits 1 when a layer's key hides nothing
+# (a key that names no layer, or a hide that doesn't work).
+# bench/layers/oracle.sh OUT (RAEA_BIN: the raea to run, default target/release/raea)
+cd "$(dirname "$0")/../.." || exit 2
+OUT=${1:?OUT folder}
+BIN=${RAEA_BIN:-$PWD/target/release/raea}
+export RAEA_SIGN=0 RAEA_MARK=0 RAEA_EXPORT_CACHE=0 RAEA_SOURCE_STAMPS=1 RAEA_AUDIO_MUTE=1
+MOMENTS=(10 45 80)
+mkdir -p "$OUT/src"
+cp bench/layers/conformance.tsx bench/layers/badge.tsx "$OUT/src/"
+magick -size 96x64 gradient:'#ffd400-#ff2d55' "$OUT/src/pic.png"
+read -r W H <<< "$(sed -n 's/.*width={\([0-9]*\)} height={\([0-9]*\)}.*/\1 \2/p' bench/layers/conformance.tsx)"
+MW=$(( (W + 3) / 4 )); MH=$(( (H + 3) / 4 ))
+
+# file:line:col of the nth (1-based) "<Tag" in a file (ASCII sources: a byte column is TypeScript's UTF-16 one)
+at() { awk -v t="<$2" -v n="$3" -v f="$1" '{ s = $0; c = 0; while ((i = index(s, t)) > 0) { c += i; nx = substr(s, i + length(t), 1); if (nx ~ /[ >\/]/ && ++k == n) { print f ":" NR ":" c; exit } s = substr(s, i + length(t)); c += length(t) - 1 } }' "$OUT/src/$1"; }
+B1=$(at conformance.tsx Badge 1); B2=$(at conformance.tsx Badge 2); LI=$(at conformance.tsx li 1)
+# not the AbsoluteFill: its background over 3D is the 3D's clear (raea-runtime-export program.rs), behind every layer
+# and none of them, so hiding it changes nothing and the layer pass gives no layer there
+KEYS=(
+ "$(at conformance.tsx Mesh3d 1)" "$(at conformance.tsx Mesh3d 2)"
+ "$(at badge.tsx div 1)|$B1" "$(at badge.tsx span 1)|$B1" "$(at badge.tsx div 1)|$B2" "$(at badge.tsx span 1)|$B2"
+ "$(at conformance.tsx div 1)" "$(at conformance.tsx h1 1)" "$(at conformance.tsx Img 1)" "$(at conformance.tsx div 2)"
+ "$(at conformance.tsx ul 1)" "$LI#one" "$LI#two" "$LI#three"
+)
+: > "$OUT/keys.txt"
+for i in "${!KEYS[@]}"; do echo "$i ${KEYS[$i]}" >> "$OUT/keys.txt"; done
+
+at_list=$(IFS=,; echo "${MOMENTS[*]}")
+# --at's frames come out in order as frame-00000.png ...: moment m is the m'th
+render() { "$BIN" render "$OUT/src/conformance.tsx" --at "$at_list" -o "$1" > "$1.log" 2>&1 && [ -f "$1/frame-00002.png" ] || { echo "FAIL the render ($2) failed"; tail -4 "$1.log" | sed 's/^/ /'; exit 1; }; }
+render "$OUT/base" base
+quarter() { magick "$1" -crop +1+1 +repage -background black -extent "${W}x${H}" -sample "${MW}x${MH}" "$2"; }
+status=0
+for i in "${!KEYS[@]}"; do
+ RAEA_HIDE_LAYERS="${KEYS[$i]}" render "$OUT/hide$i" "hiding ${KEYS[$i]}"
+ total=0
+ for m in 0 1 2; do
+ mkdir -p "$OUT/${MOMENTS[$m]}"
+ cp "$OUT/base/frame-0000$m.png" "$OUT/${MOMENTS[$m]}/base.png"
+ # changed: some channel moved more than 10 of 255 (the encoder-free PNGs are exact; this is for dithered gradients)
+ magick "$OUT/base/frame-0000$m.png" "$OUT/hide$i/frame-0000$m.png" -compose difference -composite -separate -evaluate-sequence max -threshold 4% "$OUT/full.png"
+ quarter "$OUT/full.png" "$OUT/${MOMENTS[$m]}/$i.pgm"
+ n=$(magick "$OUT/${MOMENTS[$m]}/$i.pgm" -format "%[fx:int(mean*w*h+0.5)]" info:)
+ total=$((total + n))
+ done
+ rm -rf "${OUT:?}/hide$i" "$OUT/hide$i.log"
+ if (( total == 0 )); then echo "FAIL layer $i (${KEYS[$i]}): hiding it changed nothing"; status=1
+ else echo "ok layer $i (${KEYS[$i]}): $total quarter px over ${#MOMENTS[@]} moments"; fi
+done
+rm -f "$OUT/full.png"
+exit $status
diff --git a/engine/bundle/src/stamp.rs b/engine/bundle/src/stamp.rs
index 264de126..d6a0d769 100644
--- a/engine/bundle/src/stamp.rs
+++ b/engine/bundle/src/stamp.rs
@@ -28,14 +28,16 @@ const HTML: &[&str] = &[
"div", "span", "p", "h1", "h2", "h3", "h4", "h5", "h6", "section", "article", "header", "footer", "main", "aside", "nav", "figure", "figcaption", "blockquote", "pre", "code", "strong", "em", "b", "i", "u", "s", "small", "mark", "sub", "sup", "label", "ul", "ol", "li", "dl", "dt", "dd", "img", "video", "audio", "picture", "a", "button", "table", "tr", "td", "th", "svg", "canvas", "br", "hr",
];
-/// raea's modules whose components draw 3D parts (a Mesh3d, a Model, a Sprite): their uses are stamped as an HTML
+/// raea's modules whose components draw parts (a Mesh3d, a Model, a Sprite, an Img): their uses are stamped as an HTML
/// element is, and the scene gives the part a LayerKey component from it (runtime/scene bevy_parts), so a 3D part is
/// a layer as an element is (docs/review.feature R2a). Never a prop the part's kind reads.
-const PARTS3D: &[&str] = &["raea/3d", "raea/mesh", "raea/sprite"];
+const PARTS3D: &[&str] = &["raea/3d", "raea/mesh", "raea/sprite", "raea"];
/// The parts among them, by the name they're exported as: what draws a thing on its own. Never a shape or a material
/// inside one (a Mesh3d reads its <Cuboid> child's props, and an extra prop there made it no shape at all).
-const PART_NAMES: &[&str] = &["Mesh3d", "Model", "SceneRoot", "Entity", "Sprite", "Text3D", "Text2d", "ImagePlane", "ShaderMesh", "Plate", "Decal", "Splat", "Grass", "Mesh2d"];
+/// raea's page parts too (an Img, a Video, an AbsoluteFill), whose element is written in the SDK, unstamped: their use
+/// is the layer's place in the code
+const PART_NAMES: &[&str] = &["Mesh3d", "Model", "SceneRoot", "Entity", "Sprite", "Text3D", "Text2d", "ImagePlane", "ShaderMesh", "Plate", "Decal", "Splat", "Grass", "Mesh2d", "Img", "Video", "OffthreadVideo", "AbsoluteFill"];
/// Package components that read their children's types, whose children are left as written (a wrapper there would
/// hide a <Series.Sequence> from its <Series>, a <Cuboid> from its <Mesh3d>): every component of raea's 3D, material,
@@ -81,7 +83,7 @@ pub fn stamp(source: &str, rel: &str, jsx: bool, helper: &str) -> Option<String>
let Statement::ImportDeclaration(d) = s else { continue };
let (local, module) = module_of(rel, d.source.value.as_str());
let is3d = PARTS3D.contains(&module.as_str());
- let reads = is3d || READERS_MODULES.contains(&module.as_str());
+ let reads = READERS_MODULES.contains(&module.as_str());
for sp in d.specifiers.iter().flatten() {
imports.insert(sp.local().name.to_string(), local);
let name = sp.local().name.to_string();
@@ -257,7 +259,8 @@ fn module_of(rel: &str, spec: &str) -> (bool, String) {
return (true, spec.to_string());
}
match at.split_first() {
- Some((&"motion", rest)) if rest.is_empty() || rest == ["index"] => (false, "raea".into()),
+ // "raea" itself is motion/prelude there (raea-runtime-bundle rewrite.rs)
+ Some((&"motion", rest)) if rest.is_empty() || rest == ["index"] || rest == ["prelude"] => (false, "raea".into()),
Some((&"motion", rest)) => (false, format!("raea/{}", rest.join("/"))),
_ => (false, spec.to_string()),
}
@@ -330,6 +333,7 @@ mod tests {
assert_eq!(module_of("lib/x.tsx", "../card"), (true, "../card".into()));
assert_eq!(module_of("comp.tsx", "../motion/mesh"), (false, "raea/mesh".into()));
assert_eq!(module_of("lib/x.tsx", "../../motion"), (false, "raea".into()));
+ assert_eq!(module_of("comp.tsx", "../motion/prelude"), (false, "raea".into()));
assert_eq!(module_of("comp.tsx", "raea/3d"), (false, "raea/3d".into()));
// as the work folder has it: the SDK's parts stamped, its shapes and own wrappers not
let src = "import { Cuboid, Mesh3d } from \"../motion/mesh\";\nimport { Camera3d } from \"../motion/camera\";\nconst X = () => <><Camera3d /><Mesh3d><Cuboid /></Mesh3d></>;\n";
@@ -341,7 +345,7 @@ mod tests {
fn a_plain_package_component_is_a_parent_like_an_element() {
let src = "import { AbsoluteFill } from \"raea\";\nimport { Card } from \"./card\";\nconst X = () => <AbsoluteFill><Card /></AbsoluteFill>;\n";
let out = stamp(src, "p.tsx", false, H).unwrap();
- assert!(out.contains(r#"<AbsoluteFill><__RaeaAt value={__raeaChain("p.tsx:3:31")}><Card /></__RaeaAt></AbsoluteFill>"#), "{out}");
+ assert!(out.contains(r#"<AbsoluteFill data-raea-loc="p.tsx:3:17" data-raea-use={__raeaUse()}><__RaeaAt value={__raeaChain("p.tsx:3:31")}><Card /></__RaeaAt></AbsoluteFill>"#), "{out}");
}
#[test]
diff --git a/runtime/page/src/elements.rs b/runtime/page/src/elements.rs
index 75b0a97b..8fcf7dfe 100644
--- a/runtime/page/src/elements.rs
+++ b/runtime/page/src/elements.rs
@@ -248,8 +248,9 @@ pub(crate) fn page_props(kind: &str, props: Option<&Map<String, Value>>, dir: &P
// data-raea-bevy-ignore-scroll kept: Blitz floors its offset and keeps the node in place, vendor/blitz-dom resolve.rs;
// a spreading box shadow's data-raea-bevy-shadow kept: Blitz spreads it as bevy_ui does, vendor/blitz-paint render/box_shadow.rs;
// a Popover's data-raea-bevy-popover kept: Blitz places it beside its parent after layout, vendor/blitz-dom bevy_popover.rs;
- // the source stamps data-raea-loc and data-raea-use kept for the render manifest, engine/bundle stamp.rs)
- const KEPT: [&str; 14] = ["data-raea-start", "data-raea-bevy-debug", "data-raea-bevy-popover", "data-raea-bevy-shadow", "data-raea-bevy-text", "data-raea-bevy-slice", "data-raea-bevy-global-z", "data-raea-blend", "data-raea-bevy-scroll", "data-raea-bevy-ignore-scroll", "data-raea-bevy-scrollbar", "data-raea-bevy-thumb", "data-raea-loc", "data-raea-use"];
+ // the source stamps data-raea-loc, data-raea-use and data-raea-idx kept for the render manifest and the layer pass,
+ // engine/bundle stamp.rs)
+ const KEPT: [&str; 15] = ["data-raea-idx", "data-raea-start", "data-raea-bevy-debug", "data-raea-bevy-popover", "data-raea-bevy-shadow", "data-raea-bevy-text", "data-raea-bevy-slice", "data-raea-bevy-global-z", "data-raea-blend", "data-raea-bevy-scroll", "data-raea-bevy-ignore-scroll", "data-raea-bevy-scrollbar", "data-raea-bevy-thumb", "data-raea-loc", "data-raea-use"];
if k == "children" || k == "key" || (k.starts_with("data-raea") && !KEPT.contains(&k.as_str())) || k == "data-props" || k == "data-shape" || k == "data-anim" || v.is_null() {
continue;
}