|
|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
use bevy::pbr::ExtendedMaterial;
|
|
|
|
|
use bevy::pbr::MaterialExtension;
|
|
|
|
|
use bevy::pbr::MaterialMeshBundle;
|
|
|
|
|
use bevy::pbr::OpaqueRendererMethod;
|
|
|
|
|
use bevy::prelude::*;
|
|
|
|
|
use bevy::render::render_resource::*;
|
|
|
|
|
@ -15,7 +14,14 @@ fn main() {
|
|
|
|
|
))
|
|
|
|
|
.add_systems(Startup, setup)
|
|
|
|
|
.add_systems(Update, rotate)
|
|
|
|
|
.add_systems(Update, setup_material)
|
|
|
|
|
.add_systems(Update, toggle_material.run_if(
|
|
|
|
|
|keys: Res<Input<KeyCode>>| -> bool {
|
|
|
|
|
keys.just_pressed(KeyCode::Space)
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.add_systems(Update, init_materials.run_if(|events: Query<Entity, Added<Transform>>| -> bool {
|
|
|
|
|
!events.is_empty()
|
|
|
|
|
}))
|
|
|
|
|
.run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -52,29 +58,80 @@ impl MaterialExtension for MatExt {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn setup_material(
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
|
struct Backup<T: Component>(T);
|
|
|
|
|
|
|
|
|
|
fn init_materials(
|
|
|
|
|
events: Query<(Entity, &Handle<StandardMaterial>), Added<Handle<StandardMaterial>>>,
|
|
|
|
|
standard_materials: Res<Assets<StandardMaterial>>,
|
|
|
|
|
mut materials: ResMut<Assets<MyMat>>,
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
) {
|
|
|
|
|
events.iter().for_each(|(entity, handle)| {
|
|
|
|
|
info!("initializing materials");
|
|
|
|
|
|
|
|
|
|
events.iter().for_each(|(entity, std_handle)| {
|
|
|
|
|
// Extension we will add to existing gltf-sourced materials
|
|
|
|
|
let extension = MatExt { cutoff: 1.0 };
|
|
|
|
|
// Base material we will extend for the duration of the dissolve effect
|
|
|
|
|
let mut base = standard_materials
|
|
|
|
|
.get(handle)
|
|
|
|
|
.get(std_handle)
|
|
|
|
|
.expect("Resolve material data")
|
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
|
|
base.opaque_render_method = OpaqueRendererMethod::Auto;
|
|
|
|
|
base.alpha_mode = AlphaMode::Mask(0.5);
|
|
|
|
|
|
|
|
|
|
let ext_handle = materials.add(ExtendedMaterial { base, extension });
|
|
|
|
|
|
|
|
|
|
commands
|
|
|
|
|
.entity(entity)
|
|
|
|
|
.insert(Backup(ext_handle.clone()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn toggle_material(
|
|
|
|
|
query: Query<Entity, Or<(With<Handle<StandardMaterial>>, With<Handle<MyMat>>)>>,
|
|
|
|
|
std_mat: Query<(&Handle<StandardMaterial>, Option<&Backup<Handle<MyMat>>>)>,
|
|
|
|
|
ext_mat: Query<(&Handle<MyMat>, Option<&Backup<Handle<StandardMaterial>>>)>,
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
) {
|
|
|
|
|
query.iter().for_each(|entity| {
|
|
|
|
|
// Entity has standard material, and had extended material
|
|
|
|
|
// Swap these materials
|
|
|
|
|
if let Ok(
|
|
|
|
|
(std_handle, Some(Backup(ext_handle)))
|
|
|
|
|
) = std_mat.get(entity) {
|
|
|
|
|
info!("Swapping standard material for extended material");
|
|
|
|
|
commands
|
|
|
|
|
.entity(entity)
|
|
|
|
|
.insert(materials.add(ExtendedMaterial { base, extension }))
|
|
|
|
|
.insert(ext_handle.clone())
|
|
|
|
|
.insert(Backup(std_handle.clone()))
|
|
|
|
|
.remove::<Backup<Handle<MyMat>>>()
|
|
|
|
|
.remove::<Handle<StandardMaterial>>();
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// In this branch we have the extended material assigned to the object
|
|
|
|
|
if let Ok(
|
|
|
|
|
(ext_handle, Some(Backup(std_handle)))
|
|
|
|
|
) = ext_mat.get(entity) {
|
|
|
|
|
// Entity has standard material, and had extended material // Swap these materials
|
|
|
|
|
info!("Swapping extended material for standard material");
|
|
|
|
|
|
|
|
|
|
commands
|
|
|
|
|
.entity(entity)
|
|
|
|
|
.insert(std_handle.clone())
|
|
|
|
|
.insert(Backup(ext_handle.clone()))
|
|
|
|
|
.remove::<Backup<Handle<StandardMaterial>>>()
|
|
|
|
|
.remove::<Handle<MyMat>>();
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
panic!("What is happening?")
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn rotate(
|
|
|
|
|
@ -84,8 +141,6 @@ fn rotate(
|
|
|
|
|
) {
|
|
|
|
|
query.iter_mut().for_each(|mut t| {
|
|
|
|
|
t.rotate_local_y(time.delta_seconds() / 2.0);
|
|
|
|
|
// t.rotate_local_z(time.delta_seconds() / 2.0);
|
|
|
|
|
// t.rotate_local_x(time.delta_seconds() / 2.0);
|
|
|
|
|
});
|
|
|
|
|
materials.iter_mut().for_each(|(_id, m)| {
|
|
|
|
|
m.extension.cutoff = time.elapsed_seconds().sin().abs();
|
|
|
|
|
|