Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/picking/debug_picking.rs
6592 views
1
//! A simple scene to demonstrate picking events for UI and mesh entities,
2
//! Demonstrates how to change debug settings
3
4
use bevy::dev_tools::picking_debug::{DebugPickingMode, DebugPickingPlugin};
5
use bevy::prelude::*;
6
7
fn main() {
8
App::new()
9
.add_plugins(DefaultPlugins.set(bevy::log::LogPlugin {
10
filter: "bevy_dev_tools=trace".into(), // Show picking logs trace level and up
11
..default()
12
}))
13
.add_plugins((MeshPickingPlugin, DebugPickingPlugin))
14
.add_systems(Startup, setup_scene)
15
.insert_resource(DebugPickingMode::Normal)
16
// A system that cycles the debugging state when you press F3:
17
.add_systems(
18
PreUpdate,
19
(|mut mode: ResMut<DebugPickingMode>| {
20
*mode = match *mode {
21
DebugPickingMode::Disabled => DebugPickingMode::Normal,
22
DebugPickingMode::Normal => DebugPickingMode::Noisy,
23
DebugPickingMode::Noisy => DebugPickingMode::Disabled,
24
}
25
})
26
.distributive_run_if(bevy::input::common_conditions::input_just_pressed(
27
KeyCode::F3,
28
)),
29
)
30
.run();
31
}
32
33
fn setup_scene(
34
mut commands: Commands,
35
mut meshes: ResMut<Assets<Mesh>>,
36
mut materials: ResMut<Assets<StandardMaterial>>,
37
) {
38
commands
39
.spawn((
40
Text::new("Click Me to get a box\nDrag cubes to rotate\nPress F3 to cycle between picking debug levels"),
41
Node {
42
position_type: PositionType::Absolute,
43
top: percent(12),
44
left: percent(12),
45
..default()
46
},
47
))
48
.observe(on_click_spawn_cube)
49
.observe(
50
|out: On<Pointer<Out>>, mut texts: Query<&mut TextColor>| {
51
let mut text_color = texts.get_mut(out.entity()).unwrap();
52
text_color.0 = Color::WHITE;
53
},
54
)
55
.observe(
56
|over: On<Pointer<Over>>, mut texts: Query<&mut TextColor>| {
57
let mut color = texts.get_mut(over.entity()).unwrap();
58
color.0 = bevy::color::palettes::tailwind::CYAN_400.into();
59
},
60
);
61
62
// Base
63
commands.spawn((
64
Name::new("Base"),
65
Mesh3d(meshes.add(Circle::new(4.0))),
66
MeshMaterial3d(materials.add(Color::WHITE)),
67
Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
68
));
69
70
// Light
71
commands.spawn((
72
PointLight {
73
shadows_enabled: true,
74
..default()
75
},
76
Transform::from_xyz(4.0, 8.0, 4.0),
77
));
78
79
// Camera
80
commands.spawn((
81
Camera3d::default(),
82
Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
83
));
84
}
85
86
fn on_click_spawn_cube(
87
_click: On<Pointer<Click>>,
88
mut commands: Commands,
89
mut meshes: ResMut<Assets<Mesh>>,
90
mut materials: ResMut<Assets<StandardMaterial>>,
91
mut num: Local<usize>,
92
) {
93
commands
94
.spawn((
95
Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
96
MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
97
Transform::from_xyz(0.0, 0.25 + 0.55 * *num as f32, 0.0),
98
))
99
// With the MeshPickingPlugin added, you can add pointer event observers to meshes:
100
.observe(on_drag_rotate);
101
*num += 1;
102
}
103
104
fn on_drag_rotate(drag: On<Pointer<Drag>>, mut transforms: Query<&mut Transform>) {
105
if let Ok(mut transform) = transforms.get_mut(drag.entity()) {
106
transform.rotate_y(drag.delta.x * 0.02);
107
transform.rotate_x(drag.delta.y * 0.02);
108
}
109
}
110
111