BsReflectionProbe.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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-Internal
  13. * @{
  14. */
  15. /** Light type that determines how is light information parsed by the renderer and other systems. */
  16. enum BS_SCRIPT_EXPORT() 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. /** Signals which portion of a reflection probe is dirty. */
  30. enum class ReflectionProbeDirtyFlag
  31. {
  32. Transform = 0x01,
  33. Everything = 0x02
  34. };
  35. /** @} */
  36. /** @addtogroup Implementation
  37. * @{
  38. */
  39. /** Base class for both core and sim thread implementations of a reflection probe. */
  40. class BS_CORE_EXPORT ReflectionProbeBase
  41. {
  42. public:
  43. ReflectionProbeBase();
  44. ReflectionProbeBase(ReflectionProbeType type, float radius, const Vector3& extents);
  45. virtual ~ReflectionProbeBase() { }
  46. /** Returns the position of the probe, in world space. */
  47. Vector3 getPosition() const { return mPosition; }
  48. /** Sets the position of the probe, in world space. */
  49. void setPosition(const Vector3& position)
  50. { mPosition = position; _markCoreDirty(ReflectionProbeDirtyFlag::Transform); updateBounds(); }
  51. /** Returns the rotation of the probe, in world space. */
  52. Quaternion getRotation() const { return mRotation; }
  53. /** Sets the rotation of the probe, in world space. */
  54. void setRotation(const Quaternion& rotation)
  55. { mRotation = rotation; _markCoreDirty(ReflectionProbeDirtyFlag::Transform); updateBounds(); }
  56. /** Returns the scale of the probe. */
  57. Vector3 getScale() const { return mScale; }
  58. /** Sets the scale of the probe. */
  59. void setScale(const Vector3& scale)
  60. { mScale = scale; _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 * std::max(std::max(mScale.x, mScale.y), mScale.z); }
  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 reflection probe. */
  70. Vector3 getExtents() const { return mExtents * mScale; }
  71. /** Sets the extents of a box 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. /**
  76. * Sets a distance that will be used for fading out the box reflection probe with distance. By default it
  77. * is equal to one, and can never be less than one. Only relevant for box probes.
  78. */
  79. void setTransitionDistance(float distance) { mTransitionDistance = std::max(1.0f, distance); }
  80. /** Retrieves transition distance set by setTransitionDistance(). */
  81. float getTransitionDistance() const { return mTransitionDistance; }
  82. /** Checks whether the probe should be used or not. */
  83. bool getIsActive() const { return mIsActive; }
  84. /** Sets whether the probe should be used or not. */
  85. void setIsActive(bool active) { mIsActive = active; _markCoreDirty(); }
  86. /** Returns an identifier that uniquely identifies the probe. */
  87. const String& getUUID() const { return mUUID; }
  88. /**
  89. * Marks the simulation thread object as dirty and notifies the system its data should be synced with its core
  90. * thread counterpart.
  91. */
  92. virtual void _markCoreDirty(ReflectionProbeDirtyFlag flags = ReflectionProbeDirtyFlag::Everything) { }
  93. protected:
  94. /** Updates the internal bounds for the probe. Call this whenever a property affecting the bounds changes. */
  95. void updateBounds();
  96. Vector3 mPosition; /**< World space position. */
  97. Quaternion mRotation; /**< World space rotation. */
  98. Vector3 mScale; /** Scale applied to radius/extents. */
  99. ReflectionProbeType mType; /**< Type of probe that determines how are the rest of the parameters interpreted. */
  100. float mRadius; /**< Radius used for sphere reflection probes. */
  101. Vector3 mExtents; /**< Extents used by box reflection probe. */
  102. float mTransitionDistance; /**< Extra distance to used for fading out box probes. */
  103. String mUUID; /**< Identifier that uniquely identifies the probe. */
  104. bool mIsActive; /**< Whether the light should be rendered or not. */
  105. Sphere mBounds; /**< Sphere that bounds the light area of influence. */
  106. };
  107. /** @} */
  108. /** @addtogroup Renderer-Internal
  109. * @{
  110. */
  111. namespace ct
  112. {
  113. class RendererTask;
  114. class ReflectionProbe;
  115. }
  116. /**
  117. * Specifies a location at which a pre-computed texture containing scene radiance will be generated. This texture will
  118. * then be used by the renderer to provide specular reflections.
  119. */
  120. class BS_CORE_EXPORT ReflectionProbe : public IReflectable, public CoreObject, public ReflectionProbeBase
  121. {
  122. public:
  123. ~ReflectionProbe();
  124. /**
  125. * Allows you assign a custom texture to use as a reflection map. This will disable automatic generation of
  126. * reflections. To re-enable auto-generation call this with a null parameter.
  127. */
  128. void setCustomTexture(const HTexture& texture) { mCustomTexture = texture; filter(); }
  129. /** Gets the custom texture assigned through setCustomTexture(). */
  130. HTexture getCustomTexture() const { return mCustomTexture; }
  131. /**
  132. * Returns a pre-filtered texture that is generated either from the provided custom texture, or from scene capture.
  133. */
  134. SPtr<Texture> getFilteredTexture() const { return mFilteredTexture; }
  135. /**
  136. * Captures the scene at the current location and generates a filtered reflection cubemap. No action is taken
  137. * if a custom texture is set.
  138. */
  139. void capture();
  140. /**
  141. * Filters the custom texture, making it usable for rendering. Called automatically when custom texture changes. If
  142. * no custom texture is set, no action is taken.
  143. */
  144. void filter();
  145. /** Retrieves an implementation of the reflection probe usable only from the core thread. */
  146. SPtr<ct::ReflectionProbe> getCore() const;
  147. /** Returns the hash value that can be used to identify if the internal data needs an update. */
  148. UINT32 _getLastModifiedHash() const { return mLastUpdateHash; }
  149. /** Sets the hash value that can be used to identify if the internal data needs an update. */
  150. void _setLastModifiedHash(UINT32 hash) { mLastUpdateHash = hash; }
  151. /**
  152. * Updates internal transform values from the specified scene object, in case that scene object's transform changed
  153. * since the last call.
  154. *
  155. * @note Assumes the same scene object will be provided every time.
  156. */
  157. void _updateTransform(const HSceneObject& parent);
  158. /**
  159. * Creates a new sphere reflection probe.
  160. *
  161. * @param[in] radius Radius in which the reflection probe will be rendered within.
  162. * @returns New reflection probe.
  163. */
  164. static SPtr<ReflectionProbe> createSphere(float radius);
  165. /**
  166. * Creates a new box reflection probe.
  167. *
  168. * @param[in] extents Extents of the box in which the reflection probe will be rendered within.
  169. * @returns New reflection probe.
  170. */
  171. static SPtr<ReflectionProbe> createBox(const Vector3& extents);
  172. protected:
  173. ReflectionProbe(ReflectionProbeType type, float radius, const Vector3& extents);
  174. /** @copydoc CoreObject::createCore */
  175. SPtr<ct::CoreObject> createCore() const override;
  176. /** @copydoc ReflectionProbeBase::_markCoreDirty */
  177. void _markCoreDirty(ReflectionProbeDirtyFlag flags = ReflectionProbeDirtyFlag::Everything) override;
  178. /** @copydoc CoreObject::syncToCore */
  179. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  180. /**
  181. * Captures the scene color at current probe location and generates a filtered map. If a custom texture is set then
  182. * it will be filtered, instead of capturing scene color.
  183. */
  184. void captureAndFilter();
  185. /** Creates a light with without initializing it. Used for serialization. */
  186. static SPtr<ReflectionProbe> createEmpty();
  187. UINT32 mLastUpdateHash;
  188. HTexture mCustomTexture;
  189. SPtr<ct::RendererTask> mRendererTask;
  190. SPtr<Texture> mFilteredTexture;
  191. /************************************************************************/
  192. /* RTTI */
  193. /************************************************************************/
  194. public:
  195. friend class ReflectionProbeRTTI;
  196. static RTTITypeBase* getRTTIStatic();
  197. RTTITypeBase* getRTTI() const override;
  198. protected:
  199. ReflectionProbe(); // Serialization only
  200. };
  201. namespace ct
  202. {
  203. /** Core thread usable version of a bs::ReflectionProbe */
  204. class BS_CORE_EXPORT ReflectionProbe : public CoreObject, public ReflectionProbeBase
  205. {
  206. public:
  207. ~ReflectionProbe();
  208. /** Sets an ID that can be used for uniquely identifying this object by the renderer. */
  209. void setRendererId(UINT32 id) { mRendererId = id; }
  210. /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */
  211. UINT32 getRendererId() const { return mRendererId; }
  212. /**
  213. * Returns a pre-filtered texture that is generated either from the provided custom texture, or from scene capture.
  214. */
  215. SPtr<Texture> getFilteredTexture() const { return mFilteredTexture; }
  216. protected:
  217. friend class bs::ReflectionProbe;
  218. ReflectionProbe(ReflectionProbeType type, float radius, const Vector3& extents,
  219. const SPtr<Texture>& filteredTexture);
  220. /** @copydoc CoreObject::initialize */
  221. void initialize() override;
  222. /** @copydoc CoreObject::syncToCore */
  223. void syncToCore(const CoreSyncData& data) override;
  224. UINT32 mRendererId;
  225. SPtr<Texture> mFilteredTexture;
  226. };
  227. }
  228. /** @} */
  229. }