Example works, not sure why gray in game.

Going to move on to some smaller wins
main
Elijah C. Voigt 2 years ago
parent 52efde3756
commit 8689574e95

@ -63,7 +63,7 @@ fn fragment(
var out: FragmentOutput; var out: FragmentOutput;
out.color = apply_pbr_lighting(pbr_input); out.color = apply_pbr_lighting(pbr_input);
var pos = vec2(in.uv * 2.0); var pos = vec2(in.uv * 2.718);
var n = fbm(pos); var n = fbm(pos);

@ -70,6 +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);
discard; discard;
} }

@ -15,30 +15,16 @@ fn main() {
)) ))
.add_systems(Startup, setup) .add_systems(Startup, setup)
.add_systems(Update, rotate) .add_systems(Update, rotate)
.add_systems(Update, setup_material)
.run(); .run();
} }
fn setup( fn setup(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, assets: Res<AssetServer>,
mut materials: ResMut<Assets<MyMat>>,
) { ) {
commands.spawn(MaterialMeshBundle { commands.spawn(SceneBundle {
mesh: meshes.add( scene: assets.load("models/Martian Chess.glb#Scene0"),
Mesh::try_from(shape::Box {
..default()
}).unwrap(),
),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
material: materials.add(ExtendedMaterial {
base: StandardMaterial {
base_color: Color::RED,
opaque_render_method: OpaqueRendererMethod::Auto,
alpha_mode: AlphaMode::Mask(0.5),
..default()
},
extension: MatExt { cutoff: 0.5 },
}),
..default() ..default()
}); });
@ -69,6 +55,30 @@ impl MaterialExtension for MatExt {
} }
} }
fn setup_material(
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)| {
// 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).expect("Resolve material data").clone();
base.opaque_render_method = OpaqueRendererMethod::Auto;
base.alpha_mode = AlphaMode::Mask(0.5);
commands.entity(entity).insert(
materials.add(ExtendedMaterial {
base,
extension,
})
).remove::<Handle<StandardMaterial>>();
})
}
fn rotate( fn rotate(
mut query: Query<&mut Transform, With<Handle<Mesh>>>, mut query: Query<&mut Transform, With<Handle<Mesh>>>,
time: Res<Time>, time: Res<Time>,
@ -76,8 +86,8 @@ fn rotate(
) { ) {
query.iter_mut().for_each(|mut t| { query.iter_mut().for_each(|mut t| {
t.rotate_local_y(time.delta_seconds() / 2.0); t.rotate_local_y(time.delta_seconds() / 2.0);
t.rotate_local_z(time.delta_seconds() / 2.0); // t.rotate_local_z(time.delta_seconds() / 2.0);
t.rotate_local_x(time.delta_seconds() / 2.0); // t.rotate_local_x(time.delta_seconds() / 2.0);
}); });
materials.iter_mut().for_each(|(_id, m)| { materials.iter_mut().for_each(|(_id, m)| {
m.extension.cutoff = time.elapsed_seconds().sin().abs(); m.extension.cutoff = time.elapsed_seconds().sin().abs();

@ -1287,7 +1287,7 @@ fn capture_piece(
mut commands: Commands, mut commands: Commands,
time: Res<Time>, time: Res<Time>,
) { ) {
let duration: f32 = 2.0; let duration: f32 = 3.0;
match *state { match *state {
Some(s) => { Some(s) => {
@ -1374,9 +1374,12 @@ fn capture_piece(
commands.entity(entity).remove::<Handle<DissolveMaterial>>(); commands.entity(entity).remove::<Handle<DissolveMaterial>>();
// Re-add the original material // Re-add the original material
let orig = backup_material.get(entity).expect("Entity has original material"); if let Ok(Backup(orig)) = backup_material.get(entity) {
commands.entity(entity).insert(orig.0.clone()); commands.entity(entity).insert(orig.clone());
commands.entity(entity).remove::<Backup<Handle<StandardMaterial>>>(); commands.entity(entity).remove::<Backup<Handle<StandardMaterial>>>();
} else {
warn!("Entity {} does not have original material")
}
} }
}); });
} }

Loading…
Cancel
Save