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.

A solid read from a mesh file is kept under the file's length and modified time as well as its expression, so obj("path") or glb("path") written again at the same path is read again. The kept-shapes cache named a mesh by its expression and the build only, so a scan remeshed in place (gambit's Iron Hindenburg head, 11-17) kept drawing its first mesh, holes and all, until RAEA_SHAPE_CACHE=0. files_read finds every obj("...") and glb("...") in an expression, nested ones too, and folds each file's stamp into the key; an expression that reads no file keys as before. Test: the same path written again reads as another file.

shinyobjectzbb5cb7ffdd4b
diff --git a/core/shape/src/kept.rs b/core/shape/src/kept.rs
index 8a3b4c40..629a2141 100644
--- a/core/shape/src/kept.rs
+++ b/core/shape/src/kept.rs
@@ -46,13 +46,33 @@ pub fn place(expr: &str) -> Option<PathBuf> {
_ => caches()?.join("shapes"),
};
let (len, time) = this_build();
+ let files = files_read(expr);
let mut key: u64 = 0xcbf2_9ce4_8422_2325;
- for b in expr.as_bytes().iter().chain(&len.to_le_bytes()).chain(&time.to_le_bytes()) {
+ for b in expr.as_bytes().iter().chain(&len.to_le_bytes()).chain(&time.to_le_bytes()).chain(&files) {
key = (key ^ *b as u64).wrapping_mul(0x100_0000_01b3);
}
Some(root.join(format!("{key:016x}-{:x}.tris", expr.len())))
}
+/// The mesh files an expression reads (obj("..."), glb("..."), wherever they sit in it), each as its length and its
+/// modified time. They go into the key, so a file written again at the same path is read again rather than drawn as
+/// the mesh kept from the file it replaced (a scan remeshed in place kept drawing its first mesh, gambit, 11-17).
+pub(crate) fn files_read(expr: &str) -> Vec<u8> {
+ let mut out = Vec::new();
+ for tag in ["obj(\"", "glb(\""] {
+ for (i, _) in expr.match_indices(tag) {
+ let rest = &expr[i + tag.len()..];
+ let Some(end) = rest.find("\")") else { continue };
+ // the path as the expression quotes it ({:?}): a backslash is written doubled
+ let m = std::fs::metadata(rest[..end].replace("\\\\", "\\")).ok();
+ let t = m.as_ref().and_then(|m| m.modified().ok()).and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()).map(|d| d.as_nanos()).unwrap_or(0);
+ out.extend_from_slice(&m.map(|m| m.len()).unwrap_or(0).to_le_bytes());
+ out.extend_from_slice(&t.to_le_bytes());
+ }
+ }
+ out
+}
+
const MAGIC: &[u8; 8] = b"raeatri1";
/// The mesh kept at `p`: None when there is none or it isn't whole (a write that was cut short).
diff --git a/core/shape/src/lib.rs b/core/shape/src/lib.rs
index 9cfffc63..e2e92b24 100644
--- a/core/shape/src/lib.rs
+++ b/core/shape/src/lib.rs
@@ -242,6 +242,23 @@ mod tests {
std::fs::remove_dir_all(&dir).unwrap();
}
+ #[test]
+ fn a_mesh_file_written_again_is_kept_under_a_new_key() {
+ let dir = std::env::temp_dir().join(format!("raea-shape-files-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let p = dir.join("scan.obj");
+ std::fs::write(&p, "v 0 0 0\nv 1 0 0\nv 0 1 0\nv 0 0 1\nf 1 3 2\nf 1 2 4\nf 1 4 3\nf 2 3 4\n").unwrap();
+ let expr = format!("minus(obj({:?}), box(0.1, 0.1, 0.1))", p.to_string_lossy());
+ let first = kept::files_read(&expr);
+ assert_eq!(first.len(), 24, "one file read: its length and modified time");
+ // the same file written again with other content (a larger mesh) reads as another file
+ std::fs::write(&p, "v 0 0 0\nv 2 0 0\nv 0 2 0\nv 0 0 2\nf 1 3 2\nf 1 2 4\nf 1 4 3\nf 2 3 4\n# remeshed\n").unwrap();
+ assert_ne!(kept::files_read(&expr), first);
+ // an expression that reads no file adds nothing to its key
+ assert!(kept::files_read("box(1, 1, 1)").is_empty());
+ std::fs::remove_dir_all(&dir).unwrap();
+ }
+
#[test]
fn csg_volumes_are_right() {
let v = |e: &str| solid(e).unwrap().volume();