From 0a3339a2983cfcd736a64fa8046152bff4694470 Mon Sep 17 00:00:00 2001 From: Sascha Wise Date: Tue, 14 Nov 2023 09:43:13 -0800 Subject: [PATCH] fix: rollback use of `embedded_asset!` --- macros/src/model.rs | 28 +++++++++++++++++++++------- src/collection/gradient.rs | 2 +- src/collection/nishita.rs | 2 +- src/plugin.rs | 11 ++++++++--- src/skybox.rs | 7 +++++-- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/macros/src/model.rs b/macros/src/model.rs index 8cd6c2e..2f6a46f 100644 --- a/macros/src/model.rs +++ b/macros/src/model.rs @@ -49,6 +49,14 @@ pub fn derive_atmospheric(ast: syn::DeriveInput) -> Result { let asset_path = manifest.get_path("bevy_asset"); let ecs_path = manifest.get_path("bevy_ecs"); + let id = { + use std::collections::hash_map::DefaultHasher; + use std::hash::{Hash, Hasher}; + let mut hasher = DefaultHasher::new(); + ast.ident.hash(&mut hasher); + hasher.finish() + }; + let mut shader_path = ShaderPathType::None; let mut binding_states: Vec = Vec::new(); let mut binding_impls = Vec::new(); @@ -132,16 +140,22 @@ pub fn derive_atmospheric(ast: syn::DeriveInput) -> Result { asset_server.load(#s) } }, - ShaderPathType::Internal(s) => { - quote! { + ShaderPathType::Internal(s) => quote! { { + use bevy::reflect::TypeUuid; + let handle: #asset_path::Handle = #asset_path::Handle::weak_from_u128(#id as u128); + + let internal_handle = handle.clone(); + #asset_path::load_internal_asset!( + app, + internal_handle, + concat!(env!("CARGO_MANIFEST_DIR"), "/src/", #s), + Shader::from_wgsl + ); - #asset_path::embedded_asset!(app, "src/", #s); - let asset_server = app.world.resource::(); - asset_server.load(format!("embedded://{}", #asset_path::embedded_path!("src/", #s).display())) + handle } - } - } + }, }; let fields = match &ast.data { diff --git a/src/collection/gradient.rs b/src/collection/gradient.rs index f11327f..214d3f1 100644 --- a/src/collection/gradient.rs +++ b/src/collection/gradient.rs @@ -6,7 +6,7 @@ use bevy::{prelude::*, render::render_resource::ShaderType}; /// A simple gradient for creating a stylized environment. #[derive(Atmospheric, ShaderType, Reflect, Debug, Clone)] #[uniform(0, Gradient)] -#[internal("../shaders/gradient.wgsl")] +#[internal("shaders/gradient.wgsl")] pub struct Gradient { /// Sky Color (Default: `Color::rgb(0.29, 0.41, 0.50)`). ///
diff --git a/src/collection/nishita.rs b/src/collection/nishita.rs index 5c9ff68..383ebfb 100644 --- a/src/collection/nishita.rs +++ b/src/collection/nishita.rs @@ -6,7 +6,7 @@ use bevy::{prelude::*, render::render_resource::ShaderType}; /// An atmospheric model that uses Rayleigh and Mie scattering to simulate a realistic sky. #[derive(Atmospheric, ShaderType, Reflect, Debug, Clone)] #[uniform(0, Nishita)] -#[internal("../shaders/nishita.wgsl")] +#[internal("shaders/nishita.wgsl")] pub struct Nishita { /// Ray Origin (Default: `(0.0, 6372e3, 0.0)`). /// diff --git a/src/plugin.rs b/src/plugin.rs index b437014..ce2ff72 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -1,7 +1,7 @@ //! Provides a `Plugin` for making skyboxes with procedural sky textures. use bevy::{ - asset::embedded_asset, + asset::load_internal_asset, pbr::{NotShadowCaster, NotShadowReceiver}, prelude::*, render::{ @@ -14,7 +14,7 @@ use bevy::{ use crate::{ model::AddAtmosphereModel, pipeline::*, - skybox::{AtmosphereSkyBoxMaterial, SkyBoxMaterial}, + skybox::{AtmosphereSkyBoxMaterial, SkyBoxMaterial, ATMOSPHERE_SKYBOX_SHADER_HANDLE}, }; /// A `Plugin` that adds the prerequisites for a procedural sky. @@ -23,7 +23,12 @@ pub struct AtmospherePlugin; impl Plugin for AtmospherePlugin { fn build(&self, app: &mut App) { - embedded_asset!(app, "src/", "shaders/skybox.wgsl"); + load_internal_asset!( + app, + ATMOSPHERE_SKYBOX_SHADER_HANDLE, + "shaders/skybox.wgsl", + Shader::from_wgsl + ); app.add_plugins(MaterialPlugin::::default()); diff --git a/src/skybox.rs b/src/skybox.rs index 3758780..bd7e503 100644 --- a/src/skybox.rs +++ b/src/skybox.rs @@ -1,7 +1,6 @@ //! Provides types and data needed for rendering a skybox. use bevy::{ - asset::AssetPath, pbr::{MaterialPipeline, MaterialPipelineKey}, prelude::*, reflect::{TypePath, TypeUuid}, @@ -15,6 +14,10 @@ use bevy::{ #[derive(Resource)] pub struct AtmosphereSkyBoxMaterial(pub Handle); +/// The `Handle` for the shader for the [`SkyBoxMaterial`]. +pub const ATMOSPHERE_SKYBOX_SHADER_HANDLE: Handle = + Handle::weak_from_u128(4511926918914205353); + /// The `Material` that renders skyboxes. #[derive(AsBindGroup, TypeUuid, TypePath, Debug, Clone, Asset)] #[uuid = "b460ff90-0ee4-42df-875f-0a62ecd1301c"] @@ -37,7 +40,7 @@ pub struct SkyBoxMaterialKey { impl Material for SkyBoxMaterial { fn fragment_shader() -> ShaderRef { - AssetPath::parse("embedded://bevy_atmosphere/shaders/skybox.wgsl").into() + ATMOSPHERE_SKYBOX_SHADER_HANDLE.into() } fn specialize(