mirror of
https://github.com/bevyengine/bevy
synced 2025-01-21 09:34:29 +00:00
603cb439d9
# Objective - This PR adds support for blend modes to the PBR `StandardMaterial`. <img width="1392" alt="Screenshot 2022-11-18 at 20 00 56" src="https://user-images.githubusercontent.com/418473/202820627-0636219a-a1e5-437a-b08b-b08c6856bf9c.png"> <img width="1392" alt="Screenshot 2022-11-18 at 20 01 01" src="https://user-images.githubusercontent.com/418473/202820615-c8d43301-9a57-49c4-bd21-4ae343c3e9ec.png"> ## Solution - The existing `AlphaMode` enum is extended, adding three more modes: `AlphaMode::Premultiplied`, `AlphaMode::Add` and `AlphaMode::Multiply`; - All new modes are rendered in the existing `Transparent3d` phase; - The existing mesh flags for alpha mode are reorganized for a more compact/efficient representation, and new values are added; - `MeshPipelineKey::TRANSPARENT_MAIN_PASS` is refactored into `MeshPipelineKey::BLEND_BITS`. - `AlphaMode::Opaque` and `AlphaMode::Mask(f32)` share a single opaque pipeline key: `MeshPipelineKey::BLEND_OPAQUE`; - `Blend`, `Premultiplied` and `Add` share a single premultiplied alpha pipeline key, `MeshPipelineKey::BLEND_PREMULTIPLIED_ALPHA`. In the shader, color values are premultiplied accordingly (or not) depending on the blend mode to produce the three different results after PBR/tone mapping/dithering; - `Multiply` uses its own independent pipeline key, `MeshPipelineKey::BLEND_MULTIPLY`; - Example and documentation are provided. --- ## Changelog ### Added - Added support for additive and multiplicative blend modes in the PBR `StandardMaterial`, via `AlphaMode::Add` and `AlphaMode::Multiply`; - Added support for premultiplied alpha in the PBR `StandardMaterial`, via `AlphaMode::Premultiplied`;
52 lines
2.3 KiB
Rust
52 lines
2.3 KiB
Rust
use bevy_ecs::{component::Component, reflect::ReflectComponent};
|
|
use bevy_reflect::std_traits::ReflectDefault;
|
|
use bevy_reflect::{FromReflect, Reflect};
|
|
|
|
// TODO: add discussion about performance.
|
|
/// Sets how a material's base color alpha channel is used for transparency.
|
|
#[derive(Component, Debug, Default, Reflect, Copy, Clone, PartialEq, FromReflect)]
|
|
#[reflect(Component, Default, Debug)]
|
|
pub enum AlphaMode {
|
|
/// Base color alpha values are overridden to be fully opaque (1.0).
|
|
#[default]
|
|
Opaque,
|
|
/// Reduce transparency to fully opaque or fully transparent
|
|
/// based on a threshold.
|
|
///
|
|
/// Compares the base color alpha value to the specified threshold.
|
|
/// If the value is below the threshold,
|
|
/// considers the color to be fully transparent (alpha is set to 0.0).
|
|
/// If it is equal to or above the threshold,
|
|
/// considers the color to be fully opaque (alpha is set to 1.0).
|
|
Mask(f32),
|
|
/// The base color alpha value defines the opacity of the color.
|
|
/// Standard alpha-blending is used to blend the fragment's color
|
|
/// with the color behind it.
|
|
Blend,
|
|
/// Similar to [`AlphaMode::Blend`], however assumes RGB channel values are
|
|
/// [premultiplied](https://en.wikipedia.org/wiki/Alpha_compositing#Straight_versus_premultiplied).
|
|
///
|
|
/// For otherwise constant RGB values, behaves more like [`AlphaMode::Blend`] for
|
|
/// alpha values closer to 1.0, and more like [`AlphaMode::Add`] for
|
|
/// alpha values closer to 0.0.
|
|
///
|
|
/// Can be used to avoid “border” or “outline” artifacts that can occur
|
|
/// when using plain alpha-blended textures.
|
|
Premultiplied,
|
|
/// Combines the color of the fragments with the colors behind them in an
|
|
/// additive process, (i.e. like light) producing lighter results.
|
|
///
|
|
/// Black produces no effect. Alpha values can be used to modulate the result.
|
|
///
|
|
/// Useful for effects like holograms, ghosts, lasers and other energy beams.
|
|
Add,
|
|
/// Combines the color of the fragments with the colors behind them in a
|
|
/// multiplicative process, (i.e. like pigments) producing darker results.
|
|
///
|
|
/// White produces no effect. Alpha values can be used to modulate the result.
|
|
///
|
|
/// Useful for effects like stained glass, window tint film and some colored liquids.
|
|
Multiply,
|
|
}
|
|
|
|
impl Eq for AlphaMode {}
|