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
//! Demonstrates all common configuration options,
//! and how to modify them at runtime
//!
//! Controls:
//! Orbit: Middle click
//! Pan: Shift + Middle click
//! Zoom: Mousewheel OR Right click + move mouse up/down
use bevy::prelude::*;
use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin, TouchControls};
use std::f32::consts::TAU;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(PanOrbitCameraPlugin)
.add_systems(Startup, setup)
.add_systems(Update, toggle_camera_controls_system)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Ground
commands.spawn((
Mesh3d(meshes.add(Plane3d::default().mesh().size(5.0, 5.0))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
));
// Cube
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
MeshMaterial3d(materials.add(Color::srgb(0.8, 0.7, 0.6))),
Transform::from_xyz(0.0, 0.5, 0.0),
));
// Light
commands.spawn((
PointLight {
shadows_enabled: true,
..default()
},
Transform::from_xyz(4.0, 8.0, 4.0),
));
// Camera
commands.spawn((
// Note we're setting the initial position below with yaw, pitch, and radius, hence
// we don't set transform on the camera.
PanOrbitCamera {
// Set focal point (what the camera should look at)
focus: Vec3::new(0.0, 1.0, 0.0),
// Set the starting position, relative to focus (overrides camera's transform).
yaw: Some(TAU / 8.0),
pitch: Some(TAU / 8.0),
radius: Some(5.0),
// Set limits on rotation and zoom
yaw_upper_limit: Some(TAU / 4.0),
yaw_lower_limit: Some(-TAU / 4.0),
pitch_upper_limit: Some(TAU / 3.0),
pitch_lower_limit: Some(-TAU / 3.0),
zoom_upper_limit: Some(5.0),
zoom_lower_limit: 1.0,
// Adjust sensitivity of controls
orbit_sensitivity: 1.5,
pan_sensitivity: 0.5,
zoom_sensitivity: 0.5,
// Allow the camera to go upside down
allow_upside_down: true,
// Change the controls (these match Blender)
button_orbit: MouseButton::Middle,
button_pan: MouseButton::Middle,
modifier_pan: Some(KeyCode::ShiftLeft),
// Also enable zooming by holding right click and moving the mouse
button_zoom: Some(MouseButton::Right),
// Optionally configure button zoom to use left-right mouse movement
// button_zoom_axis: ButtonZoomAxis::X,
// Optionally reverse the button-based zoom independently to `reversed_zoom`
// button_zoom_reverse: true,
// Reverse the zoom direction
reversed_zoom: true,
// Use alternate touch controls
touch_controls: TouchControls::TwoFingerOrbit,
..default()
},
));
}
// This is how you can change config at runtime.
// Press 'T' to toggle the camera controls.
fn toggle_camera_controls_system(
key_input: Res<ButtonInput<KeyCode>>,
mut pan_orbit_query: Query<&mut PanOrbitCamera>,
) {
if key_input.just_pressed(KeyCode::KeyT) {
for mut pan_orbit in pan_orbit_query.iter_mut() {
pan_orbit.enabled = !pan_orbit.enabled;
}
}
}