diff options
author | metamuffin <metamuffin@disroot.org> | 2025-01-13 12:45:33 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-01-13 12:45:33 +0100 |
commit | f8f94eb194fe8a4db5c7120596f64d2a864fe823 (patch) | |
tree | ce2d528ccab9cde47f1126004b2aa40fc011bfea /world/src/main.rs | |
parent | 3330af8a3417ce411ecb1c7c23343cc28f261b2f (diff) | |
download | weareserver-f8f94eb194fe8a4db5c7120596f64d2a864fe823.tar weareserver-f8f94eb194fe8a4db5c7120596f64d2a864fe823.tar.bz2 weareserver-f8f94eb194fe8a4db5c7120596f64d2a864fe823.tar.zst |
multiple prefabs world cli
Diffstat (limited to 'world/src/main.rs')
-rw-r--r-- | world/src/main.rs | 153 |
1 files changed, 80 insertions, 73 deletions
diff --git a/world/src/main.rs b/world/src/main.rs index 7548d8f..f67febb 100644 --- a/world/src/main.rs +++ b/world/src/main.rs @@ -50,8 +50,8 @@ use weareshared::{ #[derive(Parser)] pub struct Args { address: SocketAddr, - /// Path to a glTF file, binary or json format - scene: PathBuf, + /// Path(s) to a glTF file, binary or json format + scene: Vec<PathBuf>, /// Send all resources to the server then quit #[arg(short, long)] push: bool, @@ -87,74 +87,78 @@ fn main() -> Result<()> { let store = ResourceStore::new_memory(); - let path_base = args.scene.parent().unwrap(); - let mut gltf = Gltf::from_reader_without_validation(File::open(&args.scene)?)?; - let blob = gltf.blob.take(); - let buffers = import_buffers(&gltf, Some(path_base), blob)?; + let mut prefabs = Vec::new(); - let mut prefab = gltf - .nodes() - .par_bridge() - .map(|node| { - let mut prefab = Prefab::default(); - if let Some(mesh) = node.mesh() { - import_mesh(mesh, &buffers, &store, path_base, &node, &mut prefab, &args)?; - } - let (position, _, _) = node.transform().decomposed(); - if let Some(light) = node.light() { - let name = node.name().map(|e| e.to_owned()); - if let Some(name) = &name { - info!("adding light {name:?}"); - } else { - info!("adding light"); + for scenepath in &args.scene { + let path_base = scenepath.parent().unwrap(); + let mut gltf = Gltf::from_reader_without_validation(File::open(scenepath)?)?; + let blob = gltf.blob.take(); + let buffers = import_buffers(&gltf, Some(path_base), blob)?; + + let mut prefab = gltf + .nodes() + .par_bridge() + .map(|node| { + let mut prefab = Prefab::default(); + if let Some(mesh) = node.mesh() { + import_mesh(mesh, &buffers, &store, path_base, &node, &mut prefab, &args)?; } - let emission = Some(Vec3A::from_array(light.color()) * light.intensity()); - if let Some(e) = emission { - debug!("emission is {e}"); + let (position, _, _) = node.transform().decomposed(); + if let Some(light) = node.light() { + let name = node.name().map(|e| e.to_owned()); + if let Some(name) = &name { + info!("adding light {name:?}"); + } else { + info!("adding light"); + } + let emission = Some(Vec3A::from_array(light.color()) * light.intensity()); + if let Some(e) = emission { + debug!("emission is {e}"); + } + prefab.light.push(( + Vec3A::from_array(position), + store.set(&LightPart { + emission, + name, + radius: None, + })?, + )); } - prefab.light.push(( - Vec3A::from_array(position), - store.set(&LightPart { - emission, - name, - radius: None, - })?, - )); - } - import_physics(&gltf, &node, &mut prefab, &store, &buffers, &args)?; - Ok::<_, anyhow::Error>(prefab) - }) - .reduce( - || Ok(Prefab::default()), - |a, b| { - let mut a = a?; - let b = b?; - a.collision.extend(b.collision); - a.mesh.extend(b.mesh); - a.light.extend(b.light); - a.environment = a.environment.or(b.environment); - a.name = a.name.or(b.name); - Ok(a) - }, - )?; + import_physics(&gltf, &node, &mut prefab, &store, &buffers, &args)?; + Ok::<_, anyhow::Error>(prefab) + }) + .reduce( + || Ok(Prefab::default()), + |a, b| { + let mut a = a?; + let b = b?; + a.collision.extend(b.collision); + a.mesh.extend(b.mesh); + a.light.extend(b.light); + a.environment = a.environment.or(b.environment); + a.name = a.name.or(b.name); + Ok(a) + }, + )?; + + if let Some(skybox) = &args.skybox { + let mut buf = Vec::new(); + File::open(skybox)?.read_to_end(&mut buf)?; + prefab.environment = Some(store.set(&EnvironmentPart { + skybox: Some(store.set(&Image(Cow::Owned(buf)))?), + ..Default::default() + })?); + } - prefab.name = args.name.clone().or(gltf - .default_scene() - .map(|n| n.name()) - .flatten() - .map(|n| n.to_owned())); + prefab.name = args.name.clone().or(gltf + .default_scene() + .map(|n| n.name()) + .flatten() + .map(|n| n.to_owned())); - if let Some(skybox) = args.skybox { - let mut buf = Vec::new(); - File::open(skybox)?.read_to_end(&mut buf)?; - prefab.environment = Some(store.set(&EnvironmentPart { - skybox: Some(store.set(&Image(Cow::Owned(buf)))?), - ..Default::default() - })?); + prefabs.push(store.set(&prefab)?); } - let pres = store.set(&prefab)?; - let mut size = 0; store.iter(|d| size += d.len()).unwrap(); info!( @@ -168,20 +172,23 @@ fn main() -> Result<()> { let mut sock = TcpStream::connect(args.address)?; Packet::Connect(random()).write(&mut sock)?; - Packet::AnnouncePrefab(pres.clone()).write(&mut sock)?; + for p in &prefabs { + Packet::AnnouncePrefab(p.clone()).write(&mut sock)?; + } sock.flush()?; - let ob = if args.add { - let ob = Object::new(); - Packet::Add(ob, pres.clone()).write(&mut sock)?; + let mut obs = Vec::new(); + if args.add { + for p in &prefabs { + let ob = Object::new(); + Packet::Add(ob, p.clone()).write(&mut sock)?; + obs.push(ob); + } sock.flush()?; - Some(ob) - } else { - None - }; + } if args.spin { - let ob = ob.clone().unwrap(); + let ob = obs[0].clone(); let mut sock2 = sock.try_clone().unwrap(); thread::spawn(move || { let mut x = 0.; @@ -215,7 +222,7 @@ fn main() -> Result<()> { } Packet::Add(ob_a, _) => { if args.clear { - if Some(ob_a) != ob { + if !obs.contains(&ob_a) { info!("removing object {ob_a}"); Packet::Remove(ob_a).write(&mut sock)?; sock.flush()?; |