forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transparency examples (bevyengine#3695)
Adds examples demonstrating transparency for 2d, 3d and UI. Fixes bevyengine#3215.
- Loading branch information
Showing
5 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! Demonstrates how to use transparency in 2D. | ||
//! Shows 3 bevy logos on top of each other, each with a different amount of transparency. | ||
use bevy::prelude::*; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.add_startup_system(setup) | ||
.run(); | ||
} | ||
|
||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { | ||
commands.spawn_bundle(Camera2dBundle::default()); | ||
|
||
let sprite_handle = asset_server.load("branding/icon.png"); | ||
|
||
commands.spawn_bundle(SpriteBundle { | ||
texture: sprite_handle.clone(), | ||
..Default::default() | ||
}); | ||
commands.spawn_bundle(SpriteBundle { | ||
sprite: Sprite { | ||
// Alpha channel of the color controls transparency. | ||
color: Color::rgba(0.0, 0.0, 1.0, 0.7), | ||
..Default::default() | ||
}, | ||
texture: sprite_handle.clone(), | ||
transform: Transform::from_xyz(100.0, 0.0, 0.0), | ||
..Default::default() | ||
}); | ||
commands.spawn_bundle(SpriteBundle { | ||
sprite: Sprite { | ||
color: Color::rgba(0.0, 1.0, 0.0, 0.3), | ||
..Default::default() | ||
}, | ||
texture: sprite_handle, | ||
transform: Transform::from_xyz(200.0, 0.0, 0.0), | ||
..Default::default() | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
//! Demonstrates how to use transparency in 3D. | ||
//! Shows the effects of different blend modes. | ||
//! The `fade_transparency` system smoothly changes the transparency over time. | ||
use bevy::prelude::*; | ||
|
||
fn main() { | ||
App::new() | ||
.insert_resource(Msaa { samples: 4 }) | ||
.add_plugins(DefaultPlugins) | ||
.add_startup_system(setup) | ||
.add_system(fade_transparency) | ||
.run(); | ||
} | ||
|
||
#[derive(Component)] | ||
pub struct FadeTransparency; | ||
|
||
fn setup( | ||
mut commands: Commands, | ||
mut meshes: ResMut<Assets<Mesh>>, | ||
mut materials: ResMut<Assets<StandardMaterial>>, | ||
) { | ||
// Opaque plane | ||
commands.spawn_bundle(PbrBundle { | ||
mesh: meshes.add(Mesh::from(shape::Plane { size: 6.0 })), | ||
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), | ||
..Default::default() | ||
}); | ||
// transparent sphere, using alpha_mode: Mask | ||
commands.spawn_bundle(PbrBundle { | ||
mesh: meshes.add(Mesh::from(shape::Icosphere { | ||
radius: 0.5, | ||
subdivisions: 3, | ||
})), | ||
material: materials.add(StandardMaterial { | ||
// Alpha channel of the color controls transparency. | ||
// We set it to 0.0 here, because it will be changed over time in the | ||
// `fade_transparency` function. | ||
// Note that the transparency has no effect on the objects shadow. | ||
base_color: Color::rgba(0.2, 0.7, 0.1, 0.0), | ||
// Maks sets a cutoff for transparency. Alpha values below are fully transparent, | ||
// alpha values above are fully opaque. | ||
alpha_mode: AlphaMode::Mask(0.5), | ||
..default() | ||
}), | ||
transform: Transform::from_xyz(1.0, 0.5, -1.5), | ||
..Default::default() | ||
}); | ||
// transparent cube, using alpha_mode: Blend | ||
commands.spawn_bundle(PbrBundle { | ||
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), | ||
// Notice how there is no need to set the `alpha_mode` explicitly here. | ||
// When converting a color to a material using `into()`, the alpha mode is | ||
// automatically set to `Blend` if the alpha channel is anything lower than 1.0. | ||
material: materials.add(Color::rgba(0.5, 0.5, 1.0, 0.0).into()), | ||
transform: Transform::from_xyz(0.0, 0.5, 0.0), | ||
..Default::default() | ||
}); | ||
// sphere | ||
commands.spawn_bundle(PbrBundle { | ||
mesh: meshes.add(Mesh::from(shape::Icosphere { | ||
radius: 0.5, | ||
subdivisions: 3, | ||
})), | ||
material: materials.add(Color::rgb(0.7, 0.2, 0.1).into()), | ||
transform: Transform::from_xyz(0.0, 0.5, -1.5), | ||
..Default::default() | ||
}); | ||
// light | ||
commands.spawn_bundle(PointLightBundle { | ||
point_light: PointLight { | ||
intensity: 1500.0, | ||
shadows_enabled: true, | ||
..Default::default() | ||
}, | ||
transform: Transform::from_xyz(4.0, 8.0, 4.0), | ||
..Default::default() | ||
}); | ||
// camera | ||
commands.spawn_bundle(Camera3dBundle { | ||
transform: Transform::from_xyz(-2.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), | ||
..Default::default() | ||
}); | ||
} | ||
|
||
/// Fades the alpha channel of all materials between 0 and 1 over time. | ||
/// Each blend mode responds differently to this: | ||
/// - `Opaque`: Ignores alpha channel altogether, these materials stay completely opaque. | ||
/// - `Mask(f32)`: Object appears when the alpha value goes above the mask's threshold, disappears | ||
/// when the alpha value goes back below the threshold. | ||
/// - `Blend`: Object fades in and out smoothly. | ||
pub fn fade_transparency(time: Res<Time>, mut materials: ResMut<Assets<StandardMaterial>>) { | ||
let alpha = (time.time_since_startup().as_secs_f32().sin() / 2.0) + 0.5; | ||
for (_, material) in materials.iter_mut() { | ||
material.base_color.set_a(alpha); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//! Demonstrates how to use transparency with UI. | ||
//! Shows two colored buttons with transparent text. | ||
use bevy::prelude::*; | ||
|
||
fn main() { | ||
App::new() | ||
.insert_resource(ClearColor(Color::BLACK)) | ||
.add_plugins(DefaultPlugins) | ||
.add_startup_system(setup) | ||
.run(); | ||
} | ||
|
||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { | ||
commands.spawn_bundle(Camera2dBundle::default()); | ||
|
||
let font_handle = asset_server.load("fonts/FiraSans-Bold.ttf"); | ||
|
||
commands | ||
.spawn_bundle(ButtonBundle { | ||
style: Style { | ||
size: Size::new(Val::Px(150.0), Val::Px(65.0)), | ||
margin: UiRect::all(Val::Auto), | ||
justify_content: JustifyContent::Center, | ||
align_items: AlignItems::Center, | ||
..Default::default() | ||
}, | ||
color: Color::rgb(0.1, 0.5, 0.1).into(), | ||
..Default::default() | ||
}) | ||
.with_children(|parent| { | ||
parent.spawn_bundle(TextBundle { | ||
text: Text::with_section( | ||
"Button 1", | ||
TextStyle { | ||
font: font_handle.clone(), | ||
font_size: 40.0, | ||
// Alpha channel of the color controls transparency. | ||
color: Color::rgba(1.0, 1.0, 1.0, 0.2), | ||
}, | ||
Default::default(), | ||
), | ||
..Default::default() | ||
}); | ||
}); | ||
|
||
// Button with a different color, | ||
// to demonstrate the text looks different due to its transparency. | ||
commands | ||
.spawn_bundle(ButtonBundle { | ||
style: Style { | ||
size: Size::new(Val::Px(150.0), Val::Px(65.0)), | ||
margin: UiRect::all(Val::Auto), | ||
justify_content: JustifyContent::Center, | ||
align_items: AlignItems::Center, | ||
..Default::default() | ||
}, | ||
color: Color::rgb(0.5, 0.1, 0.5).into(), | ||
..Default::default() | ||
}) | ||
.with_children(|parent| { | ||
parent.spawn_bundle(TextBundle { | ||
text: Text::with_section( | ||
"Button 2", | ||
TextStyle { | ||
font: font_handle.clone(), | ||
font_size: 40.0, | ||
// Alpha channel of the color controls transparency. | ||
color: Color::rgba(1.0, 1.0, 1.0, 0.2), | ||
}, | ||
Default::default(), | ||
), | ||
..Default::default() | ||
}); | ||
}); | ||
} |