From 1ca762dedcdd6c87086560dd56ab3566b08243d0 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 17 Jan 2025 19:27:27 +0100 Subject: remove --z-up flag and fix inherited transform --- world/src/main.rs | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) (limited to 'world/src/main.rs') diff --git a/world/src/main.rs b/world/src/main.rs index c32b810..8535490 100644 --- a/world/src/main.rs +++ b/world/src/main.rs @@ -18,16 +18,16 @@ pub mod mesh; pub mod physics; -use anyhow::Result; +use anyhow::{Result, anyhow}; use clap::Parser; -use gltf::{Gltf, image::Source, import_buffers}; +use gltf::{Gltf, Node, image::Source, import_buffers, scene::Transform}; use humansize::BINARY; use image::{ImageReader, codecs::webp::WebPEncoder}; use log::{debug, info}; use mesh::import_mesh; use physics::import_physics; use rand::random; -use rayon::iter::{ParallelBridge, ParallelIterator}; +use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use std::{ borrow::Cow, collections::HashMap, @@ -41,7 +41,7 @@ use std::{ time::Duration, }; use weareshared::{ - Vec3A, + Affine3A, Vec3A, helper::ReadWrite, packets::{Data, Object, Packet, Resource}, resources::{EnvironmentPart, Image, LightPart, Prefab}, @@ -78,8 +78,6 @@ pub struct Args { #[arg(short, long)] scale: Option, #[arg(short, long)] - z_up: bool, - #[arg(short, long)] dry_run: bool, } @@ -92,20 +90,39 @@ fn main() -> Result<()> { let mut prefabs = Vec::new(); let texture_cache = Arc::new(Mutex::new(HashMap::new())); + let mut root_affine = Affine3A::IDENTITY; + root_affine.matrix3 *= args.scale.unwrap_or(1.); + root_affine.translation *= args.scale.unwrap_or(1.); + 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 root = gltf.default_scene().ok_or(anyhow!("no default scene"))?; + + let mut nodes = Vec::new(); + fn traverse<'a>(out: &mut Vec<(Affine3A, Node<'a>)>, node: Node<'a>, trans: Affine3A) { + let trans = trans * transform_to_affine(node.transform()); + for c in node.children() { + traverse(out, c, trans); + } + out.push((trans, node)); + } + + for node in root.nodes() { + traverse(&mut nodes, node, root_affine); + } + + let mut prefab = nodes + .par_iter() + .map(|(trans, node)| { let mut prefab = Prefab::default(); if let Some(mesh) = node.mesh() { import_mesh( mesh, + *trans, &buffers, &store, path_base, @@ -136,7 +153,7 @@ fn main() -> Result<()> { })?, )); } - import_physics(&gltf, &node, &mut prefab, &store, &buffers, &args)?; + import_physics(&gltf, *trans, &node, &mut prefab, &store, &buffers)?; Ok::<_, anyhow::Error>(prefab) }) .reduce( @@ -311,3 +328,13 @@ fn load_texture( } Ok(res) } + +pub fn transform_to_affine(trans: Transform) -> Affine3A { + let mat = trans.matrix(); + Affine3A::from_cols_array_2d(&[ + [mat[0][0], mat[0][1], mat[0][2]], + [mat[1][0], mat[1][1], mat[1][2]], + [mat[2][0], mat[2][1], mat[2][2]], + [mat[3][0], mat[3][1], mat[3][2]], + ]) +} -- cgit v1.2.3-70-g09d2