//! This example demonstrates the usage of '.meta' files and [`AssetServer::load_with_settings`] to override the default settings for loading an asset12use bevy::{3image::{ImageLoaderSettings, ImageSampler},4prelude::*,5};67fn main() {8App::new()9.add_plugins(10// This just tells the asset server to look in the right examples folder11DefaultPlugins.set(AssetPlugin {12file_path: "examples/asset/files".to_string(),13..Default::default()14}),15)16.add_systems(Startup, setup)17.run();18}1920fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {21// Without any .meta file specifying settings, the default sampler [ImagePlugin::default()] is used for loading images.22// If you are using a very small image and rendering it larger like seen here, the default linear filtering will result in a blurry image.23// Useful note: The default sampler specified by the ImagePlugin is *not* the same as the default implementation of sampler. This is why24// everything uses linear by default but if you look at the default of sampler, it uses nearest.25commands.spawn((26Sprite {27image: asset_server.load("bevy_pixel_dark.png"),28custom_size: Some(Vec2 { x: 160.0, y: 120.0 }),29..Default::default()30},31Transform::from_xyz(-100.0, 0.0, 0.0),32));3334// When a .meta file is added with the same name as the asset and a '.meta' extension35// you can (and must) specify all fields of the asset loader's settings for that36// particular asset, in this case [ImageLoaderSettings]. Take a look at37// examples/asset/files/bevy_pixel_dark_with_meta.png.meta38// for the format and you'll notice, the only non-default option is setting Nearest39// filtering. This tends to work much better for pixel art assets.40// A good reference when filling this out is to check out [ImageLoaderSettings::default()]41// and follow to the default implementation of each fields type.42// https://docs.rs/bevy/latest/bevy/image/struct.ImageLoaderSettings.html43commands.spawn((44Sprite {45image: asset_server.load("bevy_pixel_dark_with_meta.png"),46custom_size: Some(Vec2 { x: 160.0, y: 120.0 }),47..Default::default()48},49Transform::from_xyz(100.0, 0.0, 0.0),50));5152// Another option is to use the AssetServers load_with_settings function.53// With this you can specify the same settings upon loading your asset with a54// couple of differences. A big one is that you aren't required to set *every*55// setting, just modify the ones that you need. It works by passing in a function56// (in this case an anonymous closure) that takes a reference to the settings type57// that is then modified in the function.58// Do note that if you want to load the same asset with different settings, the59// settings changes from any loads after the first of the same asset will be ignored.60// This is why this one loads a differently named copy of the asset instead of using61// same one as without a .meta file.62commands.spawn((63Sprite {64image: asset_server.load_with_settings(65"bevy_pixel_dark_with_settings.png",66|settings: &mut ImageLoaderSettings| {67settings.sampler = ImageSampler::nearest();68},69),70custom_size: Some(Vec2 { x: 160.0, y: 120.0 }),71..Default::default()72},73Transform::from_xyz(0.0, 150.0, 0.0),74));7576commands.spawn(Camera2d);77}787980