BsRenderable.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 "BsIReflectable.h"
  6. #include "BsCoreObject.h"
  7. #include "BsIResourceListener.h"
  8. #include "BsBounds.h"
  9. #include "BsAABox.h"
  10. namespace bs
  11. {
  12. struct RendererAnimationData;
  13. /** @addtogroup Implementation
  14. * @{
  15. */
  16. /** Signals which portion of a Renderable is dirty. */
  17. enum class RenderableDirtyFlag
  18. {
  19. Transform = 0x01,
  20. Everything = 0x02,
  21. Mobility = 0x04
  22. };
  23. /** Type of animation that can be applied to a renderable object. */
  24. enum class RenderableAnimType
  25. {
  26. None,
  27. Skinned,
  28. Morph,
  29. SkinnedMorph,
  30. Count // Keep at end
  31. };
  32. /**
  33. * Renderable represents any visible object in the scene. It has a mesh, bounds and a set of materials. Renderer will
  34. * render any Renderable objects visible by a camera.
  35. */
  36. template<bool Core>
  37. class BS_CORE_EXPORT TRenderable
  38. {
  39. typedef typename TMeshType<Core>::Type MeshType;
  40. typedef typename TMaterialPtrType<Core>::Type MaterialType;
  41. public:
  42. TRenderable();
  43. virtual ~TRenderable();
  44. /**
  45. * Sets the mesh to render. All sub-meshes of the mesh will be rendered, and you may set individual materials for
  46. * each sub-mesh.
  47. */
  48. void setMesh(const MeshType& mesh);
  49. /**
  50. * Sets a material that will be used for rendering a sub-mesh with the specified index. If a sub-mesh doesn't have
  51. * a specific material set then the primary material will be used.
  52. */
  53. void setMaterial(UINT32 idx, const MaterialType& material);
  54. /**
  55. * Sets the primary material to use for rendering. Any sub-mesh that doesn't have an explicit material set will use
  56. * this material.
  57. *
  58. * @note This is equivalent to calling setMaterial(0, material).
  59. */
  60. void setMaterial(const MaterialType& material);
  61. /**
  62. * Returns all materials used for rendering this renderable. Each of the materials is used for rendering a single
  63. * sub-mesh.
  64. */
  65. const Vector<MaterialType>& getMaterials() { return mMaterials; }
  66. /**
  67. * Sets all materials used for rendering this renderable. Each of the materials is used for rendering a single
  68. * sub-mesh. If number of materials is larger than number of sub-meshes, they will be ignored. If lower, the
  69. * remaining materials will be removed.
  70. */
  71. void setMaterials(const Vector<MaterialType>& materials);
  72. /**
  73. * Sets the layer bitfield that controls whether a renderable is considered visible in a specific camera. Renderable
  74. * layer must match camera layer in order for the camera to render the component.
  75. */
  76. void setLayer(UINT64 layer);
  77. /** Sets the transform matrix that is applied to the object when its being rendered. */
  78. void setTransform(const Matrix4& transform, const Matrix4& transformNoScale);
  79. /** Sets whether the object should be rendered or not. */
  80. void setIsActive(bool active);
  81. /**
  82. * Sets the mobility of a scene object. This is used primarily as a performance hint to engine systems. Objects
  83. * with more restricted mobility will result in higher performance. Some mobility constraints will be enforced by
  84. * the engine itself, while for others the caller must be sure not to break the promise he made when mobility was
  85. * set. By default scene object's mobility is unrestricted.
  86. */
  87. void setMobility(ObjectMobility mobility);
  88. /**
  89. * Gets the mobility setting for this scene object. See setMobility();
  90. */
  91. ObjectMobility getMobility() const { return mMobility; }
  92. /**
  93. * Sets bounds that will be used when determining if object is visible. Only relevant if setUseOverrideBounds() is
  94. * set to true.
  95. *
  96. * @param[in] bounds Bounds in local space.
  97. */
  98. void setOverrideBounds(const AABox& bounds);
  99. /**
  100. * Enables or disables override bounds. When enabled the bounds provided to setOverrideBounds() will be used for
  101. * determining object visibility, otherwise the bounds from the object's mesh will be used. Disabled by default.
  102. */
  103. void setUseOverrideBounds(bool enable);
  104. /**
  105. * Gets the layer bitfield that controls whether a renderable is considered visible in a specific camera.
  106. * Renderable layer must match camera layer in order for the camera to render the component.
  107. */
  108. UINT64 getLayer() const { return mLayer; }
  109. /** Returns the mesh used for rendering. */
  110. MeshType getMesh() const { return mMesh; }
  111. /** Returns the material used for rendering a sub-mesh with the specified index. */
  112. MaterialType getMaterial(UINT32 idx) const { return mMaterials[idx]; }
  113. /** Returns the transform matrix that is applied to the object when its being rendered. */
  114. Matrix4 getTransform() const { return mTransform; }
  115. /**
  116. * Returns the transform matrix that is applied to the object when its being rendered. This transform matrix does
  117. * not include scale values.
  118. */
  119. Matrix4 getTransformNoScale() const { return mTransformNoScale; }
  120. /** Gets whether the object should be rendered or not. */
  121. bool getIsActive() const { return mIsActive; }
  122. /** Retrieves the world position of the renderable. */
  123. Vector3 getPosition() const { return mPosition; }
  124. protected:
  125. /**
  126. * Marks the simulation thread object as dirty and notifies the system its data should be synced with its core
  127. * thread counterpart.
  128. */
  129. virtual void _markCoreDirty(RenderableDirtyFlag flag = RenderableDirtyFlag::Everything) { }
  130. /**
  131. * Notifies the core object manager that this object is dependant on some other CoreObject(s), and the dependencies
  132. * changed since the last call to this method. This will trigger a call to getCoreDependencies() to collect the
  133. * new dependencies.
  134. */
  135. virtual void _markDependenciesDirty() { }
  136. /** Marks the resource dependencies list as dirty and schedules it for rebuild. */
  137. virtual void _markResourcesDirty() { }
  138. /** Triggered whenever the renderable's mesh changes. */
  139. virtual void onMeshChanged() { }
  140. MeshType mMesh;
  141. Vector<MaterialType> mMaterials;
  142. UINT64 mLayer;
  143. AABox mOverrideBounds;
  144. bool mUseOverrideBounds;
  145. Vector3 mPosition;
  146. Matrix4 mTransform;
  147. Matrix4 mTransformNoScale;
  148. bool mIsActive;
  149. RenderableAnimType mAnimType;
  150. ObjectMobility mMobility;
  151. };
  152. /** @} */
  153. /** @addtogroup Renderer-Internal
  154. * @{
  155. */
  156. /** @copydoc TRenderable */
  157. class BS_CORE_EXPORT Renderable : public IReflectable, public CoreObject, public TRenderable<false>, public IResourceListener
  158. {
  159. public:
  160. /** Gets world bounds of the mesh rendered by this object. */
  161. Bounds getBounds() const;
  162. /** Sets the animation that will be used for animating the attached mesh. */
  163. void setAnimation(const SPtr<Animation>& animation);
  164. /** Checks is the renderable animated or static. */
  165. bool isAnimated() const { return mAnimation != nullptr; }
  166. /** Retrieves an implementation of a renderable handler usable only from the core thread. */
  167. SPtr<ct::Renderable> getCore() const;
  168. /** Returns the hash value that can be used to identify if the internal data needs an update. */
  169. UINT32 _getLastModifiedHash() const { return mLastUpdateHash; }
  170. /** Sets the hash value that can be used to identify if the internal data needs an update. */
  171. void _setLastModifiedHash(UINT32 hash) { mLastUpdateHash = hash; }
  172. /** Updates the transfrom from the provided scene object, if the scene object's data is detected to be dirty. */
  173. void _updateTransform(const HSceneObject& so, bool force = false);
  174. /** Creates a new renderable handler instance. */
  175. static SPtr<Renderable> create();
  176. protected:
  177. Renderable();
  178. /** @copydoc CoreObject::createCore */
  179. SPtr<ct::CoreObject> createCore() const override;
  180. /** @copydoc TRenderable::onMeshChanged */
  181. void onMeshChanged() override;
  182. /** Updates animation properties depending on the current mesh. */
  183. void refreshAnimation();
  184. /** @copydoc TRenderable::_markCoreDirty */
  185. void _markCoreDirty(RenderableDirtyFlag flag = RenderableDirtyFlag::Everything) override;
  186. /** @copydoc TRenderable::_markResourcesDirty */
  187. void _markResourcesDirty() override;
  188. /** @copydoc CoreObject::markDependenciesDirty */
  189. void _markDependenciesDirty() override;
  190. /** @copydoc CoreObject::syncToCore */
  191. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  192. /** @copydoc CoreObject::getCoreDependencies */
  193. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  194. /** @copydoc IResourceListener::getListenerResources */
  195. void getListenerResources(Vector<HResource>& resources) override;
  196. /** @copydoc IResourceListener::notifyResourceLoaded */
  197. void notifyResourceLoaded(const HResource& resource) override;
  198. /** @copydoc IResourceListener::notifyResourceChanged */
  199. void notifyResourceChanged(const HResource& resource) override;
  200. /** Creates a new renderable handler instance without initializing it. */
  201. static SPtr<Renderable> createEmpty();
  202. UINT32 mLastUpdateHash;
  203. SPtr<Animation> mAnimation;
  204. /************************************************************************/
  205. /* RTTI */
  206. /************************************************************************/
  207. public:
  208. friend class RenderableRTTI;
  209. static RTTITypeBase* getRTTIStatic();
  210. RTTITypeBase* getRTTI() const override;
  211. };
  212. namespace ct
  213. {
  214. /** @copydoc TRenderable */
  215. class BS_CORE_EXPORT Renderable : public CoreObject, public TRenderable<true>
  216. {
  217. public:
  218. ~Renderable();
  219. /** Gets world bounds of the mesh rendered by this object. */
  220. Bounds getBounds() const;
  221. /** Sets an ID that can be used for uniquely identifying this object by the renderer. */
  222. void setRendererId(UINT32 id) { mRendererId = id; }
  223. /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */
  224. UINT32 getRendererId() const { return mRendererId; }
  225. /** Returns the type of animation influencing this renderable, if any. */
  226. RenderableAnimType getAnimType() const { return mAnimType; }
  227. /** Returns the identifier of the animation, if this object is animated using skeleton or blend shape animation. */
  228. UINT64 getAnimationId() const { return mAnimationId; }
  229. /**
  230. * Updates internal animation buffers from the contents of the provided animation data object. Does nothing if
  231. * renderable is not affected by animation.
  232. */
  233. void updateAnimationBuffers(const RendererAnimationData& animData);
  234. /** Returns the GPU buffer containing element's bone matrices, if it has any. */
  235. const SPtr<GpuBuffer>& getBoneMatrixBuffer() const { return mBoneMatrixBuffer; }
  236. /** Returns the vertex buffer containing element's morph shape vertices, if it has any. */
  237. const SPtr<VertexBuffer>& getMorphShapeBuffer() const { return mMorphShapeBuffer; }
  238. /** Returns vertex declaration used for rendering meshes containing morph shape information. */
  239. const SPtr<VertexDeclaration>& getMorphVertexDeclaration() const { return mMorphVertexDeclaration; }
  240. protected:
  241. friend class bs::Renderable;
  242. Renderable();
  243. /** @copydoc CoreObject::initialize */
  244. void initialize() override;
  245. /** @copydoc CoreObject::syncToCore */
  246. void syncToCore(const CoreSyncData& data) override;
  247. /** Creates any buffers required for renderable animation. Should be called whenever animation properties change. */
  248. void createAnimationBuffers();
  249. UINT32 mRendererId;
  250. UINT64 mAnimationId;
  251. UINT32 mMorphShapeVersion;
  252. SPtr<GpuBuffer> mBoneMatrixBuffer;
  253. SPtr<VertexBuffer> mMorphShapeBuffer;
  254. SPtr<VertexDeclaration> mMorphVertexDeclaration;
  255. };
  256. }
  257. /** @} */
  258. }