From e50e848b584545c00ec6d96c2eafa5b6acafcbe9 Mon Sep 17 00:00:00 2001 From: andristarr Date: Mon, 19 Feb 2024 17:49:32 +0100 Subject: [PATCH] Gltf loader now shows which file is missing pre baked tangents (#11854) # Objective - Gltf loader now shows which file is missing pre baked tangents - Fixes #11831 ## Solution - The file name is shown in the error message - What changed as a result of this PR? ### Changed: - Gltf loader now shows which file is missing pre baked tangents - If this PR is a breaking change (relative to the last release of Bevy), describe how a user might need to migrate their code to support these changes - Simply adding new functionality is not a breaking change. - Fixing behavior that was definitely a bug, rather than a questionable design choice is not a breaking change. --- crates/bevy_gltf/src/loader.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index b9da3643e6..d4456c4594 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -8,7 +8,7 @@ use bevy_core_pipeline::prelude::Camera3dBundle; use bevy_ecs::entity::EntityHashMap; use bevy_ecs::{entity::Entity, world::World}; use bevy_hierarchy::{BuildWorldChildren, WorldChildBuilder}; -use bevy_log::{error, warn}; +use bevy_log::{error, info_span, warn}; use bevy_math::{Mat4, Vec3}; use bevy_pbr::{ AlphaMode, DirectionalLight, DirectionalLightBundle, PbrBundle, PointLight, PointLightBundle, @@ -46,6 +46,7 @@ use gltf::{ Material, Node, Primitive, Semantic, }; use serde::{Deserialize, Serialize}; +use std::io::Error; use std::{ collections::VecDeque, path::{Path, PathBuf}, @@ -181,6 +182,15 @@ async fn load_gltf<'a, 'b, 'c>( settings: &'b GltfLoaderSettings, ) -> Result { let gltf = gltf::Gltf::from_slice(bytes)?; + let file_name = load_context + .asset_path() + .path() + .to_str() + .ok_or(GltfError::Gltf(gltf::Error::Io(Error::new( + std::io::ErrorKind::InvalidInput, + "Gltf file name invalid", + ))))? + .to_string(); let buffer_data = load_buffers(&gltf, load_context).await?; let mut linear_textures = HashSet::default(); @@ -376,7 +386,6 @@ async fn load_gltf<'a, 'b, 'c>( } materials.push(handle); } - let mut meshes = vec![]; let mut named_meshes = HashMap::default(); let mut meshes_on_skinned_nodes = HashSet::default(); @@ -481,14 +490,19 @@ async fn load_gltf<'a, 'b, 'c>( && primitive.material().normal_texture().is_some() { bevy_log::debug!( - "Missing vertex tangents, computing them using the mikktspace algorithm" + "Missing vertex tangents for {}, computing them using the mikktspace algorithm. Consider using a tool such as Blender to pre-compute the tangents.", file_name ); - if let Err(err) = mesh.generate_tangents() { - warn!( + + let generate_tangents_span = info_span!("generate_tangents", name = file_name); + + generate_tangents_span.in_scope(|| { + if let Err(err) = mesh.generate_tangents() { + warn!( "Failed to generate vertex tangents using the mikktspace algorithm: {:?}", err ); - } + } + }); } let mesh = load_context.add_labeled_asset(primitive_label, mesh);