From 8e67f76d287c0289dfa2becf6353a51674452cb1 Mon Sep 17 00:00:00 2001 From: Mark Moissette Date: Fri, 24 Nov 2023 12:30:47 +0100 Subject: [PATCH] feat(bevy_gltf_blueprints): added ability to also load gltf files, not just glb (#55) * settable via a new field in the plugin configuration * updated examples & README * closes #55 --- Cargo.lock | 2 +- crates/bevy_gltf_blueprints/Cargo.toml | 2 +- crates/bevy_gltf_blueprints/README.md | 11 +++++--- crates/bevy_gltf_blueprints/src/lib.rs | 26 +++++++++++++++++++ .../src/spawn_from_blueprints.rs | 2 +- .../animation/src/core/mod.rs | 1 + .../basic/src/core/mod.rs | 2 ++ .../basic_xpbd_physics/src/core/mod.rs | 1 + .../multiple_levels/src/core/mod.rs | 1 + 9 files changed, 41 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 19d0691..0d1ea58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -755,7 +755,7 @@ dependencies = [ [[package]] name = "bevy_gltf_blueprints" -version = "0.3.0" +version = "0.3.1" dependencies = [ "bevy", "bevy_gltf_components 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/crates/bevy_gltf_blueprints/Cargo.toml b/crates/bevy_gltf_blueprints/Cargo.toml index 2a6597b..2ccec3b 100644 --- a/crates/bevy_gltf_blueprints/Cargo.toml +++ b/crates/bevy_gltf_blueprints/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bevy_gltf_blueprints" -version = "0.3.0" +version = "0.3.1" authors = ["Mark 'kaosat-dev' Moissette"] description = "Adds the ability to define Blueprints/Prefabs for [Bevy](https://bevyengine.org/) inside gltf files and spawn them in Bevy." homepage = "https://github.com/kaosat-dev/Blender_bevy_components_workflow" diff --git a/crates/bevy_gltf_blueprints/README.md b/crates/bevy_gltf_blueprints/README.md index baabd54..7fb7fe4 100644 --- a/crates/bevy_gltf_blueprints/README.md +++ b/crates/bevy_gltf_blueprints/README.md @@ -37,7 +37,7 @@ use bevy_gltf_blueprints::*; fn main() { App::new() .add_plugins(DefaultPlugins) - .add_plugin(BlueprintsPlugin) + .add_plugins(BlueprintsPlugin) .run(); } @@ -100,7 +100,8 @@ fn main() { .add_plugins(DefaultPlugins) .add_plugin( BlueprintsPlugin{ - library_folder: "advanced/models/library".into() // replace this with your blueprints library path , relative to the assets folder + 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 } ) .run(); @@ -180,7 +181,7 @@ Typically , the order of systems should be ***bevy_gltf_components (GltfComponentsSet::Injection)*** => ***bevy_gltf_blueprints (GltfBlueprintsSet::Spawn, GltfBlueprintsSet::AfterSpawn)*** => ***replace_proxies*** -see https://github.com/kaosat-dev/Blender_bevy_components_workflow/tree/main/examples/advanced for how to set it up correctly +see https://github.com/kaosat-dev/Blender_bevy_components_workflow/tree/main/examples/bevy_gltf_blueprints/basic for how to set it up correctly @@ -242,8 +243,10 @@ onward ## Examples -https://github.com/kaosat-dev/Blender_bevy_components_workflow/tree/main/examples/advanced +https://github.com/kaosat-dev/Blender_bevy_components_workflow/tree/main/examples/basic +https://github.com/kaosat-dev/Blender_bevy_components_workflow/tree/main/examples/basic_xpbd_physics https://github.com/kaosat-dev/Blender_bevy_components_workflow/tree/main/examples/animation +https://github.com/kaosat-dev/Blender_bevy_components_workflow/tree/main/examples/multiple_levels ## Compatible Bevy versions diff --git a/crates/bevy_gltf_blueprints/src/lib.rs b/crates/bevy_gltf_blueprints/src/lib.rs index 74f64cb..a085279 100644 --- a/crates/bevy_gltf_blueprints/src/lib.rs +++ b/crates/bevy_gltf_blueprints/src/lib.rs @@ -10,6 +10,7 @@ pub use animation::*; pub mod clone_entity; pub use clone_entity::*; +use core::fmt; use std::path::PathBuf; use bevy::prelude::*; @@ -40,11 +41,34 @@ impl Default for BluePrintBundle { #[derive(Clone, Resource)] pub(crate) struct BluePrintsConfig { + pub(crate) format: GltfFormat, pub(crate) library_folder: PathBuf, } +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Default)] +pub enum GltfFormat { + #[default] + GLB, + GLTF +} + +impl fmt::Display for GltfFormat { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + GltfFormat::GLB => { + write!(f, "glb", ) + } + GltfFormat::GLTF => { + write!(f, "gltf") + } + } + } +} + + #[derive(Debug, Clone)] pub struct BlueprintsPlugin { + pub format: GltfFormat, /// The base folder where library/blueprints assets are loaded from, relative to the executable. pub library_folder: PathBuf, } @@ -52,6 +76,7 @@ pub struct BlueprintsPlugin { impl Default for BlueprintsPlugin { fn default() -> Self { Self { + format: GltfFormat::GLB, library_folder: PathBuf::from("models/library"), } } @@ -64,6 +89,7 @@ impl Plugin for BlueprintsPlugin { .register_type::() .register_type::() .insert_resource(BluePrintsConfig { + format: self.format.clone(), library_folder: self.library_folder.clone(), }) .configure_sets( diff --git a/crates/bevy_gltf_blueprints/src/spawn_from_blueprints.rs b/crates/bevy_gltf_blueprints/src/spawn_from_blueprints.rs index c13ee20..7cb7200 100644 --- a/crates/bevy_gltf_blueprints/src/spawn_from_blueprints.rs +++ b/crates/bevy_gltf_blueprints/src/spawn_from_blueprints.rs @@ -53,7 +53,7 @@ pub(crate) fn spawn_from_blueprints( for (entity, name, blupeprint_name, transform) in spawn_placeholders.iter() { debug!("need to spawn {:?}", blupeprint_name.0); let what = &blupeprint_name.0; - let model_file_name = format!("{}.glb", &what); + let model_file_name = format!("{}.{}", &what, &blueprints_config.format); let model_path = Path::new(&blueprints_config.library_folder).join(Path::new(model_file_name.as_str())); diff --git a/examples/bevy_gltf_blueprints/animation/src/core/mod.rs b/examples/bevy_gltf_blueprints/animation/src/core/mod.rs index 0ef05fb..3271ad4 100644 --- a/examples/bevy_gltf_blueprints/animation/src/core/mod.rs +++ b/examples/bevy_gltf_blueprints/animation/src/core/mod.rs @@ -22,6 +22,7 @@ impl Plugin for CorePlugin { PhysicsPlugin, BlueprintsPlugin { library_folder: "models/library".into(), + ..Default::default() }, )); } diff --git a/examples/bevy_gltf_blueprints/basic/src/core/mod.rs b/examples/bevy_gltf_blueprints/basic/src/core/mod.rs index 6dc214f..080e18f 100644 --- a/examples/bevy_gltf_blueprints/basic/src/core/mod.rs +++ b/examples/bevy_gltf_blueprints/basic/src/core/mod.rs @@ -26,6 +26,8 @@ impl Plugin for CorePlugin { // SaveLoadPlugin, BlueprintsPlugin { library_folder: "models/library".into(), + format: GltfFormat::GLB, + ..Default::default() }, )); } diff --git a/examples/bevy_gltf_blueprints/basic_xpbd_physics/src/core/mod.rs b/examples/bevy_gltf_blueprints/basic_xpbd_physics/src/core/mod.rs index 0ef05fb..3271ad4 100644 --- a/examples/bevy_gltf_blueprints/basic_xpbd_physics/src/core/mod.rs +++ b/examples/bevy_gltf_blueprints/basic_xpbd_physics/src/core/mod.rs @@ -22,6 +22,7 @@ impl Plugin for CorePlugin { PhysicsPlugin, BlueprintsPlugin { library_folder: "models/library".into(), + ..Default::default() }, )); } diff --git a/examples/bevy_gltf_blueprints/multiple_levels/src/core/mod.rs b/examples/bevy_gltf_blueprints/multiple_levels/src/core/mod.rs index 6dc214f..e2771da 100644 --- a/examples/bevy_gltf_blueprints/multiple_levels/src/core/mod.rs +++ b/examples/bevy_gltf_blueprints/multiple_levels/src/core/mod.rs @@ -26,6 +26,7 @@ impl Plugin for CorePlugin { // SaveLoadPlugin, BlueprintsPlugin { library_folder: "models/library".into(), + ..Default::default() }, )); }