Built upon bevygltfcomponents this crate adds the ability to define Blueprints/Prefabs for Bevy inside gltf files and spawn them in Bevy.
A blueprint is a set of overrideable components + a hierarchy: ie
* just a Gltf file with Gltf_extras specifying components
* a component called BlueprintName
Particularly useful when using Blender as an editor for the Bevy game engine, combined with the Blender plugin that does a lot of the work for you
Here's a minimal usage example:
```toml
[dependencies] bevy="0.11.2" bevygltfblueprints = { version = "0.1.1"}
```
```rust norun use bevy::prelude::*; use bevygltf_blueprints::*;
fn main() { App::new() .addplugins(DefaultPlugins) .addplugin(BlueprintsPlugin)
.run();
}
// not shown here: any other setup that is not specific to blueprints
fn spawnblueprint( mut commands: Commands, keycode: Res>, ){ if keycode.justpressed(KeyCode::S) { let newentity = commands.spawn(( BlueprintName("HealthPickup".tostring()), // mandatory !! SpawnHere, // mandatory !! TransformBundle::fromtransform(Transform::from_xyz(x, 2.0, y)), // VERY important !! // any other component you want to insert )); } } ```
Add the following to your [dependencies]
section in Cargo.toml
:
toml
bevy_gltf_blueprints = "0.1.1"
Or use cargo add
:
toml
cargo add bevy_gltf_blueprints
```rust norun use bevy::prelude::*; use bevygltf_blueprints::*;
fn main() { App::new() .addplugins(DefaultPlugins) .addplugin(BlueprintsPlugin)
.run();
}
```
you may want to configure your "library"/"blueprints" path: (defaults to assets/models/library
) so the plugin know where to look for the blueprint files
```rust norun use bevy::prelude::*; use bevygltf_blueprints::*;
fn main() { App::new() .addplugins(DefaultPlugins) .addplugin( BlueprintsPlugin{ library_folder: "advanced/models/library".into() // replace this with your blueprints library path , relative to the assets folder } ) .run(); }
```
You can spawn entities from blueprints like this: ```rust norun commands.spawn(( BlueprintName("HealthPickup".tostring()), // mandatory !! SpawnHere, // mandatory !! TransformBundle::fromtransform(Transform::from_xyz(x, 2.0, y)), // VERY important !! // any other component you want to insert ))
```
Once spawning of the actual entity is done, the spawned Blueprint will be gone/merged with the contents of Blueprint !
Important : you can add or override components present inside your Blueprint when spawning the BluePrint itself: ie
you can just add any additional components you need when spawning :
```rust norun commands.spawn(( BlueprintName("HealthPickup".tostring()), SpawnHere, TransformBundle::fromtransform(Transform::fromxyz(x, 2.0, y)), // from Rapier/bevyxpbd: this means the entity will also have a velocity component when inserted into the world Velocity { linvel: Vec3::new(velx, vely, vel_z), angvel: Vec3::new(0.0, 0.0, 0.0), }, ))
```
any component you specify when spawning the Blueprint that is also specified within the Blueprint will override that component in the final spawned entity
for example ```rust norun commands.spawn(( BlueprintName("HealthPickup".tostring()), SpawnHere, TransformBundle::fromtransform(Transform::fromxyz(x, 2.0, y)), HealthPowerUp(20)// if this is component is also present inside the "HealthPickup" blueprint, that one will be replaced with this component during spawning ))
```
There is also a bundle for convenience , which just has
* a BlueprintName
component
* a SpawnHere
component
* a TransformBundle
sub-bundle (so we know where to spawn)
the ordering of systems is very important !
For example to replace your proxy components (stand-in components when you cannot/ do not want to use real components in the gltf file) with actual ones, which should happen AFTER the Blueprint based spawning,
so bevy_gltf_blueprints
provides a SystemSet for that purpose:GltfBlueprintsSet
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/Blenderbevycomponents_workflow/tree/main/examples/advanced for how to set it up correctly
https://github.com/kaosat-dev/Blenderbevycomponents_workflow/tree/main/examples/advanced
This crate, all its code, contents & assets is Dual-licensed under either of