mesh2d_vertex_color_texture.rs:
use bevy::{
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
asset_server: Res<AssetServer>,
) {
let texture_handle = asset_server.load("branding/banner.png");
let mut mesh = Mesh::from(Rectangle::default());
let vertex_colors: Vec<[f32; 4]> = vec![
LinearRgba::RED.to_f32_array(),
LinearRgba::GREEN.to_f32_array(),
LinearRgba::BLUE.to_f32_array(),
LinearRgba::WHITE.to_f32_array(),
];
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, vertex_colors);
let mesh_handle: Mesh2dHandle = meshes.add(mesh).into();
commands.spawn(Camera2dBundle::default());
commands.spawn(MaterialMesh2dBundle {
mesh: mesh_handle.clone(),
transform: Transform::from_translation(Vec3::new(-96., 0., 0.))
.with_scale(Vec3::splat(128.)),
material: materials.add(ColorMaterial::default()),
..default()
});
commands.spawn(MaterialMesh2dBundle {
mesh: mesh_handle,
transform: Transform::from_translation(Vec3::new(96., 0., 0.))
.with_scale(Vec3::splat(128.)),
material: materials.add(texture_handle),
..default()
});
}