Refactor 3d_viewport_to_world example with let chains (#20071)

# Objective

Make use of let chains to reduce LoC where we previously used let else
to cut down on indentation. Best of both worlds.
This commit is contained in:
Tim 2025-07-10 04:37:56 +02:00 committed by GitHub
parent c9c8964857
commit cb64a4aa8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,30 +13,18 @@ fn main() {
fn draw_cursor( fn draw_cursor(
camera_query: Single<(&Camera, &GlobalTransform)>, camera_query: Single<(&Camera, &GlobalTransform)>,
ground: Single<&GlobalTransform, With<Ground>>, ground: Single<&GlobalTransform, With<Ground>>,
windows: Query<&Window>, window: Single<&Window>,
mut gizmos: Gizmos, mut gizmos: Gizmos,
) { ) {
let Ok(windows) = windows.single() else {
return;
};
let (camera, camera_transform) = *camera_query; let (camera, camera_transform) = *camera_query;
let Some(cursor_position) = windows.cursor_position() else { if let Some(cursor_position) = window.cursor_position()
return;
};
// Calculate a ray pointing from the camera into the world based on the cursor's position. // Calculate a ray pointing from the camera into the world based on the cursor's position.
let Ok(ray) = camera.viewport_to_world(camera_transform, cursor_position) else { && let Ok(ray) = camera.viewport_to_world(camera_transform, cursor_position)
return; // Calculate if and at what distance the ray is hitting the ground plane.
}; && let Some(distance) =
// Calculate if and where the ray is hitting the ground plane.
let Some(distance) =
ray.intersect_plane(ground.translation(), InfinitePlane3d::new(ground.up())) ray.intersect_plane(ground.translation(), InfinitePlane3d::new(ground.up()))
else { {
return;
};
let point = ray.get_point(distance); let point = ray.get_point(distance);
// Draw a circle just above the ground plane at that position. // Draw a circle just above the ground plane at that position.
@ -49,6 +37,7 @@ fn draw_cursor(
Color::WHITE, Color::WHITE,
); );
} }
}
#[derive(Component)] #[derive(Component)]
struct Ground; struct Ground;