| Flutter Impeller |

node.h
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 #ifndef FLUTTER_IMPELLER_SCENE_NODE_H_
6 #define FLUTTER_IMPELLER_SCENE_NODE_H_
7
8 #include <memory>
9 #include <optional>
10 #include <vector>
11
12 #include "impeller/base/thread.h"
13 #include "impeller/base/thread_safety.h"
14 #include "impeller/core/texture.h"
15 #include "impeller/geometry/matrix.h"
16 #include "impeller/scene/animation/animation.h"
17 #include "impeller/scene/animation/animation_clip.h"
18 #include "impeller/scene/animation/animation_player.h"
19 #include "impeller/scene/mesh.h"
20 #include "impeller/scene/scene_encoder.h"
21 #include "impeller/scene/skin.h"
22
23 namespace impeller {
24 namespace scene {
25
26 class Node final {
27 public:
28 class MutationLog {
29 public:
30 struct SetTransformEntry {
31 Matrix transform;
32 };
33
34 struct SetAnimationStateEntry {
35 std::string animation_name;
36 bool playing = false;
37 bool loop = false;
38 Scalar weight = 0;
39 Scalar time_scale = 1;
40 };
41
42 struct SeekAnimationEntry {
43 std::string animation_name;
44 float time = 0;
45 };
46
47 using Entry = std::
48 variant<SetTransformEntry, SetAnimationStateEntry, SeekAnimationEntry>;
49
50 void Append(const Entry& entry);
51
52 private:
53 std::optional<std::vector<Entry>> Flush();
54
55 RWMutex write_mutex_;
56 bool dirty_ IPLR_GUARDED_BY(write_mutex_) = false;
57 std::vector<Entry> entries_ IPLR_GUARDED_BY(write_mutex_);
58
59 friend Node;
60 };
61
62 static std::shared_ptr<Node> MakeFromFlatbuffer(
63 const fml::Mapping& ipscene_mapping,
64 Allocator& allocator);
65 static std::shared_ptr<Node> MakeFromFlatbuffer(const fb::Scene& scene,
66 Allocator& allocator);
67
68 Node();
69 ~Node();
70
71 const std::string& GetName() const;
72 void SetName(const std::string& new_name);
73
74 Node* GetParent() const;
75
76 std::shared_ptr<Node> FindChildByName(
77 const std::string& name,
78 bool exclude_animation_players = false) const;
79
80 std::shared_ptr<Animation> FindAnimationByName(const std::string& name) const;
81 AnimationClip* AddAnimation(const std::shared_ptr<Animation>& animation);
82
83 void SetLocalTransform(Matrix transform);
84 Matrix GetLocalTransform() const;
85
86 void SetGlobalTransform(Matrix transform);
87 Matrix GetGlobalTransform() const;
88
89 bool AddChild(std::shared_ptr<Node> child);
90 std::vector<std::shared_ptr<Node>>& GetChildren();
91
92 void SetMesh(Mesh mesh);
93 Mesh& GetMesh();
94
95 void SetIsJoint(bool is_joint);
96 bool IsJoint() const;
97
98 bool Render(SceneEncoder& encoder,
99 Allocator& allocator,
100 const Matrix& parent_transform);
101
102 void AddMutation(const MutationLog::Entry& entry);
103
104 private:
105 void UnpackFromFlatbuffer(
106 const fb::Node& node,
107 const std::vector<std::shared_ptr<Node>>& scene_nodes,
108 const std::vector<std::shared_ptr<Texture>>& textures,
109 Allocator& allocator);
110
111 mutable MutationLog mutation_log_;
112
113 Matrix local_transform_;
114
115 std::string name_;
116 bool is_root_ = false;
117 bool is_joint_ = false;
118 Node* parent_ = nullptr;
119 std::vector<std::shared_ptr<Node>> children_;
120 Mesh mesh_;
121
122 // For convenience purposes, deserialized nodes hang onto an animation library
123 std::vector<std::shared_ptr<Animation>> animations_;
124 mutable std::optional<AnimationPlayer> animation_player_;
125
126 std::unique_ptr<Skin> skin_;
127
128 Node(const Node&) = delete;
129
130 Node& operator=(const Node&) = delete;
131
132 friend Scene;
133 };
134
135 } // namespace scene
136 } // namespace impeller
137
138 #endif // FLUTTER_IMPELLER_SCENE_NODE_H_
animation.h
impeller::scene::Node::Node
Node()
Definition: node.cc:224
impeller::scene::Node::GetLocalTransform
Matrix GetLocalTransform() const
Definition: node.cc:282
impeller::scene::Node::GetParent
Node * GetParent() const
Definition: node.cc:240
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::scene::Mesh
Definition: mesh.h:19
impeller::scene::Node::FindChildByName
std::shared_ptr< Node > FindChildByName(const std::string &name, bool exclude_animation_players=false) const
Definition: node.cc:244
impeller::scene::Node::~Node
~Node()
impeller::scene::Node::SetIsJoint
void SetIsJoint(bool is_joint)
Definition: node.cc:337
impeller::scene::Node::MutationLog::SetAnimationStateEntry::playing
bool playing
Definition: node.h:36
animation_player.h
impeller::scene::Node::SetMesh
void SetMesh(Mesh mesh)
Definition: node.cc:329
impeller::scene::AnimationClip
Definition: animation_clip.h:21
impeller::scene::Node::GetName
const std::string & GetName() const
Definition: node.cc:232
scene_encoder.h
impeller::scene::Node::GetChildren
std::vector< std::shared_ptr< Node > > & GetChildren()
Definition: node.cc:325
impeller::scene::Node::SetName
void SetName(const std::string &new_name)
Definition: node.cc:236
matrix.h
impeller::scene::Node::MutationLog::SetTransformEntry::transform
Matrix transform
Definition: node.h:31
impeller::scene::Node::FindAnimationByName
std::shared_ptr< Animation > FindAnimationByName(const std::string &name) const
Definition: node.cc:261
animation_clip.h
impeller::scene::Node::MutationLog::SeekAnimationEntry::animation_name
std::string animation_name
Definition: node.h:43
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:231
thread_safety.h
impeller::scene::Node::MutationLog::SeekAnimationEntry::time
float time
Definition: node.h:44
impeller::scene::Node::MutationLog::SetAnimationStateEntry
Definition: node.h:34
impeller::Allocator
An object that allocates device memory.
Definition: allocator.h:22
impeller::scene::Node::GetMesh
Mesh & GetMesh()
Definition: node.cc:333
mesh.h
impeller::scene::Node::MutationLog::SetTransformEntry
Definition: node.h:30
impeller::scene::Node::SetGlobalTransform
void SetGlobalTransform(Matrix transform)
Definition: node.cc:286
impeller::scene::Node::AddChild
bool AddChild(std::shared_ptr< Node > child)
Definition: node.cc:300
impeller::scene::Node::Render
bool Render(SceneEncoder &encoder, Allocator &allocator, const Matrix &parent_transform)
Definition: node.cc:345
impeller::scene::Node::MutationLog::SetAnimationStateEntry::time_scale
Scalar time_scale
Definition: node.h:39
impeller::scene::Node::MutationLog::Entry
std::variant< SetTransformEntry, SetAnimationStateEntry, SeekAnimationEntry > Entry
Definition: node.h:48
impeller::scene::Node::IsJoint
bool IsJoint() const
Definition: node.cc:341
impeller::scene::Node::MutationLog::SeekAnimationEntry
Definition: node.h:42
impeller::scene::Node::MutationLog::SetAnimationStateEntry::loop
bool loop
Definition: node.h:37
texture.h
impeller::scene::Node::AddAnimation
AnimationClip * AddAnimation(const std::shared_ptr< Animation > &animation)
Definition: node.cc:271
impeller::scene::Node
Definition: node.h:26
impeller::scene::Scene
Definition: scene.h:18
impeller::scene::Node::AddMutation
void AddMutation(const MutationLog::Entry &entry)
Definition: node.cc:412
impeller::scene::SceneEncoder
Definition: scene_encoder.h:28
impeller::scene::Node::SetLocalTransform
void SetLocalTransform(Matrix transform)
Definition: node.cc:278
impeller::scene::Node::MakeFromFlatbuffer
static std::shared_ptr< Node > MakeFromFlatbuffer(const fml::Mapping &ipscene_mapping, Allocator &allocator)
Definition: node.cc:47
impeller::scene::Node::MutationLog::SetAnimationStateEntry::animation_name
std::string animation_name
Definition: node.h:35
thread.h
impeller
Definition: aiks_blend_unittests.cc:18
impeller::scene::Node::MutationLog::Append
void Append(const Entry &entry)
Definition: node.cc:29
skin.h
impeller::scene::Node::MutationLog
Definition: node.h:28
impeller::scene::Node::GetGlobalTransform
Matrix GetGlobalTransform() const
Definition: node.cc:293
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::scene::Node::MutationLog::SetAnimationStateEntry::weight
Scalar weight
Definition: node.h:38