Skip to content

Commit

Permalink
Fix minor issues in Random Sweep animation
Browse files Browse the repository at this point in the history
  • Loading branch information
mrozycki committed Dec 9, 2024
1 parent 2ab716d commit 9ebb019
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions animations/src/bin/random_sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub enum SweepType {

#[derive(Clone, Serialize, Deserialize, Schema)]
pub struct Parameters {
#[schema_field(name = "Lead length", number(min = 0.0, max = 2.0, step = 0.05))]
lead_length: f64,

#[schema_field(name = "Tail length", number(min = 0.0, max = 2.0, step = 0.05))]
tail_length: f64,

Expand All @@ -28,6 +31,7 @@ pub struct Parameters {
impl Default for Parameters {
fn default() -> Self {
Self {
lead_length: 0.1,
tail_length: 0.5,
sweep_type: Default::default(),
}
Expand Down Expand Up @@ -74,7 +78,9 @@ impl Animation for RandomSweep {
}

fn update(&mut self, delta: f64) {
if self.heights.is_empty() {
if self.heights.is_empty()
|| self.current_height > self.max_height + self.parameters.tail_length
{
let rotation = match self.parameters.sweep_type {
SweepType::Sweep2D => animation_utils::random_rotation_around(&Vector3::z_axis()),
SweepType::Sweep3D => animation_utils::random_rotation(),
Expand All @@ -88,28 +94,27 @@ impl Animation for RandomSweep {
self.color = animation_utils::random_hue(1.0, 1.0);
(self.current_height, self.max_height) =
match self.heights.iter().filter_map(|x| x.as_ref()).minmax() {
itertools::MinMaxResult::MinMax(min, max) => (*min, *max),
itertools::MinMaxResult::MinMax(min, max) => {
(*min - self.parameters.lead_length, *max)
}
_ => return,
};
}

self.current_height += delta;

if self.current_height > self.max_height + self.parameters.tail_length {
self.heights = Vec::new();
}
}

fn render(&self) -> lightfx::Frame {
self.heights
.iter()
.map(|h| {
if let Some(h) = h {
if *h < self.current_height
&& *h > self.current_height - self.parameters.tail_length
{
self.color
.dim(1.0 - (self.current_height - h) / self.parameters.tail_length)
let h = h - self.current_height;

if h >= 0.0 && h < self.parameters.lead_length {
self.color.dim(1.0 - h / self.parameters.lead_length)
} else if h < 0.0 && h > -self.parameters.tail_length {
self.color.dim(1.0 + h / self.parameters.tail_length)
} else {
lightfx::Color::black()
}
Expand Down

0 comments on commit 9ebb019

Please sign in to comment.