mesh2d_alpha_mode.rs:
use bevy::{
color::palettes::css::{BLUE, GREEN, WHITE},
prelude::*,
sprite::AlphaMode2d,
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.run();
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands.spawn(Camera2d);
let texture_handle = asset_server.load("branding/icon.png");
let mesh_handle = meshes.add(Rectangle::from_size(Vec2::splat(256.0)));
commands.spawn((
Mesh2d(mesh_handle.clone()),
MeshMaterial2d(materials.add(ColorMaterial {
color: WHITE.into(),
alpha_mode: AlphaMode2d::Opaque,
texture: Some(texture_handle.clone()),
})),
Transform::from_xyz(-400.0, 0.0, 0.0),
));
commands.spawn((
Mesh2d(mesh_handle.clone()),
MeshMaterial2d(materials.add(ColorMaterial {
color: BLUE.into(),
alpha_mode: AlphaMode2d::Opaque,
texture: Some(texture_handle.clone()),
})),
Transform::from_xyz(-300.0, 0.0, 1.0),
));
commands.spawn((
Mesh2d(mesh_handle.clone()),
MeshMaterial2d(materials.add(ColorMaterial {
color: GREEN.into(),
alpha_mode: AlphaMode2d::Opaque,
texture: Some(texture_handle.clone()),
})),
Transform::from_xyz(-200.0, 0.0, -1.0),
));
commands.spawn((
Mesh2d(mesh_handle.clone()),
MeshMaterial2d(materials.add(ColorMaterial {
color: WHITE.into(),
alpha_mode: AlphaMode2d::Mask(0.5),
texture: Some(texture_handle.clone()),
})),
Transform::from_xyz(200.0, 0.0, 0.0),
));
commands.spawn((
Mesh2d(mesh_handle.clone()),
MeshMaterial2d(materials.add(ColorMaterial {
color: BLUE.with_alpha(0.7).into(),
alpha_mode: AlphaMode2d::Blend,
texture: Some(texture_handle.clone()),
})),
Transform::from_xyz(300.0, 0.0, 1.0),
));
commands.spawn((
Mesh2d(mesh_handle.clone()),
MeshMaterial2d(materials.add(ColorMaterial {
color: GREEN.with_alpha(0.7).into(),
alpha_mode: AlphaMode2d::Blend,
texture: Some(texture_handle),
})),
Transform::from_xyz(400.0, 0.0, -1.0),
));
}