BsSceneObject.h 13 KB

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