made example closer to real game, but still no repro...

main
Elijah C. Voigt 2 years ago
parent 399c1c6208
commit d75658939d

@ -70,7 +70,7 @@ fn fragment(
var cutoff = dissolve.percentage; var cutoff = dissolve.percentage;
if n > cutoff { if n > cutoff {
out.color = vec4(0.0, 0.0, 0.0, cutoff); // out.color = vec4(0.0, 1.0, 0.0, cutoff);
discard; discard;
} }

@ -13,6 +13,7 @@ fn main() {
MaterialPlugin::<MyMat>::default(), MaterialPlugin::<MyMat>::default(),
)) ))
.add_systems(Startup, setup) .add_systems(Startup, setup)
.add_systems(Update, set_scene)
.add_systems(Update, rotate) .add_systems(Update, rotate)
.add_systems(Update, toggle_material.run_if( .add_systems(Update, toggle_material.run_if(
|keys: Res<Input<KeyCode>>| -> bool { |keys: Res<Input<KeyCode>>| -> bool {
@ -25,11 +26,11 @@ fn main() {
.run(); .run();
} }
fn setup(mut commands: Commands, assets: Res<AssetServer>) { #[derive(Component)]
commands.spawn(SceneBundle { struct Root;
scene: assets.load("models/Martian Chess.glb#Scene0"),
..default() fn setup(mut commands: Commands) {
}); commands.spawn((SceneBundle { ..default() }, Root));
commands.spawn(PointLightBundle { commands.spawn(PointLightBundle {
point_light: PointLight { point_light: PointLight {
@ -46,6 +47,15 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
}); });
} }
fn set_scene(
mut events: Query<&mut Handle<Scene>, (With<Root>, Added<Root>)>,
assets: Res<AssetServer>,
) {
events.iter_mut().for_each(|mut scene| {
*scene = assets.load("models/Martian Chess.glb#Scene0");
});
}
#[derive(Asset, AsBindGroup, Reflect, Debug, Clone)] #[derive(Asset, AsBindGroup, Reflect, Debug, Clone)]
struct MatExt { struct MatExt {
#[uniform(100)] #[uniform(100)]

@ -70,7 +70,7 @@ impl Plugin for Display3dPlugin {
.or_else(any_component_added::<PointLight>) .or_else(any_component_added::<PointLight>)
.or_else(on_event::<AssetEvent<Tweaks>>()), .or_else(on_event::<AssetEvent<Tweaks>>()),
), ),
setup_capture_piece.run_if(any_component_added::<Piece>), setup_capture_piece.run_if(any_component_changed::<Handle<StandardMaterial>>),
capture_piece.run_if(any_with_component::<game::Captured>()), capture_piece.run_if(any_with_component::<game::Captured>()),
), ),
) )
@ -579,9 +579,10 @@ fn set_piece_texture(
.get_handle::<Gltf>("display3d_models_assets_file") .get_handle::<Gltf>("display3d_models_assets_file")
.unwrap(); .unwrap();
if let Some(gltf) = gltfs.get(assets_handle) { if let Some(gltf) = gltfs.get(assets_handle) {
children.iter_descendants(entity).for_each(|child| { // Why can't we just models.iter_many_mut(...).for_each(...)??
if let Ok((n, mut m)) = models.get_mut(child) { let mut stuff = models.iter_many_mut(children.iter_descendants(entity));
debug!("Setting piece texture for {:?}", child); while let Some((n, mut m)) = stuff.fetch_next() {
debug!("Setting piece texture for {:?}", n);
match (*piece, side, n.as_str()) { match (*piece, side, n.as_str()) {
(Piece::Queen, Side::A, "Queen.0") => { (Piece::Queen, Side::A, "Queen.0") => {
*m = gltf *m = gltf
@ -730,9 +731,8 @@ fn set_piece_texture(
_ => warn!("???"), _ => warn!("???"),
} }
} }
});
} }
}) });
} }
/// Select tiles and pieces in 3d /// Select tiles and pieces in 3d
@ -1281,12 +1281,12 @@ struct Backup<T: Component>(T);
/// Sets up all pieces to have an associated "dissolve" material ready for capture /// Sets up all pieces to have an associated "dissolve" material ready for capture
fn setup_capture_piece( fn setup_capture_piece(
// All entities with materials are candidates for this procedure
events: Query<(Entity, &Handle<StandardMaterial>), (Added<Handle<StandardMaterial>>, Changed<Handle<StandardMaterial>>)>,
// Only process newly created pieces (we do not delete pieces at runtime) // Only process newly created pieces (we do not delete pieces at runtime)
events: Query<Entity, Added<Piece>>, query: Query<Entity, (With<Piece>, Added<Children>)>,
// Children of pieces are the actual meshes that need materials // Children of pieces are the actual meshes that need materials
children: Query<&Children>, parents: Query<&Parent>,
// All entities with materials are candidates for this procedure
query: Query<(Entity, &Handle<StandardMaterial>)>,
// Used to create DissovleMaterial // Used to create DissovleMaterial
standard_materials: Res<Assets<StandardMaterial>>, standard_materials: Res<Assets<StandardMaterial>>,
// Used to create Handle<DissolveMaterial> // Used to create Handle<DissolveMaterial>
@ -1296,9 +1296,13 @@ fn setup_capture_piece(
// Cache dissolve textures that have already been created // Cache dissolve textures that have already been created
mut cache: Local<HashMap<Handle<StandardMaterial>, Handle<DissolveMaterial>>>, mut cache: Local<HashMap<Handle<StandardMaterial>, Handle<DissolveMaterial>>>,
) { ) {
events.iter().for_each(|entity| { events
query .iter()
.iter_many(children.iter_descendants(entity)) // Only process if this is a child of a piece
.filter(|(child, _)| {
query.iter_many(parents.iter_ancestors(*child)).count() > 0
})
// Handle this entity (mesh)
.for_each(|(child, std_handle)| { .for_each(|(child, std_handle)| {
let dis_handle = match cache.get(std_handle) { let dis_handle = match cache.get(std_handle) {
// We have not seen this material, so create a new dissolve material // We have not seen this material, so create a new dissolve material
@ -1319,16 +1323,14 @@ fn setup_capture_piece(
Some(dis_handle) => dis_handle.clone(), Some(dis_handle) => dis_handle.clone(),
}; };
// Insert this handle into the cache (may be redundant)
cache.insert(std_handle.clone(), dis_handle.clone()); cache.insert(std_handle.clone(), dis_handle.clone());
// Add the dissolve handle as a Backup(T)
commands commands
.entity(child) .entity(child)
.insert(dis_handle.clone()) .insert(Backup(dis_handle.clone()));
.insert(Backup(std_handle.clone())) });
.remove::<Handle<StandardMaterial>>();
})
}
);
} }
/// When a piece is captured... /// When a piece is captured...
@ -1363,9 +1365,8 @@ fn capture_piece(
*prog = 1.0; *prog = 1.0;
// store the kids we want to process // store the kids we want to process
*kids = children *kids = object_standard_materials
.iter_descendants(entity) .iter_many(children.iter_descendants(entity))
.filter_map(|e| object_standard_materials.get(e).ok())
.map(|(child, std_handle, Backup(dis_handle))| { .map(|(child, std_handle, Backup(dis_handle))| {
// Swap standard and dissolve material // Swap standard and dissolve material
commands commands
@ -1418,6 +1419,10 @@ fn capture_piece(
"Play fade out animation {:?} {:?}", "Play fade out animation {:?} {:?}",
delta, dissolve_material.extension.percentage delta, dissolve_material.extension.percentage
); );
commands
.entity(_child)
.log_components();
} }
); );
} }

Loading…
Cancel
Save