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 30 of file node.h.

Constructor & Destructor Documentation

◆ Node()

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

Definition at line 234 of file node.cc.

234 : 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 281 of file node.cc.

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

Referenced by Render().

◆ AddChild()

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

Definition at line 310 of file node.cc.

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

References VALIDATION_LOG.

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

◆ AddMutation()

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

Definition at line 422 of file node.cc.

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

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

◆ FindAnimationByName()

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

Definition at line 271 of file node.cc.

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

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 254 of file node.cc.

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

◆ GetChildren()

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

Definition at line 335 of file node.cc.

335  {
336  return children_;
337 }

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

◆ GetGlobalTransform()

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

Definition at line 303 of file node.cc.

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

References GetGlobalTransform().

Referenced by GetGlobalTransform(), and SetGlobalTransform().

◆ GetLocalTransform()

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

Definition at line 292 of file node.cc.

292  {
293  return local_transform_;
294 }

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

◆ GetMesh()

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

Definition at line 343 of file node.cc.

343  {
344  return mesh_;
345 }

◆ GetName()

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

Definition at line 242 of file node.cc.

242  {
243  return name_;
244 }

◆ GetParent()

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

Definition at line 250 of file node.cc.

250  {
251  return parent_;
252 }

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

◆ IsJoint()

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

Definition at line 351 of file node.cc.

351  {
352  return is_joint_;
353 }

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 136 of file node.cc.

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

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 48 of file node.cc.

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

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 355 of file node.cc.

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

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 296 of file node.cc.

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

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

◆ SetIsJoint()

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

Definition at line 347 of file node.cc.

347  {
348  is_joint_ = is_joint;
349 }

◆ SetLocalTransform()

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

Definition at line 288 of file node.cc.

288  {
289  local_transform_ = transform;
290 }

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

◆ SetMesh()

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

Definition at line 339 of file node.cc.

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

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

◆ SetName()

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

Definition at line 246 of file node.cc.

246  {
247  name_ = new_name;
248 }

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:13
impeller::scene::Node::FindAnimationByName
std::shared_ptr< Animation > FindAnimationByName(const std::string &name) const
Definition: node.cc:271
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:62
impeller::scene::kNextNodeID
static std::atomic_uint64_t kNextNodeID
Definition: node.cc:28
impeller::Matrix::Invert
Matrix Invert() const
Definition: matrix.cc:97
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:67
impeller::scene::Node::AddAnimation
AnimationClip * AddAnimation(const std::shared_ptr< Animation > &animation)
Definition: node.cc:281
impeller::scene::Node::MakeFromFlatbuffer
static std::shared_ptr< Node > MakeFromFlatbuffer(const fml::Mapping &ipscene_mapping, Allocator &allocator)
Definition: node.cc:48
impeller::scene::Node::MutationLog::Append
void Append(const Entry &entry)
Definition: node.cc:30
impeller::scene::Node::GetGlobalTransform
Matrix GetGlobalTransform() const
Definition: node.cc:303
impeller::scene::importer::ToMatrix
Matrix ToMatrix(const std::vector< double > &m)
Definition: conversions.cc:15