BsRenderable.h 11 KB

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