feat(bevy_gltf_blueprints): added back materials handling ,removing clutter

* removed format, materials library path, & library path from blueprints, obsoleted through "blueprints_path"
 * related cleanups
 * added back materials library support but using the assets path
This commit is contained in:
kaosat.dev 2024-06-10 11:03:52 +02:00
parent ed0c85b66e
commit 2f7f3024d9
12 changed files with 16 additions and 59 deletions

View File

@ -101,11 +101,8 @@ fn main() {
App::new() App::new()
.add_plugins(( .add_plugins((
BlueprintsPlugin{ BlueprintsPlugin{
library_folder: "advanced/models/library".into() // replace this with your blueprints library path , relative to the assets folder,
format: GltfFormat::GLB,// optional, use either format: GltfFormat::GLB, or format: GltfFormat::GLTF, or ..Default::default() if you want to keep the default .glb extension, this sets what extensions/ gltf files will be looked for by the library
aabbs: true, // defaults to false, enable this to automatically calculate aabb for the scene/blueprint aabbs: true, // defaults to false, enable this to automatically calculate aabb for the scene/blueprint
material_library: true, // defaults to false, enable this to enable automatic injection of materials from material library files material_library: true, // defaults to false, enable this to enable automatic injection of materials from material library files
material_library_folder: "materials".into() //defaults to "materials" the folder to look for for the material files
..Default::default() ..Default::default()
} }
)) ))
@ -294,7 +291,6 @@ Ie for example without this option, 56 different blueprints using the same mater
you can configure this with the settings: you can configure this with the settings:
```rust ```rust
material_library: true // defaults to false, enable this to enable automatic injection of materials from material library files material_library: true // defaults to false, enable this to enable automatic injection of materials from material library files
material_library_folder: "materials".into() //defaults to "materials" the folder to look for for the material files
``` ```
> Important! you must take care of preloading your material librairy gltf files in advance, using for example ```bevy_asset_loader```since > Important! you must take care of preloading your material librairy gltf files in advance, using for example ```bevy_asset_loader```since

View File

@ -51,12 +51,10 @@ impl Default for BluePrintBundle {
#[derive(Clone, Resource)] #[derive(Clone, Resource)]
pub struct BluePrintsConfig { pub struct BluePrintsConfig {
pub(crate) format: GltfFormat, pub(crate) format: GltfFormat,
pub(crate) library_folder: PathBuf,
pub(crate) aabbs: bool, pub(crate) aabbs: bool,
pub(crate) aabb_cache: HashMap<String, Aabb>, // cache for aabbs pub(crate) aabb_cache: HashMap<String, Aabb>, // cache for aabbs
pub(crate) material_library: bool, pub(crate) material_library: bool,
pub(crate) material_library_folder: PathBuf,
pub(crate) material_library_cache: HashMap<String, Handle<StandardMaterial>>, pub(crate) material_library_cache: HashMap<String, Handle<StandardMaterial>>,
} }
@ -84,23 +82,18 @@ impl fmt::Display for GltfFormat {
/// Plugin for gltf blueprints /// Plugin for gltf blueprints
pub struct BlueprintsPlugin { pub struct BlueprintsPlugin {
pub format: GltfFormat, pub format: GltfFormat,
/// The base folder where library/blueprints assets are loaded from, relative to the executable.
pub library_folder: PathBuf,
/// Automatically generate aabbs for the blueprints root objects /// Automatically generate aabbs for the blueprints root objects
pub aabbs: bool, pub aabbs: bool,
/// ///
pub material_library: bool, pub material_library: bool,
pub material_library_folder: PathBuf,
} }
impl Default for BlueprintsPlugin { impl Default for BlueprintsPlugin {
fn default() -> Self { fn default() -> Self {
Self { Self {
format: GltfFormat::GLB, format: GltfFormat::GLB,
library_folder: PathBuf::from("models/library"),
aabbs: false, aabbs: false,
material_library: false, material_library: false
material_library_folder: PathBuf::from("materials"),
} }
} }
} }
@ -139,13 +132,11 @@ impl Plugin for BlueprintsPlugin {
.register_type::<HashMap<String, Vec<String>>>() .register_type::<HashMap<String, Vec<String>>>()
.insert_resource(BluePrintsConfig { .insert_resource(BluePrintsConfig {
format: self.format, format: self.format,
library_folder: self.library_folder.clone(),
aabbs: self.aabbs, aabbs: self.aabbs,
aabb_cache: HashMap::new(), aabb_cache: HashMap::new(),
material_library: self.material_library, material_library: self.material_library,
material_library_folder: self.material_library_folder.clone(),
material_library_cache: HashMap::new(), material_library_cache: HashMap::new(),
}) })
.configure_sets( .configure_sets(
@ -174,7 +165,7 @@ impl Plugin for BlueprintsPlugin {
apply_deferred, apply_deferred,
( (
materials_inject, materials_inject,
// check_for_material_loaded, check_for_material_loaded,
materials_inject2, materials_inject2,
) )
.chain() .chain()

View File

@ -37,21 +37,16 @@ pub(crate) struct BlueprintMaterialAssetsNotLoaded;
/// system that injects / replaces materials from material library /// system that injects / replaces materials from material library
pub(crate) fn materials_inject( pub(crate) fn materials_inject(
blueprints_config: ResMut<BluePrintsConfig>, blueprints_config: ResMut<BluePrintsConfig>,
material_infos: Query<(Entity, &MaterialInfo), Added<MaterialInfo>>,
ready_blueprints: Query<(Entity, &Children), (With<BlueprintInstanceReady>)>,
material_infos: Query<(Entity, &MaterialInfo, &Parent), Added<MaterialInfo>>,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
mut commands: Commands, mut commands: Commands,
) { ) {
/*for(entity, children) in ready_blueprints.iter() {
println!("Blueprint ready !"); for (entity, material_info) in material_infos.iter() {
} */
for (entity, material_info, parent) in material_infos.iter() {
println!("Entity with material info {:?} {:?}", entity, material_info); println!("Entity with material info {:?} {:?}", entity, material_info);
let parent_blueprint = ready_blueprints.get(parent.get()); let material_full_path = format!("{}#{}", material_info.path, material_info.name);
println!("Parent blueprint {:?}", parent_blueprint) if blueprints_config
/*if blueprints_config
.material_library_cache .material_library_cache
.contains_key(&material_full_path) .contains_key(&material_full_path)
{ {
@ -64,12 +59,11 @@ pub(crate) fn materials_inject(
.entity(entity) .entity(entity)
.insert(BlueprintMaterialAssetsLoaded); .insert(BlueprintMaterialAssetsLoaded);
} else { } else {
let material_file_handle: Handle<Gltf> = asset_server.load(materials_path.clone()); let material_file_handle = asset_server.load_untyped(&material_info.path.clone()); // : Handle<Gltf>
let material_file_id = material_file_handle.id(); let material_file_id = material_file_handle.id();
// FIXME: fix this stuff
let asset_infos: Vec<AssetLoadTracker> = vec![AssetLoadTracker { let asset_infos: Vec<AssetLoadTracker> = vec![AssetLoadTracker {
name: material_full_path, name: material_info.name.clone(),
id: material_file_id, id: material_file_id,
loaded: false, loaded: false,
handle: material_file_handle.clone(), handle: material_file_handle.clone(),
@ -84,13 +78,11 @@ pub(crate) fn materials_inject(
}) })
.insert(BlueprintMaterialAssetsNotLoaded); .insert(BlueprintMaterialAssetsNotLoaded);
} */ }
} }
} }
// TODO, merge with check_for_loaded, make generic ? // TODO, merge with check_for_loaded, make generic ?
// FIXME: fix this:
pub(crate) fn check_for_material_loaded( pub(crate) fn check_for_material_loaded(
mut blueprint_assets_to_load: Query< mut blueprint_assets_to_load: Query<
(Entity, &mut AssetsToLoad), (Entity, &mut AssetsToLoad),
@ -150,15 +142,7 @@ pub(crate) fn materials_inject2(
mut commands: Commands, mut commands: Commands,
) { ) {
for (material_info, children) in material_infos.iter() { for (material_info, children) in material_infos.iter() {
let model_file_name = format!( let material_full_path = format!("{}#{}", material_info.path, material_info.name);
"{}_materials_library.{}",
&material_info.path, &blueprints_config.format
);
let materials_path = Path::new(&blueprints_config.material_library_folder)
.join(Path::new(model_file_name.as_str()));
let material_name = &material_info.name;
let material_full_path = materials_path.to_str().unwrap().to_string() + "#" + material_name; // TODO: yikes, cleanup
let mut material_found: Option<&Handle<StandardMaterial>> = None; let mut material_found: Option<&Handle<StandardMaterial>> = None;
if blueprints_config if blueprints_config
@ -172,14 +156,14 @@ pub(crate) fn materials_inject2(
.expect("we should have the material available"); .expect("we should have the material available");
material_found = Some(material); material_found = Some(material);
} else { } else {
let model_handle: Handle<Gltf> = asset_server.load(materials_path.clone()); // FIXME: kinda weird now let model_handle: Handle<Gltf> = asset_server.load(material_info.path.clone()); // FIXME: kinda weird now
let mat_gltf = assets_gltf let mat_gltf = assets_gltf
.get(model_handle.id()) .get(model_handle.id())
.expect("material should have been preloaded"); .expect("material should have been preloaded");
if mat_gltf.named_materials.contains_key(material_name) { if mat_gltf.named_materials.contains_key(&material_info.name) {
let material = mat_gltf let material = mat_gltf
.named_materials .named_materials
.get(material_name) .get(&material_info.name)
.expect("this material should have been loaded"); .expect("this material should have been loaded");
blueprints_config blueprints_config
.material_library_cache .material_library_cache
@ -193,8 +177,8 @@ pub(crate) fn materials_inject2(
if with_materials_and_meshes.contains(*child) { if with_materials_and_meshes.contains(*child) {
debug!( debug!(
"injecting material {}, path: {:?}", "injecting material {}, path: {:?}",
material_name, material_info.name,
materials_path.clone() material_info.path.clone()
); );
commands.entity(*child).insert(material.clone()); commands.entity(*child).insert(material.clone());

View File

@ -264,9 +264,6 @@ pub(crate) fn spawn_from_blueprints2(
blupeprint_name.0, name, entity, original_parent blupeprint_name.0, name, entity, original_parent
); );
let what = &blupeprint_name.0;
let model_file_name = format!("{}.{}", &what, &blueprints_config.format);
// info!("attempting to spawn {:?}", model_path); // info!("attempting to spawn {:?}", model_path);
let model_handle: Handle<Gltf> = asset_server.load(blueprint_path.0.clone()); // FIXME: kinda weird now let model_handle: Handle<Gltf> = asset_server.load(blueprint_path.0.clone()); // FIXME: kinda weird now

View File

@ -191,7 +191,6 @@ fn main() {
}, },
// you need to configure the blueprints plugin as well (might be pre_configured in the future, but for now you need to do it manually) // you need to configure the blueprints plugin as well (might be pre_configured in the future, but for now you need to do it manually)
BlueprintsPlugin { BlueprintsPlugin {
library_folder: "models/library".into(),
format: GltfFormat::GLB, format: GltfFormat::GLB,
aabbs: true, aabbs: true,
..Default::default() ..Default::default()

View File

@ -5,8 +5,6 @@ pub struct CorePlugin;
impl Plugin for CorePlugin { impl Plugin for CorePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins((BlueprintsPlugin { app.add_plugins((BlueprintsPlugin {
library_folder: "models/library".into(),
format: GltfFormat::GLB,
aabbs: true, aabbs: true,
..Default::default() ..Default::default()
},)); },));

View File

@ -5,7 +5,6 @@ pub struct CorePlugin;
impl Plugin for CorePlugin { impl Plugin for CorePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins((BlueprintsPlugin { app.add_plugins((BlueprintsPlugin {
library_folder: "models/library".into(),
format: GltfFormat::GLB, format: GltfFormat::GLB,
aabbs: true, aabbs: true,
..Default::default() ..Default::default()

View File

@ -5,7 +5,6 @@ pub struct CorePlugin;
impl Plugin for CorePlugin { impl Plugin for CorePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins((BlueprintsPlugin { app.add_plugins((BlueprintsPlugin {
library_folder: "models/library".into(),
..Default::default() ..Default::default()
},)); },));
} }

View File

@ -5,7 +5,6 @@ pub struct CorePlugin;
impl Plugin for CorePlugin { impl Plugin for CorePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins((BlueprintsPlugin { app.add_plugins((BlueprintsPlugin {
library_folder: "models/library".into(),
material_library: true, material_library: true,
..Default::default() ..Default::default()
},)); },));

View File

@ -5,7 +5,6 @@ pub struct CorePlugin;
impl Plugin for CorePlugin { impl Plugin for CorePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins((BlueprintsPlugin { app.add_plugins((BlueprintsPlugin {
library_folder: "models/library".into(),
material_library: true, material_library: true,
..Default::default() ..Default::default()
},)); },));

View File

@ -35,8 +35,6 @@ impl Plugin for CorePlugin {
..Default::default() ..Default::default()
}, },
BlueprintsPlugin { BlueprintsPlugin {
library_folder: "models/library".into(),
format: GltfFormat::GLB,
aabbs: true, aabbs: true,
..Default::default() ..Default::default()
}, },

View File

@ -11,8 +11,6 @@ impl Plugin for CorePlugin {
..Default::default() ..Default::default()
}, },
BlueprintsPlugin { BlueprintsPlugin {
library_folder: "models/library".into(),
format: GltfFormat::GLB,
aabbs: true, aabbs: true,
..Default::default() ..Default::default()
}, },