From b81b8627aad9a823ab97b34fad92b4c512aec540 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Sat, 1 Apr 2023 00:20:57 +0200 Subject: [PATCH] gpu picking in the viewer picks up depth now --- .../src/ui/view_spatial/scene/picking.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/crates/re_viewer/src/ui/view_spatial/scene/picking.rs b/crates/re_viewer/src/ui/view_spatial/scene/picking.rs index 196eaaf827a17..84179c62fb81b 100644 --- a/crates/re_viewer/src/ui/view_spatial/scene/picking.rs +++ b/crates/re_viewer/src/ui/view_spatial/scene/picking.rs @@ -207,14 +207,19 @@ pub fn picking( if state.closest_opaque_pick.instance_path_hash == InstancePathHash::NONE { if let Some(gpu_picking_result) = gpu_picking_result { // TODO(andreas): Pick middle pixel for now. But we soon want to snap to the closest object using a bigger picking rect. - let rect = gpu_picking_result.rect; - let picked_id = gpu_picking_result.picking_id_data - [(rect.width() / 2 + (rect.height() / 2) * rect.width()) as usize]; + let picking_rect_position = gpu_picking_result.rect.extent / 2; + let picked_id = gpu_picking_result.picked_id(picking_rect_position); let picked_object = instance_path_hash_from_picking_layer_id(picked_id); - // TODO(andreas): We're lacking depth information! - state.closest_opaque_pick.instance_path_hash = picked_object; - state.closest_opaque_pick.used_gpu_picking = true; + // It is old data, the object might be gone by now! + if picked_object.is_some() { + // TODO(andreas): Once this is the primary path we should not awkwardly reconstruct the ray_t here. It's entirely correct either! + state.closest_opaque_pick.ray_t = gpu_picking_result + .picked_world_position(picking_rect_position) + .distance(context.ray_in_world.origin); + state.closest_opaque_pick.instance_path_hash = picked_object; + state.closest_opaque_pick.used_gpu_picking = true; + } } else { // It is possible that some frames we don't get a picking result and the frame after we get several. // We need to cache the last picking result and use it until we get a new one or the mouse leaves the screen.