Skip to content

Commit 79fe77b

Browse files
IceSentryItsDoot
authored andcommitted
set alpha_mode based on alpha value (bevyengine#4658)
# Objective - When spawning a sprite the alpha is used for transparency, but when using the `Color::into()` implementation to spawn a `StandardMaterial`, the alpha is ignored. - Pretty much everytime I want to make something transparent I started with a `Color::rgb().into()` and I'm always surprised that it doesn't work when changing it to `Color::rgba().into()` - It's possible there's an issue with this approach I am not thinking of, but I'm not sure what's the point of setting an alpha value without the goal of making a color transparent. ## Solution - Set the alpha_mode to AlphaMode::Blend when the alpha is not the default value. --- ## Migration Guide This is not a breaking change, but it can easily be migrated to reduce boilerplate ```rust commands.spawn_bundle(PbrBundle { mesh: meshes.add(shape::Cube::default().into()), material: materials.add(StandardMaterial { base_color: Color::rgba(1.0, 0.0, 0.0, 0.75), alpha_mode: AlphaMode::Blend, ..default() }), ..default() }); // becomes commands.spawn_bundle(PbrBundle { mesh: meshes.add(shape::Cube::default().into()), material: materials.add(Color::rgba(1.0, 0.0, 0.0, 0.75).into()), ..default() }); ``` Co-authored-by: Charles <[email protected]>
1 parent 26e2182 commit 79fe77b

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

crates/bevy_pbr/src/pbr_material.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ impl From<Color> for StandardMaterial {
100100
fn from(color: Color) -> Self {
101101
StandardMaterial {
102102
base_color: color,
103+
alpha_mode: if color.a() < 1.0 {
104+
AlphaMode::Blend
105+
} else {
106+
AlphaMode::Opaque
107+
},
103108
..Default::default()
104109
}
105110
}

0 commit comments

Comments
 (0)