-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cube.rs
164 lines (152 loc) · 4.66 KB
/
cube.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
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
use bevy::{
prelude::*,
window::{close_on_esc, WindowPlugin, WindowResolution},
};
use bevy_mod_paramap::*;
fn main() {
let mut app = App::new();
app.add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: "simple cube".into(),
resolution: WindowResolution::new(756., 574.),
..default()
}),
..default()
})
// Tell the asset server to watch for asset changes on disk:
.set(AssetPlugin {
watch_for_changes: !cfg!(target_arch = "wasm32"),
..default()
}),
)
.add_plugin(ParallaxMaterialPlugin)
.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 0.2,
})
.add_startup_system(setup)
.add_system(spin_cube)
.add_system(handle_camera)
.add_system(close_on_esc);
app.run();
}
/// SPIN THE CUBE, SPIN IT! SPINNNN!
#[derive(Component)]
struct Spin;
/// The camera, used to move camera on click.
#[derive(Component)]
struct OurCamera;
fn spin_cube(time: Res<Time>, mut query: Query<&mut Transform, With<Spin>>) {
for mut transform in query.iter_mut() {
transform.rotate_local_y(0.3 * time.delta_seconds());
transform.rotate_local_x(0.3 * time.delta_seconds());
transform.rotate_local_z(0.3 * time.delta_seconds());
}
}
const CAMERA_POSITIONS: &[Transform] = &[
Transform {
translation: Vec3::new(1.5, 1.5, 1.5),
rotation: Quat::from_xyzw(-0.279, 0.364, 0.115, 0.880),
scale: Vec3::ONE,
},
Transform {
translation: Vec3::new(2.4, 0.0, 0.2),
rotation: Quat::from_xyzw(0.094, 0.676, 0.116, 0.721),
scale: Vec3::ONE,
},
Transform {
translation: Vec3::new(2.4, 2.6, -4.3),
rotation: Quat::from_xyzw(0.17055528, 0.9080315, 0.30884093, 0.22584707),
scale: Vec3::ONE,
},
Transform {
translation: Vec3::new(-1.0, 0.8, -1.2),
rotation: Quat::from_xyzw(-0.004, 0.909, 0.247, -0.335),
scale: Vec3::ONE,
},
];
fn handle_camera(
mut camera: Query<&mut Transform, With<OurCamera>>,
mut current_view: Local<usize>,
button: Res<Input<MouseButton>>,
) {
let mut camera = camera.single_mut();
if button.just_pressed(MouseButton::Left) {
println!("{camera:#?}");
*current_view = (*current_view + 1) % CAMERA_POSITIONS.len();
}
let target = CAMERA_POSITIONS[*current_view];
camera.translation = camera.translation.lerp(target.translation, 0.2);
camera.rotation = camera.rotation.slerp(target.rotation, 0.2);
}
fn setup(
mut cmd: Commands,
mut mats: ResMut<Assets<ParallaxMaterial>>,
mut std_mats: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
assets: Res<AssetServer>,
) {
// Camera
cmd.spawn(Camera3dBundle {
transform: Transform::from_xyz(1.5, 1.5, 1.5).looking_at(Vec3::ZERO, Vec3::Y),
..default()
})
.insert(OurCamera);
// light
cmd.spawn(PointLightBundle {
transform: Transform::from_xyz(1.8, 0.7, -1.1),
point_light: PointLight {
intensity: 226.0,
shadows_enabled: true,
..default()
},
..default()
})
.with_children(|cmd| {
let sphere = shape::Icosphere {
radius: 0.05,
subdivisions: 3,
};
cmd.spawn(PbrBundle {
mesh: meshes.add(sphere.try_into().unwrap()),
..default()
});
});
// Plane
cmd.spawn(PbrBundle {
mesh: meshes.add(
shape::Plane {
size: 10.0,
subdivisions: 1,
}
.into(),
),
material: std_mats.add(StandardMaterial {
perceptual_roughness: 0.45,
reflectance: 0.18,
..Color::rgb_u8(0, 80, 0).into()
}),
transform: Transform::from_xyz(0.0, -1.0, 0.0),
..default()
});
// Cube
let mut cube: Mesh = shape::Cube { size: 1.0 }.into();
cube.generate_tangents().unwrap();
cmd.spawn(MaterialMeshBundle {
mesh: meshes.add(cube),
material: mats.add(ParallaxMaterial {
perceptual_roughness: 0.5,
base_color_texture: Some(assets.load("cube/paramap_color.jpg")),
normal_map_texture: assets.load("cube/paramap_normal.jpg"),
height_map: assets.load("cube/paramap_bump.jpg"),
height_depth: 0.1,
algorithm: ParallaxAlgo::ReliefMapping,
max_height_layers: 64.0,
..default()
}),
..default()
})
.insert(Spin);
}