Curves and easing
Ease values between two states with EaseFunction and EasingCurve, shape curves with adaptors, mix colours, and smooth values every frame with smooth_nudge.
On this page
Not everything that moves needs a clip. For a menu that slides in, a camera that follows the player, or a light that pulses, you can sample a curve yourself in a system, or nudge a value toward a target each frame. The same curves also feed clips, when you'd rather the animation system play them.
Easing functions#
EaseFunction maps progress from 0 to 1 onto a shaped 0 to 1. Every family comes as In, Out and InOut:
| Family | Feel |
|---|---|
Sine, Quadratic, Cubic, Quartic, Quintic | Smooth, from gentle to sharp. |
SmoothStep, SmootherStep | Gentle at both ends (plain SmoothStep and SmootherStep are the in-out forms). |
Circular, Exponential | Slow start or end, then fast. |
Back | Overshoots a little before settling. |
Elastic | Springs past the end and wobbles back. Elastic(omega) sets the frequency. |
Bounce | Bounces at the end. |
Linear | No easing. |
Steps(n, JumpAt) | Jumps in n steps, like a clock hand. |
So EaseFunction::CubicInOut, EaseFunction::BackOut, EaseFunction::BounceIn. An EaseFunction is itself a curve over 0..=1, so EaseFunction::CubicIn.sample_clamped(t) gives the eased value.
Easing between two values#
EasingCurve::new(start, end, ease) is a curve from one value to another. It works for floats, vectors, rotations (Quat, Rot2), directions, isometries, colours and tuples of them.
use neo::prelude::*;
#[derive(Component)]
struct SlideIn {
curve: EasingCurve<Vec3>,
seconds: f32,
elapsed: f32,
}
fn spawn_card(mut commands: Commands) {
commands.spawn((
Transform::from_xyz(-6.0, 0.0, 0.0),
SlideIn {
curve: EasingCurve::new(Vec3::new(-6.0, 0.0, 0.0), Vec3::ZERO, EaseFunction::BackOut),
seconds: 0.6,
elapsed: 0.0,
},
));
}
fn slide(time: Res<Time>, mut q: Query<(&mut Transform, &mut SlideIn)>) {
for (mut transform, mut slide) in &mut q {
slide.elapsed += time.delta_secs();
let t = (slide.elapsed / slide.seconds).min(1.0);
transform.translation = slide.curve.sample_clamped(t);
}
}Curves#
Everything above is a Curve<T>: a value for each time in a domain (an Interval). The engine has several kinds and adaptors that build new curves from old ones:
| Curve or method | What it gives |
|---|---|
EasingCurve::new(a, b, ease) | a to b over 0..=1. |
SampleAutoCurve, UnevenSampleAutoCurve | Interpolation through samples, evenly spaced or at given times. |
FunctionCurve::new(domain, f), ConstantCurve | A closure, or one value. |
CubicBezier, CubicCardinalSpline and other cubic splines | Smooth paths through control points; .to_curve() makes them curves. |
sample(t), sample_clamped(t) | The value at t: None outside the domain, or clamped to it. |
reparametrize_linear(interval(0.0, 3.0)?) | Stretches the curve over a new domain, such as three seconds. |
map(f) | Transforms each value. |
reverse(), ping_pong(), repeat(n), forever(), chain(other) | Plays backwards, there and back, n times, endlessly, or one after another. |
The adaptors return a Result, because some only make sense for a bounded domain. ping_pong on an easing curve gives a back-and-forth motion:
let bob = EasingCurve::new(0.0, 0.5, EaseFunction::SineInOut)
.reparametrize_linear(interval(0.0, 1.0).unwrap())
.unwrap()
.ping_pong()
.unwrap()
.forever()
.unwrap();
// in a system: transform.translation.y = bob.sample_unchecked(time.elapsed_secs());Curves in clips#
Any of these curves can be a clip curve: wrap it with AnimatableCurve::new(animated_field!(Transform::translation), curve) and add it to an AnimationClip. The animation system then handles time, pausing, speed and blending for you. See Animation clips.
Smoothing toward a target#
For values that should follow something that moves, like a camera behind a player, use smooth_nudge. It moves a value part of the way toward a target each frame, at the same speed whatever the frame rate:
fn follow(time: Res<Time>, player: Single<&Transform, With<Player>>, mut camera: Single<&mut Transform, (With<Camera2d>, Without<Player>)>) {
let target = player.translation.with_z(camera.translation.z);
camera.translation.smooth_nudge(&target, 4.0, time.delta_secs()); // higher decay rate = snappier
}smooth_nudge comes from StableInterpolate, implemented for floats, vectors, rotations and directions.
Colours#
Colours mix with a.mix(&b, t) (the Mix trait). Where you mix changes the look: Oklcha and Oklaba keep brightness even, while Srgba can go grey in the middle. The color_animation example compares them.
Examples: cargo run --example easing_functions, eased_motion and color_animation. For curves over UI, see animated_ui.