Skip to content

Commit 6804424

Browse files
Create an example to show generating assets at runtime. (#23116)
# Objective - Show how to create assets at runtime. - https://discord.com/channels/691052431525675048/691052431974465548/1475244907047424236 ## Solution - Create an example. ## Testing - The example works! --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
1 parent 1fdf426 commit 6804424

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,17 @@ category = "Assets"
20432043
# Uses non-standard asset path
20442044
wasm = false
20452045

2046+
[[example]]
2047+
name = "generated_assets"
2048+
path = "examples/asset/generated_assets.rs"
2049+
doc-scrape-examples = true
2050+
2051+
[package.metadata.example.generated_assets]
2052+
name = "Generated Assets"
2053+
description = "Shows how to generate and store assets at runtime"
2054+
category = "Assets"
2055+
wasm = true
2056+
20462057
[[example]]
20472058
name = "web_asset"
20482059
path = "examples/asset/web_asset.rs"

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ Example | Description
263263
[Custom Asset IO](../examples/asset/custom_asset_reader.rs) | Implements a custom AssetReader
264264
[Embedded Asset](../examples/asset/embedded_asset.rs) | Embed an asset in the application binary and load it
265265
[Extra Asset Source](../examples/asset/extra_source.rs) | Load an asset from a non-standard asset source
266+
[Generated Assets](../examples/asset/generated_assets.rs) | Shows how to generate and store assets at runtime
266267
[Hot Reloading of Assets](../examples/asset/hot_asset_reloading.rs) | Demonstrates automatic reloading of assets when modified on disk
267268
[Multi-asset synchronization](../examples/asset/multi_asset_sync.rs) | Demonstrates how to wait for multiple assets to be loaded.
268269
[Repeated texture configuration](../examples/asset/repeated_texture.rs) | How to configure the texture to repeat instead of the default clamp to edges

examples/asset/generated_assets.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//! Shows how to generate and store assets at runtime.
2+
3+
use bevy::prelude::*;
4+
5+
fn main() {
6+
App::new()
7+
.add_plugins(DefaultPlugins)
8+
.add_systems(Startup, setup)
9+
.add_systems(Update, generate_mesh_system.run_if(run_once))
10+
.run();
11+
}
12+
13+
fn setup(
14+
mut commands: Commands,
15+
asset_server: Res<AssetServer>,
16+
mut materials: ResMut<Assets<StandardMaterial>>,
17+
meshes: Res<Assets<Mesh>>,
18+
) {
19+
commands.spawn((Camera3d::default(), Transform::from_xyz(0.0, 0.0, 5.0)));
20+
21+
commands.spawn((
22+
DirectionalLight::default(),
23+
Transform::default().looking_to(Dir3::new(Vec3::new(-1.0, -1.0, -1.0)).unwrap(), Dir3::Y),
24+
));
25+
26+
// The simplest way to generate an asset is to add it directly to the `Assets`.
27+
let material_handle = materials.add(StandardMaterial::default());
28+
29+
commands.spawn((
30+
Transform::from_xyz(-2.0, 0.0, 0.0),
31+
MeshMaterial3d(material_handle.clone()),
32+
// Alternatively, `add_async` creates a task that runs your async function. Once it
33+
// completes, the asset is added to the `Assets`. This is "deferred" meaning that the asset
34+
// may take a frame to be added after the task completes.
35+
Mesh3d(asset_server.add_async(generate_mesh_async())),
36+
));
37+
38+
// The last way to generate assets is to reserve a handle, and then use `Assets::insert` to
39+
// populate the asset later. In this example, the `generate_mesh_system` system runs to populate
40+
// the mesh.
41+
let mesh_handle = meshes.reserve_handle();
42+
commands.insert_resource(HandleToGenerate(mesh_handle.clone()));
43+
commands.spawn((
44+
Transform::from_xyz(2.0, 0.0, 0.0)
45+
.with_rotation(Quat::from_rotation_x(50.0f32.to_radians())),
46+
Mesh3d(mesh_handle),
47+
MeshMaterial3d(material_handle),
48+
));
49+
}
50+
51+
async fn generate_mesh_async() -> Result<Mesh, std::io::Error> {
52+
// This mesh could take a while to generate. It could even take several frames (though in this
53+
// example it should be ~instant).
54+
55+
Ok(Mesh::from(Cone::new(1.0, 2.0)))
56+
}
57+
58+
#[derive(Resource)]
59+
struct HandleToGenerate(Handle<Mesh>);
60+
61+
/// This system runs once to populate the handle in [`HandleToGenerate`].
62+
///
63+
/// This generates a runtime mesh. Since it's a system, it can use other data in the world to
64+
/// generate the asset!
65+
fn generate_mesh_system(
66+
handle_to_generate: Res<HandleToGenerate>,
67+
mut meshes: ResMut<Assets<Mesh>>,
68+
) {
69+
let mesh = Mesh::from(Torus::new(0.8, 1.2));
70+
meshes.insert(&handle_to_generate.0, mesh).unwrap();
71+
}

0 commit comments

Comments
 (0)