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;
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);

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

@ -15,30 +15,16 @@ fn main() {
))
.add_systems(Startup, setup)
.add_systems(Update, rotate)
.add_systems(Update, setup_material)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<MyMat>>,
assets: Res<AssetServer>,
) {
commands.spawn(MaterialMeshBundle {
mesh: meshes.add(
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 },
}),
commands.spawn(SceneBundle {
scene: assets.load("models/Martian Chess.glb#Scene0"),
..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(
mut query: Query<&mut Transform, With<Handle<Mesh>>>,
time: Res<Time>,
@ -76,8 +86,8 @@ 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);
// 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();

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

Loading…
Cancel
Save