Skip to content

Commit

Permalink
Revert "[Impeller Scene] Change how property resolution works to fix …
Browse files Browse the repository at this point in the history
…Animation blending; add mutation log to nodes; enable backface culling; add vertex color contribution back to meshes (#38766)"

This reverts commit 2b024cb.
  • Loading branch information
itsjustkevin authored Jan 13, 2023
1 parent a512ceb commit f28d200
Show file tree
Hide file tree
Showing 22 changed files with 93 additions and 416 deletions.
2 changes: 0 additions & 2 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,6 @@ ORIGIN: ../../../flutter/impeller/scene/animation/animation_clip.cc + ../../../f
ORIGIN: ../../../flutter/impeller/scene/animation/animation_clip.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/scene/animation/animation_player.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/scene/animation/animation_player.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/scene/animation/animation_transforms.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/scene/animation/property_resolver.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/scene/animation/property_resolver.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/scene/camera.cc + ../../../flutter/LICENSE
Expand Down Expand Up @@ -4122,7 +4121,6 @@ FILE: ../../../flutter/impeller/scene/animation/animation_clip.cc
FILE: ../../../flutter/impeller/scene/animation/animation_clip.h
FILE: ../../../flutter/impeller/scene/animation/animation_player.cc
FILE: ../../../flutter/impeller/scene/animation/animation_player.h
FILE: ../../../flutter/impeller/scene/animation/animation_transforms.h
FILE: ../../../flutter/impeller/scene/animation/property_resolver.cc
FILE: ../../../flutter/impeller/scene/animation/property_resolver.h
FILE: ../../../flutter/impeller/scene/camera.cc
Expand Down
2 changes: 0 additions & 2 deletions impeller/geometry/quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ struct Quaternion {
return {x * m, y * m, z * m, w * m};
}

Quaternion Invert() const { return {-x, -y, -z, w}; }

Quaternion Slerp(const Quaternion& to, double time) const;

Quaternion operator*(const Quaternion& o) const {
Expand Down
1 change: 0 additions & 1 deletion impeller/scene/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ impeller_component("scene") {
"animation/animation_clip.h",
"animation/animation_player.cc",
"animation/animation_player.h",
"animation/animation_transforms.h",
"animation/property_resolver.cc",
"animation/property_resolver.h",
"camera.cc",
Expand Down
13 changes: 3 additions & 10 deletions impeller/scene/animation/animation_clip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Scalar AnimationClip::GetWeight() const {
}

void AnimationClip::SetWeight(Scalar weight) {
weight_ = std::max(0.0f, weight);
weight_ = weight;
}

SecondsF AnimationClip::GetPlaybackTime() const {
Expand Down Expand Up @@ -110,16 +110,9 @@ void AnimationClip::Advance(SecondsF delta_time) {
}
}

void AnimationClip::ApplyToBindings(
std::unordered_map<Node*, AnimationTransforms>& transform_decomps,
Scalar weight_multiplier) const {
void AnimationClip::ApplyToBindings() const {
for (auto& binding : bindings_) {
auto transforms = transform_decomps.find(binding.node);
if (transforms == transform_decomps.end()) {
continue;
}
binding.channel.resolver->Apply(transforms->second, playback_time_,
weight_ * weight_multiplier);
binding.channel.resolver->Apply(*binding.node, playback_time_, weight_);
}
}

Expand Down
5 changes: 1 addition & 4 deletions impeller/scene/animation/animation_clip.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "flutter/fml/macros.h"
#include "impeller/scene/animation/animation.h"
#include "impeller/scene/animation/animation_transforms.h"

namespace impeller {
namespace scene {
Expand Down Expand Up @@ -61,9 +60,7 @@ class AnimationClip final {
void Advance(SecondsF delta_time);

/// @brief Applies the animation to all binded properties in the scene.
void ApplyToBindings(
std::unordered_map<Node*, AnimationTransforms>& transform_decomps,
Scalar weight_multiplier) const;
void ApplyToBindings() const;

private:
void BindToTarget(Node* node);
Expand Down
57 changes: 15 additions & 42 deletions impeller/scene/animation/animation_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "impeller/scene/animation/animation_player.h"

#include <memory>
#include <unordered_map>

#include "flutter/fml/time/time_point.h"
#include "impeller/base/timing.h"
Expand All @@ -20,37 +19,20 @@ AnimationPlayer::~AnimationPlayer() = default;
AnimationPlayer::AnimationPlayer(AnimationPlayer&&) = default;
AnimationPlayer& AnimationPlayer::operator=(AnimationPlayer&&) = default;

AnimationClip* AnimationPlayer::AddAnimation(
const std::shared_ptr<Animation>& animation,
AnimationClip& AnimationPlayer::AddAnimation(
std::shared_ptr<Animation> animation,
Node* bind_target) {
if (!animation) {
VALIDATION_LOG << "Cannot add null animation.";
return nullptr;
}

AnimationClip clip(animation, bind_target);
AnimationClip clip(std::move(animation), bind_target);

// Record all of the unique default transforms that this AnimationClip
// will mutate.
for (const auto& binding : clip.bindings_) {
auto decomp = binding.node->GetLocalTransform().Decompose();
if (!decomp.has_value()) {
continue;
}
target_transforms_.insert(
{binding.node, AnimationTransforms{.bind_pose = decomp.value()}});
default_target_transforms_.insert(
{binding.node, binding.node->GetLocalTransform()});
}

auto result = clips_.insert({animation->GetName(), std::move(clip)});
return &result.first->second;
}

AnimationClip* AnimationPlayer::GetClip(const std::string& name) const {
auto result = clips_.find(name);
if (result == clips_.end()) {
return nullptr;
}
return const_cast<AnimationClip*>(&result->second);
clips_.push_back(std::move(clip));
return clips_.back();
}

void AnimationPlayer::Update() {
Expand All @@ -61,27 +43,18 @@ void AnimationPlayer::Update() {
auto delta_time = new_time - previous_time_.value();
previous_time_ = new_time;

// Reset the animated pose state.
for (auto& [node, transforms] : target_transforms_) {
transforms.animated_pose = transforms.bind_pose;
}

// Compute a weight multiplier for normalizing the animation.
Scalar total_weight = 0;
for (auto& [_, clip] : clips_) {
total_weight += clip.GetWeight();
}
Scalar weight_multiplier = total_weight > 1 ? 1 / total_weight : 1;
Reset();

// Update and apply all clips to the animation pose state.
for (auto& [_, clip] : clips_) {
// Update and apply all clips.
for (auto& clip : clips_) {
clip.Advance(delta_time);
clip.ApplyToBindings(target_transforms_, weight_multiplier);
clip.ApplyToBindings();
}
}

// Apply the animated pose to the bound joints.
for (auto& [node, transforms] : target_transforms_) {
node->SetLocalTransform(Matrix(transforms.animated_pose));
void AnimationPlayer::Reset() {
for (auto& [node, transform] : default_target_transforms_) {
node->SetLocalTransform(Matrix());
}
}

Expand Down
14 changes: 7 additions & 7 deletions impeller/scene/animation/animation_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

#pragma once

#include <map>
#include <memory>
#include <optional>
#include <unordered_map>
#include <vector>

#include "flutter/fml/hash_combine.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/time/time_delta.h"
#include "impeller/base/timing.h"
#include "impeller/geometry/matrix.h"
#include "impeller/geometry/matrix_decomposition.h"
#include "impeller/scene/animation/animation_clip.h"

namespace impeller {
Expand All @@ -30,18 +29,19 @@ class AnimationPlayer final {
AnimationPlayer(AnimationPlayer&&);
AnimationPlayer& operator=(AnimationPlayer&&);

AnimationClip* AddAnimation(const std::shared_ptr<Animation>& animation,
AnimationClip& AddAnimation(std::shared_ptr<Animation> animation,
Node* bind_target);

AnimationClip* GetClip(const std::string& name) const;

/// @brief Advanced all clips and updates animated properties in the scene.
void Update();

/// @brief Reset all bound animation target transforms.
void Reset();

private:
std::unordered_map<Node*, AnimationTransforms> target_transforms_;
std::unordered_map<Node*, Matrix> default_target_transforms_;

std::map<std::string, AnimationClip> clips_;
std::vector<AnimationClip> clips_;

std::optional<TimePoint> previous_time_;

Expand Down
18 changes: 0 additions & 18 deletions impeller/scene/animation/animation_transforms.h

This file was deleted.

25 changes: 9 additions & 16 deletions impeller/scene/animation/property_resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <iterator>
#include <memory>

#include "impeller/geometry/matrix_decomposition.h"
#include "impeller/geometry/point.h"
#include "impeller/scene/node.h"

Expand Down Expand Up @@ -79,7 +78,7 @@ TranslationTimelineResolver::TranslationTimelineResolver() = default;

TranslationTimelineResolver::~TranslationTimelineResolver() = default;

void TranslationTimelineResolver::Apply(AnimationTransforms& target,
void TranslationTimelineResolver::Apply(Node& target,
SecondsF time,
Scalar weight) {
if (values_.empty()) {
Expand All @@ -90,16 +89,15 @@ void TranslationTimelineResolver::Apply(AnimationTransforms& target,
if (key.lerp < 1) {
value = values_[key.index - 1].Lerp(value, key.lerp);
}

target.animated_pose.translation +=
(value - target.bind_pose.translation) * weight;
target.SetLocalTransform(target.GetLocalTransform() *
Matrix::MakeTranslation(value * weight));
}

RotationTimelineResolver::RotationTimelineResolver() = default;

RotationTimelineResolver::~RotationTimelineResolver() = default;

void RotationTimelineResolver::Apply(AnimationTransforms& target,
void RotationTimelineResolver::Apply(Node& target,
SecondsF time,
Scalar weight) {
if (values_.empty()) {
Expand All @@ -110,19 +108,15 @@ void RotationTimelineResolver::Apply(AnimationTransforms& target,
if (key.lerp < 1) {
value = values_[key.index - 1].Slerp(value, key.lerp);
}

target.animated_pose.rotation =
target.animated_pose.rotation *
Quaternion().Slerp(target.bind_pose.rotation.Invert() * value, weight);
target.SetLocalTransform(target.GetLocalTransform() *
Matrix::MakeRotation(value * weight));
}

ScaleTimelineResolver::ScaleTimelineResolver() = default;

ScaleTimelineResolver::~ScaleTimelineResolver() = default;

void ScaleTimelineResolver::Apply(AnimationTransforms& target,
SecondsF time,
Scalar weight) {
void ScaleTimelineResolver::Apply(Node& target, SecondsF time, Scalar weight) {
if (values_.empty()) {
return;
}
Expand All @@ -131,9 +125,8 @@ void ScaleTimelineResolver::Apply(AnimationTransforms& target,
if (key.lerp < 1) {
value = values_[key.index - 1].Lerp(value, key.lerp);
}

target.animated_pose.scale *=
Vector3(1, 1, 1).Lerp(value / target.bind_pose.scale, weight);
target.SetLocalTransform(target.GetLocalTransform() *
Matrix::MakeScale(value * weight));
}

} // namespace scene
Expand Down
18 changes: 4 additions & 14 deletions impeller/scene/animation/property_resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
#include "flutter/fml/hash_combine.h"
#include "flutter/fml/macros.h"
#include "impeller/base/timing.h"
#include "impeller/geometry/matrix_decomposition.h"
#include "impeller/geometry/quaternion.h"
#include "impeller/geometry/scalar.h"
#include "impeller/geometry/vector.h"
#include "impeller/scene/animation/animation_transforms.h"

namespace impeller {
namespace scene {
Expand Down Expand Up @@ -48,9 +46,7 @@ class PropertyResolver {
/// many different PropertyResolvers prior to rendering. For example,
/// an AnimationPlayer may blend multiple Animations together by
/// applying several AnimationClips.
virtual void Apply(AnimationTransforms& target,
SecondsF time,
Scalar weight) = 0;
virtual void Apply(Node& target, SecondsF time, Scalar weight) = 0;
};

class TimelineResolver : public PropertyResolver {
Expand Down Expand Up @@ -78,9 +74,7 @@ class TranslationTimelineResolver final : public TimelineResolver {
~TranslationTimelineResolver();

// |Resolver|
void Apply(AnimationTransforms& target,
SecondsF time,
Scalar weight) override;
void Apply(Node& target, SecondsF time, Scalar weight) override;

private:
TranslationTimelineResolver();
Expand All @@ -97,9 +91,7 @@ class RotationTimelineResolver final : public TimelineResolver {
~RotationTimelineResolver();

// |Resolver|
void Apply(AnimationTransforms& target,
SecondsF time,
Scalar weight) override;
void Apply(Node& target, SecondsF time, Scalar weight) override;

private:
RotationTimelineResolver();
Expand All @@ -116,9 +108,7 @@ class ScaleTimelineResolver final : public TimelineResolver {
~ScaleTimelineResolver();

// |Resolver|
void Apply(AnimationTransforms& target,
SecondsF time,
Scalar weight) override;
void Apply(Node& target, SecondsF time, Scalar weight) override;

private:
ScaleTimelineResolver();
Expand Down
2 changes: 1 addition & 1 deletion impeller/scene/geometry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ std::shared_ptr<Geometry> Geometry::MakeFromFlatbuffer(
}

DeviceBufferDescriptor buffer_desc;
buffer_desc.size = vertices_bytes + indices_bytes;
buffer_desc.size = vertices_bytes * indices_bytes;
buffer_desc.storage_mode = StorageMode::kHostVisible;

auto buffer = allocator.CreateBuffer(buffer_desc);
Expand Down
2 changes: 2 additions & 0 deletions impeller/scene/material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ std::unique_ptr<UnlitMaterial> UnlitMaterial::MakeFromFlatbuffer(

if (material.base_color_factor()) {
result->SetColor(importer::ToColor(*material.base_color_factor()));
result->SetVertexColorWeight(0);
}

if (material.base_color_texture() >= 0 &&
material.base_color_texture() < static_cast<int32_t>(textures.size())) {
result->SetColorTexture(textures[material.base_color_texture()]);
result->SetVertexColorWeight(0);
}

return result;
Expand Down
Loading

0 comments on commit f28d200

Please sign in to comment.