Unravel Engine C++ Reference
Loading...
Searching...
No Matches
animation_player.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <hpp/variant.hpp>
8
9namespace unravel
10{
11
12using blend_easing_t = std::function<float(float)>;
13
15{
16 auto get_progress() const -> float
17 {
18 auto clp = clip.get();
19 if(clp && clp->duration > animation_clip::seconds_t{0})
20 {
21 return elapsed / clp->duration;
22 }
23
24 return 0.0f;
25 }
26 void set_progress(float progress)
27 {
28
29 auto clp = clip.get();
30 if(clp && clp->duration > animation_clip::seconds_t{0})
31 {
32 progress = math::clamp(progress, 0.0f, 1.0f);
33 elapsed = clp->duration * progress;
34 }
35 }
36
39 bool loop{false};
40 // Add blend space support
41 std::shared_ptr<blend_space_def> blend_space{};
42 std::vector<std::pair<asset_handle<animation_clip>, float>> blend_clips{};
43 std::vector<animation_pose> blend_poses{};
44};
45
47{
50
51 auto get_progress() const -> float
52 {
53 // Compute the normalized blending time (clamped between 0 and 1)
54 auto normalized_blend_time = static_cast<float>(elapsed.count() / duration.count());
55 normalized_blend_time = std::clamp(normalized_blend_time, 0.0f, 1.0f);
56 return normalized_blend_time;
57 }
58};
59
61{
62 float param{};
63
64 auto get_progress() const -> float
65 {
66 return param;
67 }
68};
69
71{
72 bool loop{};
73 blend_easing_t easing{math::linearInterpolation<float>};
74 hpp::variant<hpp::monostate, blend_over_time, blend_over_param> state{};
75};
76
84{
85public:
87 using update_callback_t = std::function<void(const animation_pose::node_desc& desc,
88 const math::transform& abs,
89 const animation_pose::root_motion_result& motion_result)>;
90
99 void blend_to(size_t layer_idx,
101 seconds_t duration = seconds_t(0.3),
102 bool loop = true,
103 bool phase_sync = false,
104 const blend_easing_t& easing = math::linearInterpolation<float>);
105
106 void clear(size_t layer_idx);
107
108 void set_blend_space(size_t layer_idx, const std::shared_ptr<blend_space_def>& blend_space, bool loop = true);
109
110 void set_blend_space_parameters(size_t layer_idx, const std::vector<float>& params);
114 auto play() -> bool;
115
119 void pause();
120
124 void resume();
125
129 void stop();
130
137 auto update_time(seconds_t delta_time, bool force = false) -> bool;
138 void update_poses(const animation_pose& ref_pose,
139 const update_callback_t& set_transform_callback);
140
146 auto is_playing() const -> bool;
147
153 auto is_paused() const -> bool;
154
155private:
156 struct animation_layer_state
157 {
158 auto is_valid() const -> bool
159 {
160 return state.clip || state.blend_space;
161 }
163 animation_pose pose{};
164 animation_state state{};
165 std::vector<float> parameters;
166 };
167
168 struct animation_layer
169 {
170 auto get_final_pose() const -> const animation_pose*
171 {
172 auto final_pose = &current_state.pose;
173
174 if(target_state.is_valid())
175 {
176 final_pose = &blend_pose;
177 }
178
179 return final_pose;
180 }
181
182 animation_layer_state current_state{};
183 animation_layer_state target_state{};
184
186 animation_pose blend_pose{};
187 blend_state blending_state{};
188 };
189
190 auto get_layer(size_t index) -> animation_layer&;
191
192 void sample_animation(const animation_clip* anim_clip,
193 seconds_t time,
194 animation_pose& pose) const noexcept;
195 auto compute_blend_factor(const animation_layer& layer, float normalized_blend_time) noexcept -> float;
196 void update_state(seconds_t delta_time, animation_state& state);
197 auto get_blend_progress(const animation_layer& layer) const -> float;
198 auto update_pose(animation_layer_state& layer)
199 -> bool;
200
201 std::vector<animation_layer> layers_;
202
203 bool playing_{};
204 bool paused_{};
205};
206
207} // namespace unravel
General purpose transformation class designed to maintain each component of the transformation separa...
Definition transform.hpp:27
Class responsible for playing animations on a skeletal mesh.
auto play() -> bool
Starts or resumes the animation playback.
void pause()
Pauses the animation playback.
animation_clip::seconds_t seconds_t
void clear(size_t layer_idx)
void set_blend_space(size_t layer_idx, const std::shared_ptr< blend_space_def > &blend_space, bool loop=true)
void stop()
Stops the animation playback and resets the time.
std::function< void(const animation_pose::node_desc &desc, const math::transform &abs, const animation_pose::root_motion_result &motion_result)> update_callback_t
auto is_playing() const -> bool
Returns whether the animation is currently playing.
void blend_to(size_t layer_idx, const asset_handle< animation_clip > &clip, seconds_t duration=seconds_t(0.3), bool loop=true, bool phase_sync=false, const blend_easing_t &easing=math::linearInterpolation< float >)
Blends to the animation over the specified time with the specified easing.
void set_blend_space_parameters(size_t layer_idx, const std::vector< float > &params)
void resume()
Resumes the animation playback.
auto update_time(seconds_t delta_time, bool force=false) -> bool
Updates the animation player, advancing the animation time and applying transformations.
void update_poses(const animation_pose &ref_pose, const update_callback_t &set_transform_callback)
auto is_paused() const -> bool
Returns whether the animation is currently paused.
std::function< float(float)> blend_easing_t
Represents a handle to an asset, providing access and management functions.
animation_channel::seconds_t seconds_t
Definition animation.h:100
void set_progress(float progress)
animation_clip::seconds_t elapsed
std::vector< std::pair< asset_handle< animation_clip >, float > > blend_clips
auto get_progress() const -> float
std::vector< animation_pose > blend_poses
asset_handle< animation_clip > clip
std::shared_ptr< blend_space_def > blend_space
auto get_progress() const -> float
animation_clip::seconds_t elapsed
animation_clip::seconds_t duration
auto get_progress() const -> float
hpp::variant< hpp::monostate, blend_over_time, blend_over_param > state