BsRenderable.h 11 KB

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