| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- #pragma once
- #include "BsPrerequisites.h"
- #include "BsIReflectable.h"
- #include "BsCoreObject.h"
- #include "BsIResourceListener.h"
- #include "BsBounds.h"
- #include "BsAABox.h"
- namespace BansheeEngine
- {
- /**
- * @brief Signals which portion of a Renderable is dirty.
- */
- enum class RenderableDirtyFlag
- {
- Transform = 0x01,
- Everything = 0x02
- };
- /**
- * @brief Renderable represents any visible object in the scene. It has a mesh,
- * bounds and a set of materials. Renderer will render any Renderable objects
- * visible by a camera.
- */
- template<bool Core>
- class BS_EXPORT TRenderable
- {
- template<bool Core> struct TMeshType {};
- template<> struct TMeshType < false > { typedef HMesh Type; };
- template<> struct TMeshType < true > { typedef SPtr<MeshCore> Type; };
- template<bool Core> struct TMaterialType {};
- template<> struct TMaterialType < false > { typedef HMaterial Type; };
- template<> struct TMaterialType < true > { typedef SPtr<MaterialCore> Type; };
- typedef typename TMeshType<Core>::Type MeshType;
- typedef typename TMaterialType<Core>::Type MaterialType;
- public:
- TRenderable();
- virtual ~TRenderable();
- /**
- * @brief Sets the mesh to render. All sub-meshes of the mesh will be rendered,
- * and you may set individual materials for each sub-mesh.
- */
- void setMesh(const MeshType& mesh);
- /**
- * @brief Sets a material that will be used for rendering a sub-mesh with
- * the specified index. If a sub-mesh doesn't have a specific material set
- * then the primary material will be used.
- */
- void setMaterial(UINT32 idx, const MaterialType& material);
- /**
- * @brief Sets the primary material to use for rendering. Any sub-mesh that
- * doesn't have an explicit material set will use this material.
- *
- * @note This is equivalent to calling setMaterial(0, material).
- */
- void setMaterial(const MaterialType& material);
- /**
- * @brief Returns all materials used for rendering this renderable. Each of the materials is used for rendering
- * a single sub-mesh.
- */
- const Vector<MaterialType>& getMaterials() { return mMaterials; }
- /**
- * @brief Sets all materials used for rendering this renderable. Each of the materials is used for rendering
- * a single sub-mesh. If number of materials is larger than number of sub-meshes, they will be ignored.
- * If lower, the remaining materials will be removed.
- */
- void setMaterials(const Vector<MaterialType>& materials);
- /**
- * @brief Sets the layer bitfield that controls whether a renderable is considered
- * visible in a specific camera. Renderable layer must match camera layer
- * in order for the camera to render the component.
- */
- void setLayer(UINT64 layer);
- /**
- * @brief Sets the transform matrix that is applied to the object
- * when its being rendered.
- */
- void setTransform(const Matrix4& transform);
- /**
- * @brief Sets the world position of the renderable.
- */
- void setPosition(const Vector3& position);
- /**
- * @brief Sets whether the object should be rendered or not.
- */
- void setIsActive(bool active);
- /**
- * @brief Gets the layer bitfield that controls whether a renderable is considered
- * visible in a specific camera. Renderable layer must match camera layer
- * in order for the camera to render the component.
- */
- UINT64 getLayer() const { return mLayer; }
- /**
- * @brief Returns the mesh used for rendering.
- */
- MeshType getMesh() const { return mMesh; }
- /**
- * @brief Returns the material used for rendering a sub-mesh with
- * the specified index.
- */
- MaterialType getMaterial(UINT32 idx) const { return mMaterials[idx]; }
- /**
- * @brief Returns the transform matrix that is applied to the object
- * when its being rendered.
- */
- Matrix4 getTransform() const { return mTransform; }
- /**
- * @brief Gets whether the object should be rendered or not.
- */
- bool getIsActive() const { return mIsActive; }
- /**
- * @brief Retrieves the world position of the renderable.
- */
- Vector3 getPosition() const { return mPosition; }
- protected:
- /**
- * @copydoc CoreObject::markCoreDirty
- */
- virtual void _markCoreDirty(RenderableDirtyFlag flag = RenderableDirtyFlag::Everything) { }
- /**
- * @copydoc IResourceListener::markResourcesDirty
- */
- virtual void _markResourcesDirty() { }
- MeshType mMesh;
- Vector<MaterialType> mMaterials;
- UINT64 mLayer;
- Vector<AABox> mWorldBounds;
- Vector3 mPosition;
- Matrix4 mTransform;
- bool mIsActive;
- };
- /**
- * @copydoc TRenderable
- */
- class BS_EXPORT RenderableCore : public CoreObjectCore, public TRenderable<true>
- {
- public:
- ~RenderableCore();
- /**
- * @brief Gets world bounds of the mesh rendered by this object.
- */
- Bounds getBounds() const;
- /**
- * @brief Returns the type that controls how is this object rendered.
- */
- RenderableType getRenderableType() const { return RenType_LitTextured; }
- /**
- * @brief Sets an ID that can be used for uniquely identifying this handler by the renderer.
- */
- void setRendererId(UINT32 id) { mRendererId = id; }
- /**
- * @brief Retrieves an ID that can be used for uniquely identifying this handler by the renderer.
- */
- UINT32 getRendererId() const { return mRendererId; }
- protected:
- friend class Renderable;
- RenderableCore();
- /**
- * @copydoc CoreObject::initialize
- */
- void initialize() override;
- /**
- * @copydoc CoreObject::syncToCore
- */
- void syncToCore(const CoreSyncData& data) override;
- UINT32 mRendererId;
- };
- /**
- * @copydoc TRenderable
- */
- class BS_EXPORT Renderable : public IReflectable, public CoreObject, public TRenderable<false>, public IResourceListener
- {
- public:
- /**
- * @brief Gets world bounds of the mesh rendered by this object.
- */
- Bounds getBounds() const;
- /**
- * @brief Retrieves an implementation of a renderable handler usable only from the
- * core thread.
- */
- SPtr<RenderableCore> getCore() const;
- /**
- * @brief Returns the hash value that can be used to identify if the internal data needs an update.
- */
- UINT32 _getLastModifiedHash() const { return mLastUpdateHash; }
- /**
- * @brief Sets the hash value that can be used to identify if the internal data needs an update.
- */
- void _setLastModifiedHash(UINT32 hash) { mLastUpdateHash = hash; }
- /**
- * @brief Creates a new renderable handler instance.
- */
- static RenderablePtr create();
- protected:
- Renderable();
- /**
- * @copydoc CoreObject::createCore
- */
- SPtr<CoreObjectCore> createCore() const override;
- /**
- * @copydoc CoreObject::markCoreDirty
- */
- void _markCoreDirty(RenderableDirtyFlag flag = RenderableDirtyFlag::Everything) override;
- /**
- * @copydoc IResourceListener::markResourcesDirty
- */
- void _markResourcesDirty() override;
- /**
- * @copydoc CoreObject::syncToCore
- */
- CoreSyncData syncToCore(FrameAlloc* allocator) override;
- /**
- * @copydoc CoreObject::getCoreDependencies
- */
- void getCoreDependencies(FrameVector<SPtr<CoreObject>>& dependencies) override;
- /**
- * @copydoc IResourceListener::getListenerResources
- */
- void getListenerResources(Vector<HResource>& resources) override;
- /**
- * @copydoc IResourceListener::notifyResourceLoaded
- */
- void notifyResourceLoaded(const HResource& resource) override { markCoreDirty(); }
- /**
- * @copydoc IResourceListener::notifyResourceDestroyed
- */
- void notifyResourceDestroyed(const HResource& resource) override { markCoreDirty(); }
- /**
- * @copydoc IResourceListener::notifyResourceChanged
- */
- void notifyResourceChanged(const HResource& resource) override { markCoreDirty(); }
- /**
- * @brief Creates a new renderable handler instance without initializing it.
- */
- static RenderablePtr createEmpty();
- UINT32 mLastUpdateHash;
- /************************************************************************/
- /* RTTI */
- /************************************************************************/
- public:
- friend class RenderableRTTI;
- static RTTITypeBase* getRTTIStatic();
- virtual RTTITypeBase* getRTTI() const override;
- };
- }
|