BsSceneManager.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Scene/BsSceneManager.h"
  4. #include "Scene/BsSceneObject.h"
  5. #include "Scene/BsComponent.h"
  6. #include "Renderer/BsRenderable.h"
  7. #include "Renderer/BsCamera.h"
  8. #include "Renderer/BsLight.h"
  9. #include "RenderAPI/BsViewport.h"
  10. #include "Scene/BsGameObjectManager.h"
  11. #include "RenderAPI/BsRenderTarget.h"
  12. #include "Renderer/BsLightProbeVolume.h"
  13. #include "Scene/BsSceneActor.h"
  14. namespace bs
  15. {
  16. enum ListType
  17. {
  18. ActiveList = 0,
  19. InactiveList = 1,
  20. UninitializedList = 2
  21. };
  22. SceneManager::SceneManager()
  23. {
  24. mRootNode = SceneObject::createInternal("SceneRoot");
  25. }
  26. SceneManager::~SceneManager()
  27. {
  28. if (mRootNode != nullptr && !mRootNode.isDestroyed())
  29. mRootNode->destroy(true);
  30. }
  31. void SceneManager::clearScene(bool forceAll)
  32. {
  33. UINT32 numChildren = mRootNode->getNumChildren();
  34. UINT32 curIdx = 0;
  35. for (UINT32 i = 0; i < numChildren; i++)
  36. {
  37. HSceneObject child = mRootNode->getChild(curIdx);
  38. if (forceAll || !child->hasFlag(SOF_Persistent))
  39. child->destroy();
  40. else
  41. curIdx++;
  42. }
  43. GameObjectManager::instance().destroyQueuedObjects();
  44. HSceneObject newRoot = SceneObject::createInternal("SceneRoot");
  45. _setRootNode(newRoot);
  46. }
  47. void SceneManager::_setRootNode(const HSceneObject& root)
  48. {
  49. if (root == nullptr)
  50. return;
  51. HSceneObject oldRoot = mRootNode;
  52. UINT32 numChildren = oldRoot->getNumChildren();
  53. // Make sure to keep persistent objects
  54. bs_frame_mark();
  55. {
  56. FrameVector<HSceneObject> toRemove;
  57. for (UINT32 i = 0; i < numChildren; i++)
  58. {
  59. HSceneObject child = oldRoot->getChild(i);
  60. if (child->hasFlag(SOF_Persistent))
  61. toRemove.push_back(child);
  62. }
  63. for (auto& entry : toRemove)
  64. entry->setParent(root, false);
  65. }
  66. bs_frame_clear();
  67. mRootNode = root;
  68. mRootNode->_setParent(HSceneObject());
  69. oldRoot->destroy();
  70. }
  71. void SceneManager::_bindActor(const SPtr<SceneActor>& actor, const HSceneObject& so)
  72. {
  73. mBoundActors[actor.get()] = BoundActorData(actor, so);
  74. }
  75. void SceneManager::_unbindActor(const SPtr<SceneActor>& actor)
  76. {
  77. mBoundActors.erase(actor.get());
  78. }
  79. HSceneObject SceneManager::_getActorSO(const SPtr<SceneActor>& actor) const
  80. {
  81. auto iterFind = mBoundActors.find(actor.get());
  82. if (iterFind != mBoundActors.end())
  83. return iterFind->second.so;
  84. return HSceneObject();
  85. }
  86. void SceneManager::_registerCamera(const SPtr<Camera>& camera)
  87. {
  88. mCameras[camera.get()] = camera;
  89. }
  90. void SceneManager::_unregisterCamera(const SPtr<Camera>& camera)
  91. {
  92. mCameras.erase(camera.get());
  93. auto iterFind = std::find_if(mMainCameras.begin(), mMainCameras.end(),
  94. [&](const SPtr<Camera>& x)
  95. {
  96. return x == camera;
  97. });
  98. if (iterFind != mMainCameras.end())
  99. mMainCameras.erase(iterFind);
  100. }
  101. void SceneManager::_notifyMainCameraStateChanged(const SPtr<Camera>& camera)
  102. {
  103. auto iterFind = std::find_if(mMainCameras.begin(), mMainCameras.end(),
  104. [&](const SPtr<Camera>& entry)
  105. {
  106. return entry == camera;
  107. });
  108. SPtr<Viewport> viewport = camera->getViewport();
  109. if (camera->isMain())
  110. {
  111. if (iterFind == mMainCameras.end())
  112. mMainCameras.push_back(mCameras[camera.get()]);
  113. viewport->setTarget(mMainRT);
  114. }
  115. else
  116. {
  117. if (iterFind != mMainCameras.end())
  118. mMainCameras.erase(iterFind);
  119. if (viewport->getTarget() == mMainRT)
  120. viewport->setTarget(nullptr);
  121. }
  122. }
  123. void SceneManager::_updateCoreObjectTransforms()
  124. {
  125. for (auto& entry : mBoundActors)
  126. entry.second.actor->_updateState(*entry.second.so);
  127. }
  128. SPtr<Camera> SceneManager::getMainCamera() const
  129. {
  130. if (mMainCameras.size() > 0)
  131. return mMainCameras[0];
  132. return nullptr;
  133. }
  134. void SceneManager::setMainRenderTarget(const SPtr<RenderTarget>& rt)
  135. {
  136. if (mMainRT == rt)
  137. return;
  138. mMainRTResizedConn.disconnect();
  139. if (rt != nullptr)
  140. mMainRTResizedConn = rt->onResized.connect(std::bind(&SceneManager::onMainRenderTargetResized, this));
  141. mMainRT = rt;
  142. float aspect = 1.0f;
  143. if (rt != nullptr)
  144. {
  145. auto& rtProps = rt->getProperties();
  146. aspect = rtProps.width / (float)rtProps.height;
  147. }
  148. for (auto& entry : mMainCameras)
  149. {
  150. entry->getViewport()->setTarget(rt);
  151. entry->setAspectRatio(aspect);
  152. }
  153. }
  154. void SceneManager::setComponentState(ComponentState state)
  155. {
  156. if (mComponentState == state)
  157. return;
  158. // Wake up all components with onInitialize/onEnable events if moving to running or paused state
  159. if(state == ComponentState::Running || state == ComponentState::Paused)
  160. {
  161. if(mComponentState == ComponentState::Stopped)
  162. {
  163. // Trigger enable on all components that don't have AlwaysRun flag (at this point those will be all
  164. // inactive components that have active scene object parents)
  165. for(auto& entry : mInactiveComponents)
  166. {
  167. if (entry->sceneObject()->getActive())
  168. entry->onEnabled();
  169. }
  170. // Initialize and enable uninitialized components
  171. for(auto& entry : mUninitializedComponents)
  172. {
  173. entry->onInitialized();
  174. if (entry->sceneObject()->getActive())
  175. {
  176. entry->onEnabled();
  177. UINT32 idx = (UINT32)mActiveComponents.size();
  178. mActiveComponents.push_back(entry);
  179. entry->setSceneManagerId(encodeComponentId(idx, ActiveList));
  180. }
  181. else
  182. {
  183. UINT32 idx = (UINT32)mInactiveComponents.size();
  184. mInactiveComponents.push_back(entry);
  185. entry->setSceneManagerId(encodeComponentId(idx, InactiveList));
  186. }
  187. }
  188. mUninitializedComponents.clear();
  189. }
  190. }
  191. // Start updates on all active components
  192. if (state == ComponentState::Running)
  193. {
  194. // Move from inactive to active list
  195. for(INT32 i = 0; i < (INT32)mInactiveComponents.size(); i++)
  196. {
  197. HComponent component = mInactiveComponents[i];
  198. if (!component->sceneObject()->getActive())
  199. continue;
  200. removeFromInactiveList(component);
  201. i--; // Keep the same index next iteration to process the component we just swapped
  202. UINT32 activeIdx = (UINT32)mActiveComponents.size();
  203. mActiveComponents.push_back(component);
  204. component->setSceneManagerId(encodeComponentId(activeIdx, ActiveList));
  205. }
  206. }
  207. // Stop updates on all active components
  208. else if(state == ComponentState::Paused || state == ComponentState::Stopped)
  209. {
  210. // Trigger onDisable events if stopping
  211. if (state == ComponentState::Stopped)
  212. {
  213. for (INT32 i = 0; i < (INT32)mActiveComponents.size(); i++)
  214. {
  215. HComponent component = mActiveComponents[i];
  216. bool alwaysRun = component->hasFlag(ComponentFlag::AlwaysRun);
  217. if (alwaysRun)
  218. continue;
  219. component->onDisabled();
  220. }
  221. }
  222. // Move from active to inactive list
  223. for (INT32 i = 0; i < (INT32)mActiveComponents.size(); i++)
  224. {
  225. HComponent component = mActiveComponents[i];
  226. bool alwaysRun = component->hasFlag(ComponentFlag::AlwaysRun);
  227. if (alwaysRun)
  228. continue;
  229. removeFromActiveList(component);
  230. i--; // Keep the same index next iteration to process the component we just swapped
  231. UINT32 inactiveIdx = (UINT32)mInactiveComponents.size();
  232. mInactiveComponents.push_back(component);
  233. component->setSceneManagerId(encodeComponentId(inactiveIdx, InactiveList));
  234. }
  235. }
  236. mComponentState = state;
  237. }
  238. void SceneManager::_notifyComponentCreated(const HComponent& component, bool parentActive)
  239. {
  240. component->onCreated();
  241. bool alwaysRun = component->hasFlag(ComponentFlag::AlwaysRun);
  242. if(alwaysRun || mComponentState != ComponentState::Stopped)
  243. {
  244. component->onInitialized();
  245. if (parentActive)
  246. {
  247. component->onEnabled();
  248. UINT32 idx = (UINT32)mActiveComponents.size();
  249. mActiveComponents.push_back(component);
  250. component->setSceneManagerId(encodeComponentId(idx, ActiveList));
  251. }
  252. else
  253. {
  254. UINT32 idx = (UINT32)mInactiveComponents.size();
  255. mInactiveComponents.push_back(component);
  256. component->setSceneManagerId(encodeComponentId(idx, InactiveList));
  257. }
  258. }
  259. else // Stopped
  260. {
  261. UINT32 idx = (UINT32)mUninitializedComponents.size();
  262. mUninitializedComponents.push_back(component);
  263. component->setSceneManagerId(encodeComponentId(idx, UninitializedList));
  264. }
  265. }
  266. void SceneManager::_notifyComponentActivated(const HComponent& component, bool triggerEvent)
  267. {
  268. bool alwaysRun = component->hasFlag(ComponentFlag::AlwaysRun);
  269. if (alwaysRun || mComponentState == ComponentState::Running || mComponentState == ComponentState::Paused)
  270. {
  271. if (triggerEvent)
  272. component->onEnabled();
  273. removeFromInactiveList(component);
  274. UINT32 activeIdx = (UINT32)mActiveComponents.size();
  275. mActiveComponents.push_back(component);
  276. component->setSceneManagerId(encodeComponentId(activeIdx, ActiveList));
  277. }
  278. }
  279. void SceneManager::_notifyComponentDeactivated(const HComponent& component, bool triggerEvent)
  280. {
  281. bool alwaysRun = component->hasFlag(ComponentFlag::AlwaysRun);
  282. if (alwaysRun || mComponentState == ComponentState::Running || mComponentState == ComponentState::Paused)
  283. {
  284. if (triggerEvent)
  285. component->onDisabled();
  286. removeFromActiveList(component);
  287. UINT32 inactiveIdx = (UINT32)mInactiveComponents.size();
  288. mInactiveComponents.push_back(component);
  289. component->setSceneManagerId(encodeComponentId(inactiveIdx, InactiveList));
  290. }
  291. }
  292. void SceneManager::_notifyComponentDestroyed(const HComponent& component)
  293. {
  294. UINT32 listType;
  295. UINT32 idx;
  296. decodeComponentId(component->getSceneManagerId(), idx, listType);
  297. switch(listType)
  298. {
  299. case ActiveList:
  300. removeFromActiveList(component);
  301. break;
  302. case InactiveList:
  303. removeFromInactiveList(component);
  304. break;
  305. case UninitializedList:
  306. removeFromUninitializedList(component);
  307. break;
  308. default:
  309. assert(false);
  310. break;
  311. }
  312. bool alwaysRun = component->hasFlag(ComponentFlag::AlwaysRun);
  313. bool isEnabled = component->sceneObject()->getActive() && (alwaysRun || mComponentState != ComponentState::Stopped);
  314. if (isEnabled)
  315. component->onDisabled();
  316. component->onDestroyed();
  317. }
  318. void SceneManager::removeFromActiveList(const HComponent& component)
  319. {
  320. UINT32 listType;
  321. UINT32 idx;
  322. decodeComponentId(component->getSceneManagerId(), idx, listType);
  323. UINT32 lastIdx;
  324. decodeComponentId(mActiveComponents.back()->getSceneManagerId(), lastIdx, listType);
  325. assert(mActiveComponents[idx] == component);
  326. if (idx != lastIdx)
  327. {
  328. std::swap(mActiveComponents[idx], mActiveComponents[lastIdx]);
  329. mActiveComponents[idx]->setSceneManagerId(encodeComponentId(idx, ActiveList));
  330. }
  331. mActiveComponents.erase(mActiveComponents.end() - 1);
  332. }
  333. void SceneManager::removeFromInactiveList(const HComponent& component)
  334. {
  335. UINT32 listType;
  336. UINT32 idx;
  337. decodeComponentId(component->getSceneManagerId(), idx, listType);
  338. UINT32 lastIdx;
  339. decodeComponentId(mInactiveComponents.back()->getSceneManagerId(), lastIdx, listType);
  340. assert(mInactiveComponents[idx] == component);
  341. if (idx != lastIdx)
  342. {
  343. std::swap(mInactiveComponents[idx], mInactiveComponents[lastIdx]);
  344. mInactiveComponents[idx]->setSceneManagerId(encodeComponentId(idx, InactiveList));
  345. }
  346. mInactiveComponents.erase(mInactiveComponents.end() - 1);
  347. }
  348. void SceneManager::removeFromUninitializedList(const HComponent& component)
  349. {
  350. UINT32 listType;
  351. UINT32 idx;
  352. decodeComponentId(component->getSceneManagerId(), idx, listType);
  353. UINT32 lastIdx;
  354. decodeComponentId(mUninitializedComponents.back()->getSceneManagerId(), lastIdx, listType);
  355. assert(mUninitializedComponents[idx] == component);
  356. if (idx != lastIdx)
  357. {
  358. std::swap(mUninitializedComponents[idx], mUninitializedComponents[lastIdx]);
  359. mUninitializedComponents[idx]->setSceneManagerId(encodeComponentId(idx, UninitializedList));
  360. }
  361. mUninitializedComponents.erase(mUninitializedComponents.end() - 1);
  362. }
  363. UINT32 SceneManager::encodeComponentId(UINT32 idx, UINT32 type)
  364. {
  365. assert(idx <= (0x3FFFFFFF));
  366. return (type << 30) | idx;
  367. }
  368. void SceneManager::decodeComponentId(UINT32 id, UINT32& idx, UINT32& type)
  369. {
  370. idx = id & 0x3FFFFFFF;
  371. type = id >> 30;
  372. }
  373. bool SceneManager::isComponentOfType(const HComponent& component, UINT32 rttiId)
  374. {
  375. return component->getRTTI()->getRTTIId() == rttiId;
  376. }
  377. void SceneManager::_update()
  378. {
  379. // Note: Eventually perform updates based on component types and/or on component priority. Right now we just
  380. // iterate in an undefined order, but it wouldn't be hard to change it.
  381. for (auto& entry : mActiveComponents)
  382. entry->update();
  383. GameObjectManager::instance().destroyQueuedObjects();
  384. }
  385. void SceneManager::registerNewSO(const HSceneObject& node)
  386. {
  387. if(mRootNode)
  388. node->setParent(mRootNode);
  389. }
  390. void SceneManager::onMainRenderTargetResized()
  391. {
  392. auto& rtProps = mMainRT->getProperties();
  393. float aspect = rtProps.width / (float)rtProps.height;
  394. for (auto& entry : mMainCameras)
  395. entry->setAspectRatio(aspect);
  396. }
  397. SceneManager& gSceneManager()
  398. {
  399. return SceneManager::instance();
  400. }
  401. }