Skip to main content

bevy_material/
alpha.rs

1//! Allows configuring a material's transparency behavior.
2
3use bevy_reflect::{std_traits::ReflectDefault, Reflect};
4
5// TODO: add discussion about performance.
6/// Sets how a material's base color alpha channel is used for transparency.
7#[derive(Debug, Default, Reflect, Copy, Clone, PartialEq)]
8#[reflect(Default, Debug, Clone)]
9pub enum AlphaMode {
10    /// Base color alpha values are overridden to be fully opaque (1.0).
11    #[default]
12    Opaque,
13    /// Reduce transparency to fully opaque or fully transparent
14    /// based on a threshold.
15    ///
16    /// Compares the base color alpha value to the specified threshold.
17    /// If the value is below the threshold,
18    /// considers the color to be fully transparent (alpha is set to 0.0).
19    /// If it is equal to or above the threshold,
20    /// considers the color to be fully opaque (alpha is set to 1.0).
21    Mask(f32),
22    /// The base color alpha value defines the opacity of the color.
23    /// Standard alpha-blending is used to blend the fragment's color
24    /// with the color behind it.
25    Blend,
26    /// Similar to [`AlphaMode::Blend`], however assumes RGB channel values are
27    /// [premultiplied](https://en.wikipedia.org/wiki/Alpha_compositing#Straight_versus_premultiplied).
28    ///
29    /// For otherwise constant RGB values, behaves more like [`AlphaMode::Blend`] for
30    /// alpha values closer to 1.0, and more like [`AlphaMode::Add`] for
31    /// alpha values closer to 0.0.
32    ///
33    /// Can be used to avoid “border” or “outline” artifacts that can occur
34    /// when using plain alpha-blended textures.
35    Premultiplied,
36    /// Spreads the fragment out over a hardware-dependent number of sample
37    /// locations proportional to the alpha value. This requires multisample
38    /// antialiasing; if MSAA isn't on, this is identical to
39    /// [`AlphaMode::Mask`] with a value of 0.5.
40    ///
41    /// Alpha to coverage provides improved performance and better visual
42    /// fidelity over [`AlphaMode::Blend`], as Bevy doesn't have to sort objects
43    /// when it's in use. It's especially useful for complex transparent objects
44    /// like foliage.
45    ///
46    /// [alpha to coverage]: https://en.wikipedia.org/wiki/Alpha_to_coverage
47    AlphaToCoverage,
48    /// Combines the color of the fragments with the colors behind them in an
49    /// additive process, (i.e. like light) producing lighter results.
50    ///
51    /// Black produces no effect. Alpha values can be used to modulate the result.
52    ///
53    /// Useful for effects like holograms, ghosts, lasers and other energy beams.
54    Add,
55    /// Combines the color of the fragments with the colors behind them in a
56    /// multiplicative process, (i.e. like pigments) producing darker results.
57    ///
58    /// White produces no effect. Alpha values can be used to modulate the result.
59    ///
60    /// Useful for effects like stained glass, window tint film and some colored liquids.
61    Multiply,
62}
63
64impl Eq for AlphaMode {}