55#include " impeller/scene/animation/animation_player.h"
66
77#include < memory>
8- #include < unordered_map>
98
109#include " flutter/fml/time/time_point.h"
1110#include " impeller/base/timing.h"
@@ -20,37 +19,20 @@ AnimationPlayer::~AnimationPlayer() = default;
2019AnimationPlayer::AnimationPlayer (AnimationPlayer&&) = default ;
2120AnimationPlayer& AnimationPlayer::operator =(AnimationPlayer&&) = default ;
2221
23- AnimationClip* AnimationPlayer::AddAnimation (
24- const std::shared_ptr<Animation>& animation,
22+ AnimationClip& AnimationPlayer::AddAnimation (
23+ std::shared_ptr<Animation> animation,
2524 Node* bind_target) {
26- if (!animation) {
27- VALIDATION_LOG << " Cannot add null animation." ;
28- return nullptr ;
29- }
30-
31- AnimationClip clip (animation, bind_target);
25+ AnimationClip clip (std::move (animation), bind_target);
3226
3327 // Record all of the unique default transforms that this AnimationClip
3428 // will mutate.
3529 for (const auto & binding : clip.bindings_ ) {
36- auto decomp = binding.node ->GetLocalTransform ().Decompose ();
37- if (!decomp.has_value ()) {
38- continue ;
39- }
40- target_transforms_.insert (
41- {binding.node , AnimationTransforms{.bind_pose = decomp.value ()}});
30+ default_target_transforms_.insert (
31+ {binding.node , binding.node ->GetLocalTransform ()});
4232 }
4333
44- auto result = clips_.insert ({animation->GetName (), std::move (clip)});
45- return &result.first ->second ;
46- }
47-
48- AnimationClip* AnimationPlayer::GetClip (const std::string& name) const {
49- auto result = clips_.find (name);
50- if (result == clips_.end ()) {
51- return nullptr ;
52- }
53- return const_cast <AnimationClip*>(&result->second );
34+ clips_.push_back (std::move (clip));
35+ return clips_.back ();
5436}
5537
5638void AnimationPlayer::Update () {
@@ -61,27 +43,18 @@ void AnimationPlayer::Update() {
6143 auto delta_time = new_time - previous_time_.value ();
6244 previous_time_ = new_time;
6345
64- // Reset the animated pose state.
65- for (auto & [node, transforms] : target_transforms_) {
66- transforms.animated_pose = transforms.bind_pose ;
67- }
68-
69- // Compute a weight multiplier for normalizing the animation.
70- Scalar total_weight = 0 ;
71- for (auto & [_, clip] : clips_) {
72- total_weight += clip.GetWeight ();
73- }
74- Scalar weight_multiplier = total_weight > 1 ? 1 / total_weight : 1 ;
46+ Reset ();
7547
76- // Update and apply all clips to the animation pose state .
77- for (auto & [_, clip] : clips_) {
48+ // Update and apply all clips.
49+ for (auto & clip : clips_) {
7850 clip.Advance (delta_time);
79- clip.ApplyToBindings (target_transforms_, weight_multiplier );
51+ clip.ApplyToBindings ();
8052 }
53+ }
8154
82- // Apply the animated pose to the bound joints.
83- for (auto & [node, transforms ] : target_transforms_ ) {
84- node->SetLocalTransform (Matrix (transforms. animated_pose ));
55+ void AnimationPlayer::Reset () {
56+ for (auto & [node, transform ] : default_target_transforms_ ) {
57+ node->SetLocalTransform (Matrix ());
8558 }
8659}
8760
0 commit comments