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()
|
2020-07-30 21:35:07 +00:00
|
|
|
.add_resource(Msaa { samples: 4 })
|
2020-11-03 03:01:17 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2020-05-17 03:18:30 +00:00
|
|
|
.add_startup_system(setup.system())
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(
|
2020-11-08 20:34:05 +00:00
|
|
|
commands: &mut Commands,
|
2020-06-07 18:45:18 +00:00
|
|
|
asset_server: Res<AssetServer>,
|
2020-10-18 20:48:15 +00:00
|
|
|
meshes: Res<Assets<Mesh>>,
|
2020-05-17 03:18:30 +00:00
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
) {
|
2020-11-11 00:19:55 +00:00
|
|
|
// 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"
|
2020-10-18 20:48:15 +00:00
|
|
|
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:
|
2020-10-18 20:48:15 +00:00
|
|
|
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.
|
2020-11-02 21:15:07 +00:00
|
|
|
println!("{:?}", sphere.primitive_topology());
|
2020-10-18 20:48:15 +00:00
|
|
|
} 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
|
2020-07-10 04:18:35 +00:00
|
|
|
.spawn(PbrComponents {
|
2020-05-17 03:18:30 +00:00
|
|
|
mesh: monkey_handle,
|
2020-10-18 20:48:15 +00:00
|
|
|
material: material_handle.clone(),
|
2020-09-14 21:00:32 +00:00
|
|
|
transform: Transform::from_translation(Vec3::new(-3.0, 0.0, 0.0)),
|
2020-05-17 03:18:30 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
// cube
|
2020-07-10 04:18:35 +00:00
|
|
|
.spawn(PbrComponents {
|
2020-05-17 03:18:30 +00:00
|
|
|
mesh: cube_handle,
|
2020-10-18 20:48:15 +00:00
|
|
|
material: material_handle.clone(),
|
2020-09-14 21:00:32 +00:00
|
|
|
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
|
2020-05-17 03:18:30 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
// sphere
|
2020-07-10 04:18:35 +00:00
|
|
|
.spawn(PbrComponents {
|
2020-05-17 03:18:30 +00:00
|
|
|
mesh: sphere_handle,
|
|
|
|
material: material_handle,
|
2020-09-14 21:00:32 +00:00
|
|
|
transform: Transform::from_translation(Vec3::new(3.0, 0.0, 0.0)),
|
2020-05-17 03:18:30 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
// light
|
2020-07-10 04:18:35 +00:00
|
|
|
.spawn(LightComponents {
|
2020-09-14 21:00:32 +00:00
|
|
|
transform: Transform::from_translation(Vec3::new(4.0, 5.0, 4.0)),
|
2020-05-17 03:18:30 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
// camera
|
2020-07-20 00:00:08 +00:00
|
|
|
.spawn(Camera3dComponents {
|
2020-10-18 20:48:15 +00:00
|
|
|
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()
|
|
|
|
});
|
|
|
|
}
|