2
0

BsSceneManager.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. ComponentState oldState = mComponentState;
  159. // Make sure to change the state before calling any callbacks, so callbacks can query the state
  160. mComponentState = state;
  161. // Wake up all components with onInitialize/onEnable events if moving to running or paused state
  162. if(state == ComponentState::Running || state == ComponentState::Paused)
  163. {
  164. if(oldState == ComponentState::Stopped)
  165. {
  166. // Disable, and then re-enable components that have an AlwaysRun flag
  167. for(auto& entry : mActiveComponents)
  168. {
  169. if (entry->sceneObject()->getActive())
  170. {
  171. entry->onDisabled();
  172. entry->onEnabled();
  173. }
  174. }
  175. // Trigger enable on all components that don't have AlwaysRun flag (at this point those will be all
  176. // inactive components that have active scene object parents)
  177. for(auto& entry : mInactiveComponents)
  178. {
  179. if (entry->sceneObject()->getActive())
  180. entry->onEnabled();
  181. }
  182. // Initialize and enable uninitialized components
  183. for(auto& entry : mUninitializedComponents)
  184. {
  185. entry->onInitialized();
  186. if (entry->sceneObject()->getActive())
  187. {
  188. entry->onEnabled();
  189. UINT32 idx = (UINT32)mActiveComponents.size();
  190. mActiveComponents.push_back(entry);
  191. entry->setSceneManagerId(encodeComponentId(idx, ActiveList));
  192. }
  193. else
  194. {
  195. UINT32 idx = (UINT32)mInactiveComponents.size();
  196. mInactiveComponents.push_back(entry);
  197. entry->setSceneManagerId(encodeComponentId(idx, InactiveList));
  198. }
  199. }
  200. mUninitializedComponents.clear();
  201. }
  202. }
  203. // Start updates on all active components
  204. if (state == ComponentState::Running)
  205. {
  206. // Move from inactive to active list
  207. for(INT32 i = 0; i < (INT32)mInactiveComponents.size(); i++)
  208. {
  209. HComponent component = mInactiveComponents[i];
  210. if (!component->sceneObject()->getActive())
  211. continue;
  212. removeFromInactiveList(component);
  213. i--; // Keep the same index next iteration to process the component we just swapped
  214. UINT32 activeIdx = (UINT32)mActiveComponents.size();
  215. mActiveComponents.push_back(component);
  216. component->setSceneManagerId(encodeComponentId(activeIdx, ActiveList));
  217. }
  218. }
  219. // Stop updates on all active components
  220. else if(state == ComponentState::Paused || state == ComponentState::Stopped)
  221. {
  222. // Trigger onDisable events if stopping
  223. if (state == ComponentState::Stopped)
  224. {
  225. for (INT32 i = 0; i < (INT32)mActiveComponents.size(); i++)
  226. {
  227. HComponent component = mActiveComponents[i];
  228. bool alwaysRun = component->hasFlag(ComponentFlag::AlwaysRun);
  229. component->onDisabled();
  230. if(alwaysRun)
  231. component->onEnabled();
  232. }
  233. }
  234. // Move from active to inactive list
  235. for (INT32 i = 0; i < (INT32)mActiveComponents.size(); i++)
  236. {
  237. HComponent component = mActiveComponents[i];
  238. bool alwaysRun = component->hasFlag(ComponentFlag::AlwaysRun);
  239. if (alwaysRun)
  240. continue;
  241. removeFromActiveList(component);
  242. i--; // Keep the same index next iteration to process the component we just swapped
  243. UINT32 inactiveIdx = (UINT32)mInactiveComponents.size();
  244. mInactiveComponents.push_back(component);
  245. component->setSceneManagerId(encodeComponentId(inactiveIdx, InactiveList));
  246. }
  247. }
  248. }
  249. void SceneManager::_notifyComponentCreated(const HComponent& component, bool parentActive)
  250. {
  251. component->onCreated();
  252. bool alwaysRun = component->hasFlag(ComponentFlag::AlwaysRun);
  253. if(alwaysRun || mComponentState != ComponentState::Stopped)
  254. {
  255. component->onInitialized();
  256. if (parentActive)
  257. {
  258. component->onEnabled();
  259. UINT32 idx = (UINT32)mActiveComponents.size();
  260. mActiveComponents.push_back(component);
  261. component->setSceneManagerId(encodeComponentId(idx, ActiveList));
  262. }
  263. else
  264. {
  265. UINT32 idx = (UINT32)mInactiveComponents.size();
  266. mInactiveComponents.push_back(component);
  267. component->setSceneManagerId(encodeComponentId(idx, InactiveList));
  268. }
  269. }
  270. else // Stopped
  271. {
  272. UINT32 idx = (UINT32)mUninitializedComponents.size();
  273. mUninitializedComponents.push_back(component);
  274. component->setSceneManagerId(encodeComponentId(idx, UninitializedList));
  275. }
  276. }
  277. void SceneManager::_notifyComponentActivated(const HComponent& component, bool triggerEvent)
  278. {
  279. bool alwaysRun = component->hasFlag(ComponentFlag::AlwaysRun);
  280. if (alwaysRun || mComponentState == ComponentState::Running || mComponentState == ComponentState::Paused)
  281. {
  282. if (triggerEvent)
  283. component->onEnabled();
  284. removeFromInactiveList(component);
  285. UINT32 activeIdx = (UINT32)mActiveComponents.size();
  286. mActiveComponents.push_back(component);
  287. component->setSceneManagerId(encodeComponentId(activeIdx, ActiveList));
  288. }
  289. }
  290. void SceneManager::_notifyComponentDeactivated(const HComponent& component, bool triggerEvent)
  291. {
  292. bool alwaysRun = component->hasFlag(ComponentFlag::AlwaysRun);
  293. if (alwaysRun || mComponentState == ComponentState::Running || mComponentState == ComponentState::Paused)
  294. {
  295. if (triggerEvent)
  296. component->onDisabled();
  297. removeFromActiveList(component);
  298. UINT32 inactiveIdx = (UINT32)mInactiveComponents.size();
  299. mInactiveComponents.push_back(component);
  300. component->setSceneManagerId(encodeComponentId(inactiveIdx, InactiveList));
  301. }
  302. }
  303. void SceneManager::_notifyComponentDestroyed(const HComponent& component)
  304. {
  305. UINT32 listType;
  306. UINT32 idx;
  307. decodeComponentId(component->getSceneManagerId(), idx, listType);
  308. switch(listType)
  309. {
  310. case ActiveList:
  311. removeFromActiveList(component);
  312. break;
  313. case InactiveList:
  314. removeFromInactiveList(component);
  315. break;
  316. case UninitializedList:
  317. removeFromUninitializedList(component);
  318. break;
  319. default:
  320. assert(false);
  321. break;
  322. }
  323. bool alwaysRun = component->hasFlag(ComponentFlag::AlwaysRun);
  324. bool isEnabled = component->sceneObject()->getActive() && (alwaysRun || mComponentState != ComponentState::Stopped);
  325. if (isEnabled)
  326. component->onDisabled();
  327. component->onDestroyed();
  328. }
  329. void SceneManager::removeFromActiveList(const HComponent& component)
  330. {
  331. UINT32 listType;
  332. UINT32 idx;
  333. decodeComponentId(component->getSceneManagerId(), idx, listType);
  334. UINT32 lastIdx;
  335. decodeComponentId(mActiveComponents.back()->getSceneManagerId(), lastIdx, listType);
  336. assert(mActiveComponents[idx] == component);
  337. if (idx != lastIdx)
  338. {
  339. std::swap(mActiveComponents[idx], mActiveComponents[lastIdx]);
  340. mActiveComponents[idx]->setSceneManagerId(encodeComponentId(idx, ActiveList));
  341. }
  342. mActiveComponents.erase(mActiveComponents.end() - 1);
  343. }
  344. void SceneManager::removeFromInactiveList(const HComponent& component)
  345. {
  346. UINT32 listType;
  347. UINT32 idx;
  348. decodeComponentId(component->getSceneManagerId(), idx, listType);
  349. UINT32 lastIdx;
  350. decodeComponentId(mInactiveComponents.back()->getSceneManagerId(), lastIdx, listType);
  351. assert(mInactiveComponents[idx] == component);
  352. if (idx != lastIdx)
  353. {
  354. std::swap(mInactiveComponents[idx], mInactiveComponents[lastIdx]);
  355. mInactiveComponents[idx]->setSceneManagerId(encodeComponentId(idx, InactiveList));
  356. }
  357. mInactiveComponents.erase(mInactiveComponents.end() - 1);
  358. }
  359. void SceneManager::removeFromUninitializedList(const HComponent& component)
  360. {
  361. UINT32 listType;
  362. UINT32 idx;
  363. decodeComponentId(component->getSceneManagerId(), idx, listType);
  364. UINT32 lastIdx;
  365. decodeComponentId(mUninitializedComponents.back()->getSceneManagerId(), lastIdx, listType);
  366. assert(mUninitializedComponents[idx] == component);
  367. if (idx != lastIdx)
  368. {
  369. std::swap(mUninitializedComponents[idx], mUninitializedComponents[lastIdx]);
  370. mUninitializedComponents[idx]->setSceneManagerId(encodeComponentId(idx, UninitializedList));
  371. }
  372. mUninitializedComponents.erase(mUninitializedComponents.end() - 1);
  373. }
  374. UINT32 SceneManager::encodeComponentId(UINT32 idx, UINT32 type)
  375. {
  376. assert(idx <= (0x3FFFFFFF));
  377. return (type << 30) | idx;
  378. }
  379. void SceneManager::decodeComponentId(UINT32 id, UINT32& idx, UINT32& type)
  380. {
  381. idx = id & 0x3FFFFFFF;
  382. type = id >> 30;
  383. }
  384. bool SceneManager::isComponentOfType(const HComponent& component, UINT32 rttiId)
  385. {
  386. return component->getRTTI()->getRTTIId() == rttiId;
  387. }
  388. void SceneManager::_update()
  389. {
  390. // Note: Eventually perform updates based on component types and/or on component priority. Right now we just
  391. // iterate in an undefined order, but it wouldn't be hard to change it.
  392. for (auto& entry : mActiveComponents)
  393. entry->update();
  394. GameObjectManager::instance().destroyQueuedObjects();
  395. }
  396. void SceneManager::registerNewSO(const HSceneObject& node)
  397. {
  398. if(mRootNode)
  399. node->setParent(mRootNode);
  400. }
  401. void SceneManager::onMainRenderTargetResized()
  402. {
  403. auto& rtProps = mMainRT->getProperties();
  404. float aspect = rtProps.width / (float)rtProps.height;
  405. for (auto& entry : mMainCameras)
  406. entry->setAspectRatio(aspect);
  407. }
  408. SceneManager& gSceneManager()
  409. {
  410. return SceneManager::instance();
  411. }
  412. }