2
0

BsRenderable.h 9.4 KB

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