Skip to content

Commit 6c95b58

Browse files
committed
Make many_cubes example more interesting (#4015)
# Objective - Make the many_cubes example more interesting (and look more like many_sprites) ## Solution - Actually display many cubes - Move the camera around
1 parent 0eec2ea commit 6c95b58

File tree

1 file changed

+78
-5
lines changed

1 file changed

+78
-5
lines changed

examples/3d/many_cubes.rs

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ fn main() {
99
.add_plugin(FrameTimeDiagnosticsPlugin::default())
1010
.add_plugin(LogDiagnosticsPlugin::default())
1111
.add_startup_system(setup)
12+
.add_system(move_camera)
13+
.add_system(print_mesh_count)
1214
.run();
1315
}
1416

@@ -17,8 +19,8 @@ fn setup(
1719
mut meshes: ResMut<Assets<Mesh>>,
1820
mut materials: ResMut<Assets<StandardMaterial>>,
1921
) {
20-
const WIDTH: usize = 100;
21-
const HEIGHT: usize = 100;
22+
const WIDTH: usize = 200;
23+
const HEIGHT: usize = 200;
2224
let mesh = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
2325
let material = materials.add(StandardMaterial {
2426
base_color: Color::PINK,
@@ -28,17 +30,88 @@ fn setup(
2830
for y in 0..HEIGHT {
2931
// cube
3032
commands.spawn_bundle(PbrBundle {
31-
mesh: mesh.clone(),
32-
material: material.clone(),
33+
mesh: mesh.clone_weak(),
34+
material: material.clone_weak(),
3335
transform: Transform::from_xyz((x as f32) * 2.0, (y as f32) * 2.0, 0.0),
3436
..default()
3537
});
38+
commands.spawn_bundle(PbrBundle {
39+
mesh: mesh.clone_weak(),
40+
material: material.clone_weak(),
41+
transform: Transform::from_xyz(
42+
(x as f32) * 2.0,
43+
HEIGHT as f32 * 2.0,
44+
(y as f32) * 2.0,
45+
),
46+
..Default::default()
47+
});
48+
commands.spawn_bundle(PbrBundle {
49+
mesh: mesh.clone_weak(),
50+
material: material.clone_weak(),
51+
transform: Transform::from_xyz((x as f32) * 2.0, 0.0, (y as f32) * 2.0),
52+
..Default::default()
53+
});
54+
commands.spawn_bundle(PbrBundle {
55+
mesh: mesh.clone_weak(),
56+
material: material.clone_weak(),
57+
transform: Transform::from_xyz(0.0, (x as f32) * 2.0, (y as f32) * 2.0),
58+
..Default::default()
59+
});
3660
}
3761
}
3862

63+
// add one cube, the only one with strong handles
64+
// also serves as a reference point during rotation
65+
commands.spawn_bundle(PbrBundle {
66+
mesh,
67+
material,
68+
transform: Transform {
69+
translation: Vec3::new(0.0, HEIGHT as f32 * 2.0, 0.0),
70+
scale: Vec3::splat(5.0),
71+
..Default::default()
72+
},
73+
..Default::default()
74+
});
75+
3976
// camera
4077
commands.spawn_bundle(PerspectiveCameraBundle {
41-
transform: Transform::from_xyz(80.0, 80.0, 300.0),
78+
transform: Transform::from_xyz(WIDTH as f32, HEIGHT as f32, WIDTH as f32),
4279
..default()
4380
});
81+
82+
commands.spawn_bundle(DirectionalLightBundle {
83+
..Default::default()
84+
});
85+
}
86+
87+
// System for rotating the camera
88+
fn move_camera(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Camera>>) {
89+
let mut camera_transform = camera_query.single_mut();
90+
camera_transform.rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5));
91+
camera_transform.rotate(Quat::from_rotation_x(time.delta_seconds() * 0.5));
92+
}
93+
94+
// System for printing the number of meshes on every tick of the timer
95+
fn print_mesh_count(
96+
time: Res<Time>,
97+
mut timer: Local<PrintingTimer>,
98+
sprites: Query<(&Handle<Mesh>, &ComputedVisibility)>,
99+
) {
100+
timer.0.tick(time.delta());
101+
102+
if timer.0.just_finished() {
103+
info!(
104+
"Meshes: {} - Visible Meshes {}",
105+
sprites.iter().len(),
106+
sprites.iter().filter(|(_, cv)| cv.is_visible).count(),
107+
);
108+
}
109+
}
110+
111+
struct PrintingTimer(Timer);
112+
113+
impl Default for PrintingTimer {
114+
fn default() -> Self {
115+
Self(Timer::from_seconds(1.0, true))
116+
}
44117
}

0 commit comments

Comments
 (0)