BsSceneObject.h 12 KB

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