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.

R3F23: a ShaderMaterial's texture follows the render target an app drew this frame (R3F23c, part). raea-runtime-look's three_uniforms puts a material's textures that name render images in its keyed three:uniforms rows, and raea-sys-three-shader's patch binds the image each names to that texture's slot by name (ThreeShader keeps its textures' names in bind order), so a display material reading GPUComputationRenderer's current target reads the one drawn last

shinyobjectzae905b9fd185
diff --git a/runtime/look/src/lib.rs b/runtime/look/src/lib.rs
index 4e6f33c0..b7af2f32 100644
--- a/runtime/look/src/lib.rs
+++ b/runtime/look/src/lib.rs
@@ -440,12 +440,14 @@ pub fn material_fields(p: &mut Program, id: &str, its: &[Item]) {
keyed(p, "component", Some(id), "m", each.iter().map(|(f, v)| (*f, v)));
}
-/// A three.js shader's uniforms and time (a <ThreeShaderMaterial>'s, docs/r3f.feature R3F11), sent as keyed
+/// A three.js shader's uniforms, time and render-image textures (a <ThreeShaderMaterial>'s, docs/r3f.feature R3F11), sent as keyed
/// `three:uniforms` rows from each frame they change, which raea-sys-three-shader's `patch` writes into its slots.
pub fn three_uniforms(p: &mut Program, id: &str, its: &[Item]) {
let each: Vec<(usize, Value)> = its.iter().enumerate().filter_map(|(f, it)| {
let m = it.material.as_ref().filter(|m| &*m.kind == "ThreeShaderMaterial")?;
- let v = serde_json::json!({ "uniforms": m.props.get("uniforms").cloned().unwrap_or(Value::Null), "time": m.props.get("time").cloned().unwrap_or(Value::Null) });
+ // its textures that name render images: an app ping-ponging two targets reads another each frame (R3F23c)
+ let renders: Map<String, Value> = m.props.get("textures").and_then(Value::as_object).into_iter().flatten().filter(|(_, s)| s.as_str().is_some_and(|s| s.starts_with("render:"))).map(|(k, s)| (k.clone(), s.clone())).collect();
+ let v = serde_json::json!({ "uniforms": m.props.get("uniforms").cloned().unwrap_or(Value::Null), "time": m.props.get("time").cloned().unwrap_or(Value::Null), "textures": renders });
Some((f, serde_json::json!({ "type": "three:uniforms", "value": v })))
}).collect();
if each.is_empty() {
diff --git a/systems/three-shader/src/lib.rs b/systems/three-shader/src/lib.rs
index 61311395..4e107a16 100644
--- a/systems/three-shader/src/lib.rs
+++ b/systems/three-shader/src/lib.rs
@@ -85,6 +85,8 @@ pub struct ThreeShader {
/// whether its colour is what three's canvas shows (taken back through Bevy's tone mapping) or scene light
display: bool,
layout: Vec<Uniform>,
+ /// its textures' names, as tex0 to tex3 bind them (a frame's `three:uniforms` swaps one by name)
+ bound: Vec<String>,
}
/// What picks a three shader's pipeline.
@@ -295,18 +297,34 @@ pub fn build(world: &mut World, v: &Value, mut image: impl FnMut(&mut World, &Va
depth_test: o.get("depthTest").and_then(Value::as_bool).unwrap_or(true),
display,
layout,
+ bound: bound.clone(),
};
Ok((m, attrs))
}
/// A `three:uniforms` component row: the frame's uniform values ({uniforms: {name: value}, time: [t, delta, frame]})
-/// onto the part's three shader.
+/// onto the part's three shader, and the render images its textures name this frame (textures: {name: "render:..."}).
pub fn patch(world: &mut World, e: Entity, v: &Value) -> Result<(), String> {
// a part whose shader didn't build here is drawn in its colour (of_row warned once, naming why): its uniforms have
// nothing to move, and its frame is never refused for them (docs/r3f.feature R3F16)
let Some(h) = world.get::<MeshMaterial3d<ThreeShader>>(e).map(|m| m.0.clone()) else { return Ok(()) };
+ // a texture naming the render image an app drew this frame (R3F23c: two targets ping-ponged, the one drawn last)
+ let mut swap: Vec<(String, Handle<Image>)> = vec![];
+ for (k, s) in v.get("textures").and_then(Value::as_object).into_iter().flatten() {
+ swap.push((k.clone(), raea_sys_frame::RenderImages::image(world, s.as_str().unwrap_or_default(), None).map_err(|e| format!("three:uniforms: texture {k}: {e}"))?));
+ }
let mut mats = world.resource_mut::<Assets<ThreeShader>>();
let Some(mut m) = mats.get_mut(&h) else { return Err("three:uniforms: its material isn't loaded".into()) };
+ for (k, img) in swap {
+ let slot = match m.bound.iter().position(|b| *b == k) {
+ Some(0) => &mut m.tex0,
+ Some(1) => &mut m.tex1,
+ Some(2) => &mut m.tex2,
+ Some(3) => &mut m.tex3,
+ _ => continue,
+ };
+ *slot = Some(img);
+ }
let mut slots = m.slots.map(|s| s.to_array());
if let Some(u) = v.get("uniforms").and_then(Value::as_object) {
module::pack(&m.layout, u, &mut slots)?;