3 unstable releases

0.2.0 Nov 4, 2020
0.1.1 Oct 8, 2020
0.1.0 Sep 29, 2020

#48 in Graphics APIs

Download history 43/week @ 2020-09-26 142/week @ 2020-10-03 110/week @ 2020-10-10 52/week @ 2020-10-17 12/week @ 2020-10-24 45/week @ 2020-10-31 20/week @ 2020-11-07 13/week @ 2020-11-14 12/week @ 2020-11-21

122 downloads per month

MIT license

33KB
666 lines

Bevy Easings

License: MIT Doc Crate

Easings on Bevy components using interpolation.

Usage

System setup

Add the plugin to your app:

    App::build()
        .add_default_plugins()
        .add_plugin(EasingsPlugin)
        ...

Easing a component to a new value

And then just ease your components to their new state!

commands
    .spawn(SpriteComponents {
        material: materials.add(Color::RED.into()),
        ..Default::default()
    })
    .with(
        Sprite {
            size: Vec2::new(10., 10.),
            ..Default::default()
        }
        .ease_to(
            Sprite {
                size: Vec2::new(100., 100.),
                ..Default::default()
            },
            EaseFunction::QuadraticIn,
            EasingType::PingPong {
                duration: std::time::Duration::from_secs(1),
                pause: std::time::Duration::from_millis(500),
            },
        ),
    );

If the component being eased is not already a component of the entity, the component should first be inserted for the target entity.

Chaining easing

You can chain easings, if they are not set to repeat they will happen in sequence.

commands
    .spawn(SpriteComponents {
        material: materials.add(Color::RED.into()),
        ..Default::default()
    })
    .with(
        Sprite {
            size: Vec2::new(10., 10.),
            ..Default::default()
        }
        .ease_to(
            Sprite {
                size: Vec2::new(300., 300.),
                ..Default::default()
            },
            EaseFunction::QuadraticIn,
            EasingType::Once {
                duration: std::time::Duration::from_secs(1),
            },
        )
        .ease_to(
            Sprite {
                size: Vec2::new(350., 350.),
                ..Default::default()
            },
            EaseFunction::QuadraticIn,
            EasingType::PingPong {
                duration: std::time::Duration::from_millis(500),
                pause: std::time::Duration::from_millis(200),
            },
        ),
    );

Components Supported

⚠️ While doing easings on Handle<ColorMaterial> is possible (as shown in this example), it is probably not a good idea as many ColorMaterials will need to be added to the assets and it will slow down your game.

Custom component support

To be able to ease a component, it needs to implement the traits Default and Lerp. This trait is re-exported by beavy_easings.

#[Derive(Default)]
struct CustomComponent(f32);
impl Lerp for CustomComponent {
    type Scalar = f32;

    fn lerp(&self, other: &Self, scalar: &Self::Scalar) -> Self {
        CustomComponent(self.0.lerp(&other.0, scalar))
    }
}

The basic formula for lerp (linear interpolation) is self + (other - self) * scalar.

Then, the system custom_ease_system::<CustomComponent>.system() needs to be added to the application.

Examples

See examples

colormaterial_color

sprite_size

transform_rotation

transform_translation

Ease Functions

Many ease functions are available:

  • QuadraticIn
  • QuadraticOut
  • QuadraticInOut
  • CubicIn
  • CubicOut
  • CubicInOut
  • QuarticIn
  • QuarticOut
  • QuarticInOut
  • QuinticIn
  • QuinticOut
  • QuinticInOut
  • SineIn
  • SineOut
  • SineInOut
  • CircularIn
  • CircularOut
  • CircularInOut
  • ExponentialIn
  • ExponentialOut
  • ExponentialInOut
  • ElasticIn
  • ElasticOut
  • ElasticInOut
  • BackIn
  • BackOut
  • BackInOut
  • BounceIn
  • BounceOut
  • BounceInOut

Features

Feature ease_handle is enabled by default, and control wether easing on Handle<ColorMaterial> is possible or not. Disabling this feature removes a system and a resource that are not used otherwise, the rand dependency and remove a i128 from the EasingComponent.

Dependencies

~8–20MB
~319K SLoC