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;
if n > cutoff {
out.color = vec4(0.0, 0.0, 0.0, cutoff);
// out.color = vec4(0.0, 1.0, 0.0, cutoff);
discard;
}

@ -13,6 +13,7 @@ fn main() {
MaterialPlugin::<MyMat>::default(),
))
.add_systems(Startup, setup)
.add_systems(Update, set_scene)
.add_systems(Update, rotate)
.add_systems(Update, toggle_material.run_if(
|keys: Res<Input<KeyCode>>| -> bool {
@ -25,11 +26,11 @@ fn main() {
.run();
}
fn setup(mut commands: Commands, assets: Res<AssetServer>) {
commands.spawn(SceneBundle {
scene: assets.load("models/Martian Chess.glb#Scene0"),
..default()
});
#[derive(Component)]
struct Root;
fn setup(mut commands: Commands) {
commands.spawn((SceneBundle { ..default() }, Root));
commands.spawn(PointLightBundle {
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)]
struct MatExt {
#[uniform(100)]

@ -70,7 +70,7 @@ impl Plugin for Display3dPlugin {
.or_else(any_component_added::<PointLight>)
.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>()),
),
)
@ -579,9 +579,10 @@ fn set_piece_texture(
.get_handle::<Gltf>("display3d_models_assets_file")
.unwrap();
if let Some(gltf) = gltfs.get(assets_handle) {
children.iter_descendants(entity).for_each(|child| {
if let Ok((n, mut m)) = models.get_mut(child) {
debug!("Setting piece texture for {:?}", child);
// Why can't we just models.iter_many_mut(...).for_each(...)??
let mut stuff = models.iter_many_mut(children.iter_descendants(entity));
while let Some((n, mut m)) = stuff.fetch_next() {
debug!("Setting piece texture for {:?}", n);
match (*piece, side, n.as_str()) {
(Piece::Queen, Side::A, "Queen.0") => {
*m = gltf
@ -730,9 +731,8 @@ fn set_piece_texture(
_ => warn!("???"),
}
}
});
}
})
});
}
/// 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
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)
events: Query<Entity, Added<Piece>>,
query: Query<Entity, (With<Piece>, Added<Children>)>,
// Children of pieces are the actual meshes that need materials
children: Query<&Children>,
// All entities with materials are candidates for this procedure
query: Query<(Entity, &Handle<StandardMaterial>)>,
parents: Query<&Parent>,
// Used to create DissovleMaterial
standard_materials: Res<Assets<StandardMaterial>>,
// Used to create Handle<DissolveMaterial>
@ -1296,9 +1296,13 @@ fn setup_capture_piece(
// Cache dissolve textures that have already been created
mut cache: Local<HashMap<Handle<StandardMaterial>, Handle<DissolveMaterial>>>,
) {
events.iter().for_each(|entity| {
query
.iter_many(children.iter_descendants(entity))
events
.iter()
// 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)| {
let dis_handle = match cache.get(std_handle) {
// 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(),
};
// Insert this handle into the cache (may be redundant)
cache.insert(std_handle.clone(), dis_handle.clone());
// Add the dissolve handle as a Backup(T)
commands
.entity(child)
.insert(dis_handle.clone())
.insert(Backup(std_handle.clone()))
.remove::<Handle<StandardMaterial>>();
})
}
);
.insert(Backup(dis_handle.clone()));
});
}
/// When a piece is captured...
@ -1363,9 +1365,8 @@ fn capture_piece(
*prog = 1.0;
// store the kids we want to process
*kids = children
.iter_descendants(entity)
.filter_map(|e| object_standard_materials.get(e).ok())
*kids = object_standard_materials
.iter_many(children.iter_descendants(entity))
.map(|(child, std_handle, Backup(dis_handle))| {
// Swap standard and dissolve material
commands
@ -1418,6 +1419,10 @@ fn capture_piece(
"Play fade out animation {:?} {:?}",
delta, dissolve_material.extension.percentage
);
commands
.entity(_child)
.log_components();
}
);
}

Loading…
Cancel
Save