diff options
author | metamuffin <metamuffin@disroot.org> | 2025-01-17 19:27:27 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-01-17 19:27:27 +0100 |
commit | 1ca762dedcdd6c87086560dd56ab3566b08243d0 (patch) | |
tree | f8380d79dfaafe07a252fbf27f4a22c7a750078b /world/src | |
parent | 7ead3dfc316fb109f8f30eea1aa00eb01a4ce1a6 (diff) | |
download | weareserver-1ca762dedcdd6c87086560dd56ab3566b08243d0.tar weareserver-1ca762dedcdd6c87086560dd56ab3566b08243d0.tar.bz2 weareserver-1ca762dedcdd6c87086560dd56ab3566b08243d0.tar.zst |
remove --z-up flag and fix inherited transform
Diffstat (limited to 'world/src')
-rw-r--r-- | world/src/main.rs | 49 | ||||
-rw-r--r-- | world/src/mesh.rs | 27 | ||||
-rw-r--r-- | world/src/physics.rs | 6 |
3 files changed, 45 insertions, 37 deletions
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<f32>, #[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]], + ]) +} diff --git a/world/src/mesh.rs b/world/src/mesh.rs index 2bcf4c0..79e0edc 100644 --- a/world/src/mesh.rs +++ b/world/src/mesh.rs @@ -18,9 +18,9 @@ use crate::{Args, TextureCache, load_texture}; use anyhow::Result; use gltf::{Mesh, Node, buffer::Data}; use log::{debug, info}; -use std::{f32::consts::PI, path::Path}; +use std::path::Path; use weareshared::{ - Affine3A, Mat3A, Vec3A, + Affine3A, Vec3A, resources::{MeshPart, Prefab}, store::ResourceStore, vec2, vec3a, @@ -28,6 +28,7 @@ use weareshared::{ pub fn import_mesh( mesh: Mesh, + trans: Affine3A, buffers: &[Data], store: &ResourceStore, path_base: &Path, @@ -350,26 +351,6 @@ pub fn import_mesh( va_roughness: None, })?; - prefab - .mesh - .push((node_transform_to_affine(node, args), mesh)) + prefab.mesh.push((trans, mesh)) }) } - -pub fn node_transform_to_affine(node: &Node, args: &Args) -> Affine3A { - let mat = node.transform().matrix(); - let mut aff = 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]], - ]); - aff.matrix3 *= args.scale.unwrap_or(1.); - aff.translation *= args.scale.unwrap_or(1.); - if args.z_up { - let r = Mat3A::from_rotation_x(PI / 2.); - aff.matrix3 *= r; - aff.translation = r * aff.translation; - } - aff -} diff --git a/world/src/physics.rs b/world/src/physics.rs index b0c3ae6..37201af 100644 --- a/world/src/physics.rs +++ b/world/src/physics.rs @@ -14,11 +14,11 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use crate::{Args, mesh::node_transform_to_affine}; use anyhow::{Result, anyhow}; use gltf::{Gltf, Node, buffer::Data, json::Value}; use log::{debug, info}; use weareshared::{ + Affine3A, resources::{CollisionPart, Prefab}, store::ResourceStore, vec3a, @@ -26,11 +26,11 @@ use weareshared::{ pub fn import_physics( gltf: &Gltf, + trans: Affine3A, node: &Node, prefab: &mut Prefab, store: &ResourceStore, buffers: &[Data], - args: &Args, ) -> Result<()> { if let Some(physics) = node .extensions() @@ -73,7 +73,7 @@ pub fn import_physics( ); prefab.collision.push(( - node_transform_to_affine(&node, args), + trans, store.set(&CollisionPart { sh_mesh: Some((store.set(&index)?, store.set(&position)?)), ..Default::default() |