bevy/crates
Beni Bachmann 10daca0947 Return triangle index instead of vertex index (Fixes #18081) (#18647)
# Objective

- Fixes #18081
- Enable use-cases like getting UVs or texture colors for the hit point
(which are currently not possible due to this bug).

## Solution

- Return the triangle index instead of the first vertex index of the
triangle.

## Testing

Tested successfully with my project which does a raycast to get the UV
coordinates of the hit. My code:
```rust
fn get_uv(
    mesh: &Mesh,
    attribute: &MeshVertexAttribute,
    hit: &RayMeshHit,
    _gizmos: &mut Gizmos,
) -> Result<Vec2> {
    let (a, b, c) = get_indices(mesh, hit)?;

    let attrs = mesh
        .attribute(*attribute)
        .ok_or_eyre(format!("Attribute {:?} not found", &attribute))?;
    let all_uvs: &Vec<[f32; 2]> = match &attrs {
        VertexAttributeValues::Float32x2(positions) => positions,
        _ => bail!("Unexpected types in {:?}", Mesh::ATTRIBUTE_UV_0),
    };

    let bary = hit.barycentric_coords;

    Ok(Vec2::from_array(all_uvs[a]) * bary.x
        + Vec2::from_array(all_uvs[b]) * bary.y
        + Vec2::from_array(all_uvs[c]) * bary.z)
}

fn get_indices(mesh: &Mesh, hit: &RayMeshHit) -> Result<(usize, usize, usize)> {
    let i = hit
        .triangle_index
        .ok_or_eyre("Intersection Index not found")?;

    Ok(mesh.indices().map_or_else(
        || (i, i + 1, i + 2),
        |indices| match indices {
            Indices::U16(indices) => (
                indices[i * 3] as usize,
                indices[i * 3 + 1] as usize,
                indices[i * 3 + 2] as usize,
            ),
            Indices::U32(indices) => (
                indices[i * 3] as usize,
                indices[i * 3 + 1] as usize,
                indices[i * 3 + 2] as usize,
            ),
        },
    ))
}
```

PS: created a new PR because the old one was coming from and targeting
the wrong branches
2025-03-31 22:33:28 +02:00
..
bevy_a11y Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_animation Fix animation transitions affecting other entities (#18572) 2025-03-27 22:58:21 +01:00
bevy_app Fix misleading documentation of Main schedule (#18579) 2025-03-28 23:33:00 +01:00
bevy_asset Only send unused event when final handle is dropped. (#18641) 2025-03-31 22:33:27 +02:00
bevy_audio Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_color Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_core_pipeline reexport entity set collections in entity module (#18413) 2025-03-30 10:24:00 +02:00
bevy_derive Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_dev_tools Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_diagnostic Fix LogDiagnosticsPlugin log target typo (#18534) 2025-03-30 10:21:19 +02:00
bevy_dylib Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_ecs Use Display instead of Debug in the default error handler (#18629) 2025-03-31 22:33:28 +02:00
bevy_encase_derive Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_gilrs reexport entity set collections in entity module (#18413) 2025-03-30 10:24:00 +02:00
bevy_gizmos Rename EntityBorrow/TrustedEntityBorrow to ContainsEntity/EntityEquivalent (#18470) 2025-03-30 10:24:00 +02:00
bevy_gltf reexport entity set collections in entity module (#18413) 2025-03-30 10:24:00 +02:00
bevy_image bevy_image: derive TypePath when Reflect is not available (#18501) 2025-03-30 10:23:55 +02:00
bevy_input Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_input_focus reexport entity set collections in entity module (#18413) 2025-03-30 10:24:00 +02:00
bevy_internal Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_log Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_macro_utils Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_math Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_mesh Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_mikktspace Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_pbr Make bindings behind pbr_specular_textures flag consistent with other gated fields (#18645) 2025-03-31 22:33:28 +02:00
bevy_picking Return triangle index instead of vertex index (Fixes #18081) (#18647) 2025-03-31 22:33:28 +02:00
bevy_platform_support Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_ptr Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_reflect bevy_reflect: Fix TypePath string concatenation (#18609) 2025-03-30 10:21:19 +02:00
bevy_remote Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_render Parallelize bevy 0.16-rc bottlenecks (#18632) 2025-03-31 22:33:28 +02:00
bevy_scene reexport entity set collections in entity module (#18413) 2025-03-30 10:24:00 +02:00
bevy_sprite Parallelize bevy 0.16-rc bottlenecks (#18632) 2025-03-31 22:33:28 +02:00
bevy_state Fix misleading documentation of Main schedule (#18579) 2025-03-28 23:33:00 +01:00
bevy_tasks Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_text reexport entity set collections in entity module (#18413) 2025-03-30 10:24:00 +02:00
bevy_time Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_transform Transform Propagation Optimization: Static Subtree Marking (#18589) 2025-03-30 10:21:20 +02:00
bevy_ui Rename EntityBorrow/TrustedEntityBorrow to ContainsEntity/EntityEquivalent (#18470) 2025-03-30 10:24:00 +02:00
bevy_utils Release 0.16.0-rc.2 2025-03-26 19:18:20 +01:00
bevy_window Rename EntityBorrow/TrustedEntityBorrow to ContainsEntity/EntityEquivalent (#18470) 2025-03-30 10:24:00 +02:00
bevy_winit 0.16 Regression fix: re-expose the display handle via a wrapper resource (#18644) 2025-03-31 22:33:28 +02:00