mesh_picking/
mesh_picking.rs

1//! A simple 3D scene to demonstrate mesh picking.
2//!
3//! [`bevy::picking::backend`] provides an API for adding picking hit tests to any entity. To get
4//! started with picking 3d meshes, the [`MeshPickingPlugin`] is provided as a simple starting
5//! point, especially useful for debugging. For your game, you may want to use a 3d picking backend
6//! provided by your physics engine, or a picking shader, depending on your specific use case.
7//!
8//! [`bevy::picking`] allows you to compose backends together to make any entity on screen pickable
9//! with pointers, regardless of how that entity is rendered. For example, `bevy_ui` and
10//! `bevy_sprite` provide their own picking backends that can be enabled at the same time as this
11//! mesh picking backend. This makes it painless to deal with cases like the UI or sprites blocking
12//! meshes underneath them, or vice versa.
13//!
14//! If you want to build more complex interactions than afforded by the provided pointer events, you
15//! may want to use [`MeshRayCast`] or a full physics engine with raycasting capabilities.
16//!
17//! By default, the mesh picking plugin will raycast against all entities, which is especially
18//! useful for debugging. If you want mesh picking to be opt-in, you can set
19//! [`MeshPickingSettings::require_markers`] to `true` and add a [`Pickable`] component to the
20//! desired camera and target entities.
21
22use std::f32::consts::PI;
23
24use bevy::{color::palettes::tailwind::*, picking::pointer::PointerInteraction, prelude::*};
25
26fn main() {
27    App::new()
28        // MeshPickingPlugin is not a default plugin
29        .add_plugins((DefaultPlugins, MeshPickingPlugin))
30        .add_systems(Startup, setup_scene)
31        .add_systems(Update, (draw_mesh_intersections, rotate))
32        .run();
33}
34
35/// A marker component for our shapes so we can query them separately from the ground plane.
36#[derive(Component)]
37struct Shape;
38
39const SHAPES_X_EXTENT: f32 = 14.0;
40const EXTRUSION_X_EXTENT: f32 = 16.0;
41const Z_EXTENT: f32 = 5.0;
42
43fn setup_scene(
44    mut commands: Commands,
45    mut meshes: ResMut<Assets<Mesh>>,
46    mut materials: ResMut<Assets<StandardMaterial>>,
47) {
48    // Set up the materials.
49    let white_matl = materials.add(Color::WHITE);
50    let ground_matl = materials.add(Color::from(GRAY_300));
51    let hover_matl = materials.add(Color::from(CYAN_300));
52    let pressed_matl = materials.add(Color::from(YELLOW_300));
53
54    let shapes = [
55        meshes.add(Cuboid::default()),
56        meshes.add(Tetrahedron::default()),
57        meshes.add(Capsule3d::default()),
58        meshes.add(Torus::default()),
59        meshes.add(Cylinder::default()),
60        meshes.add(Cone::default()),
61        meshes.add(ConicalFrustum::default()),
62        meshes.add(Sphere::default().mesh().ico(5).unwrap()),
63        meshes.add(Sphere::default().mesh().uv(32, 18)),
64    ];
65
66    let extrusions = [
67        meshes.add(Extrusion::new(Rectangle::default(), 1.)),
68        meshes.add(Extrusion::new(Capsule2d::default(), 1.)),
69        meshes.add(Extrusion::new(Annulus::default(), 1.)),
70        meshes.add(Extrusion::new(Circle::default(), 1.)),
71        meshes.add(Extrusion::new(Ellipse::default(), 1.)),
72        meshes.add(Extrusion::new(RegularPolygon::default(), 1.)),
73        meshes.add(Extrusion::new(Triangle2d::default(), 1.)),
74    ];
75
76    let num_shapes = shapes.len();
77
78    // Spawn the shapes. The meshes will be pickable by default.
79    for (i, shape) in shapes.into_iter().enumerate() {
80        commands
81            .spawn((
82                Mesh3d(shape),
83                MeshMaterial3d(white_matl.clone()),
84                Transform::from_xyz(
85                    -SHAPES_X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * SHAPES_X_EXTENT,
86                    2.0,
87                    Z_EXTENT / 2.,
88                )
89                .with_rotation(Quat::from_rotation_x(-PI / 4.)),
90                Shape,
91            ))
92            .observe(update_material_on::<Pointer<Over>>(hover_matl.clone()))
93            .observe(update_material_on::<Pointer<Out>>(white_matl.clone()))
94            .observe(update_material_on::<Pointer<Press>>(pressed_matl.clone()))
95            .observe(update_material_on::<Pointer<Release>>(hover_matl.clone()))
96            .observe(rotate_on_drag);
97    }
98
99    let num_extrusions = extrusions.len();
100
101    for (i, shape) in extrusions.into_iter().enumerate() {
102        commands
103            .spawn((
104                Mesh3d(shape),
105                MeshMaterial3d(white_matl.clone()),
106                Transform::from_xyz(
107                    -EXTRUSION_X_EXTENT / 2.
108                        + i as f32 / (num_extrusions - 1) as f32 * EXTRUSION_X_EXTENT,
109                    2.0,
110                    -Z_EXTENT / 2.,
111                )
112                .with_rotation(Quat::from_rotation_x(-PI / 4.)),
113                Shape,
114            ))
115            .observe(update_material_on::<Pointer<Over>>(hover_matl.clone()))
116            .observe(update_material_on::<Pointer<Out>>(white_matl.clone()))
117            .observe(update_material_on::<Pointer<Press>>(pressed_matl.clone()))
118            .observe(update_material_on::<Pointer<Release>>(hover_matl.clone()))
119            .observe(rotate_on_drag);
120    }
121
122    // Ground
123    commands.spawn((
124        Mesh3d(meshes.add(Plane3d::default().mesh().size(50.0, 50.0).subdivisions(10))),
125        MeshMaterial3d(ground_matl.clone()),
126        Pickable::IGNORE, // Disable picking for the ground plane.
127    ));
128
129    // Light
130    commands.spawn((
131        PointLight {
132            shadows_enabled: true,
133            intensity: 10_000_000.,
134            range: 100.0,
135            shadow_depth_bias: 0.2,
136            ..default()
137        },
138        Transform::from_xyz(8.0, 16.0, 8.0),
139    ));
140
141    // Camera
142    commands.spawn((
143        Camera3d::default(),
144        Transform::from_xyz(0.0, 7., 14.0).looking_at(Vec3::new(0., 1., 0.), Vec3::Y),
145    ));
146
147    // Instructions
148    commands.spawn((
149        Text::new("Hover over the shapes to pick them\nDrag to rotate"),
150        Node {
151            position_type: PositionType::Absolute,
152            top: px(12),
153            left: px(12),
154            ..default()
155        },
156    ));
157}
158
159/// Returns an observer that updates the entity's material to the one specified.
160fn update_material_on<E: EntityEvent>(
161    new_material: Handle<StandardMaterial>,
162) -> impl Fn(On<E>, Query<&mut MeshMaterial3d<StandardMaterial>>) {
163    // An observer closure that captures `new_material`. We do this to avoid needing to write four
164    // versions of this observer, each triggered by a different event and with a different hardcoded
165    // material. Instead, the event type is a generic, and the material is passed in.
166    move |event, mut query| {
167        if let Ok(mut material) = query.get_mut(event.event_target()) {
168            material.0 = new_material.clone();
169        }
170    }
171}
172
173/// A system that draws hit indicators for every pointer.
174fn draw_mesh_intersections(pointers: Query<&PointerInteraction>, mut gizmos: Gizmos) {
175    for (point, normal) in pointers
176        .iter()
177        .filter_map(|interaction| interaction.get_nearest_hit())
178        .filter_map(|(_entity, hit)| hit.position.zip(hit.normal))
179    {
180        gizmos.sphere(point, 0.05, RED_500);
181        gizmos.arrow(point, point + normal.normalize() * 0.5, PINK_100);
182    }
183}
184
185/// A system that rotates all shapes.
186fn rotate(mut query: Query<&mut Transform, With<Shape>>, time: Res<Time>) {
187    for mut transform in &mut query {
188        transform.rotate_y(time.delta_secs() / 2.);
189    }
190}
191
192/// An observer to rotate an entity when it is dragged
193fn rotate_on_drag(drag: On<Pointer<Drag>>, mut transforms: Query<&mut Transform>) {
194    let mut transform = transforms.get_mut(drag.entity).unwrap();
195    transform.rotate_y(drag.delta.x * 0.02);
196    transform.rotate_x(drag.delta.y * 0.02);
197}