bevy/examples/3d/spawner.rs

77 lines
2.6 KiB
Rust
Raw Normal View History

2020-05-04 13:42:49 -07:00
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, PrintDiagnosticsPlugin},
prelude::*,
};
2020-01-28 01:53:28 -08:00
use rand::{rngs::StdRng, Rng, SeedableRng};
2020-01-19 13:24:01 -08:00
2020-07-28 13:43:07 -07:00
/// 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
2020-01-19 13:24:01 -08:00
fn main() {
App::build()
.add_default_plugins()
2020-05-04 13:21:14 -07:00
.add_plugin(FrameTimeDiagnosticsPlugin::default())
2020-05-04 13:42:49 -07:00
.add_plugin(PrintDiagnosticsPlugin::default())
2020-05-13 17:31:56 -07:00
.add_startup_system(setup.system())
.add_system(move_cubes.system())
2020-02-07 23:17:51 -08:00
.run();
2020-01-27 01:40:53 -08:00
}
2020-05-13 17:31:56 -07:00
fn move_cubes(
2020-05-13 17:52:47 -07:00
time: Res<Time>,
mut materials: ResMut<Assets<StandardMaterial>>,
2020-07-09 21:18:35 -07:00
mut query: Query<(&mut Translation, &Handle<StandardMaterial>)>,
2020-04-28 01:00:30 -07:00
) {
for (mut translation, material_handle) in &mut query.iter() {
let material = materials.get_mut(&material_handle).unwrap();
2020-07-16 19:23:47 -07:00
translation.0 += Vec3::new(1.0, 0.0, 0.0) * time.delta_seconds;
2020-07-28 13:43:07 -07:00
material.albedo =
Color::BLUE * Vec3::splat((3.0 * time.seconds_since_startup as f32).sin());
}
2020-01-19 13:24:01 -08:00
}
2020-05-13 17:31:56 -07:00
fn setup(
2020-07-09 21:18:35 -07:00
mut commands: Commands,
2020-05-13 17:52:47 -07:00
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
2020-05-13 17:31:56 -07:00
) {
2020-07-09 21:18:35 -07:00
commands
2020-01-19 13:24:01 -08:00
// light
2020-07-09 21:18:35 -07:00
.spawn(LightComponents {
translation: Translation::new(4.0, -4.0, 5.0),
2020-02-17 19:06:12 -08:00
..Default::default()
})
2020-01-19 13:24:01 -08:00
// camera
.spawn(Camera3dComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward(
2020-07-28 13:43:07 -07:00
Vec3::new(0.0, 15.0, 150.0),
2020-01-19 13:24:01 -08:00
Vec3::new(0.0, 0.0, 0.0),
2020-07-28 13:43:07 -07:00
Vec3::new(0.0, 0.0, 1.0),
2020-01-19 13:24:01 -08:00
)),
2020-03-21 21:55:33 -07:00
..Default::default()
2020-01-28 01:53:28 -08:00
});
let mut rng = StdRng::from_entropy();
2020-07-31 17:10:29 -07:00
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
2020-01-28 01:53:28 -08:00
for _ in 0..10000 {
2020-07-09 21:18:35 -07:00
commands.spawn(PbrComponents {
2020-02-23 23:50:44 -08:00
mesh: cube_handle,
2020-07-28 13:43:07 -07:00
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()
}),
2020-02-07 23:17:51 -08:00
translation: Translation::new(
rng.gen_range(-50.0, 50.0),
rng.gen_range(-50.0, 50.0),
2020-07-28 13:43:07 -07:00
0.0,
2020-02-07 23:17:51 -08:00
),
2020-02-11 09:31:49 -08:00
..Default::default()
2020-04-07 13:25:01 -07:00
});
2020-01-28 01:53:28 -08:00
}
2020-01-19 13:24:01 -08:00
}