mirror of
https://github.com/bevyengine/bevy
synced 2024-12-19 01:23:09 +00:00
0e30b68b20
# Objective Closes #15545. `bevy_picking` supports UI and sprite picking, but not mesh picking. Being able to pick meshes would be extremely useful for various games, tools, and our own examples, as well as scene editors and inspectors. So, we need a mesh picking backend! Luckily, [`bevy_mod_picking`](https://github.com/aevyrie/bevy_mod_picking) (which `bevy_picking` is based on) by @aevyrie already has a [backend for it](74f0c3c0fb/backends/bevy_picking_raycast/src/lib.rs
) using [`bevy_mod_raycast`](https://github.com/aevyrie/bevy_mod_raycast). As a side product of adding mesh picking, we also get support for performing ray casts on meshes! ## Solution Upstream a large chunk of the immediate-mode ray casting functionality from `bevy_mod_raycast`, and add a mesh picking backend based on `bevy_mod_picking`. Huge thanks to @aevyrie who did all the hard work on these incredible crates! All meshes are pickable by default. Picking can be disabled for individual entities by adding `PickingBehavior::IGNORE`, like normal. Or, if you want mesh picking to be entirely opt-in, you can set `MeshPickingBackendSettings::require_markers` to `true` and add a `RayCastPickable` component to the desired camera and target entities. You can also use the new `MeshRayCast` system parameter to cast rays into the world manually: ```rust fn ray_cast_system(mut ray_cast: MeshRayCast, foo_query: Query<(), With<Foo>>) { let ray = Ray3d::new(Vec3::ZERO, Dir3::X); // Only ray cast against entities with the `Foo` component. let filter = |entity| foo_query.contains(entity); // Never early-exit. Note that you can change behavior per-entity. let early_exit_test = |_entity| false; // Ignore the visibility of entities. This allows ray casting hidden entities. let visibility = RayCastVisibility::Any; let settings = RayCastSettings::default() .with_filter(&filter) .with_early_exit_test(&early_exit_test) .with_visibility(visibility); // Cast the ray with the settings, returning a list of intersections. let hits = ray_cast.cast_ray(ray, &settings); } ``` This is largely a direct port, but I did make several changes to match our APIs better, remove things we don't need or that I think are unnecessary, and do some general improvements to code quality and documentation. ### Changes Relative to `bevy_mod_raycast` and `bevy_mod_picking` - Every `Raycast` and "raycast" has been renamed to `RayCast` and "ray cast" (similar reasoning as the "Naming" section in #15724) - `Raycast` system param has been renamed to `MeshRayCast` to avoid naming conflicts and to be explicit that it is not for colliders - `RaycastBackend` has been renamed to `MeshPickingBackend` - `RayCastVisibility` variants are now `Any`, `Visible`, and `VisibleInView` instead of `Ignore`, `MustBeVisible`, and `MustBeVisibleAndInView` - `NoBackfaceCulling` has been renamed to `RayCastBackfaces`, to avoid implying that it affects the rendering of backfaces for meshes (it doesn't) - `SimplifiedMesh` and `RayCastBackfaces` live near other ray casting API types, not in their own 10 LoC module - All intersection logic and types are in the same `intersections` module, not split across several modules - Some intersection types have been renamed to be clearer and more consistent - `IntersectionData` -> `RayMeshHit` - `RayHit` -> `RayTriangleHit` - General documentation and code quality improvements ### Removed / Not Ported - Removed unused ray helpers and types, like `PrimitiveIntersection` - Removed getters on intersection types, and made their properties public - There is no `2d` feature, and `Raycast::mesh_query` and `Raycast::mesh2d_query` have been merged into `MeshRayCast::mesh_query`, which handles both 2D and 3D - I assume this existed previously because `Mesh2dHandle` used to be in `bevy_sprite`. Now both the 2D and 3D mesh are in `bevy_render`. - There is no `debug` feature or ray debug rendering - There is no deferred API (`RaycastSource`) - There is no `CursorRayPlugin` (the picking backend handles this) ### Note for Reviewers In case it's helpful, the [first commit](281638ef10
) here is essentially a one-to-one port. The rest of the commits are primarily refactoring and cleaning things up in the ways listed earlier, as well as changes to the module structure. It may also be useful to compare the original [picking backend](74f0c3c0fb/backends/bevy_picking_raycast/src/lib.rs
) and [`bevy_mod_raycast`](https://github.com/aevyrie/bevy_mod_raycast) to this PR. Feel free to mention if there are any changes that I should revert or something I should not include in this PR. ## Testing I tested mesh picking and relevant components in some examples, for both 2D and 3D meshes, and added a new `mesh_picking` example. I also ~~stole~~ ported over the [ray-mesh intersection benchmark](dbc5ef32fe/benches/ray_mesh_intersection.rs
) from `bevy_mod_raycast`. --- ## Showcase Below is a version of the `2d_shapes` example modified to demonstrate 2D mesh picking. This is not included in this PR. https://github.com/user-attachments/assets/7742528c-8630-4c00-bacd-81576ac432bf And below is the new `mesh_picking` example: https://github.com/user-attachments/assets/b65c7a5a-fa3a-4c2d-8bbd-e7a2c772986e There is also a really cool new `mesh_ray_cast` example ported over from `bevy_mod_raycast`: https://github.com/user-attachments/assets/3c5eb6c0-bd94-4fb0-bec6-8a85668a06c9 --------- Co-authored-by: Aevyrie <aevyrie@gmail.com> Co-authored-by: Trent <2771466+tbillington@users.noreply.github.com> Co-authored-by: François Mockers <mockersf@gmail.com>
89 lines
2.1 KiB
TOML
89 lines
2.1 KiB
TOML
[package]
|
|
name = "benches"
|
|
edition = "2021"
|
|
description = "Benchmarks that test Bevy's performance"
|
|
publish = false
|
|
license = "MIT OR Apache-2.0"
|
|
|
|
[dev-dependencies]
|
|
glam = "0.29"
|
|
rand = "0.8"
|
|
rand_chacha = "0.3"
|
|
criterion = { version = "0.3", features = ["html_reports"] }
|
|
bevy_app = { path = "../crates/bevy_app" }
|
|
bevy_ecs = { path = "../crates/bevy_ecs", features = ["multi_threaded"] }
|
|
bevy_hierarchy = { path = "../crates/bevy_hierarchy" }
|
|
bevy_math = { path = "../crates/bevy_math" }
|
|
bevy_picking = { path = "../crates/bevy_picking", features = ["bevy_mesh"] }
|
|
bevy_reflect = { path = "../crates/bevy_reflect", features = ["functions"] }
|
|
bevy_render = { path = "../crates/bevy_render" }
|
|
bevy_tasks = { path = "../crates/bevy_tasks" }
|
|
bevy_utils = { path = "../crates/bevy_utils" }
|
|
|
|
# make bevy_render compile on linux. x11 vs wayland does not matter here as the benches do not actually use a window
|
|
[target.'cfg(target_os = "linux")'.dev-dependencies]
|
|
bevy_winit = { path = "../crates/bevy_winit", features = ["x11"] }
|
|
|
|
[profile.release]
|
|
opt-level = 3
|
|
lto = true
|
|
|
|
[[bench]]
|
|
name = "change_detection"
|
|
path = "benches/bevy_ecs/change_detection.rs"
|
|
harness = false
|
|
|
|
[[bench]]
|
|
name = "ecs"
|
|
path = "benches/bevy_ecs/benches.rs"
|
|
harness = false
|
|
|
|
[[bench]]
|
|
name = "ray_mesh_intersection"
|
|
path = "benches/bevy_picking/ray_mesh_intersection.rs"
|
|
harness = false
|
|
|
|
[[bench]]
|
|
name = "reflect_function"
|
|
path = "benches/bevy_reflect/function.rs"
|
|
harness = false
|
|
|
|
[[bench]]
|
|
name = "reflect_list"
|
|
path = "benches/bevy_reflect/list.rs"
|
|
harness = false
|
|
|
|
[[bench]]
|
|
name = "reflect_map"
|
|
path = "benches/bevy_reflect/map.rs"
|
|
harness = false
|
|
|
|
[[bench]]
|
|
name = "reflect_struct"
|
|
path = "benches/bevy_reflect/struct.rs"
|
|
harness = false
|
|
|
|
[[bench]]
|
|
name = "parse_reflect_path"
|
|
path = "benches/bevy_reflect/path.rs"
|
|
harness = false
|
|
|
|
[[bench]]
|
|
name = "iter"
|
|
path = "benches/bevy_tasks/iter.rs"
|
|
harness = false
|
|
|
|
[[bench]]
|
|
name = "bezier"
|
|
path = "benches/bevy_math/bezier.rs"
|
|
harness = false
|
|
|
|
[[bench]]
|
|
name = "torus"
|
|
path = "benches/bevy_render/torus.rs"
|
|
harness = false
|
|
|
|
[[bench]]
|
|
name = "entity_hash"
|
|
path = "benches/bevy_ecs/world/entity_hash.rs"
|
|
harness = false
|