Struct tetra::graphics::BlendState

source ·
pub struct BlendState {
    pub color_operation: BlendOperation,
    pub color_src: BlendFactor,
    pub color_dst: BlendFactor,
    pub alpha_operation: BlendOperation,
    pub alpha_src: BlendFactor,
    pub alpha_dst: BlendFactor,
}
Expand description

Defines how colors should be blended when drawing to the screen.

The blend state can be changed by calling set_blend_state or reset_blend_state.

There are constructors for the most common configurations, but if you know what you’re doing, you can set each part of the blend config manually via the fields on this struct.

What is blending?

Blending is how we determine the result of drawing one color on top of another one. This is what lets you (among other things) draw semi-transparent objects and see their colors mix together!

There are two steps to blending:

  • First, the source and destination colors are factored (or in simpler terms, multiplied) by values. This determines how much the source and destination contribute to the final output. The RGB and alpha components of each color can have different factors applied.
  • Then, an operation (aka a function or an equation) is performed on the two factored values. Again, the RGB and alpha components can be combined via two different operations.

This is all quite abstract, so here’s an example of how the default alpha blending BlendState works:

  • We try to draw the color (1.0, 0.2, 0.2, 0.5) on top of the color (0.2, 1.0, 0.2, 1.0), which requires a blend to take place.
  • The RGB components of the source color are factored by the alpha of the source color, which gives (0.5, 0.1, 0.1, 0.5). The alpha component is left as it is.
  • The entire destination color is factored by the alpha of the source color, which gives (0.25, 0.05, 0.05, 0.5).
  • The ‘add’ operation is applied to the two colors, giving us (0.75, 0.15, 0.15, 1.0) as the final color.

Notice that the resulting color is fully opaque and is made up of 50% of the source RGB, and 50% of the destination RGB - which is exactly what we’d expect when we’re drawing something that’s 50% transparent!

For a more in-depth explanation of blending, see this page on Learn OpenGL.

Fields§

§color_operation: BlendOperation

The operation that should be applied to the RGB components of the source and destination colors.

§color_src: BlendFactor

The factor that should be applied to the RGB components of the source color.

§color_dst: BlendFactor

The factor that should be applied to the RGB components of the destination color.

§alpha_operation: BlendOperation

The operation that should be applied to the alpha components of the source and destination colors.

§alpha_src: BlendFactor

The factor that should be applied to the alpha component of the source color.

§alpha_dst: BlendFactor

The factor that should be applied to the alpha component of the destination color.

Implementations§

source§

impl BlendState

source

pub const fn alpha(premultiplied: bool) -> BlendState

The alpha of the drawn content will determine its opacity.

If premultiplied is false, the RGB components of the color will be multiplied by the alpha component before blending with the target. If it is true, this step will be skipped, and you will need to do it yourself (e.g. in your own code, or your asset pipeline).

For more information on premultiplied alpha, and why you might want to use it, see these blog posts.

source

pub const fn add(premultiplied: bool) -> BlendState

The pixel colors of the drawn content will be added to the pixel colors already in the target.

The target’s alpha will not be affected.

If premultiplied is false, the RGB components of the color will be multiplied by the alpha component before blending with the target. If it is true, this step will be skipped, and you will need to do it yourself (e.g. in your own code, or your asset pipeline).

For more information on premultiplied alpha, and why you might want to use it, see these blog posts.

source

pub const fn subtract(premultiplied: bool) -> BlendState

The pixel colors of the drawn content will be subtracted from the pixel colors already in the target.

The target’s alpha will not be affected.

If premultiplied is false, the RGB components of the color will be multiplied by the alpha component before blending with the target. If it is true, this step will be skipped, and you will need to do it yourself (e.g. in your own code, or your asset pipeline).

For more information on premultiplied alpha, and why you might want to use it, see these blog posts.

source

pub const fn multiply() -> BlendState

The pixel colors of the drawn content will be multiplied with the pixel colors already in the target.

The alpha component will also be multiplied.

Trait Implementations§

source§

impl Clone for BlendState

source§

fn clone(&self) -> BlendState

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for BlendState

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for BlendState

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl PartialEq<BlendState> for BlendState

source§

fn eq(&self, other: &BlendState) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for BlendState

source§

impl Eq for BlendState

source§

impl StructuralEq for BlendState

source§

impl StructuralPartialEq for BlendState

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<F, T> IntoSample<T> for Fwhere T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> ToSample<U> for Twhere U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<S, T> Duplex<S> for Twhere T: FromSample<S> + ToSample<S>,