forked from NiklasEi/bevy_asset_loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
full_collection.rs
206 lines (192 loc) · 7.7 KB
/
full_collection.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use bevy::app::AppExit;
use bevy::asset::RecursiveDependencyLoadState;
use bevy::prelude::*;
use bevy::render::texture::{ImageSampler, ImageSamplerDescriptor};
use bevy::utils::HashMap;
use bevy_asset_loader::prelude::*;
fn main() {
App::new()
.init_state::<MyStates>()
.add_plugins(DefaultPlugins)
.add_loading_state(
LoadingState::new(MyStates::AssetLoading)
.continue_to_state(MyStates::Next)
.load_collection::<MyAssets>(),
)
.add_systems(OnEnter(MyStates::Next), expectations)
.run();
}
#[derive(AssetCollection, Resource)]
struct MyAssets {
// Any file that can be loaded to a Handle<T>
#[asset(path = "audio/background.ogg")]
single_file: Handle<AudioSource>,
// Any file that can be loaded and turned into a standard material
#[asset(path = "images/player.png", standard_material)]
standard_material: Handle<StandardMaterial>,
// Create a texture atlas layout
#[asset(texture_atlas_layout(tile_size_x = 96., tile_size_y = 99., columns = 8, rows = 1))]
texture_atlas_layout: Handle<TextureAtlasLayout>,
// Example field with type that implements `FromWorld`
// If no derive attributes are set, `from_world` will be used to set the value.
from_world: ColorStandardMaterial<{ u8::MAX }, 0, 0, { u8::MAX }>,
// Image asset with sampler nearest (good for crisp pixel art)
#[asset(path = "images/pixel_tree.png")]
#[asset(image(sampler = nearest))]
image_tree_nearest: Handle<Image>,
// Load collections of assets
// A folder (not supported on the web)
#[asset(path = "images", collection)]
folder_untyped: Vec<UntypedHandle>,
// A folder loaded to typed asset handles (not supported on the web)
#[asset(path = "images", collection(typed))]
folder_typed: Vec<Handle<Image>>,
// A folder loaded as map (not supported on the web)
#[asset(path = "images", collection(mapped))]
mapped_folder_untyped: HashMap<String, UntypedHandle>,
// A folder loaded to typed asset handles mapped with their file names (not supported on the web)
#[asset(path = "images", collection(typed, mapped))]
mapped_folder_typed: HashMap<String, Handle<Image>>,
// A collection of asset files
#[asset(paths("images/player.png", "images/tree.png"), collection)]
files_untyped: Vec<UntypedHandle>,
// A collection of asset files loaded to typed asset handles
#[asset(paths("images/player.png", "images/tree.png"), collection(typed))]
files_typed: Vec<Handle<Image>>,
// A mapped collection of asset files
#[asset(paths("images/player.png", "images/tree.png"), collection(mapped))]
mapped_files_untyped: HashMap<String, UntypedHandle>,
// A mapped collection of asset files loaded to typed asset handles
#[asset(
paths("images/player.png", "images/tree.png"),
collection(typed, mapped)
)]
mapped_files_typed: HashMap<String, Handle<Image>>,
}
fn expectations(
assets: Res<MyAssets>,
asset_server: Res<AssetServer>,
standard_materials: Res<Assets<StandardMaterial>>,
texture_atlas_layouts: Res<Assets<TextureAtlasLayout>>,
images: Res<Assets<Image>>,
mut quit: EventWriter<AppExit>,
) {
info!("Done loading the collection. Checking expectations...");
assert_eq!(
asset_server.get_recursive_dependency_load_state(assets.single_file.clone()),
Some(RecursiveDependencyLoadState::Loaded)
);
let material = standard_materials
.get(&assets.standard_material)
.expect("Standard material should be added to its assets resource.");
assert_eq!(
asset_server.get_recursive_dependency_load_state(
material
.base_color_texture
.clone()
.expect("Material should have image as base color texture")
),
Some(RecursiveDependencyLoadState::Loaded)
);
texture_atlas_layouts
.get(&assets.texture_atlas_layout)
.expect("Texture atlas layout should be added to its assets resource.");
let material = standard_materials
.get(&assets.from_world.handle)
.expect("Standard material should be added to its assets resource.");
assert_eq!(material.base_color, Color::RED);
let image = images
.get(&assets.image_tree_nearest)
.expect("Image should be added to its asset resource");
let ImageSampler::Descriptor(descriptor) = &image.sampler else {
panic!("Descriptor was not set to non default value nearest");
};
assert_eq!(
descriptor.as_wgpu(),
ImageSamplerDescriptor::nearest().as_wgpu()
);
assert_eq!(assets.folder_untyped.len(), 7);
for handle in assets.folder_untyped.iter() {
assert_eq!(
asset_server.get_recursive_dependency_load_state(handle.id()),
Some(RecursiveDependencyLoadState::Loaded)
);
}
assert_eq!(assets.folder_typed.len(), 7);
for handle in assets.folder_typed.iter() {
assert_eq!(
asset_server.get_recursive_dependency_load_state(handle.id()),
Some(RecursiveDependencyLoadState::Loaded)
);
}
assert_eq!(assets.mapped_folder_untyped.len(), 7);
for (name, handle) in assets.mapped_folder_untyped.iter() {
assert_eq!(
asset_server.get_recursive_dependency_load_state(handle.id()),
Some(RecursiveDependencyLoadState::Loaded)
);
assert_eq!(&handle.path().unwrap().to_string(), name);
}
assert_eq!(assets.mapped_folder_typed.len(), 7);
for (name, handle) in assets.mapped_folder_typed.iter() {
assert_eq!(
asset_server.get_recursive_dependency_load_state(handle.id()),
Some(RecursiveDependencyLoadState::Loaded)
);
assert_eq!(&handle.path().unwrap().to_string(), name);
}
assert_eq!(assets.files_untyped.len(), 2);
for handle in assets.files_untyped.iter() {
assert_eq!(
asset_server.get_recursive_dependency_load_state(handle.id()),
Some(RecursiveDependencyLoadState::Loaded)
);
}
assert_eq!(assets.files_typed.len(), 2);
for handle in assets.files_typed.iter() {
assert_eq!(
asset_server.get_recursive_dependency_load_state(handle.id()),
Some(RecursiveDependencyLoadState::Loaded)
);
}
assert_eq!(assets.mapped_files_untyped.len(), 2);
for (name, handle) in assets.mapped_files_untyped.iter() {
assert_eq!(
asset_server.get_recursive_dependency_load_state(handle.id()),
Some(RecursiveDependencyLoadState::Loaded)
);
assert_eq!(&handle.path().unwrap().to_string(), name);
}
assert_eq!(assets.mapped_files_typed.len(), 2);
for (name, handle) in assets.mapped_files_typed.iter() {
assert_eq!(
asset_server.get_recursive_dependency_load_state(handle.id()),
Some(RecursiveDependencyLoadState::Loaded)
);
assert_eq!(&handle.path().unwrap().to_string(), name);
}
info!("Everything looks good!");
info!("Quitting the application...");
quit.send(AppExit);
}
struct ColorStandardMaterial<const R: u8, const G: u8, const B: u8, const A: u8> {
pub handle: Handle<StandardMaterial>,
}
impl<const R: u8, const G: u8, const B: u8, const A: u8> FromWorld
for ColorStandardMaterial<R, G, B, A>
{
fn from_world(world: &mut World) -> Self {
let mut materials = world
.get_resource_mut::<Assets<StandardMaterial>>()
.unwrap();
ColorStandardMaterial {
handle: materials.add(StandardMaterial::from(Color::rgba_u8(R, G, B, A))),
}
}
}
#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, States)]
enum MyStates {
#[default]
AssetLoading,
Next,
}