 de004da8d5
			
		
	
	
		de004da8d5
		
			
		
	
	
	
	
		
			
			# Objective The migration process for `bevy_color` (#12013) will be fairly involved: there will be hundreds of affected files, and a large number of APIs. ## Solution To allow us to proceed granularly, we're going to keep both `bevy_color::Color` (new) and `bevy_render::Color` (old) around until the migration is complete. However, simply doing this directly is confusing! They're both called `Color`, making it very hard to tell when a portion of the code has been ported. As discussed in #12056, by renaming the old `Color` type, we can make it easier to gradually migrate over, one API at a time. ## Migration Guide THIS MIGRATION GUIDE INTENTIONALLY LEFT BLANK. This change should not be shipped to end users: delete this section in the final migration guide! --------- Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com>
		
			
				
	
	
		
			88 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			88 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! A shader that reads a mesh's custom vertex attribute.
 | |
| 
 | |
| use bevy::{
 | |
|     pbr::{MaterialPipeline, MaterialPipelineKey},
 | |
|     prelude::*,
 | |
|     reflect::TypePath,
 | |
|     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 mesh = Mesh::from(Cuboid::default())
 | |
|         // Sets the custom attribute
 | |
|         .with_inserted_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: LegacyColor::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(Asset, TypePath, AsBindGroup, Debug, Clone)]
 | |
| struct CustomMaterial {
 | |
|     #[uniform(0)]
 | |
|     color: LegacyColor,
 | |
| }
 | |
| 
 | |
| 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(())
 | |
|     }
 | |
| }
 |