mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 15:14:50 +00:00
f18f28874a
# Objective - Better consistency with `add_systems`. - Deprecating `add_plugin` in favor of a more powerful `add_plugins`. - Allow passing `Plugin` to `add_plugins`. - Allow passing tuples to `add_plugins`. ## Solution - `App::add_plugins` now takes an `impl Plugins` parameter. - `App::add_plugin` is deprecated. - `Plugins` is a new sealed trait that is only implemented for `Plugin`, `PluginGroup` and tuples over `Plugins`. - All examples, benchmarks and tests are changed to use `add_plugins`, using tuples where appropriate. --- ## Changelog ### Changed - `App::add_plugins` now accepts all types that implement `Plugins`, which is implemented for: - Types that implement `Plugin`. - Types that implement `PluginGroup`. - Tuples (up to 16 elements) over types that implement `Plugins`. - Deprecated `App::add_plugin` in favor of `App::add_plugins`. ## Migration Guide - Replace `app.add_plugin(plugin)` calls with `app.add_plugins(plugin)`. --------- Co-authored-by: Carter Anderson <mcanders1@gmail.com>
87 lines
2.7 KiB
Rust
87 lines
2.7 KiB
Rust
//! A shader that reads a mesh's custom vertex attribute.
|
|
|
|
use bevy::{
|
|
pbr::{MaterialPipeline, MaterialPipelineKey},
|
|
prelude::*,
|
|
reflect::{TypePath, TypeUuid},
|
|
render::{
|
|
mesh::{MeshVertexAttribute, MeshVertexBufferLayout},
|
|
render_resource::{
|
|
AsBindGroup, RenderPipelineDescriptor, ShaderRef, SpecializedMeshPipelineError,
|
|
VertexFormat,
|
|
},
|
|
},
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))
|
|
.add_systems(Startup, setup)
|
|
.run();
|
|
}
|
|
|
|
// A "high" random id should be used for custom attributes to ensure consistent sorting and avoid collisions with other attributes.
|
|
// See the MeshVertexAttribute docs for more info.
|
|
const ATTRIBUTE_BLEND_COLOR: MeshVertexAttribute =
|
|
MeshVertexAttribute::new("BlendColor", 988540917, VertexFormat::Float32x4);
|
|
|
|
/// set up a simple 3D scene
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<CustomMaterial>>,
|
|
) {
|
|
let mut mesh = Mesh::from(shape::Cube { size: 1.0 });
|
|
mesh.insert_attribute(
|
|
ATTRIBUTE_BLEND_COLOR,
|
|
// The cube mesh has 24 vertices (6 faces, 4 vertices per face), so we insert one BlendColor for each
|
|
vec![[1.0, 0.0, 0.0, 1.0]; 24],
|
|
);
|
|
|
|
// cube
|
|
commands.spawn(MaterialMeshBundle {
|
|
mesh: meshes.add(mesh),
|
|
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
|
material: materials.add(CustomMaterial {
|
|
color: Color::WHITE,
|
|
}),
|
|
..default()
|
|
});
|
|
|
|
// camera
|
|
commands.spawn(Camera3dBundle {
|
|
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
..default()
|
|
});
|
|
}
|
|
|
|
// This is the struct that will be passed to your shader
|
|
#[derive(AsBindGroup, Debug, Clone, TypeUuid, TypePath)]
|
|
#[uuid = "f690fdae-d598-45ab-8225-97e2a3f056e0"]
|
|
pub struct CustomMaterial {
|
|
#[uniform(0)]
|
|
color: Color,
|
|
}
|
|
|
|
impl Material for CustomMaterial {
|
|
fn vertex_shader() -> ShaderRef {
|
|
"shaders/custom_vertex_attribute.wgsl".into()
|
|
}
|
|
fn fragment_shader() -> ShaderRef {
|
|
"shaders/custom_vertex_attribute.wgsl".into()
|
|
}
|
|
|
|
fn specialize(
|
|
_pipeline: &MaterialPipeline<Self>,
|
|
descriptor: &mut RenderPipelineDescriptor,
|
|
layout: &MeshVertexBufferLayout,
|
|
_key: MaterialPipelineKey<Self>,
|
|
) -> Result<(), SpecializedMeshPipelineError> {
|
|
let vertex_layout = layout.get_layout(&[
|
|
Mesh::ATTRIBUTE_POSITION.at_shader_location(0),
|
|
ATTRIBUTE_BLEND_COLOR.at_shader_location(1),
|
|
])?;
|
|
descriptor.vertex.buffers = vec![vertex_layout];
|
|
Ok(())
|
|
}
|
|
}
|