BsReflectionProbe.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 "BsVector3.h"
  8. #include "BsQuaternion.h"
  9. #include "BsSphere.h"
  10. namespace bs
  11. {
  12. /** @addtogroup Renderer-Engine-Internal
  13. * @{
  14. */
  15. /** Light type that determines how is light information parsed by the renderer and other systems. */
  16. enum class ReflectionProbeType
  17. {
  18. /**
  19. * Reflection probe cubemap is generated, and box extents are used for calculating influence ranges and box
  20. * geometry.
  21. */
  22. Box,
  23. /**
  24. * Reflection probe cubemap is generated, but sphere is used for calculating the influence radius and
  25. * proxy geometry.
  26. */
  27. Sphere,
  28. /**
  29. * Reflection probe 2D texture is generated instead of a cubemap. This texture corresponds to the -Z direction face
  30. * of the cubemap.
  31. */
  32. Plane
  33. };
  34. /** Signals which portion of a reflection probe is dirty. */
  35. enum class ReflectionProbeDirtyFlag
  36. {
  37. Transform = 0x01,
  38. Everything = 0x02
  39. };
  40. /** @} */
  41. /** @addtogroup Implementation
  42. * @{
  43. */
  44. /** Base class for both core and sim thread implementations of a reflection probe. */
  45. class BS_CORE_EXPORT ReflectionProbeBase
  46. {
  47. public:
  48. ReflectionProbeBase();
  49. ReflectionProbeBase(ReflectionProbeType type, float radius, const Vector3& extents);
  50. virtual ~ReflectionProbeBase() { }
  51. /** Returns the position of the probe, in world space. */
  52. Vector3 getPosition() const { return mPosition; }
  53. /** Sets the position of the probe, in world space. */
  54. void setPosition(const Vector3& position)
  55. { mPosition = position; _markCoreDirty(ReflectionProbeDirtyFlag::Transform); updateBounds(); }
  56. /** Returns the rotation of the probe, in world space. */
  57. Quaternion getRotation() const { return mRotation; }
  58. /** Sets the rotation of the probe, in world space. */
  59. void setRotation(const Quaternion& rotation)
  60. { mRotation = rotation; _markCoreDirty(ReflectionProbeDirtyFlag::Transform); updateBounds(); }
  61. /** Returns the type of the probe. */
  62. ReflectionProbeType getType() const { return mType; }
  63. /** Changes the type of the probe. */
  64. void setType(ReflectionProbeType type) { mType = type; _markCoreDirty(); updateBounds(); }
  65. /** Returns the radius of a sphere reflection probe. Determines range of influence. */
  66. float getRadius() const { return mRadius; }
  67. /** Sets the radius of a sphere reflection probe. */
  68. void setRadius(float radius) { mRadius = radius; _markCoreDirty(); updateBounds(); }
  69. /** Returns the extents of a box or plane reflection probe. */
  70. Vector3 getExtents() const { return mExtents; }
  71. /** Sets the extents of a box or a plane reflection probe. Determines range of influence. */
  72. void setExtents(const Vector3& extents) { mExtents = extents; _markCoreDirty(); updateBounds(); }
  73. /** Returns world space bounds that completely encompass the probe's area of influence. */
  74. Sphere getBounds() const { return mBounds; }
  75. /** Checks whether the probe should be used or not. */
  76. bool getIsActive() const { return mIsActive; }
  77. /** Sets whether the probe should be used or not. */
  78. void setIsActive(bool active) { mIsActive = active; _markCoreDirty(); }
  79. /** Returns an identifier that uniquely identifies the probe. */
  80. const String& getUUID() const { return mUUID; }
  81. /**
  82. * Marks the simulation thread object as dirty and notifies the system its data should be synced with its core
  83. * thread counterpart.
  84. */
  85. virtual void _markCoreDirty(ReflectionProbeDirtyFlag flags = ReflectionProbeDirtyFlag::Everything) { }
  86. protected:
  87. /** Updates the internal bounds for the probe. Call this whenever a property affecting the bounds changes. */
  88. void updateBounds();
  89. Vector3 mPosition; /**< World space position. */
  90. Quaternion mRotation; /**< World space rotation. */
  91. ReflectionProbeType mType; /**< Type of probe that determines how are the rest of the parameters interpreted. */
  92. float mRadius; /**< Radius used for sphere reflection probes. */
  93. Vector3 mExtents; /**< Extents used by box & plane reflection probes. */
  94. String mUUID; /**< Identifier that uniquely identifies the probe. */
  95. bool mIsActive; /**< Whether the light should be rendered or not. */
  96. Sphere mBounds; /**< Sphere that bounds the light area of influence. */
  97. };
  98. /** Templated base class for both core and sim thread implementations of a reflection probe. */
  99. template<bool Core>
  100. class BS_CORE_EXPORT TReflectionProbe : public ReflectionProbeBase
  101. {
  102. typedef typename TTextureType<Core>::Type TextureType;
  103. public:
  104. TReflectionProbe();
  105. TReflectionProbe(ReflectionProbeType type, float radius, const Vector3& extents);
  106. virtual ~TReflectionProbe() { }
  107. /**
  108. * Allows you assign a custom texture to use as a reflection map. This will disable automatic generation of
  109. * reflections. To re-enable auto-generation call this with a null parameter.
  110. */
  111. void setCustomTexture(const TextureType& texture) { mCustomTexture = texture; _markCoreDirty(); }
  112. /** Gets the custom texture assigned through setCustomTexture(). */
  113. TextureType getCustomTexture() const { return mCustomTexture; }
  114. /** Forces the reflection probe to regenerate its texture. Call is ignored if the probe uses a custom texture. */
  115. void generate();
  116. protected:
  117. TextureType mCustomTexture;
  118. };
  119. /** @} */
  120. /** @addtogroup Renderer-Engine-Internal
  121. * @{
  122. */
  123. namespace ct { class ReflectionProbe; }
  124. /**
  125. * Specifies a location at which a pre-computed texture containing scene radiance will be generated. This texture will
  126. * then be used by the renderer to provide specular reflections.
  127. */
  128. class BS_CORE_EXPORT ReflectionProbe : public IReflectable, public CoreObject, public TReflectionProbe<false>
  129. {
  130. public:
  131. /** Retrieves an implementation of the reflection probe usable only from the core thread. */
  132. SPtr<ct::ReflectionProbe> getCore() const;
  133. /** Returns the hash value that can be used to identify if the internal data needs an update. */
  134. UINT32 _getLastModifiedHash() const { return mLastUpdateHash; }
  135. /** Sets the hash value that can be used to identify if the internal data needs an update. */
  136. void _setLastModifiedHash(UINT32 hash) { mLastUpdateHash = hash; }
  137. /**
  138. * Updates internal transform values from the specified scene object, in case that scene object's transform changed
  139. * since the last call.
  140. *
  141. * @note Assumes the same scene object will be provided every time.
  142. */
  143. void _updateTransform(const HSceneObject& parent);
  144. /**
  145. * Creates a new sphere reflection probe.
  146. *
  147. * @param[in] radius Radius in which the reflection probe will be rendered within.
  148. * @returns New reflection probe.
  149. */
  150. static SPtr<ReflectionProbe> createSphere(float radius);
  151. /**
  152. * Creates a new box reflection probe.
  153. *
  154. * @param[in] extents Extents of the box in which the reflection probe will be rendered within.
  155. * @returns New reflection probe.
  156. */
  157. static SPtr<ReflectionProbe> createBox(const Vector3& extents);
  158. /**
  159. * Creates a new plane reflection probe.
  160. *
  161. * @param[in] extents Extents of the box in which the reflection probe will be rendered within.
  162. * @returns New reflection probe.
  163. */
  164. static SPtr<ReflectionProbe> createPlane(const Vector3& extents);
  165. protected:
  166. ReflectionProbe(ReflectionProbeType type, float radius, const Vector3& extents);
  167. /** @copydoc CoreObject::createCore */
  168. SPtr<ct::CoreObject> createCore() const override;
  169. /** @copydoc ReflectionProbeBase::_markCoreDirty */
  170. void _markCoreDirty(ReflectionProbeDirtyFlag flags = ReflectionProbeDirtyFlag::Everything) override;
  171. /** @copydoc CoreObject::syncToCore */
  172. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  173. /** Creates a light with without initializing it. Used for serialization. */
  174. static SPtr<ReflectionProbe> createEmpty();
  175. UINT32 mLastUpdateHash;
  176. /************************************************************************/
  177. /* RTTI */
  178. /************************************************************************/
  179. public:
  180. friend class ReflectionProbeRTTI;
  181. static RTTITypeBase* getRTTIStatic();
  182. RTTITypeBase* getRTTI() const override;
  183. protected:
  184. ReflectionProbe(); // Serialization only
  185. };
  186. namespace ct
  187. {
  188. /** Core thread usable version of a bs::ReflectionProbe */
  189. class BS_CORE_EXPORT ReflectionProbe : public CoreObject, public TReflectionProbe<true>
  190. {
  191. public:
  192. ~ReflectionProbe();
  193. /** Sets an ID that can be used for uniquely identifying this object by the renderer. */
  194. void setRendererId(UINT32 id) { mRendererId = id; }
  195. /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */
  196. UINT32 getRendererId() const { return mRendererId; }
  197. protected:
  198. friend class bs::ReflectionProbe;
  199. ReflectionProbe(ReflectionProbeType type, float radius, const Vector3& extents);
  200. /** @copydoc CoreObject::initialize */
  201. void initialize() override;
  202. /** @copydoc CoreObject::syncToCore */
  203. void syncToCore(const CoreSyncData& data) override;
  204. UINT32 mRendererId;
  205. };
  206. }
  207. /** @} */
  208. }