mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 06:30:19 +00:00
5abc32ceda
This PR is based on top of #12982 # Objective - Mesh2d currently only has an alpha blended phase. Most sprites don't need transparency though. - For some 2d games it can be useful to have a 2d depth buffer ## Solution - Add an opaque phase to render Mesh2d that don't need transparency - This phase currently uses the `SortedRenderPhase` to make it easier to implement based on the already existing transparent phase. A follow up PR will switch this to `BinnedRenderPhase`. - Add a 2d depth buffer - Use that depth buffer in the transparent phase to make sure that sprites and transparent mesh2d are displayed correctly ## Testing I added the mesh2d_transforms example that layers many opaque and transparent mesh2d to make sure they all get displayed correctly. I also confirmed it works with sprites by modifying that example locally. --- ## Changelog - Added `AlphaMode2d` - Added `Opaque2d` render phase - Camera2d now have a `ViewDepthTexture` component ## Migration Guide - `ColorMaterial` now contains `AlphaMode2d`. To keep previous behaviour, use `AlphaMode::BLEND`. If you know your sprite is opaque, use `AlphaMode::OPAQUE` ## Follow up PRs - See tracking issue: #13265 --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: Christopher Biscardi <chris@christopherbiscardi.com>
97 lines
3.3 KiB
Rust
97 lines
3.3 KiB
Rust
//! This example is used to test how transforms interact with alpha modes for [`MaterialMesh2dBundle`] entities.
|
|
//! This makes sure the depth buffer is correctly being used for opaque and transparent 2d meshes
|
|
|
|
use bevy::{
|
|
color::palettes::css::{BLUE, GREEN, WHITE},
|
|
prelude::*,
|
|
sprite::{AlphaMode2d, MaterialMesh2dBundle},
|
|
};
|
|
|
|
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(Camera2dBundle::default());
|
|
|
|
let texture_handle = asset_server.load("branding/icon.png");
|
|
let mesh_handle = meshes.add(Rectangle::from_size(Vec2::splat(256.0)));
|
|
|
|
// opaque
|
|
// Each sprite should be square with the transparent parts being completely black
|
|
// The blue sprite should be on top with the white and green one behind it
|
|
commands.spawn(MaterialMesh2dBundle {
|
|
mesh: mesh_handle.clone().into(),
|
|
material: materials.add(ColorMaterial {
|
|
color: WHITE.into(),
|
|
alpha_mode: AlphaMode2d::Opaque,
|
|
texture: Some(texture_handle.clone()),
|
|
}),
|
|
transform: Transform::from_xyz(-400.0, 0.0, 0.0),
|
|
..default()
|
|
});
|
|
commands.spawn(MaterialMesh2dBundle {
|
|
mesh: mesh_handle.clone().into(),
|
|
material: materials.add(ColorMaterial {
|
|
color: BLUE.into(),
|
|
alpha_mode: AlphaMode2d::Opaque,
|
|
texture: Some(texture_handle.clone()),
|
|
}),
|
|
transform: Transform::from_xyz(-300.0, 0.0, 1.0),
|
|
..default()
|
|
});
|
|
commands.spawn(MaterialMesh2dBundle {
|
|
mesh: mesh_handle.clone().into(),
|
|
material: materials.add(ColorMaterial {
|
|
color: GREEN.into(),
|
|
alpha_mode: AlphaMode2d::Opaque,
|
|
texture: Some(texture_handle.clone()),
|
|
}),
|
|
transform: Transform::from_xyz(-200.0, 0.0, -1.0),
|
|
..default()
|
|
});
|
|
|
|
// Test the interaction between opaque and transparent meshes
|
|
// The white sprite should be:
|
|
// - fully opaque
|
|
// - on top of the green sprite
|
|
// - behind the blue sprite
|
|
commands.spawn(MaterialMesh2dBundle {
|
|
mesh: mesh_handle.clone().into(),
|
|
material: materials.add(ColorMaterial {
|
|
color: WHITE.into(),
|
|
alpha_mode: AlphaMode2d::Opaque,
|
|
texture: Some(texture_handle.clone()),
|
|
}),
|
|
transform: Transform::from_xyz(200.0, 0.0, 0.0),
|
|
..default()
|
|
});
|
|
commands.spawn(MaterialMesh2dBundle {
|
|
mesh: mesh_handle.clone().into(),
|
|
material: materials.add(ColorMaterial {
|
|
color: BLUE.with_alpha(0.7).into(),
|
|
alpha_mode: AlphaMode2d::Blend,
|
|
texture: Some(texture_handle.clone()),
|
|
}),
|
|
transform: Transform::from_xyz(300.0, 0.0, 1.0),
|
|
..default()
|
|
});
|
|
commands.spawn(MaterialMesh2dBundle {
|
|
mesh: mesh_handle.clone().into(),
|
|
material: materials.add(ColorMaterial {
|
|
color: GREEN.with_alpha(0.7).into(),
|
|
alpha_mode: AlphaMode2d::Blend,
|
|
texture: Some(texture_handle),
|
|
}),
|
|
transform: Transform::from_xyz(400.0, 0.0, -1.0),
|
|
..default()
|
|
});
|
|
}
|