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

Code before:
            ));
        }
    }

    let compute_index = |(x, y): (u32, u32)| x + y * size.x;
    let mut indices = vec![];
    for y in 0..(size.y - 1) {
        for x in 0..(size.x - 1) {
            indices.extend(
                [
                    (x, y),
                    (x, y + 1),
                    (x + 1, y),
                    (x + 1, y),
                    (x, y + 1),
                    (x + 1, y + 1),
                ]
                .into_iter()
                .map(compute_index),
            );
        }
    }

    Ok(Mesh::new(
        PrimitiveTopology::TriangleList,
        RenderAssetUsages::RENDER_WORLD,
    )
    .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
    .with_inserted_indices(Indices::U32(indices))
    .with_computed_normals())
}

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

/// This system runs after a delay to populate the handle in [`HandleToGenerate`].
///
/// This generates a runtime image. Since it's a system, it can use other data in the world to
/// generate the asset!
fn generate_image(handle_to_generate: Res<HandleToGenerate>, mut images: ResMut<Assets<Image>>) {
    let mut image = Image::new_fill(
        Extent3d {
            width: 300,
            height: 300,
            depth_or_array_layers: 1,
        },
        TextureDimension::D2,
        &[0, 0, 0, 255],
        TextureFormat::Rgba8Unorm,
        RenderAssetUsages::RENDER_WORLD,
    );

Review comment (suggestion):
```suggestion
        RenderAssetUsages::default(),
```

I'd stick with the default here. If a user copy and pastes the example and then tries to access the mesh CPU side then they could easily get confused.

Code after:
//! Shows how to generate and store assets at runtime.

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .add_systems(Update, generate_mesh_system.run_if(run_once))
        .run();
}

fn setup(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    meshes: Res<Assets<Mesh>>,
) {
    commands.spawn((Camera3d::default(), Transform::from_xyz(0.0, 0.0, 5.0)));

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

    // 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();
}