BsRenderable.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "BsIReflectable.h"
  6. #include "BsCoreObject.h"
  7. #include "BsIResourceListener.h"
  8. #include "BsBounds.h"
  9. #include "BsAABox.h"
  10. namespace BansheeEngine
  11. {
  12. /** @addtogroup Implementation
  13. * @{
  14. */
  15. /** Signals which portion of a Renderable is dirty. */
  16. enum class RenderableDirtyFlag
  17. {
  18. Transform = 0x01,
  19. Everything = 0x02
  20. };
  21. template<bool Core> struct TMeshType {};
  22. template<> struct TMeshType < false > { typedef HMesh Type; };
  23. template<> struct TMeshType < true > { typedef SPtr<MeshCore> Type; };
  24. template<bool Core> struct TMaterialType {};
  25. template<> struct TMaterialType < false > { typedef HMaterial Type; };
  26. template<> struct TMaterialType < true > { typedef SPtr<MaterialCore> Type; };
  27. /**
  28. * Renderable represents any visible object in the scene. It has a mesh, bounds and a set of materials. Renderer will
  29. * render any Renderable objects visible by a camera.
  30. */
  31. template<bool Core>
  32. class BS_EXPORT TRenderable
  33. {
  34. typedef typename TMeshType<Core>::Type MeshType;
  35. typedef typename TMaterialType<Core>::Type MaterialType;
  36. public:
  37. TRenderable();
  38. virtual ~TRenderable();
  39. /**
  40. * Sets the mesh to render. All sub-meshes of the mesh will be rendered, and you may set individual materials for
  41. * each sub-mesh.
  42. */
  43. void setMesh(const MeshType& mesh);
  44. /**
  45. * Sets a material that will be used for rendering a sub-mesh with the specified index. If a sub-mesh doesn't have
  46. * a specific material set then the primary material will be used.
  47. */
  48. void setMaterial(UINT32 idx, const MaterialType& material);
  49. /**
  50. * Sets the primary material to use for rendering. Any sub-mesh that doesn't have an explicit material set will use
  51. * this material.
  52. *
  53. * @note This is equivalent to calling setMaterial(0, material).
  54. */
  55. void setMaterial(const MaterialType& material);
  56. /**
  57. * Returns all materials used for rendering this renderable. Each of the materials is used for rendering a single
  58. * sub-mesh.
  59. */
  60. const Vector<MaterialType>& getMaterials() { return mMaterials; }
  61. /**
  62. * Sets all materials used for rendering this renderable. Each of the materials is used for rendering a single
  63. * sub-mesh. If number of materials is larger than number of sub-meshes, they will be ignored. If lower, the
  64. * remaining materials will be removed.
  65. */
  66. void setMaterials(const Vector<MaterialType>& materials);
  67. /**
  68. * Sets the layer bitfield that controls whether a renderable is considered visible in a specific camera. Renderable
  69. * layer must match camera layer in order for the camera to render the component.
  70. */
  71. void setLayer(UINT64 layer);
  72. /** Sets the transform matrix that is applied to the object when its being rendered. */
  73. void setTransform(const Matrix4& transform, const Matrix4& transformNoScale);
  74. /** Sets whether the object should be rendered or not. */
  75. void setIsActive(bool active);
  76. /**
  77. * Gets the layer bitfield that controls whether a renderable is considered visible in a specific camera.
  78. * Renderable layer must match camera layer in order for the camera to render the component.
  79. */
  80. UINT64 getLayer() const { return mLayer; }
  81. /** Returns the mesh used for rendering. */
  82. MeshType getMesh() const { return mMesh; }
  83. /** Returns the material used for rendering a sub-mesh with the specified index. */
  84. MaterialType getMaterial(UINT32 idx) const { return mMaterials[idx]; }
  85. /** Returns the transform matrix that is applied to the object when its being rendered. */
  86. Matrix4 getTransform() const { return mTransform; }
  87. /**
  88. * Returns the transform matrix that is applied to the object when its being rendered. This transform matrix does
  89. * not include scale values.
  90. */
  91. Matrix4 getTransformNoScale() const { return mTransformNoScale; }
  92. /** Gets whether the object should be rendered or not. */
  93. bool getIsActive() const { return mIsActive; }
  94. /** Retrieves the world position of the renderable. */
  95. Vector3 getPosition() const { return mPosition; }
  96. protected:
  97. /**
  98. * Marks the simulation thread object as dirty and notifies the system its data should be synced with its core
  99. * thread counterpart.
  100. */
  101. virtual void _markCoreDirty(RenderableDirtyFlag flag = RenderableDirtyFlag::Everything) { }
  102. /**
  103. * Notifies the core object manager that this object is dependant on some other CoreObject(s), and the dependencies
  104. * changed since the last call to this method. This will trigger a call to getCoreDependencies() to collect the
  105. * new dependencies.
  106. */
  107. virtual void _markDependenciesDirty() { }
  108. /** Marks the resource dependencies list as dirty and schedules it for rebuild. */
  109. virtual void _markResourcesDirty() { }
  110. /** Triggered whenever the renderable's mesh changes. */
  111. virtual void onMeshChanged() { }
  112. MeshType mMesh;
  113. Vector<MaterialType> mMaterials;
  114. UINT64 mLayer;
  115. Vector<AABox> mWorldBounds;
  116. Vector3 mPosition;
  117. Matrix4 mTransform;
  118. Matrix4 mTransformNoScale;
  119. bool mIsActive;
  120. };
  121. /** @} */
  122. /** @addtogroup Renderer-Engine-Internal
  123. * @{
  124. */
  125. /** @copydoc TRenderable */
  126. class BS_EXPORT RenderableCore : public CoreObjectCore, public TRenderable<true>
  127. {
  128. public:
  129. ~RenderableCore();
  130. /** Gets world bounds of the mesh rendered by this object. */
  131. Bounds getBounds() const;
  132. /** Sets an ID that can be used for uniquely identifying this handler by the renderer. */
  133. void setRendererId(UINT32 id) { mRendererId = id; }
  134. /** Retrieves an ID that can be used for uniquely identifying this handler by the renderer. */
  135. UINT32 getRendererId() const { return mRendererId; }
  136. /** Checks is the mesh geometry rendered by this renderable animated using skeleton or blend shape animation. */
  137. bool isAnimated() const { return mAnimationId != (UINT64)-1; }
  138. /** Returns the identifier of the animation, if this object is animated using skeleton or blend shape animation. */
  139. UINT64 getAnimationId() const { return mAnimationId; }
  140. protected:
  141. friend class Renderable;
  142. RenderableCore();
  143. /** @copydoc CoreObject::initialize */
  144. void initialize() override;
  145. /** @copydoc CoreObject::syncToCore */
  146. void syncToCore(const CoreSyncData& data) override;
  147. UINT32 mRendererId;
  148. UINT64 mAnimationId;
  149. };
  150. /** @copydoc TRenderable */
  151. class BS_EXPORT Renderable : public IReflectable, public CoreObject, public TRenderable<false>, public IResourceListener
  152. {
  153. public:
  154. /** Gets world bounds of the mesh rendered by this object. */
  155. Bounds getBounds() const;
  156. /** Sets the animation that will be used for animating the attached mesh. */
  157. void setAnimation(const SPtr<Animation>& animation);
  158. /** Retrieves an implementation of a renderable handler usable only from the core thread. */
  159. SPtr<RenderableCore> getCore() const;
  160. /** Returns the hash value that can be used to identify if the internal data needs an update. */
  161. UINT32 _getLastModifiedHash() const { return mLastUpdateHash; }
  162. /** Sets the hash value that can be used to identify if the internal data needs an update. */
  163. void _setLastModifiedHash(UINT32 hash) { mLastUpdateHash = hash; }
  164. /** Creates a new renderable handler instance. */
  165. static SPtr<Renderable> create();
  166. protected:
  167. Renderable();
  168. /** @copydoc CoreObject::createCore */
  169. SPtr<CoreObjectCore> createCore() const override;
  170. /** @copydoc TRenderable::onMeshChanged */
  171. void onMeshChanged() override;
  172. /** @copydoc TRenderable::_markCoreDirty */
  173. void _markCoreDirty(RenderableDirtyFlag flag = RenderableDirtyFlag::Everything) override;
  174. /** @copydoc TRenderable::_markResourcesDirty */
  175. void _markResourcesDirty() override;
  176. /** @copydoc CoreObject::markDependenciesDirty */
  177. void _markDependenciesDirty() override;
  178. /** @copydoc CoreObject::syncToCore */
  179. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  180. /** @copydoc CoreObject::getCoreDependencies */
  181. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  182. /** @copydoc IResourceListener::getListenerResources */
  183. void getListenerResources(Vector<HResource>& resources) override;
  184. /** @copydoc IResourceListener::notifyResourceLoaded */
  185. void notifyResourceLoaded(const HResource& resource) override;
  186. /** @copydoc IResourceListener::notifyResourceChanged */
  187. void notifyResourceChanged(const HResource& resource) override;
  188. /** Creates a new renderable handler instance without initializing it. */
  189. static SPtr<Renderable> createEmpty();
  190. UINT32 mLastUpdateHash;
  191. SPtr<Animation> mAnimation;
  192. /************************************************************************/
  193. /* RTTI */
  194. /************************************************************************/
  195. public:
  196. friend class RenderableRTTI;
  197. static RTTITypeBase* getRTTIStatic();
  198. RTTITypeBase* getRTTI() const override;
  199. };
  200. /** @} */
  201. }