From 520a09a083ea6e660d79a938e7ec0ddc7108c448 Mon Sep 17 00:00:00 2001 From: BrayMatter <77391373+Braymatter@users.noreply.github.com> Date: Wed, 15 Nov 2023 06:48:36 -0600 Subject: [PATCH] Ensure ExtendedMaterial works with reflection (to enable bevy_egui_inspector integration) (#10548) # Objective - Ensure ExtendedMaterial can be referenced in bevy_egui_inspector correctly ## Solution Add a more manual `TypePath` implementation to work around bugs in the derive macro. --- crates/bevy_pbr/src/extended_material.rs | 9 +++++++-- examples/shader/extended_material.rs | 3 +-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/bevy_pbr/src/extended_material.rs b/crates/bevy_pbr/src/extended_material.rs index 096c3f6315d1b..260051d05098a 100644 --- a/crates/bevy_pbr/src/extended_material.rs +++ b/crates/bevy_pbr/src/extended_material.rs @@ -1,5 +1,5 @@ use bevy_asset::{Asset, Handle}; -use bevy_reflect::TypePath; +use bevy_reflect::{impl_type_path, Reflect}; use bevy_render::{ mesh::MeshVertexBufferLayout, render_asset::RenderAssets, @@ -97,12 +97,17 @@ pub trait MaterialExtension: Asset + AsBindGroup + Clone + Sized { /// When used with `StandardMaterial` as the base, all the standard material fields are /// present, so the `pbr_fragment` shader functions can be called from the extension shader (see /// the `extended_material` example). -#[derive(Asset, Clone, TypePath)] +#[derive(Asset, Clone, Reflect)] +#[reflect(type_path = false)] pub struct ExtendedMaterial { pub base: B, pub extension: E, } +// We don't use the `TypePath` derive here due to a bug where `#[reflect(type_path = false)]` +// causes the `TypePath` derive to not generate an implementation. +impl_type_path!((in bevy_pbr::extended_material) ExtendedMaterial); + impl AsBindGroup for ExtendedMaterial { type Data = (::Data, ::Data); diff --git a/examples/shader/extended_material.rs b/examples/shader/extended_material.rs index 704537423708a..7f055a9bef860 100644 --- a/examples/shader/extended_material.rs +++ b/examples/shader/extended_material.rs @@ -1,6 +1,5 @@ //! Demonstrates using a custom extension to the `StandardMaterial` to modify the results of the builtin pbr shader. -use bevy::reflect::TypePath; use bevy::{ pbr::{ExtendedMaterial, MaterialExtension, OpaqueRendererMethod}, prelude::*, @@ -73,7 +72,7 @@ fn rotate_things(mut q: Query<&mut Transform, With>, time: Res