BsReflectionProbe.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. protected:
  63. /** Updates the internal bounds for the probe. Call this whenever a property affecting the bounds changes. */
  64. void updateBounds();
  65. ReflectionProbeType mType; /**< Type of probe that determines how are the rest of the parameters interpreted. */
  66. float mRadius; /**< Radius used for sphere reflection probes. */
  67. Vector3 mExtents; /**< Extents used by box reflection probe. */
  68. float mTransitionDistance; /**< Extra distance to used for fading out box probes. */
  69. Sphere mBounds; /**< Sphere that bounds the light area of influence. */
  70. };
  71. /** @} */
  72. /** @addtogroup Renderer-Internal
  73. * @{
  74. */
  75. namespace ct
  76. {
  77. class RendererTask;
  78. class ReflectionProbe;
  79. }
  80. /**
  81. * Specifies a location at which a pre-computed texture containing scene radiance will be generated. This texture will
  82. * then be used by the renderer to provide specular reflections.
  83. */
  84. class BS_CORE_EXPORT ReflectionProbe : public IReflectable, public CoreObject, public ReflectionProbeBase
  85. {
  86. public:
  87. ~ReflectionProbe();
  88. /**
  89. * Allows you assign a custom texture to use as a reflection map. This will disable automatic generation of
  90. * reflections. To re-enable auto-generation call this with a null parameter.
  91. */
  92. void setCustomTexture(const HTexture& texture) { mCustomTexture = texture; filter(); }
  93. /** Gets the custom texture assigned through setCustomTexture(). */
  94. HTexture getCustomTexture() const { return mCustomTexture; }
  95. /**
  96. * Returns a pre-filtered texture that is generated either from the provided custom texture, or from scene capture.
  97. */
  98. SPtr<Texture> getFilteredTexture() const { return mFilteredTexture; }
  99. /**
  100. * Captures the scene at the current location and generates a filtered reflection cubemap. No action is taken
  101. * if a custom texture is set.
  102. */
  103. void capture();
  104. /**
  105. * Filters the custom texture, making it usable for rendering. Called automatically when custom texture changes. If
  106. * no custom texture is set, no action is taken.
  107. */
  108. void filter();
  109. /** Retrieves an implementation of the reflection probe usable only from the core thread. */
  110. SPtr<ct::ReflectionProbe> getCore() const;
  111. /**
  112. * Creates a new sphere reflection probe.
  113. *
  114. * @param[in] radius Radius in which the reflection probe will be rendered within.
  115. * @returns New reflection probe.
  116. */
  117. static SPtr<ReflectionProbe> createSphere(float radius);
  118. /**
  119. * Creates a new box reflection probe.
  120. *
  121. * @param[in] extents Extents of the box in which the reflection probe will be rendered within.
  122. * @returns New reflection probe.
  123. */
  124. static SPtr<ReflectionProbe> createBox(const Vector3& extents);
  125. protected:
  126. ReflectionProbe(ReflectionProbeType type, float radius, const Vector3& extents);
  127. /** @copydoc CoreObject::createCore */
  128. SPtr<ct::CoreObject> createCore() const override;
  129. /** @copydoc ReflectionProbeBase::_markCoreDirty */
  130. void _markCoreDirty(ActorDirtyFlag flags = ActorDirtyFlag::Everything) override;
  131. /** @copydoc CoreObject::syncToCore */
  132. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  133. /**
  134. * Captures the scene color at current probe location and generates a filtered map. If a custom texture is set then
  135. * it will be filtered, instead of capturing scene color.
  136. */
  137. void captureAndFilter();
  138. /** Creates a light with without initializing it. Used for serialization. */
  139. static SPtr<ReflectionProbe> createEmpty();
  140. HTexture mCustomTexture;
  141. SPtr<ct::RendererTask> mRendererTask;
  142. SPtr<Texture> mFilteredTexture;
  143. /************************************************************************/
  144. /* RTTI */
  145. /************************************************************************/
  146. public:
  147. friend class ReflectionProbeRTTI;
  148. static RTTITypeBase* getRTTIStatic();
  149. RTTITypeBase* getRTTI() const override;
  150. protected:
  151. ReflectionProbe(); // Serialization only
  152. };
  153. namespace ct
  154. {
  155. /** Core thread usable version of a bs::ReflectionProbe */
  156. class BS_CORE_EXPORT ReflectionProbe : public CoreObject, public ReflectionProbeBase
  157. {
  158. public:
  159. ~ReflectionProbe();
  160. /** Sets an ID that can be used for uniquely identifying this object by the renderer. */
  161. void setRendererId(UINT32 id) { mRendererId = id; }
  162. /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */
  163. UINT32 getRendererId() const { return mRendererId; }
  164. /**
  165. * Returns a pre-filtered texture that is generated either from the provided custom texture, or from scene capture.
  166. */
  167. SPtr<Texture> getFilteredTexture() const { return mFilteredTexture; }
  168. protected:
  169. friend class bs::ReflectionProbe;
  170. ReflectionProbe(ReflectionProbeType type, float radius, const Vector3& extents,
  171. const SPtr<Texture>& filteredTexture);
  172. /** @copydoc CoreObject::initialize */
  173. void initialize() override;
  174. /** @copydoc CoreObject::syncToCore */
  175. void syncToCore(const CoreSyncData& data) override;
  176. UINT32 mRendererId;
  177. SPtr<Texture> mFilteredTexture;
  178. };
  179. }
  180. /** @} */
  181. }