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
|
|
|
|
|
|
|
fn main() {
|
2020-03-20 23:35:19 +00:00
|
|
|
App::build()
|
2020-04-04 19:40:32 +00:00
|
|
|
.add_default_plugins()
|
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-07-10 04:18:35 +00:00
|
|
|
mut query: Query<(&mut Translation, &Handle<StandardMaterial>)>,
|
2020-04-28 08:00:30 +00:00
|
|
|
) {
|
2020-07-20 20:59:51 +00:00
|
|
|
for (mut translation, material_handle) in query.iter() {
|
2020-06-27 19:06:12 +00:00
|
|
|
let material = materials.get_mut(&material_handle).unwrap();
|
2020-07-17 02:23:47 +00:00
|
|
|
translation.0 += Vec3::new(1.0, 0.0, 0.0) * time.delta_seconds;
|
2020-06-27 19:06:12 +00:00
|
|
|
material.albedo += Color::rgb(-time.delta_seconds, -time.delta_seconds, time.delta_seconds);
|
|
|
|
}
|
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-05-16 07:21:04 +00:00
|
|
|
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
|
2020-05-13 23:42:27 +00:00
|
|
|
let plane_handle = meshes.add(Mesh::from(shape::Plane { size: 10.0 }));
|
|
|
|
let cube_material_handle = materials.add(StandardMaterial {
|
2020-03-18 05:02:01 +00:00
|
|
|
albedo: Color::rgb(0.5, 0.4, 0.3),
|
|
|
|
..Default::default()
|
2020-03-16 07:45:28 +00:00
|
|
|
});
|
2020-05-13 23:42:27 +00:00
|
|
|
let plane_material_handle = materials.add(StandardMaterial {
|
2020-03-18 05:02:01 +00:00
|
|
|
albedo: Color::rgb(0.1, 0.2, 0.1),
|
|
|
|
..Default::default()
|
2020-03-16 07:45:28 +00:00
|
|
|
});
|
2020-01-19 21:24:01 +00:00
|
|
|
|
2020-07-10 04:18:35 +00:00
|
|
|
commands
|
2020-01-19 21:24:01 +00:00
|
|
|
// plane
|
2020-07-10 04:18:35 +00:00
|
|
|
.spawn(PbrComponents {
|
2020-02-24 07:50:44 +00:00
|
|
|
mesh: plane_handle,
|
2020-03-16 07:45:28 +00:00
|
|
|
material: plane_material_handle,
|
2020-02-11 17:31:49 +00:00
|
|
|
..Default::default()
|
2020-01-27 09:44:02 +00:00
|
|
|
})
|
2020-01-19 21:24:01 +00:00
|
|
|
// cube
|
2020-07-10 04:18:35 +00:00
|
|
|
.spawn(PbrComponents {
|
2020-02-24 07:50:44 +00:00
|
|
|
mesh: cube_handle,
|
2020-03-16 07:45:28 +00:00
|
|
|
material: cube_material_handle,
|
2020-01-19 21:24:01 +00:00
|
|
|
translation: Translation::new(0.0, 0.0, 1.0),
|
2020-02-11 17:31:49 +00:00
|
|
|
..Default::default()
|
2020-01-19 21:24:01 +00:00
|
|
|
})
|
|
|
|
// light
|
2020-07-10 04:18:35 +00:00
|
|
|
.spawn(LightComponents {
|
2020-02-05 17:59:36 +00:00
|
|
|
translation: Translation::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-06-24 02:15:05 +00:00
|
|
|
transform: Transform::new_sync_disabled(Mat4::face_toward(
|
2020-07-20 08:35:23 +00:00
|
|
|
Vec3::new(0.0, 150.0, -15.0),
|
2020-01-19 21:24:01 +00:00
|
|
|
Vec3::new(0.0, 0.0, 0.0),
|
2020-06-24 22:29:10 +00:00
|
|
|
Vec3::new(0.0, 1.0, 0.0),
|
2020-01-19 21:24:01 +00:00
|
|
|
)),
|
2020-03-22 04:55:33 +00:00
|
|
|
..Default::default()
|
2020-01-28 09:53:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut rng = StdRng::from_entropy();
|
|
|
|
for _ in 0..10000 {
|
2020-05-13 23:42:27 +00:00
|
|
|
let spawned_material_handle = materials.add(StandardMaterial {
|
2020-03-16 07:45:28 +00:00
|
|
|
albedo: Color::rgb(
|
|
|
|
rng.gen_range(0.0, 1.0),
|
|
|
|
rng.gen_range(0.0, 1.0),
|
|
|
|
rng.gen_range(0.0, 1.0),
|
2020-03-18 05:02:01 +00:00
|
|
|
),
|
|
|
|
..Default::default()
|
2020-03-16 07:45:28 +00:00
|
|
|
});
|
2020-07-10 04:18:35 +00:00
|
|
|
commands.spawn(PbrComponents {
|
2020-02-24 07:50:44 +00:00
|
|
|
mesh: cube_handle,
|
2020-03-16 07:45:28 +00:00
|
|
|
material: spawned_material_handle,
|
2020-02-08 07:17:51 +00:00
|
|
|
translation: Translation::new(
|
|
|
|
rng.gen_range(-50.0, 50.0),
|
|
|
|
0.0,
|
2020-06-24 22:29:10 +00:00
|
|
|
rng.gen_range(-50.0, 50.0),
|
2020-02-08 07:17:51 +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
|
|
|
}
|