|
| 1 | +//! Demonstrates how to animate colors in different color spaces using mixing and splines. |
| 2 | +
|
| 3 | +use bevy::{math::cubic_splines::Point, prelude::*}; |
| 4 | + |
| 5 | +// We define this trait so we can reuse the same code for multiple color types that may be implemented using curves. |
| 6 | +trait CurveColor: Point + Into<Color> + Send + Sync + 'static {} |
| 7 | +impl<T: Point + Into<Color> + Send + Sync + 'static> CurveColor for T {} |
| 8 | + |
| 9 | +// We define this trait so we can reuse the same code for multiple color types that may be implemented using mixing. |
| 10 | +trait MixedColor: Mix + Into<Color> + Send + Sync + 'static {} |
| 11 | +impl<T: Mix + Into<Color> + Send + Sync + 'static> MixedColor for T {} |
| 12 | + |
| 13 | +#[derive(Debug, Component)] |
| 14 | +struct Curve<T: CurveColor>(CubicCurve<T>); |
| 15 | + |
| 16 | +#[derive(Debug, Component)] |
| 17 | +struct Mixed<T: MixedColor>([T; 4]); |
| 18 | + |
| 19 | +fn main() { |
| 20 | + App::new() |
| 21 | + .add_plugins(DefaultPlugins) |
| 22 | + .add_systems(Startup, setup) |
| 23 | + .add_systems( |
| 24 | + Update, |
| 25 | + ( |
| 26 | + animate_curve::<LinearRgba>, |
| 27 | + animate_curve::<Oklaba>, |
| 28 | + animate_curve::<Xyza>, |
| 29 | + animate_mixed::<Hsla>, |
| 30 | + animate_mixed::<Srgba>, |
| 31 | + animate_mixed::<Oklcha>, |
| 32 | + ), |
| 33 | + ) |
| 34 | + .run(); |
| 35 | +} |
| 36 | + |
| 37 | +fn setup(mut commands: Commands) { |
| 38 | + commands.spawn(Camera2dBundle::default()); |
| 39 | + |
| 40 | + // The color spaces `Oklaba`, `Laba`, `LinearRgba` and `Xyza` all are either perceptually or physically linear. |
| 41 | + // This property allows us to define curves, e.g. bezier curves through these spaces. |
| 42 | + |
| 43 | + // Define the control points for the curve. |
| 44 | + // For more information, please see the cubic curve example. |
| 45 | + let colors = [ |
| 46 | + LinearRgba::WHITE, |
| 47 | + LinearRgba::rgb(1., 1., 0.), // Yellow |
| 48 | + LinearRgba::RED, |
| 49 | + LinearRgba::BLACK, |
| 50 | + ]; |
| 51 | + // Spawn a sprite using the provided colors as control points. |
| 52 | + spawn_curve_sprite(&mut commands, 275., colors); |
| 53 | + |
| 54 | + // Spawn another sprite using the provided colors as control points after converting them to the `Xyza` color space. |
| 55 | + spawn_curve_sprite(&mut commands, 175., colors.map(Xyza::from)); |
| 56 | + |
| 57 | + spawn_curve_sprite(&mut commands, 75., colors.map(Oklaba::from)); |
| 58 | + |
| 59 | + // Other color spaces like `Srgba` or `Hsva` are neither perceptually nor physically linear. |
| 60 | + // As such, we cannot use curves in these spaces. |
| 61 | + // However, we can still mix these colours and animate that way. In fact, mixing colors works in any color space. |
| 62 | + |
| 63 | + // Spawn a spritre using the provided colors for mixing. |
| 64 | + spawn_mixed_sprite(&mut commands, -75., colors.map(Hsla::from)); |
| 65 | + |
| 66 | + spawn_mixed_sprite(&mut commands, -175., colors.map(Srgba::from)); |
| 67 | + |
| 68 | + spawn_mixed_sprite(&mut commands, -275., colors.map(Oklcha::from)); |
| 69 | +} |
| 70 | + |
| 71 | +fn spawn_curve_sprite<T: CurveColor>(commands: &mut Commands, y: f32, points: [T; 4]) { |
| 72 | + commands.spawn(( |
| 73 | + SpriteBundle { |
| 74 | + transform: Transform::from_xyz(0., y, 0.), |
| 75 | + sprite: Sprite { |
| 76 | + custom_size: Some(Vec2::new(75., 75.)), |
| 77 | + ..Default::default() |
| 78 | + }, |
| 79 | + ..Default::default() |
| 80 | + }, |
| 81 | + Curve(CubicBezier::new([points]).to_curve()), |
| 82 | + )); |
| 83 | +} |
| 84 | + |
| 85 | +fn spawn_mixed_sprite<T: MixedColor>(commands: &mut Commands, y: f32, colors: [T; 4]) { |
| 86 | + commands.spawn(( |
| 87 | + SpriteBundle { |
| 88 | + transform: Transform::from_xyz(0., y, 0.), |
| 89 | + sprite: Sprite { |
| 90 | + custom_size: Some(Vec2::new(75., 75.)), |
| 91 | + ..Default::default() |
| 92 | + }, |
| 93 | + ..Default::default() |
| 94 | + }, |
| 95 | + Mixed(colors), |
| 96 | + )); |
| 97 | +} |
| 98 | + |
| 99 | +fn animate_curve<T: CurveColor>( |
| 100 | + time: Res<Time>, |
| 101 | + mut query: Query<(&mut Transform, &mut Sprite, &Curve<T>)>, |
| 102 | +) { |
| 103 | + let t = (time.elapsed_seconds().sin() + 1.) / 2.; |
| 104 | + |
| 105 | + for (mut transform, mut sprite, cubic_curve) in &mut query { |
| 106 | + // position takes a point from the curve where 0 is the initial point |
| 107 | + // and 1 is the last point |
| 108 | + sprite.color = cubic_curve.0.position(t).into(); |
| 109 | + transform.translation.x = 600. * (t - 0.5); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +fn animate_mixed<T: MixedColor>( |
| 114 | + time: Res<Time>, |
| 115 | + mut query: Query<(&mut Transform, &mut Sprite, &Mixed<T>)>, |
| 116 | +) { |
| 117 | + let t = (time.elapsed_seconds().sin() + 1.) / 2.; |
| 118 | + |
| 119 | + for (mut transform, mut sprite, mixed) in &mut query { |
| 120 | + sprite.color = { |
| 121 | + // First, we determine the amount of intervals between colors. |
| 122 | + // For four colors, there are three intervals between those colors; |
| 123 | + let intervals = (mixed.0.len() - 1) as f32; |
| 124 | + |
| 125 | + // Next we determine the index of the first of the two colorts to mix. |
| 126 | + let start_i = (t * intervals).floor().min(intervals - 1.); |
| 127 | + |
| 128 | + // Lastly we determine the 'local' value of t in this interval. |
| 129 | + let local_t = (t * intervals) - start_i; |
| 130 | + |
| 131 | + let color = mixed.0[start_i as usize].mix(&mixed.0[start_i as usize + 1], local_t); |
| 132 | + color.into() |
| 133 | + }; |
| 134 | + transform.translation.x = 600. * (t - 0.5); |
| 135 | + } |
| 136 | +} |
0 commit comments