BsSceneObject.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsMatrix4.h"
  4. #include "BsVector3.h"
  5. #include "BsQuaternion.h"
  6. #include "BsRTTIType.h"
  7. #include "BsCoreSceneManager.h"
  8. #include "BsGameObjectManager.h"
  9. #include "BsGameObject.h"
  10. namespace BansheeEngine
  11. {
  12. /**
  13. * @brief SceneObject represents an object in the scene graph. It has a world position,
  14. * place in the hierarchy and optionally a number of attached components.
  15. */
  16. class BS_CORE_EXPORT SceneObject : public GameObject
  17. {
  18. /**
  19. * @brief Flags that signify which part of the SceneObject needs updating.
  20. */
  21. enum DirtyFlags
  22. {
  23. LocalTfrmDirty = 0x01,
  24. WorldTfrmDirty = 0x02
  25. };
  26. friend class CoreSceneManager;
  27. public:
  28. ~SceneObject();
  29. /**
  30. * @brief Creates a new SceneObject with the specified name. Object will be placed in the top
  31. * of the scene hierarchy.
  32. */
  33. static HSceneObject create(const String& name);
  34. /**
  35. * @brief Destroys this object and any of its held components.
  36. *
  37. * @param [in] immediate If true, the object will be deallocated and become unusable
  38. * right away. Otherwise the deallocation will be delayed to the end of
  39. * frame (preferred method).
  40. */
  41. void destroy(bool immediate = false);
  42. /**
  43. * @copydoc GameObject::_setInstanceData
  44. */
  45. void _setInstanceData(GameObjectInstanceDataPtr& other);
  46. /**
  47. * @brief Returns a handle to this object.
  48. */
  49. HSceneObject getHandle() const { return mThisHandle; }
  50. private:
  51. SceneObject(const String& name);
  52. static HSceneObject createInternal(const String& name);
  53. /**
  54. * @brief Destroys this object and any of its held components.
  55. *
  56. * @param [in] immediate If true, the object will be deallocated and become unusable
  57. * right away. Otherwise the deallocation will be delayed to the end of
  58. * frame (preferred method).
  59. *
  60. * @note Unlike "destroy", does not remove the object from its parent.
  61. */
  62. void destroyInternal(bool immediate = false);
  63. private:
  64. HSceneObject mThisHandle;
  65. /************************************************************************/
  66. /* Transform */
  67. /************************************************************************/
  68. public:
  69. /**
  70. * @brief Sets the local position of the object.
  71. */
  72. void setPosition(const Vector3& position);
  73. /**
  74. * @brief Gets the local position of the object.
  75. */
  76. const Vector3& getPosition() const { return mPosition; }
  77. /**
  78. * @brief Sets the world position of the object.
  79. */
  80. void setWorldPosition(const Vector3& position);
  81. /**
  82. * @brief Gets the world position of the object.
  83. *
  84. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  85. */
  86. const Vector3& getWorldPosition() const;
  87. /**
  88. * @brief Sets the local rotation of the object.
  89. */
  90. void setRotation(const Quaternion& rotation);
  91. /**
  92. * @brief Gets the local rotation of the object.
  93. */
  94. const Quaternion& getRotation() const { return mRotation; }
  95. /**
  96. * @brief Sets the world rotation of the object.
  97. */
  98. void setWorldRotation(const Quaternion& rotation);
  99. /**
  100. * @brief Gets world rotation of the object.
  101. *
  102. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  103. */
  104. const Quaternion& getWorldRotation() const;
  105. /**
  106. * @brief Sets the local scale of the object.
  107. */
  108. void setScale(const Vector3& scale);
  109. /**
  110. * @brief Gets the local scale of the object.
  111. */
  112. const Vector3& getScale() const { return mScale; }
  113. /**
  114. * @brief Gets world scale of the object.
  115. *
  116. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  117. */
  118. const Vector3& getWorldScale() const;
  119. /**
  120. * @brief Orients the object so it is looking at the provided "location" (local space)
  121. * where "up" is used for determining the location of the objects Y axis.
  122. *
  123. */
  124. void lookAt(const Vector3& location, const Vector3& up = Vector3::UNIT_Y);
  125. /**
  126. * @brief Gets the objects world transform matrix.
  127. *
  128. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  129. */
  130. const Matrix4& getWorldTfrm() const;
  131. /**
  132. * @brief Gets the objects local transform matrix.
  133. */
  134. const Matrix4& getLocalTfrm() const;
  135. /**
  136. * @brief Moves the object's position by the vector offset provided along world axes.
  137. */
  138. void move(const Vector3& vec);
  139. /**
  140. * @brief Moves the object's position by the vector offset provided along it's own axes (relative to orientation).
  141. */
  142. void moveRelative(const Vector3& vec);
  143. /**
  144. * @brief Gets the Z (forward) axis of the object, in world space.
  145. *
  146. * @return Forward axis of the object.
  147. */
  148. Vector3 getForward() const { return getWorldRotation().rotate(-Vector3::UNIT_Z); }
  149. /**
  150. * @brief Gets the Y (up) axis of the object, in world space.
  151. *
  152. * @return Up axis of the object.
  153. */
  154. Vector3 getUp() const { return getWorldRotation().rotate(Vector3::UNIT_Y); }
  155. /**
  156. * @brief Gets the X (right) axis of the object, in world space.
  157. *
  158. * @return Right axis of the object.
  159. */
  160. Vector3 getRight() const { return getWorldRotation().rotate(Vector3::UNIT_X); }
  161. /**
  162. * @brief Rotates the game object so it's forward axis faces the provided
  163. * direction.
  164. *
  165. * @note Local forward axis is considered to be negative Z.
  166. *
  167. * @param forwardDir The forward direction to face, in world space.
  168. */
  169. void setForward(const Vector3& forwardDir);
  170. /**
  171. * @brief Rotate the object around an arbitrary axis.
  172. */
  173. void rotate(const Vector3& axis, const Radian& angle);
  174. /**
  175. * @brief Rotate the object around an arbitrary axis using a Quaternion.
  176. */
  177. void rotate(const Quaternion& q);
  178. /**
  179. * @brief Rotates around local Z axis.
  180. *
  181. * @param angle Angle to rotate by.
  182. */
  183. void roll(const Radian& angle);
  184. /**
  185. * @brief Rotates around Y axis.
  186. *
  187. * @param angle Angle to rotate by.
  188. */
  189. void yaw(const Radian& angle);
  190. /**
  191. * @brief Rotates around X axis
  192. *
  193. * @param angle Angle to rotate by.
  194. */
  195. void pitch(const Radian& angle);
  196. /**
  197. * @brief Forces any dirty transform matrices on this object to be updated.
  198. *
  199. * @note Normally this is done internally when retrieving a transform, but sometimes
  200. * it is useful to update transforms manually.
  201. */
  202. void updateTransformsIfDirty();
  203. /**
  204. * @brief Checks is the core dirty flag set. This is used by external systems
  205. * to know when internal data has changed and core thread potentially needs to be notified.
  206. */
  207. bool _isCoreDirty() const { return mIsCoreDirtyFlags != 0; }
  208. /**
  209. * @brief Marks the core dirty flag as clean.
  210. */
  211. void _markCoreClean() { mIsCoreDirtyFlags = 0; }
  212. /**
  213. * @brief Returns a hash value that changes whenever a scene objects
  214. * transform gets updated. It allows you to detect changes with
  215. * the local or world transforms without directly comparing their
  216. * values with some older state.
  217. */
  218. UINT32 getTransformHash() const { return mDirtyHash; }
  219. private:
  220. Vector3 mPosition;
  221. Quaternion mRotation;
  222. Vector3 mScale;
  223. mutable Vector3 mWorldPosition;
  224. mutable Quaternion mWorldRotation;
  225. mutable Vector3 mWorldScale;
  226. mutable Matrix4 mCachedLocalTfrm;
  227. mutable Matrix4 mCachedWorldTfrm;
  228. mutable UINT32 mDirtyFlags;
  229. mutable UINT32 mIsCoreDirtyFlags;
  230. mutable UINT32 mDirtyHash;
  231. /**
  232. * @brief Marks the transform as dirty so that we know to update
  233. * it when the transform is requested.
  234. */
  235. void markTfrmDirty() const;
  236. /**
  237. * @brief Updates the local transform. Normally just reconstructs the
  238. * transform matrix from the position/rotation/scale.
  239. */
  240. void updateLocalTfrm() const;
  241. /**
  242. * @brief Updates the world transform. Reconstructs the local transform
  243. * matrix and multiplies it with any parent transforms.
  244. *
  245. * @note If parent transforms are dirty they will be updated.
  246. */
  247. void updateWorldTfrm() const;
  248. /**
  249. * @brief Checks if cached local transform needs updating.
  250. */
  251. bool isCachedLocalTfrmUpToDate() const { return (mDirtyFlags & DirtyFlags::LocalTfrmDirty) == 0; }
  252. /**
  253. * @brief Checks if cached world transform needs updating.
  254. */
  255. bool isCachedWorldTfrmUpToDate() const { return (mDirtyFlags & DirtyFlags::WorldTfrmDirty) == 0; }
  256. /************************************************************************/
  257. /* Hierarchy */
  258. /************************************************************************/
  259. public:
  260. /**
  261. * @brief Changes the parent of this object. Also removes the object from the current parent,
  262. * and assigns it to the new parent.
  263. *
  264. * @param [in] parent New parent.
  265. */
  266. void setParent(const HSceneObject& parent);
  267. /**
  268. * @brief Gets the parent of this object.
  269. *
  270. * @return Parent object, or nullptr if this SceneObject is at root level.
  271. */
  272. HSceneObject getParent() const { return mParent; }
  273. /**
  274. * @brief Gets a child of this item.
  275. *
  276. * @param idx The zero based index of the child.
  277. *
  278. * @return SceneObject of the child.
  279. */
  280. HSceneObject getChild(UINT32 idx) const;
  281. /**
  282. * @brief Find the index of the specified child. Don't persist this value as
  283. * it may change whenever you add/remove children.
  284. *
  285. * @param child The child to look for.
  286. *
  287. * @return The zero-based index of the found child, or -1 if no match was found.
  288. */
  289. int indexOfChild(const HSceneObject& child) const;
  290. /**
  291. * @brief Gets the number of all child GameObjects.
  292. */
  293. UINT32 getNumChildren() const { return (UINT32)mChildren.size(); }
  294. /**
  295. * @brief Enables or disables this object. Disabled objects also implicitly disable
  296. * all their child objects. No components on the disabled object are updated.
  297. */
  298. void setActive(bool active);
  299. /**
  300. * @brief Returns whether or not an object is active.
  301. *
  302. * @param self If true, the method will only check if this particular object was activated
  303. * or deactivated directly via setActive. If false we we also check if any of
  304. * the objects parents are inactive.
  305. */
  306. bool getActive(bool self = false);
  307. /**
  308. * @brief Makes a deep copy of this object.
  309. */
  310. HSceneObject clone();
  311. private:
  312. HSceneObject mParent;
  313. Vector<HSceneObject> mChildren;
  314. bool mActiveSelf;
  315. bool mActiveHierarchy;
  316. /**
  317. * @brief Adds a child to the child array. This method doesn't check for null or duplicate values.
  318. *
  319. * @param [in] object New child.
  320. */
  321. void addChild(const HSceneObject& object);
  322. /**
  323. * @brief Removes the child from the object.
  324. *
  325. * @param [in] object Child to remove.
  326. *
  327. * @throws INTERNAL_ERROR If the provided child isn't a child of the current object.
  328. */
  329. void removeChild(const HSceneObject& object);
  330. /**
  331. * @brief Changes the object active in hierarchy state.
  332. */
  333. void setActiveHierarchy(bool active);
  334. /************************************************************************/
  335. /* Component */
  336. /************************************************************************/
  337. public:
  338. /**
  339. * @brief Constructs a new component of the specified type and adds it to
  340. * the internal component list.
  341. */
  342. template<class T, class... Args>
  343. GameObjectHandle<T> addComponent(Args &&... args)
  344. {
  345. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  346. "Specified type is not a valid Component.");
  347. std::shared_ptr<T> gameObject(new (bs_alloc<T, PoolAlloc>()) T(mThisHandle,
  348. std::forward<Args>(args)...),
  349. &bs_delete<PoolAlloc, T>, StdAlloc<T, PoolAlloc>());
  350. GameObjectHandle<T> newComponent =
  351. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  352. mComponents.push_back(newComponent);
  353. gCoreSceneManager().notifyComponentAdded(newComponent);
  354. return newComponent;
  355. }
  356. /**
  357. * @brief Searches for a component with the specific type and returns the first one
  358. * it finds.
  359. *
  360. * @note Don't call this too often as it is relatively slow. It is more efficient
  361. * to call it once and store the result for further use.
  362. *
  363. * @tparam typename T Type of the component.
  364. *
  365. * @return Component if found, nullptr otherwise.
  366. */
  367. template <typename T>
  368. GameObjectHandle<T> getComponent()
  369. {
  370. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  371. "Specified type is not a valid Component.");
  372. return static_object_cast<T>(getComponent(T::getRTTIStatic()->getRTTIId()));
  373. }
  374. /**
  375. * @brief Checks if the current object contains the specified component
  376. *
  377. * @note Don't call this too often as it is relatively slow.
  378. *
  379. * @tparam typename T Type of the component.
  380. *
  381. * @return True if component exists on the object.
  382. */
  383. template <typename T>
  384. bool hasComponent()
  385. {
  386. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  387. "Specified type is not a valid Component.");
  388. for (auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  389. {
  390. if ((*iter)->getRTTI()->getRTTIId() == T::getRTTIStatic()->getRTTIId())
  391. return true;
  392. }
  393. return false;
  394. }
  395. /**
  396. * @brief Searches for a component with the specified type id and returns the first one it
  397. * finds.
  398. *
  399. * @note Don't call this too often as it is relatively slow. It is more efficient to
  400. * call it once and store the result for further use.
  401. *
  402. * @param typeId Identifier for the type.
  403. *
  404. * @return Component if found, nullptr otherwise.
  405. */
  406. HComponent getComponent(UINT32 typeId) const;
  407. /**
  408. * @brief Removes the component from this object, and deallocates it.
  409. *
  410. * @param [in] component The component to destroy.
  411. * @param [in] immediate If true, the component will be deallocated and become unusable
  412. * right away. Otherwise the deallocation will be delayed to the end of
  413. * frame (preferred method).
  414. */
  415. void destroyComponent(const HComponent& component, bool immediate = false);
  416. /**
  417. * @brief Removes the component from this object, and deallocates it.
  418. *
  419. * @param [in] component The component to destroy.
  420. * @param [in] immediate If true, the component will be deallocated and become unusable
  421. * right away. Otherwise the deallocation will be delayed to the end of
  422. * frame (preferred method).
  423. */
  424. void destroyComponent(Component* component, bool immediate = false);
  425. /**
  426. * @brief Returns all components on this object.
  427. */
  428. const Vector<HComponent>& getComponents() const { return mComponents; }
  429. private:
  430. /**
  431. * @brief Creates an empty component with the default constructor.
  432. */
  433. template <typename T>
  434. static std::shared_ptr<T> createEmptyComponent()
  435. {
  436. static_assert((std::is_base_of<BansheeEngine::Component, T>::value), "Specified type is not a valid Component.");
  437. std::shared_ptr<T> gameObject(new (bs_alloc<T, PoolAlloc>()) T(), &bs_delete<PoolAlloc, T>, StdAlloc<T, PoolAlloc>());
  438. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  439. return gameObject;
  440. }
  441. /**
  442. * @brief Adds the component to the internal component array.
  443. */
  444. void addComponentInternal(const std::shared_ptr<Component> component);
  445. Vector<HComponent> mComponents;
  446. /************************************************************************/
  447. /* RTTI */
  448. /************************************************************************/
  449. public:
  450. friend class GameObjectRTTI;
  451. friend class SceneObjectRTTI;
  452. static RTTITypeBase* getRTTIStatic();
  453. virtual RTTITypeBase* getRTTI() const;
  454. };
  455. }