BsSceneObject.h 13 KB

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