From a021ed1ce8e9971d91252281845b3ea16503e38f Mon Sep 17 00:00:00 2001 From: IQuick 143 Date: Tue, 25 Mar 2025 19:27:35 +0100 Subject: [PATCH] Fix `mesh_picking` not working due to mixing vertex and triangle indices. (#18533) # Objective - #18495 ## Solution - The code in the PR #18232 accidentally used a vertex index as a triangle index, causing the wrong triangle to be used for normal computation and if the triangle went out of bounds, it would skip the ray-hit. - Don't do that. ## Testing - Run `cargo run --example mesh_picking` --- .../bevy_picking/src/mesh_picking/ray_cast/intersections.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/bevy_picking/src/mesh_picking/ray_cast/intersections.rs b/crates/bevy_picking/src/mesh_picking/ray_cast/intersections.rs index 7f4f54b5c0..b0f2750b53 100644 --- a/crates/bevy_picking/src/mesh_picking/ray_cast/intersections.rs +++ b/crates/bevy_picking/src/mesh_picking/ray_cast/intersections.rs @@ -83,9 +83,10 @@ pub fn ray_mesh_intersection + Clone + Copy>( indices .chunks_exact(3) + .enumerate() .fold( (f32::MAX, None), - |(closest_distance, closest_hit), triangle| { + |(closest_distance, closest_hit), (tri_idx, triangle)| { let [Ok(a), Ok(b), Ok(c)] = [ triangle[0].try_into(), triangle[1].try_into(), @@ -104,7 +105,7 @@ pub fn ray_mesh_intersection + Clone + Copy>( match ray_triangle_intersection(&ray, &tri_vertices, backface_culling) { Some(hit) if hit.distance >= 0. && hit.distance < closest_distance => { - (hit.distance, Some((a, hit))) + (hit.distance, Some((tri_idx, hit))) } _ => (closest_distance, closest_hit), }