-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Use embedded_asset to load PBR shaders #19137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,10 @@ use crate::io::{ | |
memory::{Dir, MemoryAssetReader, Value}, | ||
AssetSource, AssetSourceBuilders, | ||
}; | ||
use crate::AssetServer; | ||
use alloc::boxed::Box; | ||
use bevy_ecs::resource::Resource; | ||
use bevy_app::App; | ||
use bevy_ecs::{resource::Resource, world::World}; | ||
use std::path::{Path, PathBuf}; | ||
|
||
#[cfg(feature = "embedded_watcher")] | ||
|
@@ -132,6 +134,71 @@ impl EmbeddedAssetRegistry { | |
} | ||
} | ||
|
||
/// Trait for the [`load_embedded_asset!`] macro, to access [`AssetServer`] | ||
/// from arbitrary things. | ||
/// | ||
/// [`load_embedded_asset!`]: crate::load_embedded_asset | ||
pub trait GetAssetServer { | ||
fn get_asset_server(&self) -> &AssetServer; | ||
} | ||
impl GetAssetServer for App { | ||
fn get_asset_server(&self) -> &AssetServer { | ||
self.world().get_asset_server() | ||
} | ||
} | ||
impl GetAssetServer for World { | ||
fn get_asset_server(&self) -> &AssetServer { | ||
self.resource() | ||
} | ||
} | ||
impl GetAssetServer for AssetServer { | ||
fn get_asset_server(&self) -> &AssetServer { | ||
self | ||
} | ||
} | ||
|
||
/// Load an [embedded asset](crate::embedded_asset). | ||
/// | ||
/// This is useful if the embedded asset in question is not publicly exposed, but | ||
/// you need to use it internally. | ||
/// | ||
/// # Syntax | ||
/// | ||
/// This macro takes two arguments and an optional third one: | ||
/// 1. The asset source. It may be `AssetServer`, `World` or `App`. | ||
/// 2. The path to the asset to embed, as a string literal. | ||
/// 3. Optionally, a closure of the same type as in [`AssetServer::load_with_settings`]. | ||
/// Consider explicitly typing the closure argument in case of type error. | ||
/// | ||
/// # Usage | ||
/// | ||
/// The advantage compared to using directly [`AssetServer::load`] is: | ||
/// - This also accepts [`World`] and [`App`] arguments. | ||
/// - This uses the exact same path as `embedded_asset!`, so you can keep it | ||
/// consistent. | ||
/// | ||
/// As a rule of thumb: | ||
/// - If the asset in used in the same module as it is declared using `embedded_asset!`, | ||
/// use this macro. | ||
/// - Otherwise, use `AssetServer::load`. | ||
#[macro_export] | ||
macro_rules! load_embedded_asset { | ||
(@get: $path: literal, $provider: expr) => {{ | ||
let path = $crate::embedded_path!($path); | ||
let path = $crate::AssetPath::from_path_buf(path).with_source("embedded"); | ||
let asset_server = $crate::io::embedded::GetAssetServer::get_asset_server($provider); | ||
(path, asset_server) | ||
}}; | ||
($provider: expr, $path: literal, $settings: expr) => {{ | ||
let (path, asset_server) = $crate::load_embedded_asset!(@get: $path, $provider); | ||
asset_server.load_with_settings(path, $settings) | ||
}}; | ||
($provider: expr, $path: literal) => {{ | ||
let (path, asset_server) = $crate::load_embedded_asset!(@get: $path, $provider); | ||
asset_server.load(path) | ||
}}; | ||
} | ||
|
||
/// Returns the [`Path`] for a given `embedded` asset. | ||
/// This is used internally by [`embedded_asset`] and can be used to get a [`Path`] | ||
/// that matches the [`AssetPath`](crate::AssetPath) used by that asset. | ||
|
@@ -140,7 +207,7 @@ impl EmbeddedAssetRegistry { | |
#[macro_export] | ||
macro_rules! embedded_path { | ||
($path_str: expr) => {{ | ||
embedded_path!("src", $path_str) | ||
$crate::embedded_path!("src", $path_str) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this meant to fix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. im not sure why it doesn't work without this. |
||
}}; | ||
|
||
($source_path: expr, $path_str: expr) => {{ | ||
|
@@ -192,7 +259,7 @@ pub fn _embedded_asset_path( | |
/// Creates a new `embedded` asset by embedding the bytes of the given path into the current binary | ||
/// and registering those bytes with the `embedded` [`AssetSource`]. | ||
/// | ||
/// This accepts the current [`App`](bevy_app::App) as the first parameter and a path `&str` (relative to the current file) as the second. | ||
/// This accepts the current [`App`] as the first parameter and a path `&str` (relative to the current file) as the second. | ||
/// | ||
/// By default this will generate an [`AssetPath`] using the following rules: | ||
/// | ||
|
@@ -217,14 +284,19 @@ pub fn _embedded_asset_path( | |
/// | ||
/// `embedded_asset!(app, "rock.wgsl")` | ||
/// | ||
/// `rock.wgsl` can now be loaded by the [`AssetServer`](crate::AssetServer) with the following path: | ||
/// `rock.wgsl` can now be loaded by the [`AssetServer`] as follows: | ||
/// | ||
/// ```no_run | ||
/// # use bevy_asset::{Asset, AssetServer}; | ||
/// # use bevy_asset::{Asset, AssetServer, load_embedded_asset}; | ||
/// # use bevy_reflect::TypePath; | ||
/// # let asset_server: AssetServer = panic!(); | ||
/// # #[derive(Asset, TypePath)] | ||
/// # struct Shader; | ||
/// // If we are loading the shader in the same module we used `embedded_asset!`: | ||
/// let shader = load_embedded_asset!(&asset_server, "rock.wgsl"); | ||
/// # let _: bevy_asset::Handle<Shader> = shader; | ||
/// | ||
/// // If the goal is to expose the asset **to the end user**: | ||
/// let shader = asset_server.load::<Shader>("embedded://bevy_rock/render/rock.wgsl"); | ||
/// ``` | ||
/// | ||
|
@@ -258,11 +330,11 @@ pub fn _embedded_asset_path( | |
/// [`embedded_path`]: crate::embedded_path | ||
#[macro_export] | ||
macro_rules! embedded_asset { | ||
($app: ident, $path: expr) => {{ | ||
($app: expr, $path: expr) => {{ | ||
$crate::embedded_asset!($app, "src", $path) | ||
}}; | ||
|
||
($app: ident, $source_path: expr, $path: expr) => {{ | ||
($app: expr, $source_path: expr, $path: expr) => {{ | ||
let mut embedded = $app | ||
.world_mut() | ||
.resource_mut::<$crate::io::embedded::EmbeddedAssetRegistry>(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we want to name the
AssetPath
s given they have a label field for that.