26 个版本 (13 个破坏性更新)

0.14.0 2024年7月4日
0.13.1 2024年3月9日
0.12.2 2023年11月4日
0.11.1 2023年7月11日
0.2.0 2020年11月4日

#105 in 游戏开发

Download history 473/week @ 2024-04-29 409/week @ 2024-05-06 408/week @ 2024-05-13 508/week @ 2024-05-20 603/week @ 2024-05-27 622/week @ 2024-06-03 512/week @ 2024-06-10 592/week @ 2024-06-17 415/week @ 2024-06-24 618/week @ 2024-07-01 483/week @ 2024-07-08 449/week @ 2024-07-15 413/week @ 2024-07-22 778/week @ 2024-07-29 679/week @ 2024-08-05 769/week @ 2024-08-12

2,703 每月下载量
8 个crate中使用 (4 个直接使用)

MIT/Apache

48KB
704

Bevy Easings

MIT/Apache 2.0 Doc Crate Bevy Tracking CI

使用插值在Bevy组件上进行缓动。

menu example

用法

系统设置

将插件添加到您的应用

use bevy::prelude::*;
use bevy_easings::EasingsPlugin;

fn main() {
    App::new()
        .add_plugins(EasingsPlugin);
}

将组件缓动到新值

然后将组件缓动到它们的新状态!

use bevy::prelude::*;
use bevy_easings::*;

fn my_system(mut commands: Commands){
    commands
        .spawn((
            SpriteBundle {
                ..Default::default()
            },
            Sprite {
                custom_size: Some(Vec2::new(10., 10.)),
                ..Default::default()
            }
            .ease_to(
                Sprite {
                    custom_size: Some(Vec2::new(100., 100.)),
                    ..Default::default()
                },
                EaseFunction::QuadraticIn,
                EasingType::PingPong {
                    duration: std::time::Duration::from_secs(1),
                    pause: Some(std::time::Duration::from_millis(500)),
                },
            ),
        ));
}

如果正在缓动的组件不是实体的组件,则应首先将组件插入目标实体。

使用EaseMethod进行缓动

可以使用EaseMethod枚举提供EaseFunction中不可用的缓动方法。

pub enum EaseMethod {
    /// Follow `EaseFunction`
    EaseFunction(EaseFunction),
    /// Linear interpolation, with no function
    Linear,
    /// Discrete interpolation, eased value will jump from start to end
    Discrete,
    /// Use a custom function to interpolate the value
    CustomFunction(fn(f32) -> f32),
}

如下所示

use bevy::prelude::*;
use bevy_easings::*;

fn my_system(mut commands: Commands){
    commands
        .spawn((
            SpriteBundle {
                ..Default::default()
            },
            Sprite {
                custom_size: Some(Vec2::new(10., 10.)),
                ..Default::default()
            }
            .ease_to(
                Sprite {
                    custom_size: Some(Vec2::new(100., 100.)),
                    ..Default::default()
                },
                EaseMethod::Linear,
                EasingType::PingPong {
                    duration: std::time::Duration::from_secs(1),
                    pause: Some(std::time::Duration::from_millis(500)),
                },
            ),
        ));
}

链式缓动

您可以链式使用缓动,如果它们未设置为重复,则将按顺序发生。

use bevy::prelude::*;
use bevy_easings::*;

fn my_system(mut commands: Commands){
    commands
        .spawn((
            SpriteBundle {
                ..Default::default()
            },
            Sprite {
                custom_size: Some(Vec2::new(10., 10.)),
                ..Default::default()
            }
            .ease_to(
                Sprite {
                    custom_size: Some(Vec2::new(300., 300.)),
                    ..Default::default()
                },
                EaseFunction::QuadraticIn,
                EasingType::Once {
                    duration: std::time::Duration::from_secs(1),
                },
            )
            .ease_to(
                Sprite {
                    custom_size: Some(Vec2::new(350., 350.)),
                    ..Default::default()
                },
                EaseFunction::QuadraticIn,
                EasingType::PingPong {
                    duration: std::time::Duration::from_millis(500),
                    pause: Some(std::time::Duration::from_millis(200)),
                },
            ),
        ));
}

自定义组件支持

要能够缓动组件,它需要实现 DefaultLerp 特性。此特性由 bevy_easings 重导出。

use bevy::prelude::*;
use bevy_easings::*;

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

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

lerp (线性插值) 的基本公式是 self + (other - self) * scalar

然后,需要将系统 custom_ease_system::<CustomComponent> 添加到应用中。

示例

查看 示例

选择缓动函数

easing on size

easing on color

在缓动颜色时,请注意您正在使用的颜色空间

color spaces

缓动函数

有许多 缓动函数

  • 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

Bevy支持的版本

Bevy bevy_easings
main main
0.14 0.14
0.13 0.14
0.12 0.12
0.11 0.11
0.10 0.10
0.9 0.9
0.8 0.8
0.7 0.7
0.6 0.6
0.5 0.4

依赖关系

~18–59MB
~1M SLoC