mirror of
https://github.com/bevyengine/bevy
synced 2024-11-14 00:47:32 +00:00
1efc762924
# Objective
- Introduce a stable alternative to
[`std::any::type_name`](https://doc.rust-lang.org/std/any/fn.type_name.html).
- Rewrite of #5805 with heavy inspiration in design.
- On the path to #5830.
- Part of solving #3327.
## Solution
- Add a `TypePath` trait for static stable type path/name information.
- Add a `TypePath` derive macro.
- Add a `impl_type_path` macro for implementing internal and foreign
types in `bevy_reflect`.
---
## Changelog
- Added `TypePath` trait.
- Added `DynamicTypePath` trait and `get_type_path` method to `Reflect`.
- Added a `TypePath` derive macro.
- Added a `bevy_reflect::impl_type_path` for implementing `TypePath` on
internal and foreign types in `bevy_reflect`.
- Changed `bevy_reflect::utility::(Non)GenericTypeInfoCell` to
`(Non)GenericTypedCell<T>` which allows us to be generic over both
`TypeInfo` and `TypePath`.
- `TypePath` is now a supertrait of `Asset`, `Material` and
`Material2d`.
- `impl_reflect_struct` needs a `#[type_path = "..."]` attribute to be
specified.
- `impl_reflect_value` needs to either specify path starting with a
double colon (`::core::option::Option`) or an `in my_crate::foo`
declaration.
- Added `bevy_reflect_derive::ReflectTypePath`.
- Most uses of `Ident` in `bevy_reflect_derive` changed to use
`ReflectTypePath`.
## Migration Guide
- Implementors of `Asset`, `Material` and `Material2d` now also need to
derive `TypePath`.
- Manual implementors of `Reflect` will need to implement the new
`get_type_path` method.
## Open Questions
- [x] ~This PR currently does not migrate any usages of
`std::any::type_name` to use `bevy_reflect::TypePath` to ease the review
process. Should it?~ Migration will be left to a follow-up PR.
- [ ] This PR adds a lot of `#[derive(TypePath)]` and `T: TypePath` to
satisfy new bounds, mostly when deriving `TypeUuid`. Should we make
`TypePath` a supertrait of `TypeUuid`? [Should we remove `TypeUuid` in
favour of
`TypePath`?](2afbd85532 (r961067892)
)
102 lines
3 KiB
Rust
102 lines
3 KiB
Rust
//! A shader that uses "shaders defs", which selectively toggle parts of a shader.
|
|
|
|
use bevy::{
|
|
pbr::{MaterialPipeline, MaterialPipelineKey},
|
|
prelude::*,
|
|
reflect::{TypePath, TypeUuid},
|
|
render::{
|
|
mesh::MeshVertexBufferLayout,
|
|
render_resource::{
|
|
AsBindGroup, RenderPipelineDescriptor, ShaderRef, SpecializedMeshPipelineError,
|
|
},
|
|
},
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_plugin(MaterialPlugin::<CustomMaterial>::default())
|
|
.add_systems(Startup, setup)
|
|
.run();
|
|
}
|
|
|
|
/// set up a simple 3D scene
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<CustomMaterial>>,
|
|
) {
|
|
// blue cube
|
|
commands.spawn(MaterialMeshBundle {
|
|
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
|
|
transform: Transform::from_xyz(-1.0, 0.5, 0.0),
|
|
material: materials.add(CustomMaterial {
|
|
color: Color::BLUE,
|
|
is_red: false,
|
|
}),
|
|
..default()
|
|
});
|
|
|
|
// red cube (with green color overridden by the IS_RED "shader def")
|
|
commands.spawn(MaterialMeshBundle {
|
|
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
|
|
transform: Transform::from_xyz(1.0, 0.5, 0.0),
|
|
material: materials.add(CustomMaterial {
|
|
color: Color::GREEN,
|
|
is_red: true,
|
|
}),
|
|
..default()
|
|
});
|
|
|
|
// camera
|
|
commands.spawn(Camera3dBundle {
|
|
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
..default()
|
|
});
|
|
}
|
|
|
|
impl Material for CustomMaterial {
|
|
fn fragment_shader() -> ShaderRef {
|
|
"shaders/shader_defs.wgsl".into()
|
|
}
|
|
|
|
fn specialize(
|
|
_pipeline: &MaterialPipeline<Self>,
|
|
descriptor: &mut RenderPipelineDescriptor,
|
|
_layout: &MeshVertexBufferLayout,
|
|
key: MaterialPipelineKey<Self>,
|
|
) -> Result<(), SpecializedMeshPipelineError> {
|
|
if key.bind_group_data.is_red {
|
|
let fragment = descriptor.fragment.as_mut().unwrap();
|
|
fragment.shader_defs.push("IS_RED".into());
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
// This is the struct that will be passed to your shader
|
|
#[derive(AsBindGroup, TypeUuid, TypePath, Debug, Clone)]
|
|
#[uuid = "f690fdae-d598-45ab-8225-97e2a3f056e0"]
|
|
#[bind_group_data(CustomMaterialKey)]
|
|
pub struct CustomMaterial {
|
|
#[uniform(0)]
|
|
color: Color,
|
|
is_red: bool,
|
|
}
|
|
|
|
// This key is used to identify a specific permutation of this material pipeline.
|
|
// In this case, we specialize on whether or not to configure the "IS_RED" shader def.
|
|
// Specialization keys should be kept as small / cheap to hash as possible,
|
|
// as they will be used to look up the pipeline for each drawn entity with this material type.
|
|
#[derive(Eq, PartialEq, Hash, Clone)]
|
|
pub struct CustomMaterialKey {
|
|
is_red: bool,
|
|
}
|
|
|
|
impl From<&CustomMaterial> for CustomMaterialKey {
|
|
fn from(material: &CustomMaterial) -> Self {
|
|
Self {
|
|
is_red: material.is_red,
|
|
}
|
|
}
|
|
}
|