BsReflectionProbe.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "Math/BsVector3.h"
  8. #include "Math/BsQuaternion.h"
  9. #include "Math/BsSphere.h"
  10. #include "Scene/BsSceneActor.h"
  11. namespace bs
  12. {
  13. /** @addtogroup Renderer-Internal
  14. * @{
  15. */
  16. /** Light type that determines how is light information parsed by the renderer and other systems. */
  17. enum BS_SCRIPT_EXPORT(m:Rendering) class ReflectionProbeType
  18. {
  19. /**
  20. * Reflection probe cubemap is generated, and box extents are used for calculating influence ranges and box
  21. * geometry.
  22. */
  23. Box,
  24. /**
  25. * Reflection probe cubemap is generated, but sphere is used for calculating the influence radius and
  26. * proxy geometry.
  27. */
  28. Sphere
  29. };
  30. /** @} */
  31. /** @addtogroup Implementation
  32. * @{
  33. */
  34. /** Base class for both core and sim thread implementations of a reflection probe. */
  35. class BS_CORE_EXPORT ReflectionProbeBase : public SceneActor
  36. {
  37. public:
  38. ReflectionProbeBase();
  39. ReflectionProbeBase(ReflectionProbeType type, float radius, const Vector3& extents);
  40. virtual ~ReflectionProbeBase() { }
  41. /** Returns the type of the probe. */
  42. ReflectionProbeType getType() const { return mType; }
  43. /** Changes the type of the probe. */
  44. void setType(ReflectionProbeType type) { mType = type; _markCoreDirty(); updateBounds(); }
  45. /** Returns the radius of a sphere reflection probe. Determines range of influence. */
  46. float getRadius() const;
  47. /** Sets the radius of a sphere reflection probe. */
  48. void setRadius(float radius) { mRadius = radius; _markCoreDirty(); updateBounds(); }
  49. /** Returns the extents of a box reflection probe. */
  50. Vector3 getExtents() const { return mExtents * mTransform.getScale(); }
  51. /** Sets the extents of a box reflection probe. Determines range of influence. */
  52. void setExtents(const Vector3& extents) { mExtents = extents; _markCoreDirty(); updateBounds(); }
  53. /** Returns world space bounds that completely encompass the probe's area of influence. */
  54. Sphere getBounds() const { return mBounds; }
  55. /**
  56. * Sets a distance that will be used for fading out the box reflection probe with distance. By default it
  57. * is equal to one, and can never be less than one. Only relevant for box probes.
  58. */
  59. void setTransitionDistance(float distance) { mTransitionDistance = std::max(1.0f, distance); }
  60. /** Retrieves transition distance set by setTransitionDistance(). */
  61. float getTransitionDistance() const { return mTransitionDistance; }
  62. /** Returns an identifier that uniquely identifies the probe. */
  63. const String& getUUID() const { return mUUID; }
  64. protected:
  65. /** Updates the internal bounds for the probe. Call this whenever a property affecting the bounds changes. */
  66. void updateBounds();
  67. ReflectionProbeType mType; /**< Type of probe that determines how are the rest of the parameters interpreted. */
  68. float mRadius; /**< Radius used for sphere reflection probes. */
  69. Vector3 mExtents; /**< Extents used by box reflection probe. */
  70. float mTransitionDistance; /**< Extra distance to used for fading out box probes. */
  71. String mUUID; /**< Identifier that uniquely identifies the probe. */
  72. Sphere mBounds; /**< Sphere that bounds the light area of influence. */
  73. };
  74. /** @} */
  75. /** @addtogroup Renderer-Internal
  76. * @{
  77. */
  78. namespace ct
  79. {
  80. class RendererTask;
  81. class ReflectionProbe;
  82. }
  83. /**
  84. * Specifies a location at which a pre-computed texture containing scene radiance will be generated. This texture will
  85. * then be used by the renderer to provide specular reflections.
  86. */
  87. class BS_CORE_EXPORT ReflectionProbe : public IReflectable, public CoreObject, public ReflectionProbeBase
  88. {
  89. public:
  90. ~ReflectionProbe();
  91. /**
  92. * Allows you assign a custom texture to use as a reflection map. This will disable automatic generation of
  93. * reflections. To re-enable auto-generation call this with a null parameter.
  94. */
  95. void setCustomTexture(const HTexture& texture) { mCustomTexture = texture; filter(); }
  96. /** Gets the custom texture assigned through setCustomTexture(). */
  97. HTexture getCustomTexture() const { return mCustomTexture; }
  98. /**
  99. * Returns a pre-filtered texture that is generated either from the provided custom texture, or from scene capture.
  100. */
  101. SPtr<Texture> getFilteredTexture() const { return mFilteredTexture; }
  102. /**
  103. * Captures the scene at the current location and generates a filtered reflection cubemap. No action is taken
  104. * if a custom texture is set.
  105. */
  106. void capture();
  107. /**
  108. * Filters the custom texture, making it usable for rendering. Called automatically when custom texture changes. If
  109. * no custom texture is set, no action is taken.
  110. */
  111. void filter();
  112. /** Retrieves an implementation of the reflection probe usable only from the core thread. */
  113. SPtr<ct::ReflectionProbe> getCore() const;
  114. /**
  115. * Creates a new sphere reflection probe.
  116. *
  117. * @param[in] radius Radius in which the reflection probe will be rendered within.
  118. * @returns New reflection probe.
  119. */
  120. static SPtr<ReflectionProbe> createSphere(float radius);
  121. /**
  122. * Creates a new box reflection probe.
  123. *
  124. * @param[in] extents Extents of the box in which the reflection probe will be rendered within.
  125. * @returns New reflection probe.
  126. */
  127. static SPtr<ReflectionProbe> createBox(const Vector3& extents);
  128. protected:
  129. ReflectionProbe(ReflectionProbeType type, float radius, const Vector3& extents);
  130. /** @copydoc CoreObject::createCore */
  131. SPtr<ct::CoreObject> createCore() const override;
  132. /** @copydoc ReflectionProbeBase::_markCoreDirty */
  133. void _markCoreDirty(ActorDirtyFlag flags = ActorDirtyFlag::Everything) override;
  134. /** @copydoc CoreObject::syncToCore */
  135. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  136. /**
  137. * Captures the scene color at current probe location and generates a filtered map. If a custom texture is set then
  138. * it will be filtered, instead of capturing scene color.
  139. */
  140. void captureAndFilter();
  141. /** Creates a light with without initializing it. Used for serialization. */
  142. static SPtr<ReflectionProbe> createEmpty();
  143. HTexture mCustomTexture;
  144. SPtr<ct::RendererTask> mRendererTask;
  145. SPtr<Texture> mFilteredTexture;
  146. /************************************************************************/
  147. /* RTTI */
  148. /************************************************************************/
  149. public:
  150. friend class ReflectionProbeRTTI;
  151. static RTTITypeBase* getRTTIStatic();
  152. RTTITypeBase* getRTTI() const override;
  153. protected:
  154. ReflectionProbe(); // Serialization only
  155. };
  156. namespace ct
  157. {
  158. /** Core thread usable version of a bs::ReflectionProbe */
  159. class BS_CORE_EXPORT ReflectionProbe : public CoreObject, public ReflectionProbeBase
  160. {
  161. public:
  162. ~ReflectionProbe();
  163. /** Sets an ID that can be used for uniquely identifying this object by the renderer. */
  164. void setRendererId(UINT32 id) { mRendererId = id; }
  165. /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */
  166. UINT32 getRendererId() const { return mRendererId; }
  167. /**
  168. * Returns a pre-filtered texture that is generated either from the provided custom texture, or from scene capture.
  169. */
  170. SPtr<Texture> getFilteredTexture() const { return mFilteredTexture; }
  171. protected:
  172. friend class bs::ReflectionProbe;
  173. ReflectionProbe(ReflectionProbeType type, float radius, const Vector3& extents,
  174. const SPtr<Texture>& filteredTexture);
  175. /** @copydoc CoreObject::initialize */
  176. void initialize() override;
  177. /** @copydoc CoreObject::syncToCore */
  178. void syncToCore(const CoreSyncData& data) override;
  179. UINT32 mRendererId;
  180. SPtr<Texture> mFilteredTexture;
  181. };
  182. }
  183. /** @} */
  184. }