| Flutter Impeller |

property_resolver.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/scene/animation/property_resolver.h"
6
7 #include <algorithm>
8 #include <iterator>
9 #include <memory>
10
11 #include "impeller/geometry/matrix_decomposition.h"
12 #include "impeller/geometry/point.h"
13 #include "impeller/scene/node.h"
14
15 namespace impeller {
16 namespace scene {
17
18 std::unique_ptr<TranslationTimelineResolver>
19 PropertyResolver::MakeTranslationTimeline(std::vector<Scalar> times,
20 std::vector<Vector3> values) {
21 FML_DCHECK(times.size() == values.size());
22 auto result = std::unique_ptr<TranslationTimelineResolver>(
23 new TranslationTimelineResolver());
24 result->times_ = std::move(times);
25 result->values_ = std::move(values);
26 return result;
27 }
28
29 std::unique_ptr<RotationTimelineResolver>
30 PropertyResolver::MakeRotationTimeline(std::vector<Scalar> times,
31 std::vector<Quaternion> values) {
32 FML_DCHECK(times.size() == values.size());
33 auto result =
34 std::unique_ptr<RotationTimelineResolver>(new RotationTimelineResolver());
35 result->times_ = std::move(times);
36 result->values_ = std::move(values);
37 return result;
38 }
39
40 std::unique_ptr<ScaleTimelineResolver> PropertyResolver::MakeScaleTimeline(
41 std::vector<Scalar> times,
42 std::vector<Vector3> values) {
43 FML_DCHECK(times.size() == values.size());
44 auto result =
45 std::unique_ptr<ScaleTimelineResolver>(new ScaleTimelineResolver());
46 result->times_ = std::move(times);
47 result->values_ = std::move(values);
48 return result;
49 }
50
51 PropertyResolver::~PropertyResolver() = default;
52
53 TimelineResolver::~TimelineResolver() = default;
54
55 SecondsF TimelineResolver::GetEndTime() {
56 if (times_.empty()) {
57 return SecondsF::zero();
58 }
59 return SecondsF(times_.back());
60 }
61
62 TimelineResolver::TimelineKey TimelineResolver::GetTimelineKey(SecondsF time) {
63 if (times_.size() <= 1 || time.count() <= times_.front()) {
64 return {.index = 0, .lerp = 1};
65 }
66 if (time.count() >= times_.back()) {
67 return {.index = times_.size() - 1, .lerp = 1};
68 }
69 auto it = std::lower_bound(times_.begin(), times_.end(), time.count());
70 size_t index = std::distance(times_.begin(), it);
71
72 Scalar previous_time = *(it - 1);
73 Scalar next_time = *it;
74 return {.index = index,
75 .lerp = (time.count() - previous_time) / (next_time - previous_time)};
76 }
77
78 TranslationTimelineResolver::TranslationTimelineResolver() = default;
79
80 TranslationTimelineResolver::~TranslationTimelineResolver() = default;
81
82 void TranslationTimelineResolver::Apply(AnimationTransforms& target,
83 SecondsF time,
84 Scalar weight) {
85 if (values_.empty()) {
86 return;
87 }
88 auto key = GetTimelineKey(time);
89 auto value = values_[key.index];
90 if (key.lerp < 1) {
91 value = values_[key.index - 1].Lerp(value, key.lerp);
92 }
93
94 target.animated_pose.translation +=
95 (value - target.bind_pose.translation) * weight;
96 }
97
98 RotationTimelineResolver::RotationTimelineResolver() = default;
99
100 RotationTimelineResolver::~RotationTimelineResolver() = default;
101
102 void RotationTimelineResolver::Apply(AnimationTransforms& target,
103 SecondsF time,
104 Scalar weight) {
105 if (values_.empty()) {
106 return;
107 }
108 auto key = GetTimelineKey(time);
109 auto value = values_[key.index];
110 if (key.lerp < 1) {
111 value = values_[key.index - 1].Slerp(value, key.lerp);
112 }
113
114 target.animated_pose.rotation =
115 target.animated_pose.rotation *
116 Quaternion().Slerp(target.bind_pose.rotation.Invert() * value, weight);
117 }
118
119 ScaleTimelineResolver::ScaleTimelineResolver() = default;
120
121 ScaleTimelineResolver::~ScaleTimelineResolver() = default;
122
123 void ScaleTimelineResolver::Apply(AnimationTransforms& target,
124 SecondsF time,
125 Scalar weight) {
126 if (values_.empty()) {
127 return;
128 }
129 auto key = GetTimelineKey(time);
130 auto value = values_[key.index];
131 if (key.lerp < 1) {
132 value = values_[key.index - 1].Lerp(value, key.lerp);
133 }
134
135 target.animated_pose.scale *=
136 Vector3(1, 1, 1).Lerp(value / target.bind_pose.scale, weight);
137 }
138
139 } // namespace scene
140 } // namespace impeller
impeller::scene::AnimationTransforms
Definition: animation_transforms.h:13
impeller::scene::ScaleTimelineResolver
Definition: property_resolver.h:120
point.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::scene::TimelineResolver::GetEndTime
SecondsF GetEndTime()
Definition: property_resolver.cc:55
impeller::Quaternion::Invert
Quaternion Invert() const
Definition: quaternion.h:49
impeller::scene::AnimationTransforms::bind_pose
MatrixDecomposition bind_pose
Definition: animation_transforms.h:14
impeller::scene::TimelineResolver::TimelineKey
Definition: property_resolver.h:65
impeller::scene::PropertyResolver::~PropertyResolver
virtual ~PropertyResolver()
impeller::scene::TimelineResolver::times_
std::vector< Scalar > times_
Definition: property_resolver.h:74
impeller::scene::PropertyResolver::MakeTranslationTimeline
static std::unique_ptr< TranslationTimelineResolver > MakeTranslationTimeline(std::vector< Scalar > times, std::vector< Vector3 > values)
Definition: property_resolver.cc:19
impeller::scene::RotationTimelineResolver
Definition: property_resolver.h:99
property_resolver.h
impeller::Quaternion
Definition: quaternion.h:14
impeller::SecondsF
std::chrono::duration< float > SecondsF
Definition: timing.h:13
node.h
impeller::scene::TimelineResolver::TimelineKey::index
size_t index
The index of the closest previous keyframe.
Definition: property_resolver.h:67
impeller::scene::TranslationTimelineResolver
Definition: property_resolver.h:77
impeller::MatrixDecomposition::translation
Vector3 translation
Definition: matrix_decomposition.h:16
impeller::Quaternion::Slerp
Quaternion Slerp(const Quaternion &to, double time) const
Definition: quaternion.cc:10
impeller::MatrixDecomposition::rotation
Quaternion rotation
Definition: matrix_decomposition.h:20
impeller::scene::RotationTimelineResolver::~RotationTimelineResolver
~RotationTimelineResolver()
impeller::scene::TranslationTimelineResolver::~TranslationTimelineResolver
~TranslationTimelineResolver()
impeller::MatrixDecomposition::scale
Vector3 scale
Definition: matrix_decomposition.h:17
impeller::scene::PropertyResolver::MakeScaleTimeline
static std::unique_ptr< ScaleTimelineResolver > MakeScaleTimeline(std::vector< Scalar > times, std::vector< Vector3 > values)
Definition: property_resolver.cc:40
impeller::scene::RotationTimelineResolver::Apply
void Apply(AnimationTransforms &target, SecondsF time, Scalar weight) override
Resolve and apply the property value to a target node. This operation is additive; a given node prope...
Definition: property_resolver.cc:102
impeller::scene::AnimationTransforms::animated_pose
MatrixDecomposition animated_pose
Definition: animation_transforms.h:15
impeller::scene::PropertyResolver::MakeRotationTimeline
static std::unique_ptr< RotationTimelineResolver > MakeRotationTimeline(std::vector< Scalar > times, std::vector< Quaternion > values)
Definition: property_resolver.cc:30
matrix_decomposition.h
impeller::scene::ScaleTimelineResolver::Apply
void Apply(AnimationTransforms &target, SecondsF time, Scalar weight) override
Resolve and apply the property value to a target node. This operation is additive; a given node prope...
Definition: property_resolver.cc:123
impeller::scene::TranslationTimelineResolver::Apply
void Apply(AnimationTransforms &target, SecondsF time, Scalar weight) override
Resolve and apply the property value to a target node. This operation is additive; a given node prope...
Definition: property_resolver.cc:82
impeller
Definition: aiks_context.cc:10
impeller::scene::TimelineResolver::GetTimelineKey
TimelineKey GetTimelineKey(SecondsF time)
Definition: property_resolver.cc:62
impeller::Vector3
Definition: vector.h:20
impeller::Vector3::Lerp
constexpr Vector3 Lerp(const Vector3 &v, Scalar t) const
Definition: vector.h:178
impeller::scene::TimelineResolver::~TimelineResolver
virtual ~TimelineResolver()
impeller::scene::ScaleTimelineResolver::~ScaleTimelineResolver
~ScaleTimelineResolver()