forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawner.rs
76 lines (72 loc) · 2.62 KB
/
spawner.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
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, PrintDiagnosticsPlugin},
prelude::*,
};
use rand::{rngs::StdRng, Rng, SeedableRng};
/// This example spawns a large number of cubes, each with its own changing position and material
/// This is intended to be a stress test of bevy's ability to render many objects with different properties
/// For the best results, run it in release mode: ```cargo run --example spawner --release
/// NOTE: Bevy still has a number of optimizations to do in this area. Expect the performance here to go way up in the future
fn main() {
App::build()
.add_default_plugins()
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(PrintDiagnosticsPlugin::default())
.add_startup_system(setup.system())
.add_system(move_cubes.system())
.run();
}
fn move_cubes(
time: Res<Time>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut query: Query<(&mut Translation, &Handle<StandardMaterial>)>,
) {
for (mut translation, material_handle) in &mut query.iter() {
let material = materials.get_mut(&material_handle).unwrap();
translation.0 += Vec3::new(1.0, 0.0, 0.0) * time.delta_seconds;
material.albedo =
Color::BLUE * Vec3::splat((3.0 * time.seconds_since_startup as f32).sin());
}
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands
// light
.spawn(LightComponents {
translation: Translation::new(4.0, -4.0, 5.0),
..Default::default()
})
// camera
.spawn(Camera3dComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(0.0, 15.0, 150.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),
)),
..Default::default()
});
let mut rng = StdRng::from_entropy();
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
for _ in 0..10000 {
commands.spawn(PbrComponents {
mesh: cube_handle,
material: materials.add(StandardMaterial {
albedo: Color::rgb(
rng.gen_range(0.0, 1.0),
rng.gen_range(0.0, 1.0),
rng.gen_range(0.0, 1.0),
),
..Default::default()
}),
translation: Translation::new(
rng.gen_range(-50.0, 50.0),
rng.gen_range(-50.0, 50.0),
0.0,
),
..Default::default()
});
}
}