transparency_3d/
transparency_3d.rs

1//! Demonstrates how to use transparency in 3D.
2//! Shows the effects of different blend modes.
3//! The `fade_transparency` system smoothly changes the transparency over time.
4
5use bevy::{math::ops, prelude::*};
6
7fn main() {
8    App::new()
9        .add_plugins(DefaultPlugins)
10        .add_systems(Startup, setup)
11        .add_systems(Update, fade_transparency)
12        .run();
13}
14
15fn setup(
16    mut commands: Commands,
17    mut meshes: ResMut<Assets<Mesh>>,
18    mut materials: ResMut<Assets<StandardMaterial>>,
19) {
20    // Opaque plane, uses `alpha_mode: Opaque` by default
21    commands.spawn((
22        Mesh3d(meshes.add(Plane3d::default().mesh().size(6.0, 6.0))),
23        MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
24    ));
25
26    // Transparent sphere, uses `alpha_mode: Mask(f32)`
27    commands.spawn((
28        Mesh3d(meshes.add(Sphere::new(0.5).mesh().ico(3).unwrap())),
29        MeshMaterial3d(materials.add(StandardMaterial {
30            // Alpha channel of the color controls transparency.
31            // We set it to 0.0 here, because it will be changed over time in the
32            // `fade_transparency` function.
33            // Note that the transparency has no effect on the objects shadow.
34            base_color: Color::srgba(0.2, 0.7, 0.1, 0.0),
35            // Mask sets a cutoff for transparency. Alpha values below are fully transparent,
36            // alpha values above are fully opaque.
37            alpha_mode: AlphaMode::Mask(0.5),
38            ..default()
39        })),
40        Transform::from_xyz(1.0, 0.5, -1.5),
41    ));
42
43    // Transparent unlit sphere, uses `alpha_mode: Mask(f32)`
44    commands.spawn((
45        Mesh3d(meshes.add(Sphere::new(0.5).mesh().ico(3).unwrap())),
46        MeshMaterial3d(materials.add(StandardMaterial {
47            base_color: Color::srgba(0.2, 0.7, 0.1, 0.0),
48            alpha_mode: AlphaMode::Mask(0.5),
49            unlit: true,
50            ..default()
51        })),
52        Transform::from_xyz(-1.0, 0.5, -1.5),
53    ));
54
55    // Transparent cube, uses `alpha_mode: Blend`
56    commands.spawn((
57        Mesh3d(meshes.add(Cuboid::default())),
58        // Notice how there is no need to set the `alpha_mode` explicitly here.
59        // When converting a color to a material using `into()`, the alpha mode is
60        // automatically set to `Blend` if the alpha channel is anything lower than 1.0.
61        MeshMaterial3d(materials.add(Color::srgba(0.5, 0.5, 1.0, 0.0))),
62        Transform::from_xyz(0.0, 0.5, 0.0),
63    ));
64
65    // Transparent cube, uses `alpha_mode: AlphaToCoverage`
66    commands.spawn((
67        Mesh3d(meshes.add(Cuboid::default())),
68        MeshMaterial3d(materials.add(StandardMaterial {
69            base_color: Color::srgba(0.5, 1.0, 0.5, 0.0),
70            alpha_mode: AlphaMode::AlphaToCoverage,
71            ..default()
72        })),
73        Transform::from_xyz(-1.5, 0.5, 0.0),
74    ));
75
76    // Opaque sphere
77    commands.spawn((
78        Mesh3d(meshes.add(Sphere::new(0.5).mesh().ico(3).unwrap())),
79        MeshMaterial3d(materials.add(Color::srgb(0.7, 0.2, 0.1))),
80        Transform::from_xyz(0.0, 0.5, -1.5),
81    ));
82
83    // Light
84    commands.spawn((
85        PointLight {
86            shadows_enabled: true,
87            ..default()
88        },
89        Transform::from_xyz(4.0, 8.0, 4.0),
90    ));
91
92    // Camera
93    commands.spawn((
94        Camera3d::default(),
95        Transform::from_xyz(-2.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
96    ));
97}
98
99/// Fades the alpha channel of all materials between 0 and 1 over time.
100/// Each blend mode responds differently to this:
101/// - [`Opaque`](AlphaMode::Opaque): Ignores alpha channel altogether, these materials stay completely opaque.
102/// - [`Mask(f32)`](AlphaMode::Mask): Object appears when the alpha value goes above the mask's threshold, disappears
103///   when the alpha value goes back below the threshold.
104/// - [`Blend`](AlphaMode::Blend): Object fades in and out smoothly.
105/// - [`AlphaToCoverage`](AlphaMode::AlphaToCoverage): Object fades in and out
106///   in steps corresponding to the number of multisample antialiasing (MSAA)
107///   samples in use. For example, assuming 8xMSAA, the object will be
108///   completely opaque, then will be 7/8 opaque (1/8 transparent), then will be
109///   6/8 opaque, then 5/8, etc.
110pub fn fade_transparency(time: Res<Time>, mut materials: ResMut<Assets<StandardMaterial>>) {
111    let alpha = (ops::sin(time.elapsed_secs()) / 2.0) + 0.5;
112    for (_, material) in materials.iter_mut() {
113        material.base_color.set_alpha(alpha);
114    }
115}