Repository: bevyengine/bevy
File: examples/asset/generated_assets.rs (Rust)

Code before:
}

fn setup(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    images: Res<Assets<Image>>,
) {
    commands.spawn((
        Camera3d::default(),
        Transform::from_xyz(0.0, 10.0, 0.0),
        FreeCamera::default(),
    ));

    commands.spawn((
        DirectionalLight::default(),
        Transform::default().looking_to(Dir3::new(Vec3::new(-1.0, -1.0, -1.0)).unwrap(), Dir3::Y),
    ));

    commands.spawn((
        // `add_async` creates a task that runs your async function. Once it completes, the asset is
        // added to the `Assets`. This is "deferred" meaning that the asset may take a frame to be
        // added after the task completes.
        Mesh3d(asset_server.add_async(generate_mesh(UVec2::new(100, 100), 1234))),
        // Another way to generate an asset is to add it directly to the `Assets`.
        MeshMaterial3d(materials.add(StandardMaterial::default())),
    ));

    // The last way to generate assets is to reserve a handle, and then use `Assets::insert` to
    // populate the asset later. In this example, the `generate_image` system runs to populate the
    // image
    let image_handle = images.reserve_handle();
    commands.insert_resource(HandleToGenerate(image_handle.clone()));
    commands.spawn(ImageNode::new(image_handle));
}

async fn generate_mesh(size: UVec2, seed: u64) -> Result<Mesh, std::io::Error> {
    // This mesh could take a while to generate!
    sleep(Duration::from_secs(3));

    let mut rng = StdRng::seed_from_u64(seed);
    let mut positions = vec![];
    for y in 0..size.y {
        for x in 0..size.x {
            positions.push(Vec3::new(
                x as f32 - size.x as f32 / 2.0,
                rng.random::<f32>() * 2.0,
                y as f32 - size.y as f32 / 2.0,
            ));
        }
    }

Review comment (suggestion):
```suggestion
        // The simplest way to generate an asset is to add it directly to the `Assets`.
        MeshMaterial3d(materials.add(StandardMaterial::default())),
        // Alternatively, `add_async` creates a task that runs your async function. Once it completes, the
        // asset is added to the `Assets`. This is "deferred" meaning that the asset may take a frame to be
        // added after the task completes.
        Mesh3d(asset_server.add_async(generate_mesh(UVec2::new(100, 100), 1234))),
```

I'd put the simplest case first.

Code after:
    ));

    // The simplest way to generate an asset is to add it directly to the `Assets`.
    let material_handle = materials.add(StandardMaterial::default());

    commands.spawn((
        Transform::from_xyz(-2.0, 0.0, 0.0),
        MeshMaterial3d(material_handle.clone()),
        // Alternatively, `add_async` creates a task that runs your async function. Once it
        // completes, the asset is added to the `Assets`. This is "deferred" meaning that the asset
        // may take a frame to be added after the task completes.
        Mesh3d(asset_server.add_async(generate_mesh_async())),
    ));

    // The last way to generate assets is to reserve a handle, and then use `Assets::insert` to
    // populate the asset later. In this example, the `generate_mesh_system` system runs to populate
    // the mesh.
    let mesh_handle = meshes.reserve_handle();
    commands.insert_resource(HandleToGenerate(mesh_handle.clone()));
    commands.spawn((
        Transform::from_xyz(2.0, 0.0, 0.0)
            .with_rotation(Quat::from_rotation_x(50.0f32.to_radians())),
        Mesh3d(mesh_handle),
        MeshMaterial3d(material_handle),
    ));
}

async fn generate_mesh_async() -> Result<Mesh, std::io::Error> {
    // This mesh could take a while to generate. It could even take several frames (though in this
    // example it should be ~instant).

    Ok(Mesh::from(Cone::new(1.0, 2.0)))
}

#[derive(Resource)]
struct HandleToGenerate(Handle<Mesh>);

/// This system runs once to populate the handle in [`HandleToGenerate`].
///
/// This generates a runtime mesh. Since it's a system, it can use other data in the world to
/// generate the asset!
fn generate_mesh_system(
    handle_to_generate: Res<HandleToGenerate>,
    mut meshes: ResMut<Assets<Mesh>>,
) {
    let mesh = Mesh::from(Torus::new(0.8, 1.2));
    meshes.insert(&handle_to_generate.0, mesh).unwrap();
}