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