Skip to content

Commit

Permalink
Fixed problem where alpha blending was not working (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrea-c authored Feb 19, 2024
1 parent 8a59e2c commit 11f530f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
32 changes: 26 additions & 6 deletions src/curves/color_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@ impl Mul<f32> for ColorPoint {
type Output = Self;

fn mul(self, rhs: f32) -> Self::Output {
Self {
color: self.color * rhs,
}
Self::rgba(
self.color.r() * rhs,
self.color.g() * rhs,
self.color.b() * rhs,
self.color.a() * rhs,
)
}
}

impl Add<Self> for ColorPoint {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Self {
color: self.color + rhs.color,
}
Self::rgba(
self.r() + rhs.r(),
self.g() + rhs.g(),
self.b() + rhs.b(),
self.a() + rhs.a(),
)
}
}

Expand Down Expand Up @@ -91,3 +97,17 @@ impl From<Color> for ColorPoint {
Self { color: value }
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn alpha_add_works() {
let l = ColorPoint::rgba(1., 1., 1., 0.);
let r = ColorPoint::rgba(1., 1., 1., 0.7);

let result = l + r;
assert!(result.a() - 0.7 < 0.00001);
}
}
34 changes: 33 additions & 1 deletion src/curves/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub trait AsParamCurve<P: Point> {
fn get(&self, t: f32) -> P;
}

#[derive(Reflect, Clone, Serialize, Deserialize)]
#[derive(Reflect, Clone, Serialize, Deserialize, Debug)]
pub enum ParamCurve<P: Point> {
Linear(LinearParamCurve<P>),
Constant(ConstantParamCurve<P>),
Expand Down Expand Up @@ -41,3 +41,35 @@ impl<P: Point> AsParamCurve<P> for ParamCurve<P> {
}

pub type Gradient = ParamCurve<ColorPoint>;

#[cfg(test)]
mod tests {
use super::*;
use bevy::render::color::Color;

#[test]
fn gradient_alpha_blending_works() {
let grad = Gradient::linear_uniform(vec![
ColorPoint::rgba(1., 1., 1., 1.),
ColorPoint::rgba(1., 1., 1., 0.),
]);

let mid = grad.get(0.5);

assert!(mid.a() - 0.5 < 0.000001);
}

#[test]
fn gradient_alpha_blending_works_large() {
let grad = Gradient::linear(vec![
(0., Color::rgba(300., 100., 1., 1.).into()),
(0.7, Color::rgba(3., 1., 1., 1.).into()),
(0.8, Color::rgba(1., 0.3, 0.3, 1.).into()),
(0.9, Color::rgba(0.3, 0.3, 0.3, 1.).into()),
(1., Color::rgba(0.1, 0.1, 0.1, 0.).into()),
]);

let col = *grad.get(0.9343);
assert!(col.a() - (1. - 0.343) < 0.000001);
}
}
8 changes: 5 additions & 3 deletions src/curves/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{curve::AsParamCurve, point::Point};
use bevy::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Reflect, Clone, Serialize, Deserialize)]
#[derive(Reflect, Clone, Serialize, Deserialize, Debug)]
struct LinearSegment<P: Point> {
pub start: P,
pub end: P,
Expand All @@ -20,7 +20,7 @@ impl<P: Point> LinearSegment<P> {
}
}

#[derive(Reflect, Clone, Serialize, Deserialize)]
#[derive(Reflect, Clone, Serialize, Deserialize, Debug)]
pub struct LinearParamCurve<P: Point> {
/// List of the `t` value at the start of the segment, followed by line segment
segments: Vec<(f32, LinearSegment<P>)>,
Expand Down Expand Up @@ -107,7 +107,9 @@ impl<P: Point> AsParamCurve<P> for LinearParamCurve<P> {
let segment_percent =
(t - self.segment_start(segment_idx)) / self.segment_length(segment_idx);

self.segment(segment_idx).get_percent(segment_percent)
let out = self.segment(segment_idx).get_percent(segment_percent);

out
}
}

Expand Down

0 comments on commit 11f530f

Please sign in to comment.