Flutter Impeller
impeller::scene::Node Class Referencefinal

#include <node.h>

Classes

class  MutationLog
 

Public Member Functions

 Node ()
 
 ~Node ()
 
const std::string & GetName () const
 
void SetName (const std::string &new_name)
 
NodeGetParent () const
 
std::shared_ptr< NodeFindChildByName (const std::string &name, bool exclude_animation_players=false) const
 
std::shared_ptr< AnimationFindAnimationByName (const std::string &name) const
 
AnimationClipAddAnimation (const std::shared_ptr< Animation > &animation)
 
void SetLocalTransform (Matrix transform)
 
Matrix GetLocalTransform () const
 
void SetGlobalTransform (Matrix transform)
 
Matrix GetGlobalTransform () const
 
bool AddChild (std::shared_ptr< Node > child)
 
std::vector< std::shared_ptr< Node > > & GetChildren ()
 
void SetMesh (Mesh mesh)
 
MeshGetMesh ()
 
void SetIsJoint (bool is_joint)
 
bool IsJoint () const
 
bool Render (SceneEncoder &encoder, Allocator &allocator, const Matrix &parent_transform)
 
void AddMutation (const MutationLog::Entry &entry)
 

Static Public Member Functions

static std::shared_ptr< NodeMakeFromFlatbuffer (const fml::Mapping &ipscene_mapping, Allocator &allocator)
 
static std::shared_ptr< NodeMakeFromFlatbuffer (const fb::Scene &scene, Allocator &allocator)
 

Detailed Description

Definition at line 29 of file node.h.

Constructor & Destructor Documentation

◆ Node()

impeller::scene::Node::Node ( )

Definition at line 233 of file node.cc.

233 : name_(SPrintF("__node%" PRIu64, kNextNodeID++)){};

◆ ~Node()

impeller::scene::Node::~Node ( )
default

Member Function Documentation

◆ AddAnimation()

AnimationClip * impeller::scene::Node::AddAnimation ( const std::shared_ptr< Animation > &  animation)

Definition at line 280 of file node.cc.

280  {
281  if (!animation_player_.has_value()) {
282  animation_player_ = AnimationPlayer();
283  }
284  return animation_player_->AddAnimation(animation, this);
285 }

Referenced by Render().

◆ AddChild()

bool impeller::scene::Node::AddChild ( std::shared_ptr< Node child)

Definition at line 309 of file node.cc.

309  {
310  if (!node) {
311  VALIDATION_LOG << "Cannot add null child to node.";
312  return false;
313  }
314 
315  // TODO(bdero): Figure out a better paradigm/rules for nodes with multiple
316  // parents. We should probably disallow this, make deep
317  // copying of nodes cheap and easy, add mesh instancing, etc.
318  // Today, the parent link is only used for skin posing, and so
319  // it's reasonable to not have a check and allow multi-parenting.
320  // Even still, there should still be some kind of cycle
321  // prevention/detection, ideally at the protocol level.
322  //
323  // if (node->parent_ != nullptr) {
324  // VALIDATION_LOG
325  // << "Cannot add a node as a child which already has a parent.";
326  // return false;
327  // }
328  node->parent_ = this;
329  children_.push_back(std::move(node));
330 
331  return true;
332 }

References VALIDATION_LOG.

Referenced by impeller::SceneContents::Render().

◆ AddMutation()

void impeller::scene::Node::AddMutation ( const MutationLog::Entry entry)

Definition at line 421 of file node.cc.

421  {
422  mutation_log_.Append(entry);
423 }

References impeller::scene::Node::MutationLog::Append().

◆ FindAnimationByName()

std::shared_ptr< Animation > impeller::scene::Node::FindAnimationByName ( const std::string &  name) const

Definition at line 270 of file node.cc.

271  {
272  for (const auto& animation : animations_) {
273  if (animation->GetName() == name) {
274  return animation;
275  }
276  }
277  return nullptr;
278 }

Referenced by Render().

◆ FindChildByName()

std::shared_ptr< Node > impeller::scene::Node::FindChildByName ( const std::string &  name,
bool  exclude_animation_players = false 
) const

Definition at line 253 of file node.cc.

255  {
256  for (auto& child : children_) {
257  if (exclude_animation_players && child->animation_player_.has_value()) {
258  continue;
259  }
260  if (child->GetName() == name) {
261  return child;
262  }
263  if (auto found = child->FindChildByName(name)) {
264  return found;
265  }
266  }
267  return nullptr;
268 }

◆ GetChildren()

std::vector< std::shared_ptr< Node > > & impeller::scene::Node::GetChildren ( )

Definition at line 334 of file node.cc.

334  {
335  return children_;
336 }

Referenced by impeller::scene::testing::TEST_P().

◆ GetGlobalTransform()

Matrix impeller::scene::Node::GetGlobalTransform ( ) const

Definition at line 302 of file node.cc.

302  {
303  if (parent_) {
304  return parent_->GetGlobalTransform() * local_transform_;
305  }
306  return local_transform_;
307 }

References GetGlobalTransform().

Referenced by GetGlobalTransform(), and SetGlobalTransform().

◆ GetLocalTransform()

Matrix impeller::scene::Node::GetLocalTransform ( ) const

Definition at line 291 of file node.cc.

291  {
292  return local_transform_;
293 }

Referenced by impeller::scene::Skin::GetJointsTexture(), and impeller::scene::testing::TEST_P().

◆ GetMesh()

Mesh & impeller::scene::Node::GetMesh ( )

Definition at line 342 of file node.cc.

342  {
343  return mesh_;
344 }

◆ GetName()

const std::string & impeller::scene::Node::GetName ( ) const

Definition at line 241 of file node.cc.

241  {
242  return name_;
243 }

◆ GetParent()

Node * impeller::scene::Node::GetParent ( ) const

Definition at line 249 of file node.cc.

249  {
250  return parent_;
251 }

Referenced by impeller::scene::Skin::GetJointsTexture().

◆ IsJoint()

bool impeller::scene::Node::IsJoint ( ) const

Definition at line 350 of file node.cc.

350  {
351  return is_joint_;
352 }

Referenced by impeller::scene::Skin::GetJointsTexture().

◆ MakeFromFlatbuffer() [1/2]

std::shared_ptr< Node > impeller::scene::Node::MakeFromFlatbuffer ( const fb::Scene &  scene,
Allocator allocator 
)
static

Definition at line 135 of file node.cc.

136  {
137  // Unpack textures.
138  std::vector<std::shared_ptr<Texture>> textures;
139  if (scene.textures()) {
140  for (const auto iptexture : *scene.textures()) {
141  // The elements of the unpacked texture array must correspond exactly with
142  // the ipscene texture array. So if a texture is empty or invalid, a
143  // nullptr is inserted as a placeholder.
144  textures.push_back(UnpackTextureFromFlatbuffer(iptexture, allocator));
145  }
146  }
147 
148  auto result = std::make_shared<Node>();
149  result->SetLocalTransform(importer::ToMatrix(*scene.transform()));
150 
151  if (!scene.nodes() || !scene.children()) {
152  return result; // The scene is empty.
153  }
154 
155  // Initialize nodes for unpacking the entire scene.
156  std::vector<std::shared_ptr<Node>> scene_nodes;
157  scene_nodes.reserve(scene.nodes()->size());
158  for (size_t node_i = 0; node_i < scene.nodes()->size(); node_i++) {
159  scene_nodes.push_back(std::make_shared<Node>());
160  }
161 
162  // Connect children to the root node.
163  for (int child : *scene.children()) {
164  if (child < 0 || static_cast<size_t>(child) >= scene_nodes.size()) {
165  VALIDATION_LOG << "Scene child index out of range.";
166  continue;
167  }
168  result->AddChild(scene_nodes[child]);
169  }
170 
171  // Unpack each node.
172  for (size_t node_i = 0; node_i < scene.nodes()->size(); node_i++) {
173  scene_nodes[node_i]->UnpackFromFlatbuffer(*scene.nodes()->Get(node_i),
174  scene_nodes, textures, allocator);
175  }
176 
177  // Unpack animations.
178  if (scene.animations()) {
179  for (const auto animation : *scene.animations()) {
180  if (auto out_animation =
181  Animation::MakeFromFlatbuffer(*animation, scene_nodes)) {
182  result->animations_.push_back(out_animation);
183  }
184  }
185  }
186 
187  return result;
188 }

References impeller::scene::Animation::MakeFromFlatbuffer(), impeller::scene::importer::ToMatrix(), impeller::scene::UnpackTextureFromFlatbuffer(), and VALIDATION_LOG.

◆ MakeFromFlatbuffer() [2/2]

std::shared_ptr< Node > impeller::scene::Node::MakeFromFlatbuffer ( const fml::Mapping &  ipscene_mapping,
Allocator allocator 
)
static

Definition at line 47 of file node.cc.

49  {
50  flatbuffers::Verifier verifier(ipscene_mapping.GetMapping(),
51  ipscene_mapping.GetSize());
52  if (!fb::VerifySceneBuffer(verifier)) {
53  VALIDATION_LOG << "Failed to unpack scene: Scene flatbuffer is invalid.";
54  return nullptr;
55  }
56 
57  return Node::MakeFromFlatbuffer(*fb::GetScene(ipscene_mapping.GetMapping()),
58  allocator);
59 }

References VALIDATION_LOG.

Referenced by impeller::scene::testing::TEST_P().

◆ Render()

bool impeller::scene::Node::Render ( SceneEncoder encoder,
Allocator allocator,
const Matrix parent_transform 
)

Definition at line 354 of file node.cc.

356  {
357  std::optional<std::vector<MutationLog::Entry>> log = mutation_log_.Flush();
358  if (log.has_value()) {
359  for (const auto& entry : log.value()) {
360  if (auto e = std::get_if<MutationLog::SetTransformEntry>(&entry)) {
361  local_transform_ = e->transform;
362  } else if (auto e =
363  std::get_if<MutationLog::SetAnimationStateEntry>(&entry)) {
364  AnimationClip* clip =
365  animation_player_.has_value()
366  ? animation_player_->GetClip(e->animation_name)
367  : nullptr;
368  if (!clip) {
369  auto animation = FindAnimationByName(e->animation_name);
370  if (!animation) {
371  continue;
372  }
373  clip = AddAnimation(animation);
374  if (!clip) {
375  continue;
376  }
377  }
378 
379  clip->SetPlaying(e->playing);
380  clip->SetLoop(e->loop);
381  clip->SetWeight(e->weight);
382  clip->SetPlaybackTimeScale(e->time_scale);
383  } else if (auto e =
384  std::get_if<MutationLog::SeekAnimationEntry>(&entry)) {
385  AnimationClip* clip =
386  animation_player_.has_value()
387  ? animation_player_->GetClip(e->animation_name)
388  : nullptr;
389  if (!clip) {
390  auto animation = FindAnimationByName(e->animation_name);
391  if (!animation) {
392  continue;
393  }
394  clip = AddAnimation(animation);
395  if (!clip) {
396  continue;
397  }
398  }
399 
400  clip->Seek(SecondsF(e->time));
401  }
402  }
403  }
404 
405  if (animation_player_.has_value()) {
406  animation_player_->Update();
407  }
408 
409  Matrix transform = parent_transform * local_transform_;
410  mesh_.Render(encoder, transform,
411  skin_ ? skin_->GetJointsTexture(allocator) : nullptr);
412 
413  for (auto& child : children_) {
414  if (!child->Render(encoder, allocator, transform)) {
415  return false;
416  }
417  }
418  return true;
419 }

References AddAnimation(), FindAnimationByName(), impeller::scene::Mesh::Render(), impeller::scene::AnimationClip::Seek(), impeller::scene::AnimationClip::SetLoop(), impeller::scene::AnimationClip::SetPlaybackTimeScale(), impeller::scene::AnimationClip::SetPlaying(), and impeller::scene::AnimationClip::SetWeight().

Referenced by impeller::scene::Scene::Render().

◆ SetGlobalTransform()

void impeller::scene::Node::SetGlobalTransform ( Matrix  transform)

Definition at line 295 of file node.cc.

295  {
296  Matrix inverse_global_transform =
297  parent_ ? parent_->GetGlobalTransform().Invert() : Matrix();
298 
299  local_transform_ = inverse_global_transform * transform;
300 }

References GetGlobalTransform(), and impeller::Matrix::Invert().

◆ SetIsJoint()

void impeller::scene::Node::SetIsJoint ( bool  is_joint)

Definition at line 346 of file node.cc.

346  {
347  is_joint_ = is_joint;
348 }

◆ SetLocalTransform()

void impeller::scene::Node::SetLocalTransform ( Matrix  transform)

Definition at line 287 of file node.cc.

287  {
288  local_transform_ = transform;
289 }

Referenced by impeller::scene::testing::TEST_P().

◆ SetMesh()

void impeller::scene::Node::SetMesh ( Mesh  mesh)

Definition at line 338 of file node.cc.

338  {
339  mesh_ = std::move(mesh);
340 }

Referenced by impeller::scene::testing::TEST_P().

◆ SetName()

void impeller::scene::Node::SetName ( const std::string &  new_name)

Definition at line 245 of file node.cc.

245  {
246  name_ = new_name;
247 }

The documentation for this class was generated from the following files:
impeller::scene::Animation::MakeFromFlatbuffer
static std::shared_ptr< Animation > MakeFromFlatbuffer(const fb::Animation &animation, const std::vector< std::shared_ptr< Node >> &scene_nodes)
Definition: animation.cc:19
impeller::scene::Mesh::Render
bool Render(SceneEncoder &encoder, const Matrix &transform, const std::shared_ptr< Texture > &joints) const
Definition: mesh.cc:36
impeller::SecondsF
std::chrono::duration< float > SecondsF
Definition: timing.h:12
impeller::scene::Node::FindAnimationByName
std::shared_ptr< Animation > FindAnimationByName(const std::string &name) const
Definition: node.cc:270
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::scene::UnpackTextureFromFlatbuffer
static std::shared_ptr< Texture > UnpackTextureFromFlatbuffer(const fb::Texture *iptexture, Allocator &allocator)
Definition: node.cc:61
impeller::scene::kNextNodeID
static std::atomic_uint64_t kNextNodeID
Definition: node.cc:27
impeller::Matrix::Invert
Matrix Invert() const
Definition: matrix.cc:97
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::scene::Node::AddAnimation
AnimationClip * AddAnimation(const std::shared_ptr< Animation > &animation)
Definition: node.cc:280
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::Append
void Append(const Entry &entry)
Definition: node.cc:29
impeller::scene::Node::GetGlobalTransform
Matrix GetGlobalTransform() const
Definition: node.cc:302
impeller::scene::importer::ToMatrix
Matrix ToMatrix(const std::vector< double > &m)
Definition: conversions.cc:15