BsSceneObject.h 14 KB

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