Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/animation/eased_motion.rs
9353 views
1
//! Demonstrates the application of easing curves to animate a transition.
2
3
use std::f32::consts::FRAC_PI_2;
4
5
use bevy::{
6
animation::{animated_field, AnimatedBy, AnimationTargetId},
7
color::palettes::css::{ORANGE, SILVER},
8
math::vec3,
9
prelude::*,
10
};
11
12
fn main() {
13
App::new()
14
.add_plugins(DefaultPlugins)
15
.add_systems(Startup, setup)
16
.run();
17
}
18
19
fn setup(
20
mut commands: Commands,
21
mut meshes: ResMut<Assets<Mesh>>,
22
mut materials: ResMut<Assets<StandardMaterial>>,
23
mut animation_graphs: ResMut<Assets<AnimationGraph>>,
24
mut animation_clips: ResMut<Assets<AnimationClip>>,
25
) {
26
// Create the animation:
27
let AnimationInfo {
28
target_name: animation_target_name,
29
target_id: animation_target_id,
30
graph: animation_graph,
31
node_index: animation_node_index,
32
} = AnimationInfo::create(&mut animation_graphs, &mut animation_clips);
33
34
// Build an animation player that automatically plays the animation.
35
let mut animation_player = AnimationPlayer::default();
36
animation_player.play(animation_node_index).repeat();
37
38
// A cube together with the components needed to animate it
39
let cube_entity = commands
40
.spawn((
41
Mesh3d(meshes.add(Cuboid::from_length(2.0))),
42
MeshMaterial3d(materials.add(Color::from(ORANGE))),
43
Transform::from_translation(vec3(-6., 2., 0.)),
44
animation_target_name,
45
animation_player,
46
AnimationGraphHandle(animation_graph),
47
))
48
.id();
49
50
commands
51
.entity(cube_entity)
52
.insert((animation_target_id, AnimatedBy(cube_entity)));
53
54
// Some light to see something
55
commands.spawn((
56
PointLight {
57
shadow_maps_enabled: true,
58
intensity: 10_000_000.,
59
range: 100.0,
60
..default()
61
},
62
Transform::from_xyz(8., 16., 8.),
63
));
64
65
// Ground plane
66
commands.spawn((
67
Mesh3d(meshes.add(Plane3d::default().mesh().size(50., 50.))),
68
MeshMaterial3d(materials.add(Color::from(SILVER))),
69
));
70
71
// The camera
72
commands.spawn((
73
Camera3d::default(),
74
Transform::from_xyz(0., 6., 12.).looking_at(Vec3::new(0., 1.5, 0.), Vec3::Y),
75
));
76
}
77
78
// Holds information about the animation we programmatically create.
79
struct AnimationInfo {
80
// The name of the animation target (in this case, the text).
81
target_name: Name,
82
// The ID of the animation target, derived from the name.
83
target_id: AnimationTargetId,
84
// The animation graph asset.
85
graph: Handle<AnimationGraph>,
86
// The index of the node within that graph.
87
node_index: AnimationNodeIndex,
88
}
89
90
impl AnimationInfo {
91
// Programmatically creates the UI animation.
92
fn create(
93
animation_graphs: &mut Assets<AnimationGraph>,
94
animation_clips: &mut Assets<AnimationClip>,
95
) -> AnimationInfo {
96
// Create an ID that identifies the text node we're going to animate.
97
let animation_target_name = Name::new("Cube");
98
let animation_target_id = AnimationTargetId::from_name(&animation_target_name);
99
100
// Allocate an animation clip.
101
let mut animation_clip = AnimationClip::default();
102
103
// Each leg of the translation motion should take 3 seconds.
104
let animation_domain = interval(0.0, 3.0).unwrap();
105
106
// The easing curve is parametrized over [0, 1], so we reparametrize it and
107
// then ping-pong, which makes it spend another 3 seconds on the return journey.
108
let translation_curve = EasingCurve::new(
109
vec3(-6., 2., 0.),
110
vec3(6., 2., 0.),
111
EaseFunction::CubicInOut,
112
)
113
.reparametrize_linear(animation_domain)
114
.expect("this curve has bounded domain, so this should never fail")
115
.ping_pong()
116
.expect("this curve has bounded domain, so this should never fail");
117
118
// Something similar for rotation. The repetition here is an illusion caused
119
// by the symmetry of the cube; it rotates on the forward journey and never
120
// rotates back.
121
let rotation_curve = EasingCurve::new(
122
Quat::IDENTITY,
123
Quat::from_rotation_y(FRAC_PI_2),
124
EaseFunction::ElasticInOut,
125
)
126
.reparametrize_linear(interval(0.0, 4.0).unwrap())
127
.expect("this curve has bounded domain, so this should never fail");
128
129
animation_clip.add_curve_to_target(
130
animation_target_id,
131
AnimatableCurve::new(animated_field!(Transform::translation), translation_curve),
132
);
133
animation_clip.add_curve_to_target(
134
animation_target_id,
135
AnimatableCurve::new(animated_field!(Transform::rotation), rotation_curve),
136
);
137
138
// Save our animation clip as an asset.
139
let animation_clip_handle = animation_clips.add(animation_clip);
140
141
// Create an animation graph with that clip.
142
let (animation_graph, animation_node_index) =
143
AnimationGraph::from_clip(animation_clip_handle);
144
let animation_graph_handle = animation_graphs.add(animation_graph);
145
146
AnimationInfo {
147
target_name: animation_target_name,
148
target_id: animation_target_id,
149
graph: animation_graph_handle,
150
node_index: animation_node_index,
151
}
152
}
153
}
154
155