diff --git a/animations/src/bin/waterfall.rs b/animations/src/bin/waterfall.rs new file mode 100644 index 0000000..7b03602 --- /dev/null +++ b/animations/src/bin/waterfall.rs @@ -0,0 +1,84 @@ +use std::f64::consts::PI; + +use animation_api::Animation; +use animation_utils::decorators::{BrightnessControlled, SpeedControlled}; +use animation_utils::ParameterSchema; +use serde::{Deserialize, Serialize}; +use serde_json::json; + +#[derive(Clone, Serialize, Deserialize, ParameterSchema)] +pub struct Parameters { + #[schema_field(name = "Color A", color)] + color_a: lightfx::Color, + + #[schema_field(name = "Color B", color)] + color_b: lightfx::Color, + + #[schema_field(name = "Density", number(min = 0.1, max = 8.0, step = 0.1))] + density: f64, +} + +impl Default for Parameters { + fn default() -> Self { + Self { + density: 1.0, + color_a: lightfx::Color::white(), + color_b: lightfx::Color::rgb(0, 0, 255), + } + } +} + +#[animation_utils::plugin] +pub struct RainbowWaterfall { + points_height: Vec, + time: f64, + parameters: Parameters, +} + +impl RainbowWaterfall { + pub fn create(points: Vec<(f64, f64, f64)>) -> impl Animation { + SpeedControlled::new(BrightnessControlled::new(Self { + parameters: Default::default(), + time: 0.0, + points_height: points.into_iter().map(|(_, h, _)| h).collect(), + })) + } +} + +impl Animation for RainbowWaterfall { + type Parameters = Parameters; + + fn update(&mut self, delta: f64) { + self.time += delta; + } + + fn render(&self) -> lightfx::Frame { + self.points_height + .iter() + .map(|h| { + (PI * (h * self.parameters.density / 2.0 + self.time)) + .sin() + .powi(2) + }) + .map(|p| { + self.parameters + .color_a + .with_alpha(p) + .blend(&self.parameters.color_b.with_alpha(1.0)) + .apply_alpha() + }) + .into() + } + + fn animation_name(&self) -> &str { + "Waterfall" + } + + fn set_parameters(&mut self, parameters: Self::Parameters) { + self.parameters = parameters; + } + + fn get_parameters(&self) -> Self::Parameters { + self.parameters.clone() + } +} diff --git a/plugins/waterfall/manifest.json b/plugins/waterfall/manifest.json new file mode 100644 index 0000000..f3cde56 --- /dev/null +++ b/plugins/waterfall/manifest.json @@ -0,0 +1,3 @@ +{ + "display_name": "Waterfall" +} diff --git a/plugins/waterfall/plugin b/plugins/waterfall/plugin new file mode 120000 index 0000000..0989640 --- /dev/null +++ b/plugins/waterfall/plugin @@ -0,0 +1 @@ +../../target/release/waterfall \ No newline at end of file