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()
.add_plugins((
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
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()
}
))
@ -294,7 +291,6 @@ Ie for example without this option, 56 different blueprints using the same mater
you can configure this with the settings:
```rust
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

View File

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

View File

@ -37,21 +37,16 @@ pub(crate) struct BlueprintMaterialAssetsNotLoaded;
/// system that injects / replaces materials from material library
pub(crate) fn materials_inject(
blueprints_config: ResMut<BluePrintsConfig>,
ready_blueprints: Query<(Entity, &Children), (With<BlueprintInstanceReady>)>,
material_infos: Query<(Entity, &MaterialInfo, &Parent), Added<MaterialInfo>>,
material_infos: Query<(Entity, &MaterialInfo), Added<MaterialInfo>>,
asset_server: Res<AssetServer>,
mut commands: Commands,
) {
/*for(entity, children) in ready_blueprints.iter() {
println!("Blueprint ready !");
} */
for (entity, material_info, parent) in material_infos.iter() {
for (entity, material_info) in material_infos.iter() {
println!("Entity with material info {:?} {:?}", entity, material_info);
let parent_blueprint = ready_blueprints.get(parent.get());
println!("Parent blueprint {:?}", parent_blueprint)
/*if blueprints_config
let material_full_path = format!("{}#{}", material_info.path, material_info.name);
if blueprints_config
.material_library_cache
.contains_key(&material_full_path)
{
@ -64,12 +59,11 @@ pub(crate) fn materials_inject(
.entity(entity)
.insert(BlueprintMaterialAssetsLoaded);
} 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();
// FIXME: fix this stuff
let asset_infos: Vec<AssetLoadTracker> = vec![AssetLoadTracker {
name: material_full_path,
name: material_info.name.clone(),
id: material_file_id,
loaded: false,
handle: material_file_handle.clone(),
@ -84,13 +78,11 @@ pub(crate) fn materials_inject(
})
.insert(BlueprintMaterialAssetsNotLoaded);
} */
}
}
}
// TODO, merge with check_for_loaded, make generic ?
// FIXME: fix this:
pub(crate) fn check_for_material_loaded(
mut blueprint_assets_to_load: Query<
(Entity, &mut AssetsToLoad),
@ -150,15 +142,7 @@ pub(crate) fn materials_inject2(
mut commands: Commands,
) {
for (material_info, children) in material_infos.iter() {
let model_file_name = format!(
"{}_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 material_full_path = format!("{}#{}", material_info.path, material_info.name);
let mut material_found: Option<&Handle<StandardMaterial>> = None;
if blueprints_config
@ -172,14 +156,14 @@ pub(crate) fn materials_inject2(
.expect("we should have the material available");
material_found = Some(material);
} 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
.get(model_handle.id())
.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
.named_materials
.get(material_name)
.get(&material_info.name)
.expect("this material should have been loaded");
blueprints_config
.material_library_cache
@ -193,8 +177,8 @@ pub(crate) fn materials_inject2(
if with_materials_and_meshes.contains(*child) {
debug!(
"injecting material {}, path: {:?}",
material_name,
materials_path.clone()
material_info.name,
material_info.path.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
);
let what = &blupeprint_name.0;
let model_file_name = format!("{}.{}", &what, &blueprints_config.format);
// info!("attempting to spawn {:?}", model_path);
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)
BlueprintsPlugin {
library_folder: "models/library".into(),
format: GltfFormat::GLB,
aabbs: true,
..Default::default()

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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