Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/2d/mesh2d_alpha_mode.rs
6592 views
1
//! This example is used to test how transforms interact with alpha modes for [`Mesh2d`] entities with a [`MeshMaterial2d`].
2
//! This makes sure the depth buffer is correctly being used for opaque and transparent 2d meshes
3
4
use bevy::{
5
color::palettes::css::{BLUE, GREEN, WHITE},
6
prelude::*,
7
sprite_render::AlphaMode2d,
8
};
9
10
fn main() {
11
App::new()
12
.add_plugins(DefaultPlugins)
13
.add_systems(Startup, setup)
14
.run();
15
}
16
17
fn setup(
18
mut commands: Commands,
19
asset_server: Res<AssetServer>,
20
mut meshes: ResMut<Assets<Mesh>>,
21
mut materials: ResMut<Assets<ColorMaterial>>,
22
) {
23
commands.spawn(Camera2d);
24
25
let texture_handle = asset_server.load("branding/icon.png");
26
let mesh_handle = meshes.add(Rectangle::from_size(Vec2::splat(256.0)));
27
28
// opaque
29
// Each sprite should be square with the transparent parts being completely black
30
// The blue sprite should be on top with the white and green one behind it
31
commands.spawn((
32
Mesh2d(mesh_handle.clone()),
33
MeshMaterial2d(materials.add(ColorMaterial {
34
color: WHITE.into(),
35
alpha_mode: AlphaMode2d::Opaque,
36
texture: Some(texture_handle.clone()),
37
..default()
38
})),
39
Transform::from_xyz(-400.0, 0.0, 0.0),
40
));
41
commands.spawn((
42
Mesh2d(mesh_handle.clone()),
43
MeshMaterial2d(materials.add(ColorMaterial {
44
color: BLUE.into(),
45
alpha_mode: AlphaMode2d::Opaque,
46
texture: Some(texture_handle.clone()),
47
..default()
48
})),
49
Transform::from_xyz(-300.0, 0.0, 1.0),
50
));
51
commands.spawn((
52
Mesh2d(mesh_handle.clone()),
53
MeshMaterial2d(materials.add(ColorMaterial {
54
color: GREEN.into(),
55
alpha_mode: AlphaMode2d::Opaque,
56
texture: Some(texture_handle.clone()),
57
..default()
58
})),
59
Transform::from_xyz(-200.0, 0.0, -1.0),
60
));
61
62
// Test the interaction between opaque/mask and transparent meshes
63
// The white sprite should be:
64
// - only the icon is opaque but background is transparent
65
// - on top of the green sprite
66
// - behind the blue sprite
67
commands.spawn((
68
Mesh2d(mesh_handle.clone()),
69
MeshMaterial2d(materials.add(ColorMaterial {
70
color: WHITE.into(),
71
alpha_mode: AlphaMode2d::Mask(0.5),
72
texture: Some(texture_handle.clone()),
73
..default()
74
})),
75
Transform::from_xyz(200.0, 0.0, 0.0),
76
));
77
commands.spawn((
78
Mesh2d(mesh_handle.clone()),
79
MeshMaterial2d(materials.add(ColorMaterial {
80
color: BLUE.with_alpha(0.7).into(),
81
alpha_mode: AlphaMode2d::Blend,
82
texture: Some(texture_handle.clone()),
83
..default()
84
})),
85
Transform::from_xyz(300.0, 0.0, 1.0),
86
));
87
commands.spawn((
88
Mesh2d(mesh_handle.clone()),
89
MeshMaterial2d(materials.add(ColorMaterial {
90
color: GREEN.with_alpha(0.7).into(),
91
alpha_mode: AlphaMode2d::Blend,
92
texture: Some(texture_handle),
93
..default()
94
})),
95
Transform::from_xyz(400.0, 0.0, -1.0),
96
));
97
}
98
99