Skip to content

Commit

Permalink
clean up example
Browse files Browse the repository at this point in the history
  • Loading branch information
IceSentry committed Jun 13, 2023
1 parent 40eadfa commit 2ce013b
Showing 1 changed file with 4 additions and 34 deletions.
38 changes: 4 additions & 34 deletions examples/input/gpu_picking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() {
// Add the plugin
.add_plugin(GpuPickingPlugin)
.add_systems(Startup, setup)
.add_systems(Update, (mouse_picking, save_buffer_to_file, move_cube))
.add_systems(Update, (mouse_picking, move_cube))
.run();
}

Expand Down Expand Up @@ -88,7 +88,7 @@ fn setup(
GpuPickingMesh,
));

// opaque cube
// This cube will move from left to right. It shows that picking works correctly when things are moving.
commands.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
Expand Down Expand Up @@ -172,38 +172,6 @@ fn mouse_picking(
}
}

// TODO remove before merging, this is just for testing purposes
fn save_buffer_to_file(
keyboard_input: Res<Input<KeyCode>>,
gpu_picking_cameras: Query<(&GpuPickingCamera, &Camera)>,
) {
if keyboard_input.just_pressed(KeyCode::P) {
for (i, (gpu_picking_camera, camera)) in gpu_picking_cameras.iter().enumerate() {
let mut data = vec![];
data.push("P3".to_string());
let size = camera.logical_viewport_size().unwrap();
data.push(format!("{} {}", size.x, size.y));
data.push("255".to_string());

for y in 0..=size.y as u32 {
for x in 0..=size.x as u32 {
let entity = gpu_picking_camera.get_entity(UVec2::new(x, y));
if entity.is_some() {
data.push("255 0 0".to_string());
} else {
data.push("0 0 0".to_string());
}
}
}

std::fs::write(format!("entity_out_{i}.ppm"), data.join("\n"))
.expect("Unable to write file");

println!("buffer saved to entity_out_{i}.ppm");
}
}
}

// You can also use a custom material with it, you just need to make sure it correctly outputs the entity id
// See assets/shaders/gpu_picking_material.wgsl for more information
#[derive(AsBindGroup, TypeUuid, TypePath, Debug, Clone)]
Expand All @@ -222,6 +190,8 @@ impl Material for GpuPickingMaterial {
#[derive(Component)]
struct MoveCube;

// Moves a mesh from left to right
// Used to show that picking works even if things are moving
fn move_cube(
mut q: Query<&mut Transform, With<MoveCube>>,
time: Res<Time>,
Expand Down

0 comments on commit 2ce013b

Please sign in to comment.