BsSceneManager.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsModule.h"
  6. #include "BsGameObject.h"
  7. namespace bs
  8. {
  9. class LightProbeVolume;
  10. /** @addtogroup Scene-Internal
  11. * @{
  12. */
  13. /** Contains information about a camera managed by the scene manager. */
  14. struct SceneCameraData
  15. {
  16. SceneCameraData() { }
  17. SceneCameraData(const SPtr<Camera>& camera, const HSceneObject& sceneObject)
  18. :camera(camera), sceneObject(sceneObject)
  19. { }
  20. SPtr<Camera> camera;
  21. HSceneObject sceneObject;
  22. };
  23. /** Contains information about a renderable managed by the scene manager. */
  24. struct SceneRenderableData
  25. {
  26. SceneRenderableData() { }
  27. SceneRenderableData(const SPtr<Renderable>& renderable, const HSceneObject& sceneObject)
  28. :renderable(renderable), sceneObject(sceneObject)
  29. { }
  30. SPtr<Renderable> renderable;
  31. HSceneObject sceneObject;
  32. };
  33. /** Contains information about a light managed by the scene manager. */
  34. struct SceneLightData
  35. {
  36. SceneLightData() { }
  37. SceneLightData(const SPtr<Light>& light, const HSceneObject& sceneObject)
  38. :light(light), sceneObject(sceneObject)
  39. { }
  40. SPtr<Light> light;
  41. HSceneObject sceneObject;
  42. };
  43. /** Contains information about a reflection probe managed by the scene manager. */
  44. struct SceneReflectionProbeData
  45. {
  46. SceneReflectionProbeData() { }
  47. SceneReflectionProbeData(const SPtr<ReflectionProbe>& probe, const HSceneObject& sceneObject)
  48. :probe(probe), sceneObject(sceneObject)
  49. { }
  50. SPtr<ReflectionProbe> probe;
  51. HSceneObject sceneObject;
  52. };
  53. /** Contains information about a light probe volume managed by the scene manager. */
  54. struct SceneLightProbeVolumeData
  55. {
  56. SceneLightProbeVolumeData() { }
  57. SceneLightProbeVolumeData(const SPtr<LightProbeVolume>& volume, const HSceneObject& sceneObject)
  58. :volume(volume), sceneObject(sceneObject)
  59. { }
  60. SPtr<LightProbeVolume> volume;
  61. HSceneObject sceneObject;
  62. };
  63. /** Possible states components can be in. Controls which component callbacks are triggered. */
  64. enum class ComponentState
  65. {
  66. Running, /**< All components callbacks are being triggered normally. */
  67. Paused, /**< All component callbacks except update are being triggered normally. */
  68. Stopped /**< No component callbacks are being triggered. */
  69. };
  70. /** Manages active SceneObjects and provides ways for querying and updating them or their components. */
  71. class BS_CORE_EXPORT SceneManager : public Module<SceneManager>
  72. {
  73. public:
  74. SceneManager();
  75. ~SceneManager();
  76. /** Returns the root scene object. */
  77. HSceneObject getRootNode() const { return mRootNode; }
  78. /**
  79. * Destroys all scene objects in the scene.
  80. *
  81. * @param[in] forceAll If true, then even the persistent objects will be unloaded.
  82. */
  83. void clearScene(bool forceAll = false);
  84. /**
  85. * Changes the component state that globally determines which component callbacks are activated. Only affects
  86. * components that don't have the ComponentFlag::AlwaysRun flag set.
  87. */
  88. void setComponentState(ComponentState state);
  89. /** Checks are the components currently in the Running state. */
  90. bool isRunning() const { return mComponentState == ComponentState::Running; }
  91. /** Returns all cameras in the scene. */
  92. const Map<Camera*, SceneCameraData>& getAllCameras() const { return mCameras; }
  93. /**
  94. * Returns the camera in the scene marked as main. Main camera controls the final render surface that is displayed
  95. * to the user. If there are multiple main cameras, the first one found returned.
  96. */
  97. SceneCameraData getMainCamera() const;
  98. /**
  99. * Sets the render target that the main camera in the scene (if any) will render its view to. This generally means
  100. * the main game window when running standalone, or the Game viewport when running in editor.
  101. */
  102. void setMainRenderTarget(const SPtr<RenderTarget>& rt);
  103. /** Returns all renderables in the scene. */
  104. const Map<Renderable*, SceneRenderableData>& getAllRenderables() const { return mRenderables; }
  105. /** Notifies the scene manager that a new renderable was created. */
  106. void _registerRenderable(const SPtr<Renderable>& renderable, const HSceneObject& so);
  107. /** Notifies the scene manager that a renderable was removed. */
  108. void _unregisterRenderable(const SPtr<Renderable>& renderable);
  109. /** Notifies the scene manager that a new light was created. */
  110. void _registerLight(const SPtr<Light>& light, const HSceneObject& so);
  111. /** Notifies the scene manager that a light was removed. */
  112. void _unregisterLight(const SPtr<Light>& light);
  113. /** Notifies the scene manager that a new camera was created. */
  114. void _registerCamera(const SPtr<Camera>& camera, const HSceneObject& so);
  115. /** Notifies the scene manager that a camera was removed. */
  116. void _unregisterCamera(const SPtr<Camera>& camera);
  117. /** Notifies the scene manager that a new reflection probe was created. */
  118. void _registerReflectionProbe(const SPtr<ReflectionProbe>& probe, const HSceneObject& so);
  119. /** Notifies the scene manager that a reflection probe was removed. */
  120. void _unregisterReflectionProbe(const SPtr<ReflectionProbe>& probe);
  121. /** Notifies the scene manager that a new light probe volume was created. */
  122. void _registerLightProbeVolume(const SPtr<LightProbeVolume>& volume, const HSceneObject& so);
  123. /** Notifies the scene manager that a light proble volume was removed. */
  124. void _unregisterLightProbeVolume(const SPtr<LightProbeVolume>& volume);
  125. /** Notifies the scene manager that a camera either became the main camera, or has stopped being main camera. */
  126. void _notifyMainCameraStateChanged(const SPtr<Camera>& camera);
  127. /** Changes the root scene object. Any persistent objects will remain in the scene, now parented to the new root. */
  128. void _setRootNode(const HSceneObject& root);
  129. /** Called every frame. Calls update methods on all scene objects and their components. */
  130. void _update();
  131. /** Updates dirty transforms on any core objects that may be tied with scene objects. */
  132. void _updateCoreObjectTransforms();
  133. /** Notifies the manager that a new component has just been created. The manager triggers necessary callbacks. */
  134. void _notifyComponentCreated(const HComponent& component, bool parentActive);
  135. /**
  136. * Notifies the manager that a scene object the component belongs to was activated. The manager triggers necessary
  137. * callbacks.
  138. */
  139. void _notifyComponentActivated(const HComponent& component, bool triggerEvent);
  140. /**
  141. * Notifies the manager that a scene object the component belongs to was deactivated. The manager triggers necessary
  142. * callbacks.
  143. */
  144. void _notifyComponentDeactivated(const HComponent& component, bool triggerEvent);
  145. /** Notifies the manager that a component is about to be destroyed. The manager triggers necessary callbacks. */
  146. void _notifyComponentDestroyed(const HComponent& component);
  147. protected:
  148. friend class SceneObject;
  149. /**
  150. * Register a new node in the scene manager, on the top-most level of the hierarchy.
  151. *
  152. * @param[in] node Node you wish to add. It's your responsibility not to add duplicate or null nodes. This
  153. * method won't check.
  154. *
  155. * @note
  156. * After you add a node in the scene manager, it takes ownership of its memory and is responsible for releasing it.
  157. * Do NOT add nodes that have already been added (if you just want to change their parent). Normally this
  158. * method will only be called by SceneObject.
  159. */
  160. void registerNewSO(const HSceneObject& node);
  161. /** Callback that is triggered when the main render target size is changed. */
  162. void onMainRenderTargetResized();
  163. /** Removes a component from the active component list. */
  164. void removeFromActiveList(const HComponent& component);
  165. /** Removes a component from the inactive component list. */
  166. void removeFromInactiveList(const HComponent& component);
  167. /** Removes a component from the uninitialized component list. */
  168. void removeFromUninitializedList(const HComponent& component);
  169. /**
  170. * Encodes an index and a type into a single 32-bit integer. Top 2 bits represent the type, while the rest represent
  171. * the index.
  172. */
  173. UINT32 encodeComponentId(UINT32 idx, UINT32 type);
  174. /** Decodes an id encoded with encodeComponentId(). */
  175. void decodeComponentId(UINT32 id, UINT32& idx, UINT32& type);
  176. protected:
  177. HSceneObject mRootNode;
  178. Map<Camera*, SceneCameraData> mCameras;
  179. Vector<SceneCameraData> mMainCameras;
  180. Map<Renderable*, SceneRenderableData> mRenderables;
  181. Map<Light*, SceneLightData> mLights;
  182. Map<ReflectionProbe*, SceneReflectionProbeData> mReflectionProbes;
  183. Map<LightProbeVolume*, SceneLightProbeVolumeData> mLightProbeVolumes;
  184. Vector<HComponent> mActiveComponents;
  185. Vector<HComponent> mInactiveComponents;
  186. Vector<HComponent> mUnintializedComponents;
  187. SPtr<RenderTarget> mMainRT;
  188. HEvent mMainRTResizedConn;
  189. ComponentState mComponentState = ComponentState::Running;
  190. };
  191. /** Provides easy access to the SceneManager. */
  192. BS_CORE_EXPORT SceneManager& gSceneManager();
  193. /** @} */
  194. }