Add generic commands for upgrading a standard material into an extension material and downgrading it back
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 4d 8m
- Merged PRs (30d)
- 147
Description
## What problem does this solve or what need does it fill?
I came up with a really clean solution for my game that allows me to upgrade an entities standard material into an extension material and downgrade it back using entity commands.
It could be nice to add these commands right into Bevy to allow people to more easily upgrade their entities materials to ExtensionMaterials and downgrade them back in a generic way . Anyways its useful for me. ! Or maybe I can toss it in a crate if not.
## What solution would you like?
```
#[derive(Default)]
pub struct DowngradeToStandardMaterial{
_marker: PhantomData, // Helps the compiler know T is used
}
impl EntityCommand for DowngradeToStandardMaterial {
fn apply(self, mat_entity: Entity, world: &mut World) {
if let Some(extension_mat_handle) = world.get::< MeshMaterial3d< ExtendedMaterial > >(mat_entity){
let ext_material_assets = world.resource ::< Assets< ExtendedMaterial > >();
if let Some(_ext_mat) = ext_material_assets.get( extension_mat_handle ){
if let Some(mut cmd) = world.commands().get_entity(mat_entity) {
cmd
.remove:: >>() ;
}
}
}
}
}
#[derive(Default)]
pub struct UpgradeToExtensionMaterial {
_marker: PhantomData, // Helps the compiler know T is used
}
impl EntityCommand for UpgradeToExtensionMaterial {
fn apply(self, mat_entity: Entity, world: &mut World) {
let Some(original_mesh_material_component) = world.get::>( mat_entity ) else {
return
};
let original_mesh_material_handle = original_mesh_material_component .0.clone() ;
let standard_material_assets = world.resource::< Assets > ();
let Some(original_material) = standard_material_assets.get( &original_mesh_material_handle ) else {return};
let ext_mat = ExtendedMaterial {
base: original_material.clone(),
extension: T::default(),
};
let mut ext_material_assets = world.resource_mut::< Assets< ExtendedMaterial > >();
let ext_mat_handle = ext_material_assets.add(ext_mat.clone());
if let Some(mut cmd) = world.commands().get_entity(mat_entity) {
cmd.remove::>()
.insert(MeshMaterial3d(ext_mat_handle)) ;
info!( "refreshed linked ext materials !" );
}
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.