BsRenderable.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #pragma once
  2. #include "BsPrerequisites.h"
  3. #include "BsIReflectable.h"
  4. #include "BsCoreObject.h"
  5. #include "BsIResourceListener.h"
  6. #include "BsBounds.h"
  7. #include "BsAABox.h"
  8. namespace BansheeEngine
  9. {
  10. /**
  11. * @brief Signals which portion of a Renderable is dirty.
  12. */
  13. enum class RenderableDirtyFlag
  14. {
  15. Transform = 0x01,
  16. Everything = 0x02
  17. };
  18. /**
  19. * @brief Renderable represents any visible object in the scene. It has a mesh,
  20. * bounds and a set of materials. Renderer will render any Renderable objects
  21. * visible by a camera.
  22. */
  23. template<bool Core>
  24. class BS_EXPORT TRenderable
  25. {
  26. template<bool Core> struct TMeshType {};
  27. template<> struct TMeshType < false > { typedef HMesh Type; };
  28. template<> struct TMeshType < true > { typedef SPtr<MeshCore> Type; };
  29. template<bool Core> struct TMaterialType {};
  30. template<> struct TMaterialType < false > { typedef HMaterial Type; };
  31. template<> struct TMaterialType < true > { typedef SPtr<MaterialCore> Type; };
  32. typedef typename TMeshType<Core>::Type MeshType;
  33. typedef typename TMaterialType<Core>::Type MaterialType;
  34. public:
  35. TRenderable();
  36. virtual ~TRenderable();
  37. /**
  38. * @brief Sets the mesh to render. All sub-meshes of the mesh will be rendered,
  39. * and you may set individual materials for each sub-mesh.
  40. */
  41. void setMesh(const MeshType& mesh);
  42. /**
  43. * @brief Sets a material that will be used for rendering a sub-mesh with
  44. * the specified index. If a sub-mesh doesn't have a specific material set
  45. * then the primary material will be used.
  46. */
  47. void setMaterial(UINT32 idx, const MaterialType& material);
  48. /**
  49. * @brief Sets the primary material to use for rendering. Any sub-mesh that
  50. * doesn't have an explicit material set will use this material.
  51. *
  52. * @note This is equivalent to calling setMaterial(0, material).
  53. */
  54. void setMaterial(const MaterialType& material);
  55. /**
  56. * @brief Returns all materials used for rendering this renderable. Each of the materials is used for rendering
  57. * a single sub-mesh.
  58. */
  59. const Vector<MaterialType>& getMaterials() { return mMaterials; }
  60. /**
  61. * @brief Sets all materials used for rendering this renderable. Each of the materials is used for rendering
  62. * a single sub-mesh. If number of materials is larger than number of sub-meshes, they will be ignored.
  63. * If lower, the remaining materials will be removed.
  64. */
  65. void setMaterials(const Vector<MaterialType>& materials);
  66. /**
  67. * @brief Sets the layer bitfield that controls whether a renderable is considered
  68. * visible in a specific camera. Renderable layer must match camera layer
  69. * in order for the camera to render the component.
  70. */
  71. void setLayer(UINT64 layer);
  72. /**
  73. * @brief Sets the transform matrix that is applied to the object
  74. * when its being rendered.
  75. */
  76. void setTransform(const Matrix4& transform);
  77. /**
  78. * @brief Sets the world position of the renderable.
  79. */
  80. void setPosition(const Vector3& position);
  81. /**
  82. * @brief Sets whether the object should be rendered or not.
  83. */
  84. void setIsActive(bool active);
  85. /**
  86. * @brief Gets the layer bitfield that controls whether a renderable is considered
  87. * visible in a specific camera. Renderable layer must match camera layer
  88. * in order for the camera to render the component.
  89. */
  90. UINT64 getLayer() const { return mLayer; }
  91. /**
  92. * @brief Returns the mesh used for rendering.
  93. */
  94. MeshType getMesh() const { return mMesh; }
  95. /**
  96. * @brief Returns the material used for rendering a sub-mesh with
  97. * the specified index.
  98. */
  99. MaterialType getMaterial(UINT32 idx) const { return mMaterials[idx]; }
  100. /**
  101. * @brief Returns the transform matrix that is applied to the object
  102. * when its being rendered.
  103. */
  104. Matrix4 getTransform() const { return mTransform; }
  105. /**
  106. * @brief Gets whether the object should be rendered or not.
  107. */
  108. bool getIsActive() const { return mIsActive; }
  109. /**
  110. * @brief Retrieves the world position of the renderable.
  111. */
  112. Vector3 getPosition() const { return mPosition; }
  113. protected:
  114. /**
  115. * @copydoc CoreObject::markCoreDirty
  116. */
  117. virtual void _markCoreDirty(RenderableDirtyFlag flag = RenderableDirtyFlag::Everything) { }
  118. /**
  119. * @copydoc IResourceListener::markResourcesDirty
  120. */
  121. virtual void _markResourcesDirty() { }
  122. MeshType mMesh;
  123. Vector<MaterialType> mMaterials;
  124. UINT64 mLayer;
  125. Vector<AABox> mWorldBounds;
  126. Vector3 mPosition;
  127. Matrix4 mTransform;
  128. bool mIsActive;
  129. };
  130. /**
  131. * @copydoc TRenderable
  132. */
  133. class BS_EXPORT RenderableCore : public CoreObjectCore, public TRenderable<true>
  134. {
  135. public:
  136. ~RenderableCore();
  137. /**
  138. * @brief Gets world bounds of the mesh rendered by this object.
  139. */
  140. Bounds getBounds() const;
  141. /**
  142. * @brief Returns the type that controls how is this object rendered.
  143. */
  144. RenderableType getRenderableType() const { return RenType_LitTextured; }
  145. /**
  146. * @brief Sets an ID that can be used for uniquely identifying this handler by the renderer.
  147. */
  148. void setRendererId(UINT32 id) { mRendererId = id; }
  149. /**
  150. * @brief Retrieves an ID that can be used for uniquely identifying this handler by the renderer.
  151. */
  152. UINT32 getRendererId() const { return mRendererId; }
  153. protected:
  154. friend class Renderable;
  155. RenderableCore();
  156. /**
  157. * @copydoc CoreObject::initialize
  158. */
  159. void initialize() override;
  160. /**
  161. * @copydoc CoreObject::syncToCore
  162. */
  163. void syncToCore(const CoreSyncData& data) override;
  164. UINT32 mRendererId;
  165. };
  166. /**
  167. * @copydoc TRenderable
  168. */
  169. class BS_EXPORT Renderable : public IReflectable, public CoreObject, public TRenderable<false>, public IResourceListener
  170. {
  171. public:
  172. /**
  173. * @brief Gets world bounds of the mesh rendered by this object.
  174. */
  175. Bounds getBounds() const;
  176. /**
  177. * @brief Retrieves an implementation of a renderable handler usable only from the
  178. * core thread.
  179. */
  180. SPtr<RenderableCore> getCore() const;
  181. /**
  182. * @brief Returns the hash value that can be used to identify if the internal data needs an update.
  183. */
  184. UINT32 _getLastModifiedHash() const { return mLastUpdateHash; }
  185. /**
  186. * @brief Sets the hash value that can be used to identify if the internal data needs an update.
  187. */
  188. void _setLastModifiedHash(UINT32 hash) { mLastUpdateHash = hash; }
  189. /**
  190. * @brief Creates a new renderable handler instance.
  191. */
  192. static RenderablePtr create();
  193. protected:
  194. Renderable();
  195. /**
  196. * @copydoc CoreObject::createCore
  197. */
  198. SPtr<CoreObjectCore> createCore() const override;
  199. /**
  200. * @copydoc CoreObject::markCoreDirty
  201. */
  202. void _markCoreDirty(RenderableDirtyFlag flag = RenderableDirtyFlag::Everything) override;
  203. /**
  204. * @copydoc IResourceListener::markResourcesDirty
  205. */
  206. void _markResourcesDirty() override;
  207. /**
  208. * @copydoc CoreObject::syncToCore
  209. */
  210. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  211. /**
  212. * @copydoc CoreObject::getCoreDependencies
  213. */
  214. void getCoreDependencies(FrameVector<SPtr<CoreObject>>& dependencies) override;
  215. /**
  216. * @copydoc IResourceListener::getListenerResources
  217. */
  218. void getListenerResources(Vector<HResource>& resources) override;
  219. /**
  220. * @copydoc IResourceListener::notifyResourceLoaded
  221. */
  222. void notifyResourceLoaded(const HResource& resource) override { markCoreDirty(); }
  223. /**
  224. * @copydoc IResourceListener::notifyResourceDestroyed
  225. */
  226. void notifyResourceDestroyed(const HResource& resource) override { markCoreDirty(); }
  227. /**
  228. * @copydoc IResourceListener::notifyResourceChanged
  229. */
  230. void notifyResourceChanged(const HResource& resource) override { markCoreDirty(); }
  231. /**
  232. * @brief Creates a new renderable handler instance without initializing it.
  233. */
  234. static RenderablePtr createEmpty();
  235. UINT32 mLastUpdateHash;
  236. /************************************************************************/
  237. /* RTTI */
  238. /************************************************************************/
  239. public:
  240. friend class RenderableRTTI;
  241. static RTTITypeBase* getRTTIStatic();
  242. virtual RTTITypeBase* getRTTI() const override;
  243. };
  244. }