BsSceneObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #include "BsSceneObject.h"
  2. #include "BsComponent.h"
  3. #include "BsCoreSceneManager.h"
  4. #include "BsException.h"
  5. #include "BsDebug.h"
  6. #include "BsSceneObjectRTTI.h"
  7. #include "BsMemorySerializer.h"
  8. #include "BsGameObjectManager.h"
  9. namespace BansheeEngine
  10. {
  11. SceneObject::SceneObject(const String& name)
  12. :GameObject(), mPosition(Vector3::ZERO), mRotation(Quaternion::IDENTITY), mScale(Vector3::ONE),
  13. mWorldPosition(Vector3::ZERO), mWorldRotation(Quaternion::IDENTITY), mWorldScale(Vector3::ONE),
  14. mCachedLocalTfrm(Matrix4::IDENTITY), mIsCachedLocalTfrmUpToDate(false),
  15. mCachedWorldTfrm(Matrix4::IDENTITY), mIsCachedWorldTfrmUpToDate(false), mIsCoreDirtyFlags(0xFFFFFFFF)
  16. {
  17. setName(name);
  18. }
  19. SceneObject::~SceneObject()
  20. {
  21. if(!mThisHandle.isDestroyed())
  22. {
  23. LOGWRN("Object is being deleted without being destroyed first?");
  24. destroyInternal();
  25. }
  26. }
  27. HSceneObject SceneObject::create(const String& name)
  28. {
  29. HSceneObject newObject = createInternal(name);
  30. gSceneManager().registerNewSO(newObject);
  31. return newObject;
  32. }
  33. HSceneObject SceneObject::createInternal(const String& name)
  34. {
  35. std::shared_ptr<SceneObject> sceneObjectPtr = std::shared_ptr<SceneObject>(new (bs_alloc<SceneObject, PoolAlloc>()) SceneObject(name),
  36. &bs_delete<PoolAlloc, SceneObject>, StdAlloc<PoolAlloc>());
  37. HSceneObject sceneObject = GameObjectManager::instance().registerObject(sceneObjectPtr);
  38. sceneObject->mThisHandle = sceneObject;
  39. return sceneObject;
  40. }
  41. void SceneObject::destroy()
  42. {
  43. // Parent is our owner, so when his reference to us is removed, delete might be called.
  44. // So make sure this is the last thing we do.
  45. if(mParent != nullptr)
  46. {
  47. if(!mParent.isDestroyed())
  48. mParent->removeChild(mThisHandle);
  49. }
  50. destroyInternal();
  51. }
  52. void SceneObject::destroyInternal()
  53. {
  54. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  55. (*iter)->destroyInternal();
  56. mChildren.clear();
  57. for(auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  58. {
  59. gSceneManager().notifyComponentRemoved((*iter));
  60. GameObjectManager::instance().unregisterObject(*iter);
  61. (*iter).destroy();
  62. }
  63. mComponents.clear();
  64. GameObjectManager::instance().unregisterObject(mThisHandle);
  65. mThisHandle.destroy();
  66. }
  67. /************************************************************************/
  68. /* Transform */
  69. /************************************************************************/
  70. void SceneObject::setPosition(const Vector3& position)
  71. {
  72. mPosition = position;
  73. markTfrmDirty();
  74. }
  75. void SceneObject::setRotation(const Quaternion& rotation)
  76. {
  77. mRotation = rotation;
  78. markTfrmDirty();
  79. }
  80. void SceneObject::setScale(const Vector3& scale)
  81. {
  82. mScale = scale;
  83. markTfrmDirty();
  84. }
  85. void SceneObject::setWorldPosition(const Vector3& position)
  86. {
  87. if (mParent != nullptr)
  88. {
  89. Vector3 invScale = mParent->getWorldScale();
  90. if (invScale.x != 0) invScale.x = 1.0f / invScale.x;
  91. if (invScale.y != 0) invScale.y = 1.0f / invScale.y;
  92. if (invScale.z != 0) invScale.z = 1.0f / invScale.z;
  93. Quaternion invRotation = mParent->getWorldRotation().inverse();
  94. mPosition = invRotation.rotate(position - mParent->getWorldPosition()) * invScale;
  95. }
  96. else
  97. mPosition = position;
  98. markTfrmDirty();
  99. }
  100. void SceneObject::setWorldRotation(const Quaternion& rotation)
  101. {
  102. if (mParent != nullptr)
  103. {
  104. Quaternion invRotation = mParent->getWorldRotation().inverse();
  105. mRotation = invRotation * rotation;
  106. }
  107. else
  108. mRotation = rotation;
  109. markTfrmDirty();
  110. }
  111. const Vector3& SceneObject::getWorldPosition() const
  112. {
  113. if(!mIsCachedWorldTfrmUpToDate)
  114. updateWorldTfrm();
  115. return mWorldPosition;
  116. }
  117. const Quaternion& SceneObject::getWorldRotation() const
  118. {
  119. if(!mIsCachedWorldTfrmUpToDate)
  120. updateWorldTfrm();
  121. return mWorldRotation;
  122. }
  123. const Vector3& SceneObject::getWorldScale() const
  124. {
  125. if(!mIsCachedWorldTfrmUpToDate)
  126. updateWorldTfrm();
  127. return mWorldScale;
  128. }
  129. void SceneObject::lookAt(const Vector3& location, const Vector3& up)
  130. {
  131. Vector3 forward = location - mPosition;
  132. forward.normalize();
  133. setForward(forward);
  134. Quaternion upRot = Quaternion::getRotationFromTo(getUp(), up);
  135. setRotation(getRotation() * upRot);
  136. }
  137. const Matrix4& SceneObject::getWorldTfrm() const
  138. {
  139. if(!mIsCachedWorldTfrmUpToDate)
  140. updateWorldTfrm();
  141. return mCachedWorldTfrm;
  142. }
  143. const Matrix4& SceneObject::getLocalTfrm() const
  144. {
  145. if(!mIsCachedLocalTfrmUpToDate)
  146. updateLocalTfrm();
  147. return mCachedLocalTfrm;
  148. }
  149. void SceneObject::move(const Vector3& vec)
  150. {
  151. setPosition(mPosition + vec);
  152. }
  153. void SceneObject::moveRelative(const Vector3& vec)
  154. {
  155. // Transform the axes of the relative vector by camera's local axes
  156. Vector3 trans = mRotation.rotate(vec);
  157. setPosition(mPosition + trans);
  158. }
  159. void SceneObject::rotate(const Vector3& axis, const Radian& angle)
  160. {
  161. Quaternion q;
  162. q.fromAxisAngle(axis, angle);
  163. rotate(q);
  164. }
  165. void SceneObject::rotate(const Quaternion& q)
  166. {
  167. // Note the order of the mult, i.e. q comes after
  168. // Normalize the quat to avoid cumulative problems with precision
  169. Quaternion qnorm = q;
  170. qnorm.normalize();
  171. setRotation(qnorm * mRotation);
  172. }
  173. void SceneObject::roll(const Radian& angle)
  174. {
  175. // Rotate around local Z axis
  176. Vector3 zAxis = mRotation.rotate(Vector3::UNIT_Z);
  177. rotate(zAxis, angle);
  178. }
  179. void SceneObject::yaw(const Radian& angle)
  180. {
  181. Vector3 yAxis = mRotation.rotate(Vector3::UNIT_Y);
  182. rotate(yAxis, angle);
  183. }
  184. void SceneObject::pitch(const Radian& angle)
  185. {
  186. // Rotate around local X axis
  187. Vector3 xAxis = mRotation.rotate(Vector3::UNIT_X);
  188. rotate(xAxis, angle);
  189. }
  190. void SceneObject::setForward(const Vector3& forwardDir)
  191. {
  192. if (forwardDir == Vector3::ZERO)
  193. return;
  194. Vector3 nrmForwardDir = Vector3::normalize(forwardDir);
  195. Vector3 currentForwardDir = getForward();
  196. const Quaternion& currentRotation = getWorldRotation();
  197. Quaternion targetRotation;
  198. if ((nrmForwardDir+currentForwardDir).squaredLength() < 0.00005f)
  199. {
  200. // Oops, a 180 degree turn (infinite possible rotation axes)
  201. // Default to yaw i.e. use current UP
  202. targetRotation = Quaternion(-currentRotation.y, -currentRotation.z, currentRotation.w, currentRotation.x);
  203. }
  204. else
  205. {
  206. // Derive shortest arc to new direction
  207. Quaternion rotQuat = Quaternion::getRotationFromTo(currentForwardDir, nrmForwardDir);
  208. targetRotation = rotQuat * currentRotation;
  209. }
  210. setRotation(targetRotation);
  211. }
  212. void SceneObject::updateTransformsIfDirty()
  213. {
  214. if (!mIsCachedLocalTfrmUpToDate)
  215. updateLocalTfrm();
  216. if (!mIsCachedWorldTfrmUpToDate)
  217. updateWorldTfrm();
  218. }
  219. void SceneObject::markTfrmDirty() const
  220. {
  221. mIsCachedLocalTfrmUpToDate = false;
  222. mIsCachedWorldTfrmUpToDate = false;
  223. mIsCoreDirtyFlags = 0xFFFFFFFF;
  224. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  225. {
  226. (*iter)->markTfrmDirty();
  227. }
  228. }
  229. void SceneObject::updateWorldTfrm() const
  230. {
  231. if(mParent != nullptr)
  232. {
  233. mCachedWorldTfrm = getLocalTfrm() * mParent->getWorldTfrm();
  234. // Update orientation
  235. const Quaternion& parentOrientation = mParent->getWorldRotation();
  236. mWorldRotation = parentOrientation * mRotation;
  237. // Update scale
  238. const Vector3& parentScale = mParent->getWorldScale();
  239. // Scale own position by parent scale, just combine
  240. // as equivalent axes, no shearing
  241. mWorldScale = parentScale * mScale;
  242. // Change position vector based on parent's orientation & scale
  243. mWorldPosition = parentOrientation.rotate(parentScale * mPosition);
  244. // Add altered position vector to parents
  245. mWorldPosition += mParent->getWorldPosition();
  246. }
  247. else
  248. {
  249. mCachedWorldTfrm = getLocalTfrm();
  250. mWorldRotation = mRotation;
  251. mWorldPosition = mPosition;
  252. mWorldScale = mScale;
  253. }
  254. mIsCachedWorldTfrmUpToDate = true;
  255. }
  256. void SceneObject::updateLocalTfrm() const
  257. {
  258. mCachedLocalTfrm.setTRS(mPosition, mRotation, mScale);
  259. mIsCachedLocalTfrmUpToDate = true;
  260. }
  261. /************************************************************************/
  262. /* Hierarchy */
  263. /************************************************************************/
  264. void SceneObject::setParent(const HSceneObject& parent)
  265. {
  266. if(parent.isDestroyed())
  267. {
  268. BS_EXCEPT(InternalErrorException,
  269. "Trying to assign a SceneObject parent that is destroyed.");
  270. }
  271. if(mParent == nullptr || mParent != parent)
  272. {
  273. if(mParent != nullptr)
  274. mParent->removeChild(mThisHandle);
  275. if(parent != nullptr)
  276. parent->addChild(mThisHandle);
  277. mParent = parent;
  278. markTfrmDirty();
  279. }
  280. }
  281. HSceneObject SceneObject::getChild(UINT32 idx) const
  282. {
  283. if(idx < 0 || idx >= mChildren.size())
  284. {
  285. BS_EXCEPT(InternalErrorException, "Child index out of range.");
  286. }
  287. return mChildren[idx];
  288. }
  289. int SceneObject::indexOfChild(const HSceneObject& child) const
  290. {
  291. for(int i = 0; i < (int)mChildren.size(); i++)
  292. {
  293. if(mChildren[i] == child)
  294. return i;
  295. }
  296. return -1;
  297. }
  298. void SceneObject::addChild(const HSceneObject& object)
  299. {
  300. mChildren.push_back(object);
  301. }
  302. void SceneObject::removeChild(const HSceneObject& object)
  303. {
  304. auto result = find(mChildren.begin(), mChildren.end(), object);
  305. if(result != mChildren.end())
  306. mChildren.erase(result);
  307. else
  308. {
  309. BS_EXCEPT(InternalErrorException,
  310. "Trying to remove a child but it's not a child of the transform.");
  311. }
  312. }
  313. HSceneObject SceneObject::clone()
  314. {
  315. UINT32 bufferSize = 0;
  316. MemorySerializer serializer;
  317. UINT8* buffer = serializer.encode(this, bufferSize, &bs_alloc);
  318. GameObjectManager::instance().startDeserialization();
  319. std::shared_ptr<SceneObject> cloneObj = std::static_pointer_cast<SceneObject>(serializer.decode(buffer, bufferSize));
  320. bs_free(buffer);
  321. GameObjectManager::instance().endDeserialization();
  322. return cloneObj->mThisHandle;
  323. }
  324. HComponent SceneObject::getComponent(UINT32 typeId) const
  325. {
  326. for(auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  327. {
  328. if((*iter)->getRTTI()->getRTTIId() == typeId)
  329. return *iter;
  330. }
  331. return HComponent();
  332. }
  333. void SceneObject::destroyComponent(const HComponent& component)
  334. {
  335. if(component == nullptr)
  336. {
  337. LOGDBG("Trying to remove a null component");
  338. return;
  339. }
  340. auto iter = std::find(mComponents.begin(), mComponents.end(), component);
  341. if(iter != mComponents.end())
  342. {
  343. gSceneManager().notifyComponentRemoved((*iter));
  344. GameObjectManager::instance().unregisterObject(component);
  345. (*iter)->onDestroyed();
  346. (*iter).destroy();
  347. mComponents.erase(iter);
  348. }
  349. else
  350. LOGDBG("Trying to remove a component that doesn't exist on this SceneObject.");
  351. }
  352. void SceneObject::destroyComponent(Component* component)
  353. {
  354. auto iterFind = std::find_if(mComponents.begin(), mComponents.end(),
  355. [component](const HComponent& x)
  356. {
  357. if(x.isDestroyed())
  358. return false;
  359. return x._getHandleData()->mPtr->object.get() == component; }
  360. );
  361. if(iterFind != mComponents.end())
  362. {
  363. destroyComponent(*iterFind);
  364. }
  365. }
  366. void SceneObject::addComponentInternal(const std::shared_ptr<Component> component)
  367. {
  368. GameObjectHandle<Component> newComponent = GameObjectHandle<Component>(component);
  369. newComponent->mParent = mThisHandle;
  370. mComponents.push_back(newComponent);
  371. gSceneManager().notifyComponentAdded(newComponent);
  372. }
  373. RTTITypeBase* SceneObject::getRTTIStatic()
  374. {
  375. return SceneObjectRTTI::instance();
  376. }
  377. RTTITypeBase* SceneObject::getRTTI() const
  378. {
  379. return SceneObject::getRTTIStatic();
  380. }
  381. }