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.

R3F19: raea's bundler resolves three and three/webgpu imported by the edit's own code (the work folder's src/ and ext/, never the SDK's copies: the playable host keeps three's real renderer) to the r3f copy's vanilla.ts and vanilla-webgpu.ts, so a plain three.js program's new THREE.WebGLRenderer() is raea's stand-in with no import of raea's (engine/bundle edit.rs, its own-code test). tools/parity.sh and shoot.ts run in-vanilla. docs/r3f.feature tags R3F19 @done: in-vanilla SSIM 0.9921 through the bundler, the R3F cases whose three is now the stand-in module unchanged (in-basic 0.9917, in-shader 0.9935, in-drei 0.9932, in-tsl-lit 0.9906, in-instanced 0.9819, in-sky 0.9986)

shinyobjectz1d04a18d54ef
diff --git a/docs/r3f.feature b/docs/r3f.feature
index c4ffe449..54a4e826 100644
--- a/docs/r3f.feature
+++ b/docs/r3f.feature
@@ -311,6 +311,7 @@ Feature: React Three Fiber both ways: an R3F scene drawn by raea, a raea composi
and do inside <R3F>
And SKILL.md's router sends an agent with three.js or R3F code to 3d/r3f.md
+ @done
Scenario: R3F19. Plain three.js with its own renderer
Given a composition whose three.js code makes its own renderer, as single-file demos do: `new
THREE.WebGLRenderer({ canvas })` or three/webgpu's `WebGPURenderer`, a scene and camera, `renderer.render(scene,
@@ -320,6 +321,19 @@ Feature: React Three Fiber both ways: an R3F scene drawn by raea, a raea composi
that scene is drawn by raea's engine as <R3F>'s scene is, in the canvas's box
And its animation loop steps 1/fps a frame from the composition's frame (render equals play)
And in-vanilla (a plain three.js page): SSIM at least 0.92 against the same page in Chrome
+ Status (11-15): `<Three program={fn}>` (raea/r3f, src/input/Three.tsx, program.ts): fn is the page's script as a
+ function given a canvas. engine/bundle edit.rs resolves `three` and `three/webgpu` imported by the edit's own
+ code (src/, ext/ of the work folder; never the SDK's copies, so the playable host keeps three's real renderer) to
+ the r3f copy's vanilla.ts and vanilla-webgpu.ts: three re-exported, its renderer a class whose constructor
+ returns R3F's stand-in (renderer.ts, moved out of session.ts with the seeded random in seed.ts, so a
+ composition's three needs nothing of React Three Fiber's) wrapped to record a render to the screen and the
+ animation loop into the program running it. Each frame steps its loops and requestAnimationFrame callbacks at
+ i*1000/fps ms with performance.now() at that time and Math.random seeded by the <Three>'s key; a frame before
+ the last runs it again; an async program (await renderer.init()) holds raea's frame and steps from 0 once ready;
+ a full-screen pass to the screen (EffectComposer's quad) draws the scene it rendered. test/program.test.ts 7/7,
+ edit.rs's own-code test; in-vanilla (test/parity/programs.ts: a knot, a ring of boxes, a sun and shadows, turned
+ by the loop's time and by += a call) SSIM 0.9921, and the R3F cases whose `three` is now the stand-in unchanged
+ (in-basic 0.9917, in-shader 0.9935, in-drei 0.9932, in-tsl-lit 0.9906, in-instanced 0.9819, in-sky 0.9986).
@done
Scenario: R3F20. An InstancedMesh's moving copies are one instance buffer a frame
diff --git a/engine/bundle/src/edit.rs b/engine/bundle/src/edit.rs
index 061ba4bf..d2b05860 100644
--- a/engine/bundle/src/edit.rs
+++ b/engine/bundle/src/edit.rs
@@ -44,6 +44,14 @@ impl EditResolve {
imp.starts_with(&self.0.work) && !imp.components().any(|c| c.as_os_str() == "node_modules")
}
+ /// whether an importer is the edit's own code: its sources (src/) or what it imports from outside its folder (ext/),
+ /// not the SDK's copies beside them
+ fn own_source(&self, imp: &str) -> bool {
+ let named = Path::new(imp);
+ let imp = named.canonicalize().map(raea_io_home::plain).unwrap_or_else(|_| named.to_path_buf());
+ self.from_edit(&imp.to_string_lossy()) && ["src", "ext"].iter().any(|d| imp.starts_with(self.0.work.join(d)))
+ }
+
/// A package's import of the SDK by its names ("raea", "raea/<module>", "@raea/<package>"): the work folder's copy
/// of that module, where the edit's own imports of it were pointed (runtime/bundle's rewrite_into: sdk/raea/src's
/// files at motion/, a package's at <package>/ beside it), so a package and the composition share one SDK and its
@@ -103,6 +111,21 @@ mod tests {
assert!(!r.from_edit(&alias.join("node_modules").join("pkg").join("index.js").to_string_lossy()));
let _ = std::fs::remove_dir_all(&d);
}
+
+ /// three's stand-in is for the edit's own code (src/, ext/): never the SDK's copies beside it, nor a package
+ #[test]
+ fn the_edits_own_code_is_its_sources_and_what_it_brings_in() {
+ let work = std::env::temp_dir().join(format!("raea-edit-own-{}", std::process::id()));
+ for f in ["src/scene.tsx", "ext/lib.ts", "r3f/input/vanilla.ts", "playable/host/main.tsx", "src/node_modules/pkg/index.js"] {
+ std::fs::create_dir_all(work.join(f).parent().unwrap()).unwrap();
+ std::fs::write(work.join(f), "").unwrap();
+ }
+ let r = EditResolve::new(Edit { dir: work.clone(), js: work.clone(), work: work.clone() });
+ let own = |f: &str| r.own_source(&work.join(f).to_string_lossy());
+ assert!(own("src/scene.tsx") && own("ext/lib.ts"));
+ assert!(!own("r3f/input/vanilla.ts") && !own("playable/host/main.tsx") && !own("src/node_modules/pkg/index.js"));
+ let _ = std::fs::remove_dir_all(&work);
+ }
}
/// The person's plugins (the raea app's Plugins page installs them here with npm): RAEA_PLUGINS, else ~/.raea/plugins.
@@ -126,6 +149,12 @@ async fn resolve_from(ctx: &PluginContext, spec: &str, dir: &Path) -> anyhow::Re
/// raea's own text (sdk/r3f/src/input/troika.ts, text.ts; docs/r3f.feature R3F13).
const STAND_INS: &[(&str, &str)] = &[("troika-three-text", "sdk/r3f/src/input/troika.ts")];
+/** three as the edit's own code imports it: three with its renderer a stand-in that draws nothing and records what it
+ * renders, for <Three program> (sdk/r3f/src/input/vanilla.ts, program.ts; docs/r3f.feature R3F19), at the work
+ * folder's copy of the r3f package so it imports the edit's three. Three's own modules, its addons and the SDK's
+ * copies (the playable host's real WebGLRenderer) import three as it is. */
+const THREE_STAND_INS: &[(&str, &str)] = &[("three", "r3f/input/vanilla.ts"), ("three/webgpu", "r3f/input/vanilla-webgpu.ts")];
+
impl Plugin for EditResolve {
fn name(&self) -> Cow<'static, str> {
Cow::Borrowed("raea:edit")
@@ -148,6 +177,12 @@ impl Plugin for EditResolve {
}
}
}
+ if let Some((_, own)) = THREE_STAND_INS.iter().find(|(p, _)| *p == spec) {
+ let f = e.work.join(own);
+ if self.own_source(imp) && f.is_file() {
+ return Ok(Some(HookResolveIdOutput::from_id(f.to_string_lossy().as_ref())));
+ }
+ }
// a package importing the SDK: the copy the edit's own sources use (the edit's own were pointed at it already)
if !self.from_edit(imp) {
if let Some(f) = self.sdk_copy(spec) {
diff --git a/sdk/r3f/tools/parity.sh b/sdk/r3f/tools/parity.sh
index a4778fee..f3caa87e 100755
--- a/sdk/r3f/tools/parity.sh
+++ b/sdk/r3f/tools/parity.sh
@@ -13,6 +13,8 @@ CASES=(in-basic:24 in-instanced:12 in-lights:0 in-things:0 in-catch:0 in-lettra:
# R3F_CASES="in-sky:0 in-shader:12": just those (a lane checking its own cases)
# the TSL cases (test/parity/tsl.tsx): a lit node material (R3F21)
CASES+=(in-tsl-lit:12)
+# a plain three.js program (test/parity/programs.ts) through <Three> (R3F19)
+CASES+=(in-vanilla:24)
[ -z "${R3F_CASES:-}" ] || read -ra CASES <<<"$R3F_CASES"
mkdir -p "$OUT"
for c in "${CASES[@]}"; do
diff --git a/sdk/r3f/tools/shoot.ts b/sdk/r3f/tools/shoot.ts
index f1f283c4..4f1a0ca5 100644
--- a/sdk/r3f/tools/shoot.ts
+++ b/sdk/r3f/tools/shoot.ts
@@ -8,7 +8,7 @@ const [baseArg, out, ...only] = process.argv.slice(2);
if (!baseArg || !out) throw new Error("usage: bun tools/shoot.ts BASE_URL|serve OUT_DIR [in-basic ...]");
const served = baseArg === "serve" ? await serve() : undefined;
const base = served?.url ?? baseArg;
-const cases = only.length ? only : ["in-basic", "in-lights", "in-things", "in-catch", "in-lettra", "in-lettrafx", "in-shader", "in-post", "in-drei", "in-approx", "in-glass", "in-sky", "out-basic", "out-tony", "out-lights", "out-bloom", "out-grade", "out-lens", "out-catch", "out-entities", "src-basic", "src-tony", "src-lights", "src-bloom", "src-grade", "src-lens", "src-catch", "src-entities"];
+const cases = only.length ? only : ["in-basic", "in-lights", "in-things", "in-catch", "in-lettra", "in-lettrafx", "in-shader", "in-post", "in-drei", "in-approx", "in-glass", "in-sky", "in-vanilla", "out-basic", "out-tony", "out-lights", "out-bloom", "out-grade", "out-lens", "out-catch", "out-entities", "src-basic", "src-tony", "src-lights", "src-bloom", "src-grade", "src-lens", "src-catch", "src-entities"];
let failed = 0;
for (const c of cases) {
// the direction, then the case (a case name may hold dashes: in-tsl-lit)