SceneNode.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Scene/SceneNode.h>
  6. #include <AnKi/Scene/SceneGraph.h>
  7. #include <AnKi/Scene/Components/SceneComponent.h>
  8. #include <AnKi/Scene/Components/MoveComponent.h>
  9. #include <AnKi/Scene/Components/BodyComponent.h>
  10. #include <AnKi/Scene/Components/CameraComponent.h>
  11. #include <AnKi/Scene/Components/DecalComponent.h>
  12. #include <AnKi/Scene/Components/FogDensityComponent.h>
  13. #include <AnKi/Scene/Components/GlobalIlluminationProbeComponent.h>
  14. #include <AnKi/Scene/Components/JointComponent.h>
  15. #include <AnKi/Scene/Components/LensFlareComponent.h>
  16. #include <AnKi/Scene/Components/LightComponent.h>
  17. #include <AnKi/Scene/Components/MoveComponent.h>
  18. #include <AnKi/Scene/Components/ParticleEmitter2Component.h>
  19. #include <AnKi/Scene/Components/PlayerControllerComponent.h>
  20. #include <AnKi/Scene/Components/ReflectionProbeComponent.h>
  21. #include <AnKi/Scene/Components/ScriptComponent.h>
  22. #include <AnKi/Scene/Components/SkinComponent.h>
  23. #include <AnKi/Scene/Components/SkyboxComponent.h>
  24. #include <AnKi/Scene/Components/TriggerComponent.h>
  25. #include <AnKi/Scene/Components/UiComponent.h>
  26. #include <AnKi/Scene/Components/MeshComponent.h>
  27. #include <AnKi/Scene/Components/MaterialComponent.h>
  28. namespace anki {
  29. // Specialize newComponent(). Do that first
  30. #define ANKI_DEFINE_SCENE_COMPONENT(name, weight, sceneNodeCanHaveMany, icon, serializable) \
  31. template<> \
  32. name##Component* SceneNode::newComponent<name##Component>() \
  33. { \
  34. if(hasComponent<name##Component>() && !kSceneComponentSceneNodeCanHaveMany[SceneComponentType::k##name]) \
  35. { \
  36. ANKI_SCENE_LOGE("Can't have many %s components in scene node %s", kSceneComponentTypeName[SceneComponentType::k##name], \
  37. getName().cstr()); \
  38. return nullptr; \
  39. } \
  40. auto it = SceneGraph::getSingleton().getComponentArrays().get##name##s().emplace(this); \
  41. it->setArrayIndex(it.getArrayIndex()); \
  42. newComponentInternal(&(*it)); \
  43. return &(*it); \
  44. }
  45. #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
  46. SceneNode::SceneNode(CString name)
  47. : m_uuid(SceneGraph::getSingleton().getNewUuid())
  48. {
  49. if(name)
  50. {
  51. m_name = name;
  52. }
  53. // Add the implicit MoveComponent
  54. newComponent<MoveComponent>();
  55. }
  56. SceneNode::~SceneNode()
  57. {
  58. for(SceneComponent* comp : m_components)
  59. {
  60. comp->onDestroy(*this);
  61. switch(comp->getType())
  62. {
  63. #define ANKI_DEFINE_SCENE_COMPONENT(name, weight, sceneNodeCanHaveMany, icon, serializable) \
  64. case SceneComponentType::k##name: \
  65. SceneGraph::getSingleton().getComponentArrays().get##name##s().erase(comp->getArrayIndex()); \
  66. break;
  67. #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
  68. default:
  69. ANKI_ASSERT(0);
  70. }
  71. }
  72. }
  73. void SceneNode::markForDeletion()
  74. {
  75. visitThisAndChildren([](SceneNode& obj) {
  76. obj.m_markedForDeletion = true;
  77. return FunctorContinue::kContinue;
  78. });
  79. }
  80. void SceneNode::newComponentInternal(SceneComponent* newc)
  81. {
  82. m_componentTypeMask |= 1 << SceneComponentTypeMask(newc->getType());
  83. // Inform all other components that some component was added
  84. for(SceneComponent* other : m_components)
  85. {
  86. other->onOtherComponentRemovedOrAdded(newc, true);
  87. }
  88. // Inform the current component about others
  89. for(SceneComponent* other : m_components)
  90. {
  91. newc->onOtherComponentRemovedOrAdded(other, true);
  92. }
  93. m_components.emplaceBack(newc);
  94. // Sort based on update weight
  95. std::sort(m_components.getBegin(), m_components.getEnd(), [](const SceneComponent* a, const SceneComponent* b) {
  96. const F32 weightA = SceneComponent::getUpdateOrderWeight(a->getType());
  97. const F32 weightB = SceneComponent::getUpdateOrderWeight(b->getType());
  98. if(weightA != weightB)
  99. {
  100. return weightA < weightB;
  101. }
  102. else
  103. {
  104. return a->getType() < b->getType();
  105. }
  106. });
  107. }
  108. Bool SceneNode::updateTransform()
  109. {
  110. const Bool needsUpdate = m_localTransformDirty;
  111. m_localTransformDirty = false;
  112. const Bool updatedLastFrame = m_transformUpdatedThisFrame;
  113. m_transformUpdatedThisFrame = needsUpdate;
  114. if(needsUpdate || updatedLastFrame)
  115. {
  116. m_prevWTrf = m_wtrf;
  117. }
  118. // Update world transform
  119. if(needsUpdate)
  120. {
  121. const SceneNode* parent = getParent();
  122. if(parent == nullptr || m_ignoreParentNodeTransform)
  123. {
  124. m_wtrf = m_ltrf;
  125. }
  126. else
  127. {
  128. m_wtrf = parent->getWorldTransform().combineTransformations(m_ltrf);
  129. }
  130. // Make children dirty as well. Don't walk the whole tree because you will re-walk it later
  131. visitChildrenMaxDepth(1, [](SceneNode& childNode) {
  132. if(!childNode.m_ignoreParentNodeTransform)
  133. {
  134. childNode.m_localTransformDirty = true;
  135. }
  136. return FunctorContinue::kContinue;
  137. });
  138. }
  139. return needsUpdate;
  140. }
  141. void SceneNode::setName(CString name)
  142. {
  143. const SceneString oldName = getName();
  144. m_name = name;
  145. SceneGraph::getSingleton().sceneNodeChangedName(*this, oldName);
  146. }
  147. Error SceneNode::serializeCommon(SceneSerializer& serializer)
  148. {
  149. ANKI_SERIALIZE(m_uuid, 1);
  150. ANKI_SERIALIZE(m_name, 1);
  151. Vec3 origin = m_ltrf.getOrigin().xyz();
  152. ANKI_SERIALIZE(origin, 1);
  153. m_ltrf.setOrigin(origin);
  154. Mat3 rotation = m_ltrf.getRotation().getRotationPart();
  155. ANKI_SERIALIZE(rotation, 1);
  156. m_ltrf.setRotation(rotation);
  157. Vec3 scale = m_ltrf.getScale().xyz();
  158. ANKI_SERIALIZE(scale, 1);
  159. m_ltrf.setScale(scale);
  160. U32 componentCount = m_components.getSize();
  161. ANKI_SERIALIZE(componentCount, 1);
  162. SceneDynamicArray<U32> componentUuids;
  163. if(serializer.isInWriteMode())
  164. {
  165. for(SceneComponent* comp : m_components)
  166. {
  167. componentUuids.emplaceBack(comp->getUuid());
  168. }
  169. }
  170. else
  171. {
  172. ANKI_ASSERT(!"TODO");
  173. }
  174. ANKI_SERIALIZE(componentUuids, 1);
  175. // Parent
  176. SceneNode* parent = getParent();
  177. U32 parentUuid = (parent) ? parent->getUuid() : 0;
  178. ANKI_SERIALIZE(parentUuid, 1);
  179. if(serializer.isInReadMode())
  180. {
  181. ANKI_ASSERT(!"TODO");
  182. }
  183. return Error::kNone;
  184. }
  185. } // end namespace anki