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