Skip to content

Commit

Permalink
premultiplied alpha in draw functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jabuwu committed Oct 4, 2022
1 parent 4ff5562 commit 907581b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ impl Color {
self.a = self.r.clamp(0., 1.);
self
}

pub fn premultiply_alpha(&mut self) {
self.r *= self.a;
self.g *= self.a;
self.b *= self.a;
}
}

impl Mul<Color> for Color {
Expand Down
19 changes: 15 additions & 4 deletions src/draw/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct CombinedRenderable {

pub struct CombinedDrawer {
pub cull_direction: CullDirection,
pub premultiplied_alpha: bool,
}

/// A combined drawer with a mesh combining optimization.
Expand Down Expand Up @@ -128,8 +129,13 @@ impl CombinedDrawer {
let (color, dark_color) = if let Some(mesh_attachment) =
slot.attachment().and_then(|a| a.as_mesh())
{
let color = mesh_attachment.color() * slot.color() * skeleton.color();
let dark_color = Color::new_rgba(0., 0., 0., 1.);
let mut color = mesh_attachment.color() * slot.color() * skeleton.color();
let mut dark_color = Color::new_rgba(0., 0., 0., 0.);
if self.premultiplied_alpha {
color.premultiply_alpha();
dark_color.premultiply_alpha();
dark_color.a = 1.;
}

uvs.resize(
vertex_base as usize + mesh_attachment.world_vertices_length() as usize,
Expand Down Expand Up @@ -197,8 +203,13 @@ impl CombinedDrawer {

(color, dark_color)
} else if let Some(region_attachment) = slot.attachment().and_then(|a| a.as_region()) {
let color = region_attachment.color() * slot.color() * skeleton.color();
let dark_color = Color::new_rgba(0., 0., 0., 1.);
let mut color = region_attachment.color() * slot.color() * skeleton.color();
let mut dark_color = Color::new_rgba(0., 0., 0., 0.);
if self.premultiplied_alpha {
color.premultiply_alpha();
dark_color.premultiply_alpha();
dark_color.a = 1.;
}

for i in 0..4 {
vertices.push([
Expand Down
18 changes: 15 additions & 3 deletions src/draw/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ pub struct SimpleRenderable {
pub uvs: Vec<[f32; 2]>,
pub indices: Vec<u16>,
pub color: Color,
pub dark_color: Option<Color>,
pub dark_color: Color,
pub blend_mode: BlendMode,
pub attachment_renderer_object: Option<*const c_void>,
}

pub struct SimpleDrawer {
pub cull_direction: CullDirection,
pub premultiplied_alpha: bool,
}

/// A simple drawer with no optimizations.
Expand Down Expand Up @@ -246,14 +247,25 @@ impl SimpleDrawer {
};

color *= slot.color() * skeleton.color();
if self.premultiplied_alpha {
color.premultiply_alpha();
}

let mut dark_color = slot
.dark_color()
.unwrap_or(Color::new_rgba(0.0, 0.0, 0.0, 0.0));
if self.premultiplied_alpha {
dark_color.premultiply_alpha();
dark_color.a = 1.0;
}

renderables.push(SimpleRenderable {
slot_index: slot_index,
slot_index,
vertices,
uvs,
indices,
color,
dark_color: slot.dark_color(),
dark_color,
blend_mode: slot.data().blend_mode(),
attachment_renderer_object,
});
Expand Down
3 changes: 2 additions & 1 deletion src/skeleton_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl SkeletonController {
pub fn renderables(&mut self) -> Vec<SkeletonRenderable> {
let renderables = SimpleDrawer {
cull_direction: self.settings.cull_direction,
premultiplied_alpha: self.settings.premultiplied_alpha,
}
.draw(&mut self.skeleton, Some(&mut self.clipper));
renderables
Expand All @@ -112,7 +113,7 @@ pub struct SkeletonRenderable {
pub uvs: Vec<[f32; 2]>,
pub indices: Vec<u16>,
pub color: Color,
pub dark_color: Option<Color>,
pub dark_color: Color,
pub blend_mode: BlendMode,
pub premultiplied_alpha: bool,
pub attachment_renderer_object: Option<*const c_void>,
Expand Down

0 comments on commit 907581b

Please sign in to comment.