-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mesh_interactive.rs
70 lines (64 loc) · 2.12 KB
/
mesh_interactive.rs
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
use bevy::{input::mouse::MouseMotion, prelude::*};
use pixelate_mesh::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(PixelateMeshPlugin::<MainCamera>::default())
.add_systems(Startup, setup)
.add_systems(Update, move_camera)
.run();
}
#[derive(Component)]
struct MainCamera;
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
Name::new("Cube"),
Pixelate::splat(64),
PbrBundle {
mesh: meshes.add(Mesh::from(Cuboid::default())),
material: materials.add(StandardMaterial::from(Color::WHITE)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
},
));
commands.spawn((
Name::new("Camera"),
MainCamera,
Camera3dBundle {
transform: Transform::from_xyz(0., 0., 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
));
commands.spawn((
Name::new("Light"),
PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
shadow_depth_bias: 0.05,
..Default::default()
},
transform: Transform::from_translation(Vec3::new(5.0, 10.0, 10.0)),
..default()
},
PIXELATION_RENDER_LAYERS.clone(),
));
}
fn move_camera(
time: Res<Time>,
mut query: Query<&mut Transform, With<MainCamera>>,
mut mouse_motion_events: EventReader<MouseMotion>,
) {
let dt = time.delta_seconds();
let mut camera_transform = query.single_mut();
let total_motion: Vec2 = mouse_motion_events.read().map(|e| e.delta).sum();
let sensitivity = 0.1;
let motion = total_motion * sensitivity * dt;
let pitch = Quat::from_axis_angle(*camera_transform.right(), -motion.y);
let yaw = Quat::from_rotation_y(-motion.x);
camera_transform.rotate_around(Vec3::ZERO, pitch * yaw);
camera_transform.look_at(Vec3::ZERO, Vec3::Y);
}