Skip to content

Commit

Permalink
simplify the alpha blending code
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulghost committed Jun 9, 2024
1 parent edb2d7a commit 104e5f0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 148 deletions.
Binary file removed assets/orange_circle.png
Binary file not shown.
123 changes: 0 additions & 123 deletions examples/additive.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub enum SimulationCondition {
/// rendered during the [`Transparent2d`] render phase.
///
/// [`Transparent2d`]: bevy::core_pipeline::core_2d::Transparent2d
#[derive(Debug, Default, Clone, Copy, PartialEq, Reflect, Serialize, Deserialize)]
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Reflect, Serialize, Deserialize, Hash)]
#[non_exhaustive]
pub enum AlphaMode {
/// Render the effect with alpha blending.
Expand Down
34 changes: 10 additions & 24 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,15 +984,6 @@ enum PipelineMode {
Camera3d,
}

#[cfg(all(feature = "2d", feature = "3d"))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
enum AlphaBlendMode {
Alpha,
Premultiply,
Add,
Multiply,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub(crate) struct ParticleRenderPipelineKey {
/// Render shader, with snippets applied, but not preprocessed yet.
Expand All @@ -1012,7 +1003,7 @@ pub(crate) struct ParticleRenderPipelineKey {
/// The effect is rendered with alpha masking.
use_alpha_mask: bool,
/// The effect needs Alpha blend.
alpha_blend_mode: AlphaBlendMode,
alpha_mode: AlphaMode,
/// Key: FLIPBOOK
/// The effect is rendered with flipbook texture animation based on the
/// sprite index of each particle.
Expand All @@ -1039,7 +1030,7 @@ impl Default for ParticleRenderPipelineKey {
has_image: false,
local_space_simulation: false,
use_alpha_mask: false,
alpha_blend_mode: AlphaBlendMode::Alpha,
alpha_mode: AlphaMode::Blend,
flipbook: false,
needs_uv: false,
#[cfg(all(feature = "2d", feature = "3d"))]
Expand Down Expand Up @@ -1227,10 +1218,10 @@ impl SpecializedRenderPipeline for ParticlesRenderPipeline {
TextureFormat::bevy_default()
};

let blend_state = match key.alpha_blend_mode {
AlphaBlendMode::Alpha => BlendState::ALPHA_BLENDING,
AlphaBlendMode::Premultiply => BlendState::PREMULTIPLIED_ALPHA_BLENDING,
AlphaBlendMode::Add => BlendState {
let blend_state = match key.alpha_mode {
AlphaMode::Blend => BlendState::ALPHA_BLENDING,
AlphaMode::Premultiply => BlendState::PREMULTIPLIED_ALPHA_BLENDING,
AlphaMode::Add => BlendState {
color: BlendComponent {
src_factor: BlendFactor::SrcAlpha,
dst_factor: BlendFactor::One,
Expand All @@ -1242,14 +1233,15 @@ impl SpecializedRenderPipeline for ParticlesRenderPipeline {
operation: BlendOperation::Add,
},
},
AlphaBlendMode::Multiply => BlendState {
AlphaMode::Multiply => BlendState {
color: BlendComponent {
src_factor: BlendFactor::Dst,
dst_factor: BlendFactor::OneMinusSrcAlpha,
operation: BlendOperation::Add,
},
alpha: BlendComponent::OVER,
},
_ => BlendState::ALPHA_BLENDING,
};

RenderPipelineDescriptor {
Expand Down Expand Up @@ -2510,13 +2502,7 @@ fn emit_draw<T, F>(
let render_shader_source = &batches.render_shaders[draw_batch.group_index as usize];
trace!("Emit for group index #{}", draw_batch.group_index);

let alpha_blend_mode = match batches.alpha_mode {
AlphaMode::Blend => AlphaBlendMode::Alpha,
AlphaMode::Premultiply => AlphaBlendMode::Premultiply,
AlphaMode::Add => AlphaBlendMode::Add,
AlphaMode::Multiply => AlphaBlendMode::Multiply,
_ => AlphaBlendMode::Alpha,
};
let alpha_mode = batches.alpha_mode;

#[cfg(feature = "trace")]
let _span_specialize = bevy::utils::tracing::info_span!("specialize").entered();
Expand All @@ -2529,7 +2515,7 @@ fn emit_draw<T, F>(
has_image,
local_space_simulation,
use_alpha_mask,
alpha_blend_mode,
alpha_mode,
flipbook,
needs_uv,
#[cfg(all(feature = "2d", feature = "3d"))]
Expand Down

0 comments on commit 104e5f0

Please sign in to comment.