bevy/examples/asset/asset_loading.rs

79 lines
2.9 KiB
Rust
Raw Normal View History

2020-05-17 03:18:30 +00:00
use bevy::prelude::*;
2020-07-28 20:43:07 +00:00
/// This example illustrates various ways to load assets
2020-05-17 03:18:30 +00:00
fn main() {
App::build()
.add_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
2020-05-17 03:18:30 +00:00
.run();
}
fn setup(
commands: &mut Commands,
asset_server: Res<AssetServer>,
meshes: Res<Assets<Mesh>>,
2020-05-17 03:18:30 +00:00
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// By default AssetServer will load assets from inside the "assets" folder
// For example, the next line will load "assets/models/cube/cube.gltf#Mesh0/Primitive0"
let cube_handle = asset_server.load("models/cube/cube.gltf#Mesh0/Primitive0");
let sphere_handle = asset_server.load("models/sphere/sphere.gltf#Mesh0/Primitive0");
2020-06-04 03:08:20 +00:00
2020-05-17 17:29:42 +00:00
// All assets end up in their Assets<T> collection once they are done loading:
if let Some(sphere) = meshes.get(&sphere_handle) {
// You might notice that this doesn't run! This is because assets load in parallel without blocking.
// When an asset has loaded, it will appear in relevant Assets<T> collection.
println!("{:?}", sphere.primitive_topology());
} else {
println!("sphere hasn't loaded yet");
}
// You can load all assets in a folder like this. They will be loaded in parallel without blocking
let _scenes: Vec<HandleUntyped> = asset_server.load_folder("models/monkey").unwrap();
// Then any asset in the folder can be accessed like this:
let monkey_handle = asset_server.get_handle("models/monkey/Monkey.gltf#Mesh0/Primitive0");
2020-05-17 03:18:30 +00:00
2020-05-17 17:29:42 +00:00
// You can also add assets directly to their Assets<T> storage:
2020-05-17 03:18:30 +00:00
let material_handle = materials.add(StandardMaterial {
2020-10-12 23:54:22 +00:00
albedo: Color::rgb(0.8, 0.7, 0.6),
2020-05-17 03:18:30 +00:00
..Default::default()
});
2020-05-17 17:29:42 +00:00
// Add entities to the world:
2020-07-10 04:18:35 +00:00
commands
2020-05-17 03:18:30 +00:00
// monkey
.spawn(PbrBundle {
2020-05-17 03:18:30 +00:00
mesh: monkey_handle,
material: material_handle.clone(),
transform: Transform::from_translation(Vec3::new(-3.0, 0.0, 0.0)),
2020-05-17 03:18:30 +00:00
..Default::default()
})
// cube
.spawn(PbrBundle {
2020-05-17 03:18:30 +00:00
mesh: cube_handle,
material: material_handle.clone(),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
2020-05-17 03:18:30 +00:00
..Default::default()
})
// sphere
.spawn(PbrBundle {
2020-05-17 03:18:30 +00:00
mesh: sphere_handle,
material: material_handle,
transform: Transform::from_translation(Vec3::new(3.0, 0.0, 0.0)),
2020-05-17 03:18:30 +00:00
..Default::default()
})
// light
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 5.0, 4.0)),
2020-05-17 03:18:30 +00:00
..Default::default()
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.0, 3.0, 10.0))
.looking_at(Vec3::default(), Vec3::unit_y()),
2020-05-17 03:18:30 +00:00
..Default::default()
});
}