|
| 1 | +use bevy::{ |
| 2 | + prelude::*, |
| 3 | + tasks::{AsyncComputeTaskPool, Task}, |
| 4 | +}; |
| 5 | +use futures_lite::future; |
| 6 | +use rand::Rng; |
| 7 | +use std::time::{Duration, Instant}; |
| 8 | + |
| 9 | +/// This example shows how to use the ECS and the AsyncComputeTaskPool |
| 10 | +/// to spawn, poll, and complete tasks across systems and system ticks. |
| 11 | +fn main() { |
| 12 | + App::build() |
| 13 | + .insert_resource(Msaa { samples: 4 }) |
| 14 | + .add_plugins(DefaultPlugins) |
| 15 | + .add_startup_system(setup_env.system()) |
| 16 | + .add_startup_system(add_assets.system()) |
| 17 | + .add_startup_system(spawn_tasks.system()) |
| 18 | + .add_system(handle_tasks.system()) |
| 19 | + .run(); |
| 20 | +} |
| 21 | + |
| 22 | +// Number of cubes to spawn across the x, y, and z axis |
| 23 | +const NUM_CUBES: u32 = 6; |
| 24 | + |
| 25 | +struct BoxMeshHandle(Handle<Mesh>); |
| 26 | +struct BoxMaterialHandle(Handle<StandardMaterial>); |
| 27 | + |
| 28 | +/// Startup system which runs only once and generates our Box Mesh |
| 29 | +/// and Box Material assets, adds them to their respective Asset |
| 30 | +/// Resources, and stores their handles as resources so we can access |
| 31 | +/// them later when we're ready to render our Boxes |
| 32 | +fn add_assets( |
| 33 | + mut commands: Commands, |
| 34 | + mut meshes: ResMut<Assets<Mesh>>, |
| 35 | + mut materials: ResMut<Assets<StandardMaterial>>, |
| 36 | +) { |
| 37 | + let box_mesh_handle = meshes.add(Mesh::from(shape::Cube { size: 0.25 })); |
| 38 | + commands.insert_resource(BoxMeshHandle(box_mesh_handle)); |
| 39 | + |
| 40 | + let box_material_handle = materials.add(Color::rgb(1.0, 0.2, 0.3).into()); |
| 41 | + commands.insert_resource(BoxMaterialHandle(box_material_handle)); |
| 42 | +} |
| 43 | + |
| 44 | +/// This system generates tasks simulating computationally intensive |
| 45 | +/// work that potentially spans multiple frames/ticks. A separate |
| 46 | +/// system, handle_tasks, will poll the spawned tasks on subsequent |
| 47 | +/// frames/ticks, and use the results to spawn cubes |
| 48 | +fn spawn_tasks(mut commands: Commands, thread_pool: Res<AsyncComputeTaskPool>) { |
| 49 | + for x in 0..NUM_CUBES { |
| 50 | + for y in 0..NUM_CUBES { |
| 51 | + for z in 0..NUM_CUBES { |
| 52 | + // Spawn new task on the AsyncComputeTaskPool |
| 53 | + let task = thread_pool.spawn(async move { |
| 54 | + let mut rng = rand::thread_rng(); |
| 55 | + let start_time = Instant::now(); |
| 56 | + let duration = Duration::from_secs_f32(rng.gen_range(0.05..0.2)); |
| 57 | + while Instant::now() - start_time < duration { |
| 58 | + // Spinning for 'duration', simulating doing hard |
| 59 | + // compute work generating translation coords! |
| 60 | + } |
| 61 | + |
| 62 | + // Such hard work, all done! |
| 63 | + Transform::from_translation(Vec3::new(x as f32, y as f32, z as f32)) |
| 64 | + }); |
| 65 | + |
| 66 | + // Spawn new entity and add our new task as a component |
| 67 | + commands.spawn().insert(task); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// This system queries for entities that have our Task<Transform> component. It polls the |
| 74 | +/// tasks to see if they're complete. If the task is complete it takes the result, adds a |
| 75 | +/// new PbrBundle of components to the entity using the result from the task's work, and |
| 76 | +/// removes the task component from the entity. |
| 77 | +fn handle_tasks( |
| 78 | + mut commands: Commands, |
| 79 | + mut transform_tasks: Query<(Entity, &mut Task<Transform>)>, |
| 80 | + box_mesh_handle: Res<BoxMeshHandle>, |
| 81 | + box_material_handle: Res<BoxMaterialHandle>, |
| 82 | +) { |
| 83 | + for (entity, mut task) in transform_tasks.iter_mut() { |
| 84 | + if let Some(transform) = future::block_on(future::poll_once(&mut *task)) { |
| 85 | + // Add our new PbrBundle of components to our tagged entity |
| 86 | + commands.entity(entity).insert_bundle(PbrBundle { |
| 87 | + mesh: box_mesh_handle.0.clone(), |
| 88 | + material: box_material_handle.0.clone(), |
| 89 | + transform, |
| 90 | + ..Default::default() |
| 91 | + }); |
| 92 | + |
| 93 | + // Task is complete, so remove task component from entity |
| 94 | + commands.entity(entity).remove::<Task<Transform>>(); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/// This system is only used to setup light and camera for the environment |
| 100 | +fn setup_env(mut commands: Commands) { |
| 101 | + // Used to center camera on spawned cubes |
| 102 | + let offset = if NUM_CUBES % 2 == 0 { |
| 103 | + (NUM_CUBES / 2) as f32 - 0.5 |
| 104 | + } else { |
| 105 | + (NUM_CUBES / 2) as f32 |
| 106 | + }; |
| 107 | + |
| 108 | + // lights |
| 109 | + commands.spawn_bundle(PointLightBundle { |
| 110 | + transform: Transform::from_translation(Vec3::new(4.0, 12.0, 15.0)), |
| 111 | + ..Default::default() |
| 112 | + }); |
| 113 | + |
| 114 | + // camera |
| 115 | + commands.spawn_bundle(PerspectiveCameraBundle { |
| 116 | + transform: Transform::from_translation(Vec3::new(offset, offset, 15.0)) |
| 117 | + .looking_at(Vec3::new(offset, offset, 0.0), Vec3::Y), |
| 118 | + ..Default::default() |
| 119 | + }); |
| 120 | +} |
0 commit comments