forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_gizmo.rs
More file actions
180 lines (168 loc) · 5.74 KB
/
Copy pathtransform_gizmo.rs
File metadata and controls
180 lines (168 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! Interactive transform gizmo example.
//!
//! Demonstrates translate, rotate, and scale gizmos with click-to-select.
//! - Click an object to select it (primary mouse button)
//! - **1** = Translate, **2** = Rotate, **3** = Scale
//! - **X** = Toggle World/Local space
use bevy::{
camera_controller::free_camera::{FreeCamera, FreeCameraPlugin},
gizmos::transform_gizmo::{
TransformGizmoCamera, TransformGizmoFocus, TransformGizmoMode, TransformGizmoPlugin,
TransformGizmoSettings, TransformGizmoSpace,
},
picking::{pointer::PointerButton, Pickable},
prelude::*,
};
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
FreeCameraPlugin,
MeshPickingPlugin,
TransformGizmoPlugin,
))
.add_systems(Startup, setup)
.add_systems(Update, (gizmo_mode_keys, update_instructions))
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Instructions
commands.spawn((
Text::new(
"Click an object to select it\n1: Translate | 2: Rotate | 3: Scale | X: World/Local space",
),
Node {
position_type: PositionType::Absolute,
top: px(12),
left: px(12),
..default()
},
InstructionsText,
));
// Ground plane (not pickable)
commands.spawn((
Mesh3d(meshes.add(Plane3d::default().mesh().size(10.0, 10.0))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.3, 0.3))),
Pickable::IGNORE,
));
// Table: a parent body with a child part, demonstrating local vs world space.
// The parent cube is selected by default.
commands
.spawn((
Mesh3d(meshes.add(Cuboid::new(1.5, 0.15, 1.0))),
MeshMaterial3d(materials.add(Color::srgb(0.8, 0.3, 0.3))),
Transform::from_xyz(-2.0, 1.0, 0.0),
TransformGizmoFocus,
))
.observe(on_click_select)
.with_children(|parent| {
// Table leg (child)
parent.spawn((
Mesh3d(meshes.add(Cuboid::new(0.1, 0.85, 0.1))),
MeshMaterial3d(materials.add(Color::srgb(0.6, 0.2, 0.2))),
Transform::from_xyz(-0.6, -0.5, 0.4),
Pickable::IGNORE,
));
parent.spawn((
Mesh3d(meshes.add(Cuboid::new(0.1, 0.85, 0.1))),
MeshMaterial3d(materials.add(Color::srgb(0.6, 0.2, 0.2))),
Transform::from_xyz(0.6, -0.5, 0.4),
Pickable::IGNORE,
));
parent.spawn((
Mesh3d(meshes.add(Cuboid::new(0.1, 0.85, 0.1))),
MeshMaterial3d(materials.add(Color::srgb(0.6, 0.2, 0.2))),
Transform::from_xyz(-0.6, -0.5, -0.4),
Pickable::IGNORE,
));
parent.spawn((
Mesh3d(meshes.add(Cuboid::new(0.1, 0.85, 0.1))),
MeshMaterial3d(materials.add(Color::srgb(0.6, 0.2, 0.2))),
Transform::from_xyz(0.6, -0.5, -0.4),
Pickable::IGNORE,
));
});
// Standalone cube
commands
.spawn((
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.8, 0.3))),
Transform::from_xyz(2.0, 0.5, 0.0),
))
.observe(on_click_select);
// Light
commands.spawn((
DirectionalLight {
shadow_maps_enabled: true,
..default()
},
Transform::from_rotation(Quat::from_euler(EulerRot::XYZ, -0.8, 0.4, 0.0)),
));
// Camera
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 4.0, 8.0).looking_at(Vec3::ZERO, Vec3::Y),
FreeCamera::default(),
TransformGizmoCamera,
));
}
fn on_click_select(
click: On<Pointer<Click>>,
mut commands: Commands,
existing: Query<Entity, With<TransformGizmoFocus>>,
) {
if click.button != PointerButton::Primary {
return;
}
// Remove focus from all entities
for e in &existing {
commands.entity(e).remove::<TransformGizmoFocus>();
}
// Add focus to clicked entity
commands.entity(click.entity).insert(TransformGizmoFocus);
}
// Note: Using 1/2/3 instead of Blender's G/R/S because S conflicts with
// the FreeCameraPlugin's WASD movement controls.
fn gizmo_mode_keys(
keyboard: Res<ButtonInput<KeyCode>>,
mut settings: ResMut<TransformGizmoSettings>,
) {
if keyboard.just_pressed(KeyCode::Digit1) {
settings.mode = TransformGizmoMode::Translate;
}
if keyboard.just_pressed(KeyCode::Digit2) {
settings.mode = TransformGizmoMode::Rotate;
}
if keyboard.just_pressed(KeyCode::Digit3) {
settings.mode = TransformGizmoMode::Scale;
}
if keyboard.just_pressed(KeyCode::KeyX) {
settings.space = match settings.space {
TransformGizmoSpace::World => TransformGizmoSpace::Local,
TransformGizmoSpace::Local => TransformGizmoSpace::World,
};
}
}
#[derive(Component)]
struct InstructionsText;
fn update_instructions(
settings: Res<TransformGizmoSettings>,
mut text: Single<&mut Text, With<InstructionsText>>,
) {
let mode_str = match settings.mode {
TransformGizmoMode::Translate => "Translate",
TransformGizmoMode::Rotate => "Rotate",
TransformGizmoMode::Scale => "Scale",
};
let space_str = match settings.space {
TransformGizmoSpace::World => "World",
TransformGizmoSpace::Local => "Local",
};
text.0 = format!(
"Click an object to select it\n1: Translate | 2: Rotate | 3: Scale | X: World/Local space\nMode: {mode_str} | Space: {space_str}"
);
}