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
//! Demonstrates how to configure the PanOrbitCamera to work like Blender.
//!
//! Controls:
//! Orbit: Middle click, trackpad scroll
//! Pan: Shift + Middle click, ShiftLeft + trackpad scroll
//! Zoom: Mousewheel, ControlLeft + trackpad scroll
use bevy::prelude::*;
use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin, TrackpadBehavior};
use std::f32::consts::TAU;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(PanOrbitCameraPlugin)
.add_systems(Startup, setup)
.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),
// 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),
// camera responds to trackpad as in Blender
// if you don't want to write out both modifiers,
// you can use this helper method:
// `trackpad_behavior: TrackpadBehavior::blender_default(),`
trackpad_behavior: TrackpadBehavior::BlenderLike {
modifier_pan: Some(KeyCode::ShiftLeft),
modifier_zoom: Some(KeyCode::ControlLeft),
},
trackpad_sensitivity: 1.0,
trackpad_pinch_to_zoom_enabled: true,
..default()
},
));
}