Skip to content

Add support for custom picking data#23245

Open
m-edlund wants to merge 5 commits intobevyengine:mainfrom
m-edlund:generic-picking-hit-data
Open

Add support for custom picking data#23245
m-edlund wants to merge 5 commits intobevyengine:mainfrom
m-edlund:generic-picking-hit-data

Conversation

@m-edlund
Copy link
Contributor

@m-edlund m-edlund commented Mar 6, 2026

Objective

Solution

  • Adds an extra field to the HitData struct which can take any Data that implements HitDataExtra. This is stored in an Arc which can be downcast with a helper function in the system that consumes the hit.
  • In the original ticket I suggested using a generic, however this caused extensive code changes and complications down the line with the HoverMap and OverMap so I decided against it.

Testing

  • I added an example custom_hit_data to test the new feature
  • I tested all other picking examples to ensure they still work as expected
  • I tested the examples on Ubuntu
  • I additionally tested the custom_hit_data example in wasm

Showcase

Click to view showcase

Creating custom hits:

let picks: Vec<(Entity, HitData)> = ray_cast
            .cast_ray(ray, &settings)
            .iter()
            .map(|(entity, hit)| {
                let extra = TriangleHitInfo {
                    triangle_vertices: hit.triangle,
                };

                let hit_data = HitData::new_with_extra(
                    ray_id.camera,
                    hit.distance,
                    Some(hit.point),
                    Some(hit.normal),
                    extra,
                );

                (*entity, hit_data)
            })
            .collect();

Reading custom hits:

   for hits in pointer_hits.read() {
        for (_, hit) in &hits.picks {
            let Some(info) = hit.extra_as::<TriangleHitInfo>() else {
                continue;
            };
            let Some(vertices) = info.triangle_vertices else {
                continue;
            };

            // do something cool with your custom hit data
        }
    }

An example of what you can do with this

image

@kfc35 kfc35 added C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward A-Picking Pointing at and selecting objects of all sorts labels Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Picking Pointing at and selecting objects of all sorts C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend the information included in HitData

2 participants