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.

scene lint, explain and export read a <BufferMesh>'s own triangles (three.js and R3F parts)

shinyobjectz2f377010ada7
diff --git a/apps/interchange/src/export.rs b/apps/interchange/src/export.rs
index 475c907c..00973a07 100644
--- a/apps/interchange/src/export.rs
+++ b/apps/interchange/src/export.rs
@@ -99,10 +99,22 @@ fn shape(expr: &str, out: &Path, meshes: &mut BTreeMap<String, String>) -> R<Val
Some(f) => f.clone(),
None => {
let f = format!("meshes/mesh{k}.glb");
- let m = raea_core_shape::solid(expr)?;
- raea_core_shape::export(&m, &out.join(&f).to_string_lossy(), "glb")?;
// and as an .obj, which Godot imports as a mesh of its own (a .glb comes in as a scene)
- raea_core_shape::export(&m, &out.join(f.replace(".glb", ".obj")).to_string_lossy(), "obj")?;
+ let (glb, obj) = (out.join(&f).to_string_lossy().into_owned(), out.join(f.replace(".glb", ".obj")).to_string_lossy().into_owned());
+ match expr.strip_prefix("buffer:") {
+ // a mesh given by its own vertices (a <BufferMesh>, a three.js geometry through raea/r3f): written as it
+ // is, with its normals and texture coordinates, since it needn't be a closed solid
+ Some(b) => {
+ let (t, uv) = raea_core_shape::buffer_tris(b)?;
+ raea_core_shape::export_tris(&t, uv.as_deref(), &glb, "glb")?;
+ raea_core_shape::export_tris(&t, uv.as_deref(), &obj, "obj")?;
+ }
+ None => {
+ let m = raea_core_shape::solid(expr)?;
+ raea_core_shape::export(&m, &glb, "glb")?;
+ raea_core_shape::export(&m, &obj, "obj")?;
+ }
+ }
meshes.insert(expr.to_string(), f.clone());
f
}
@@ -310,6 +322,10 @@ mod tests {
assert!(out.join(v["file"].as_str().unwrap()).is_file());
// the same mesh twice is one file
assert_eq!(shape("difference(box(1, 1, 1), sphere(0.6))", &out, &mut m).unwrap(), v);
+ // a buffer mesh (an open quad, as raea/r3f gives a three.js PlaneGeometry) is a mesh file too, not refused
+ let q = shape(r#"buffer:{"positions": [0,0,0, 1,0,0, 1,1,0, 0,1,0], "indices": [0,1,2, 0,2,3]}"#, &out, &mut m).unwrap();
+ assert_eq!(q["type"], "mesh");
+ assert!(out.join(q["file"].as_str().unwrap()).is_file() && out.join(q["obj"].as_str().unwrap()).is_file());
let _ = std::fs::remove_dir_all(&out);
}
}
diff --git a/core/shape/src/buffer.rs b/core/shape/src/buffer.rs
new file mode 100644
index 00000000..0e07e1db
--- /dev/null
+++ b/core/shape/src/buffer.rs
@@ -0,0 +1,163 @@
+//! A mesh given by its own vertices: a <BufferMesh>'s shape, "buffer:{positions, indices?, normals?, uvs?}" (what
+//! raea/r3f makes of every three.js geometry), read as the triangles a CSG expression's are, so contact,
+//! `raea scene lint|explain` and `raea scene export` read it instead of skipping it; and any triangles written out as a
+//! mesh file with their normals and texture coordinates. It needn't be closed, so it never goes through Manifold.
+
+use crate::Tris;
+use crate::mesh::{bounds, glb};
+
+/// A mesh from its own vertex buffers (<BufferMesh>, or a Mesh handle given as {positions, ...}): JSON
+/// {positions: [x, y, z, ...], indices?: [i, ...], normals?: [x, y, z, ...], uvs?: [u, v, ...]}, a triangle list
+/// (indexed, or each three positions a triangle), as flat triangles; without normals each face's own. Its texture
+/// coordinates when it gave them.
+pub fn buffer_tris(json: &str) -> Result<(Tris, Option<Vec<[f32; 2]>>), String> {
+ let v: serde_json::Value = serde_json::from_str(json).map_err(|e| format!("a buffer mesh isn't JSON: {e}"))?;
+ let nums = |k: &str| -> Result<Option<Vec<f32>>, String> {
+ match v.get(k) {
+ None | Some(serde_json::Value::Null) => Ok(None),
+ Some(serde_json::Value::Array(a)) => a.iter().map(|x| x.as_f64().map(|f| f as f32).ok_or_else(|| format!("a buffer mesh's {k}: numbers, got {x}"))).collect::<Result<Vec<_>, _>>().map(Some),
+ Some(x) => Err(format!("a buffer mesh's {k}: an array of numbers, got {x}")),
+ }
+ };
+ let pos = nums("positions")?.ok_or("a buffer mesh needs positions: [x, y, z, ...]")?;
+ if pos.len() % 3 != 0 || pos.is_empty() {
+ return Err(format!("a buffer mesh's positions: x, y, z for each vertex, got {} numbers", pos.len()));
+ }
+ let n = pos.len() / 3;
+ let idx: Vec<usize> = match nums("indices")? {
+ Some(i) => i.iter().map(|x| *x as usize).collect(),
+ None => (0..n).collect(),
+ };
+ if idx.len() % 3 != 0 || idx.iter().any(|&i| i >= n) {
+ return Err(format!("a buffer mesh's indices: three a triangle, each under {n} (the vertices), got {}", idx.len()));
+ }
+ let normals = nums("normals")?;
+ let uvs = nums("uvs")?;
+ if normals.as_ref().is_some_and(|x| x.len() != pos.len()) || uvs.as_ref().is_some_and(|u| u.len() != n * 2) {
+ return Err("a buffer mesh's normals are x, y, z and its uvs u, v for each vertex".into());
+ }
+ let p3 = |i: usize| [pos[i * 3], pos[i * 3 + 1], pos[i * 3 + 2]];
+ let mut t = Tris { positions: Vec::with_capacity(idx.len()), normals: Vec::with_capacity(idx.len()) };
+ let mut uv = uvs.as_ref().map(|_| Vec::with_capacity(idx.len()));
+ for tri in idx.chunks(3) {
+ let (a, b, c) = (p3(tri[0]), p3(tri[1]), p3(tri[2]));
+ let face = face_normal(a, b, c);
+ for &i in tri {
+ t.positions.push(p3(i));
+ t.normals.push(match &normals {
+ Some(nr) => [nr[i * 3], nr[i * 3 + 1], nr[i * 3 + 2]],
+ None => face,
+ });
+ if let (Some(out), Some(u)) = (uv.as_mut(), uvs.as_ref()) {
+ out.push([u[i * 2], u[i * 2 + 1]]);
+ }
+ }
+ }
+ Ok((t, uv))
+}
+
+/// A triangle's unit normal (counter-clockwise front), zero for a triangle of no area.
+fn face_normal(a: [f32; 3], b: [f32; 3], c: [f32; 3]) -> [f32; 3] {
+ let (u, w) = ([b[0] - a[0], b[1] - a[1], b[2] - a[2]], [c[0] - a[0], c[1] - a[1], c[2] - a[2]]);
+ let n = [u[1] * w[2] - u[2] * w[1], u[2] * w[0] - u[0] * w[2], u[0] * w[1] - u[1] * w[0]];
+ let l = (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]).sqrt();
+ if l > 1e-12 { [n[0] / l, n[1] / l, n[2] / l] } else { [0.0; 3] }
+}
+
+/// Triangles written as OBJ ("obj") or glTF binary ("glb"), three vertices a triangle, with their normals and, when
+/// given, texture coordinates (glTF's and raea's v down; OBJ's v up).
+pub fn export_tris(t: &Tris, uvs: Option<&[[f32; 2]]>, path: &str, format: &str) -> Result<usize, String> {
+ let uvs = uvs.filter(|u| u.len() == t.positions.len());
+ let bytes = match format {
+ "obj" => {
+ let mut s = String::from("# raea mesh\n");
+ for (p, n) in t.positions.iter().zip(&t.normals) {
+ s += &format!("v {} {} {}\nvn {} {} {}\n", p[0], p[1], p[2], n[0], n[1], n[2]);
+ }
+ for u in uvs.unwrap_or(&[]) {
+ s += &format!("vt {} {}\n", u[0], 1.0 - u[1]);
+ }
+ for k in (1..=t.positions.len()).step_by(3) {
+ let c = |i: usize| if uvs.is_some() { format!("{i}/{i}/{i}") } else { format!("{i}//{i}") };
+ s += &format!("f {} {} {}\n", c(k), c(k + 1), c(k + 2));
+ }
+ s.into_bytes()
+ }
+ "glb" => {
+ let mut bin = Vec::new();
+ let mut views = Vec::new();
+ let mut view = |bin: &mut Vec<u8>, data: &mut dyn Iterator<Item = f32>, target: u32| {
+ let at = bin.len();
+ data.for_each(|x| bin.extend_from_slice(&x.to_le_bytes()));
+ views.push(serde_json::json!({"buffer": 0, "byteOffset": at, "byteLength": bin.len() - at, "target": target}));
+ };
+ view(&mut bin, &mut t.positions.iter().flatten().copied(), 34962);
+ view(&mut bin, &mut t.normals.iter().flatten().copied(), 34962);
+ if let Some(u) = uvs {
+ view(&mut bin, &mut u.iter().flatten().copied(), 34962);
+ }
+ // sequential indices, so a reader that wants them (read_glb, Godot) has them
+ let at = bin.len();
+ (0..t.positions.len() as u32).for_each(|i| bin.extend_from_slice(&i.to_le_bytes()));
+ views.push(serde_json::json!({"buffer": 0, "byteOffset": at, "byteLength": bin.len() - at, "target": 34963}));
+ let (lo, hi) = bounds(t.positions.iter().copied());
+ let count = t.positions.len();
+ let mut accessors = vec![
+ serde_json::json!({"bufferView": 0, "componentType": 5126, "count": count, "type": "VEC3", "min": lo, "max": hi}),
+ serde_json::json!({"bufferView": 1, "componentType": 5126, "count": count, "type": "VEC3"}),
+ ];
+ let mut attributes = serde_json::json!({"POSITION": 0, "NORMAL": 1});
+ if uvs.is_some() {
+ accessors.push(serde_json::json!({"bufferView": 2, "componentType": 5126, "count": count, "type": "VEC2"}));
+ attributes["TEXCOORD_0"] = serde_json::json!(2);
+ }
+ accessors.push(serde_json::json!({"bufferView": views.len() - 1, "componentType": 5125, "count": count, "type": "SCALAR"}));
+ let json = serde_json::json!({
+ "asset": {"version": "2.0", "generator": "raea"},
+ "scene": 0, "scenes": [{"nodes": [0]}], "nodes": [{"mesh": 0}],
+ "meshes": [{"primitives": [{"attributes": attributes, "indices": accessors.len() - 1}]}],
+ "buffers": [{"byteLength": bin.len()}],
+ "bufferViews": views,
+ "accessors": accessors
+ });
+ glb(json, bin)?
+ }
+ f => return Err(format!("export: {f}? (obj, glb)")),
+ };
+ std::fs::write(path, &bytes).map_err(|e| format!("{path}: {e}"))?;
+ Ok(bytes.len())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ // a quad of two triangles, open (no Manifold solid), with uvs
+ const QUAD: &str = r#"{"positions": [0,0,0, 1,0,0, 1,1,0, 0,1,0], "indices": [0,1,2, 0,2,3], "uvs": [0,1, 1,1, 1,0, 0,0]}"#;
+
+ #[test]
+ fn a_buffer_mesh_reads_as_its_triangles() {
+ let (t, uv) = buffer_tris(QUAD).unwrap();
+ assert_eq!(t.positions.len(), 6);
+ assert_eq!(t.normals[0], [0.0, 0.0, 1.0]);
+ assert_eq!(uv.unwrap()[2], [1.0, 0.0]);
+ // and through the shape cache, as a CSG expression would be
+ assert_eq!(crate::cached(&format!("buffer:{QUAD}")).unwrap().positions.len(), 6);
+ assert!(buffer_tris(r#"{"positions": [0,0,0, 1,0,0, 1,1,0], "indices": [0,1,7]}"#).is_err());
+ }
+
+ #[test]
+ fn triangles_write_as_a_glb_and_an_obj_that_read_back() {
+ let (t, uv) = buffer_tris(QUAD).unwrap();
+ let dir = std::env::temp_dir().join(format!("raea-shape-buffer-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let (g, o) = (dir.join("q.glb"), dir.join("q.obj"));
+ export_tris(&t, uv.as_deref(), &g.to_string_lossy(), "glb").unwrap();
+ export_tris(&t, uv.as_deref(), &o.to_string_lossy(), "obj").unwrap();
+ let (v, i) = crate::read_glb(&g.to_string_lossy()).unwrap();
+ assert_eq!((v.len(), i.len()), (18, 6));
+ let (v, i) = crate::read_obj(&o.to_string_lossy()).unwrap();
+ assert_eq!((v.len(), i.len()), (18, 6));
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+}
diff --git a/core/shape/src/lib.rs b/core/shape/src/lib.rs
index e2e92b24..ccda243a 100644
--- a/core/shape/src/lib.rs
+++ b/core/shape/src/lib.rs
@@ -26,6 +26,7 @@
//! `minus(frame(), slide(disc(200, 960, 150), 680, 0, 0, 150))` is the clip with a hole moving across it.
+mod buffer;
mod build;
mod glyphs;
mod grown;
@@ -35,6 +36,7 @@ mod parse;
mod sphere;
pub use sphere::{SphereOf, cuboid_of, cylinder_of, plane_of, sphere_of};
+pub use buffer::{buffer_tris, export_tris};
pub use mesh::{export, from_mesh, read_glb, read_obj, to_mesh};
use std::collections::HashMap;
@@ -205,6 +207,12 @@ pub fn cached(expr: &str) -> Result<std::sync::Arc<Tris>, String> {
if let Some(t) = cache.lock().unwrap().get(expr) {
return Ok(t.clone());
}
+ // a buffer mesh's own triangles (buffer.rs): data already, so nothing is kept on disk for it
+ if let Some(b) = expr.strip_prefix("buffer:") {
+ let t = std::sync::Arc::new(buffer_tris(b)?.0);
+ cache.lock().unwrap().insert(expr.to_string(), t.clone());
+ return Ok(t);
+ }
// made by this build before, on disk (kept.rs): read back as it was written
let kept = kept::place(expr);
let t = match kept.as_deref().and_then(kept::read) {
diff --git a/core/shape/src/mesh.rs b/core/shape/src/mesh.rs
index fb670917..930f7dde 100644
--- a/core/shape/src/mesh.rs
+++ b/core/shape/src/mesh.rs
@@ -118,13 +118,7 @@ pub fn export(m: &Manifold, path: &str, format: &str) -> Result<usize, String> {
for i in &t {
bin.extend_from_slice(&i.to_le_bytes());
}
- let (mut lo, mut hi) = ([f32::MAX; 3], [f32::MIN; 3]);
- for c in v.chunks(3) {
- for k in 0..3 {
- lo[k] = lo[k].min(c[k]);
- hi[k] = hi[k].max(c[k]);
- }
- }
+ let (lo, hi) = bounds(v.chunks(3).map(|c| [c[0], c[1], c[2]]));
let json = serde_json::json!({
"asset": {"version": "2.0", "generator": "raea"},
"scene": 0, "scenes": [{"nodes": [0]}], "nodes": [{"mesh": 0}],
@@ -136,28 +130,44 @@ pub fn export(m: &Manifold, path: &str, format: &str) -> Result<usize, String> {
{"bufferView": 1, "componentType": 5125, "count": t.len(), "type": "SCALAR"}
]
});
- let mut js = serde_json::to_vec(&json).map_err(|e| e.to_string())?;
- while js.len() % 4 != 0 {
- js.push(b' ');
- }
- while bin.len() % 4 != 0 {
- bin.push(0);
- }
- let total = 12 + 8 + js.len() + 8 + bin.len();
- let mut out = Vec::with_capacity(total);
- out.extend_from_slice(b"glTF");
- out.extend_from_slice(&2u32.to_le_bytes());
- out.extend_from_slice(&(total as u32).to_le_bytes());
- out.extend_from_slice(&(js.len() as u32).to_le_bytes());
- out.extend_from_slice(b"JSON");
- out.extend_from_slice(&js);
- out.extend_from_slice(&(bin.len() as u32).to_le_bytes());
- out.extend_from_slice(b"BIN\0");
- out.extend_from_slice(&bin);
- out
+ glb(json, bin)?
}
f => return Err(format!("export: {f}? (obj, glb)")),
};
std::fs::write(path, &bytes).map_err(|e| format!("{path}: {e}"))?;
Ok(bytes.len())
}
+
+/// The least and greatest x, y and z of some points: a glTF POSITION accessor's min and max.
+pub(crate) fn bounds(points: impl Iterator<Item = [f32; 3]>) -> ([f32; 3], [f32; 3]) {
+ points.fold(([f32::MAX; 3], [f32::MIN; 3]), |(mut lo, mut hi), p| {
+ for k in 0..3 {
+ lo[k] = lo[k].min(p[k]);
+ hi[k] = hi[k].max(p[k]);
+ }
+ (lo, hi)
+ })
+}
+
+/// A glTF binary file from its JSON and its binary chunk, each padded to four bytes.
+pub(crate) fn glb(json: serde_json::Value, mut bin: Vec<u8>) -> Result<Vec<u8>, String> {
+ let mut js = serde_json::to_vec(&json).map_err(|e| e.to_string())?;
+ while js.len() % 4 != 0 {
+ js.push(b' ');
+ }
+ while bin.len() % 4 != 0 {
+ bin.push(0);
+ }
+ let total = 12 + 8 + js.len() + 8 + bin.len();
+ let mut out = Vec::with_capacity(total);
+ out.extend_from_slice(b"glTF");
+ out.extend_from_slice(&2u32.to_le_bytes());
+ out.extend_from_slice(&(total as u32).to_le_bytes());
+ out.extend_from_slice(&(js.len() as u32).to_le_bytes());
+ out.extend_from_slice(b"JSON");
+ out.extend_from_slice(&js);
+ out.extend_from_slice(&(bin.len() as u32).to_le_bytes());
+ out.extend_from_slice(b"BIN\0");
+ out.extend_from_slice(&bin);
+ Ok(out)
+}
diff --git a/systems/mesh/src/lib.rs b/systems/mesh/src/lib.rs
index 9bac560f..aac53b38 100644
--- a/systems/mesh/src/lib.rs
+++ b/systems/mesh/src/lib.rs
@@ -189,55 +189,9 @@ pub fn apply_json(f: &mut dyn bevy::reflect::PartialReflect, v: &serde_json::Val
}
}
-/// A mesh from its own vertex buffers (<BufferMesh>, or a Mesh handle given as {positions, ...}): JSON
-/// {positions: [x, y, z, ...], indices?: [i, ...], normals?: [x, y, z, ...], uvs?: [u, v, ...]}, a triangle list
-/// (indexed, or each three positions a triangle), as flat triangles; without normals each face's own. Its texture
-/// coordinates when it gave them.
-pub fn buffer_tris(json: &str) -> Result<(raea_core_shape::Tris, Option<Vec<[f32; 2]>>), String> {
- let v: serde_json::Value = serde_json::from_str(json).map_err(|e| format!("a buffer mesh isn't JSON: {e}"))?;
- let nums = |k: &str| -> Result<Option<Vec<f32>>, String> {
- match v.get(k) {
- None | Some(serde_json::Value::Null) => Ok(None),
- Some(serde_json::Value::Array(a)) => a.iter().map(|x| x.as_f64().map(|f| f as f32).ok_or_else(|| format!("a buffer mesh's {k}: numbers, got {x}"))).collect::<Result<Vec<_>, _>>().map(Some),
- Some(x) => Err(format!("a buffer mesh's {k}: an array of numbers, got {x}")),
- }
- };
- let pos = nums("positions")?.ok_or("a buffer mesh needs positions: [x, y, z, ...]")?;
- if pos.len() % 3 != 0 || pos.is_empty() {
- return Err(format!("a buffer mesh's positions: x, y, z for each vertex, got {} numbers", pos.len()));
- }
- let n = pos.len() / 3;
- let idx: Vec<usize> = match nums("indices")? {
- Some(i) => i.iter().map(|x| *x as usize).collect(),
- None => (0..n).collect(),
- };
- if idx.len() % 3 != 0 || idx.iter().any(|&i| i >= n) {
- return Err(format!("a buffer mesh's indices: three a triangle, each under {n} (the vertices), got {}", idx.len()));
- }
- let normals = nums("normals")?;
- let uvs = nums("uvs")?;
- if normals.as_ref().is_some_and(|x| x.len() != pos.len()) || uvs.as_ref().is_some_and(|u| u.len() != n * 2) {
- return Err("a buffer mesh's normals are x, y, z and its uvs u, v for each vertex".into());
- }
- let p3 = |i: usize| [pos[i * 3], pos[i * 3 + 1], pos[i * 3 + 2]];
- let mut t = raea_core_shape::Tris { positions: Vec::with_capacity(idx.len()), normals: Vec::with_capacity(idx.len()) };
- let mut uv = uvs.as_ref().map(|_| Vec::with_capacity(idx.len()));
- for tri in idx.chunks(3) {
- let (a, b, c) = (Vec3::from(p3(tri[0])), Vec3::from(p3(tri[1])), Vec3::from(p3(tri[2])));
- let face = (b - a).cross(c - a).normalize_or_zero().to_array();
- for &i in tri {
- t.positions.push(p3(i));
- t.normals.push(match &normals {
- Some(nr) => [nr[i * 3], nr[i * 3 + 1], nr[i * 3 + 2]],
- None => face,
- });
- if let (Some(out), Some(u)) = (uv.as_mut(), uvs.as_ref()) {
- out.push([u[i * 2], u[i * 2 + 1]]);
- }
- }
- }
- Ok((t, uv))
-}
+/// A mesh from its own vertex buffers (<BufferMesh>, or a Mesh handle given as {positions, ...}) as flat triangles and
+/// its texture coordinates: raea-core-shape's, so contact and `raea scene export` read a buffer mesh as this draws it.
+pub use raea_core_shape::buffer_tris;
/// A buffer mesh's triangle corners: the vertex each one is (its `indices`, or each of its `verts` vertices in turn).
pub fn buffer_corners(v: &serde_json::Value, verts: usize) -> Vec<usize> {