BsSceneObject.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 Called by the renderer to check if the transform changed in any way.
  168. */
  169. bool _isRenderDataUpToDate() const { return mIsRenderDataUpToDate; }
  170. /**
  171. * @brief Called by the renderer to notify the scene object that render data has been updated.
  172. */
  173. void _markRenderDataUpToDate() { mIsRenderDataUpToDate = true; }
  174. private:
  175. Vector3 mPosition;
  176. Quaternion mRotation;
  177. Vector3 mScale;
  178. mutable Vector3 mWorldPosition;
  179. mutable Quaternion mWorldRotation;
  180. mutable Vector3 mWorldScale;
  181. mutable Matrix4 mCachedLocalTfrm;
  182. mutable bool mIsCachedLocalTfrmUpToDate;
  183. mutable Matrix4 mCachedWorldTfrm;
  184. mutable bool mIsCachedWorldTfrmUpToDate;
  185. mutable bool mIsRenderDataUpToDate;
  186. /**
  187. * @brief Marks the transform as dirty so that we know to update
  188. * it when the transform is requested.
  189. */
  190. void markTfrmDirty() const;
  191. /**
  192. * @brief Updates the local transform. Normally just reconstructs the
  193. * transform matrix from the position/rotation/scale.
  194. */
  195. void updateLocalTfrm() const;
  196. /**
  197. * @brief Updates the world transform. Reconstructs the local transform
  198. * matrix and multiplies it with any parent transforms.
  199. *
  200. * @note If parent transforms are dirty they will be updated.
  201. */
  202. void updateWorldTfrm() const;
  203. /************************************************************************/
  204. /* Hierarchy */
  205. /************************************************************************/
  206. public:
  207. /**
  208. * @brief Changes the parent of this object. Also removes the object from the current parent,
  209. * and assigns it to the new parent.
  210. *
  211. * @param [in] parent New parent.
  212. */
  213. void setParent(const HSceneObject& parent);
  214. /**
  215. * @brief Gets the parent of this object.
  216. *
  217. * @return Parent object, or nullptr if this SceneObject is at root level.
  218. */
  219. HSceneObject getParent() const { return mParent; }
  220. /**
  221. * @brief Gets a child of this item.
  222. *
  223. * @param idx The zero based index of the child.
  224. *
  225. * @return SceneObject of the child.
  226. */
  227. HSceneObject getChild(UINT32 idx) const;
  228. /**
  229. * @brief Find the index of the specified child. Don't persist this value as
  230. * it may change whenever you add/remove children.
  231. *
  232. * @param child The child to look for.
  233. *
  234. * @return The zero-based index of the found child, or -1 if no match was found.
  235. */
  236. int indexOfChild(const HSceneObject& child) const;
  237. /**
  238. * @brief Gets the number of all child GameObjects.
  239. */
  240. UINT32 getNumChildren() const { return (UINT32)mChildren.size(); }
  241. /**
  242. * @brief Makes a deep copy of this object.
  243. */
  244. HSceneObject clone();
  245. private:
  246. HSceneObject mParent;
  247. Vector<HSceneObject> mChildren;
  248. /**
  249. * @brief Adds a child to the child array. This method doesn't check for null or duplicate values.
  250. *
  251. * @param [in] object New child.
  252. */
  253. void addChild(const HSceneObject& object);
  254. /**
  255. * @brief Removes the child from the object.
  256. *
  257. * @param [in] object Child to remove.
  258. *
  259. * @throws INTERNAL_ERROR If the provided child isn't a child of the current object.
  260. */
  261. void removeChild(const HSceneObject& object);
  262. /************************************************************************/
  263. /* Component */
  264. /************************************************************************/
  265. public:
  266. /**
  267. * @brief Constructs a new component of the specified type and adds it to
  268. * the internal component list.
  269. */
  270. template<class T, class... Args>
  271. GameObjectHandle<T> addComponent(Args &&... args)
  272. {
  273. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  274. "Specified type is not a valid Component.");
  275. std::shared_ptr<T> gameObject(new (bs_alloc<T, PoolAlloc>()) T(mThisHandle,
  276. std::forward<Args>(args)...),
  277. &bs_delete<PoolAlloc, T>, StdAlloc<PoolAlloc>());
  278. GameObjectHandle<T> newComponent =
  279. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  280. mComponents.push_back(newComponent);
  281. gSceneManager().notifyComponentAdded(newComponent);
  282. return newComponent;
  283. }
  284. /**
  285. * @brief Searches for a component with the specific type and returns the first one
  286. * it finds.
  287. *
  288. * @note Don't call this too often as it is relatively slow. It is more efficient
  289. * to call it once and store the result for further use.
  290. *
  291. * @tparam typename T Type of the component.
  292. *
  293. * @return Component if found, nullptr otherwise.
  294. */
  295. template <typename T>
  296. GameObjectHandle<T> getComponent()
  297. {
  298. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  299. "Specified type is not a valid Component.");
  300. return static_object_cast<T>(getComponent(T::getRTTIStatic()->getRTTIId()));
  301. }
  302. /**
  303. * @brief Checks if the current object contains the specified component
  304. *
  305. * @note Don't call this too often as it is relatively slow.
  306. *
  307. * @tparam typename T Type of the component.
  308. *
  309. * @return True if component exists on the object.
  310. */
  311. template <typename T>
  312. bool hasComponent()
  313. {
  314. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  315. "Specified type is not a valid Component.");
  316. return hasComponent(T::getRTTIStatic()->getRTTIId());
  317. }
  318. /**
  319. * @brief Searches for a component with the specified type id and returns the first one it
  320. * finds.
  321. *
  322. * @note Don't call this too often as it is relatively slow. It is more efficient to
  323. * call it once and store the result for further use.
  324. *
  325. * @param typeId Identifier for the type.
  326. *
  327. * @return Component if found, nullptr otherwise.
  328. */
  329. HComponent getComponent(UINT32 typeId) const;
  330. /**
  331. * @brief Removes the component from this object, and deallocates it.
  332. *
  333. * @param [in] component The component to destroy.
  334. */
  335. void destroyComponent(const HComponent& component);
  336. /**
  337. * @brief Removes the component from this object, and deallocates it.
  338. *
  339. * @param [in] component The component to destroy.
  340. */
  341. void destroyComponent(Component* component);
  342. /**
  343. * @brief Returns all components on this object.
  344. */
  345. const Vector<HComponent>& getComponents() const { return mComponents; }
  346. private:
  347. /**
  348. * @brief Creates an empty component with the default constructor.
  349. */
  350. template <typename T>
  351. static std::shared_ptr<T> createEmptyComponent()
  352. {
  353. static_assert((std::is_base_of<BansheeEngine::Component, T>::value), "Specified type is not a valid Component.");
  354. std::shared_ptr<T> gameObject(new (bs_alloc<T, PoolAlloc>()) T(), &bs_delete<PoolAlloc, T>, StdAlloc<PoolAlloc>());
  355. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  356. return gameObject;
  357. }
  358. /**
  359. * @brief Adds the component to the internal component array.
  360. */
  361. void addComponentInternal(const std::shared_ptr<Component> component);
  362. Vector<HComponent> mComponents;
  363. /************************************************************************/
  364. /* RTTI */
  365. /************************************************************************/
  366. public:
  367. friend class GameObjectRTTI;
  368. friend class SceneObjectRTTI;
  369. static RTTITypeBase* getRTTIStatic();
  370. virtual RTTITypeBase* getRTTI() const;
  371. };
  372. }