BsLight.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "Math/BsVector3.h"
  7. #include "Math/BsQuaternion.h"
  8. #include "Image/BsColor.h"
  9. #include "Math/BsSphere.h"
  10. #include "CoreThread/BsCoreObject.h"
  11. #include "Scene/BsSceneActor.h"
  12. namespace bs
  13. {
  14. /** @addtogroup Renderer-Internal
  15. * @{
  16. */
  17. /** Light type that determines how is light information parsed by the renderer and other systems. */
  18. enum BS_SCRIPT_EXPORT(m:Rendering) class LightType
  19. {
  20. Directional,
  21. Radial,
  22. Spot,
  23. Count BS_SCRIPT_EXPORT(ex:true) // Keep at end
  24. };
  25. /** @} */
  26. /** @addtogroup Implementation
  27. * @{
  28. */
  29. /** Base class for both sim and core thread Light implementations. */
  30. class BS_CORE_EXPORT LightBase : public SceneActor
  31. {
  32. public:
  33. LightBase();
  34. LightBase(LightType type, Color color, float intensity, float attRadius, float srcRadius,
  35. bool castsShadows, Degree spotAngle, Degree spotFalloffAngle);
  36. virtual ~LightBase() { }
  37. /** Determines the type of the light. */
  38. LightType getType() const { return mType; }
  39. /** @copydoc getType() */
  40. void setType(LightType type) { mType = type; _markCoreDirty(); updateBounds(); }
  41. /** Determines does this light cast shadows when rendered. */
  42. void setCastsShadow(bool castsShadow) { mCastsShadows = castsShadow; _markCoreDirty(); }
  43. /** @copydoc setCastsShadow */
  44. bool getCastsShadow() const { return mCastsShadows; }
  45. /**
  46. * Shadow bias determines shadow accuracy. Low bias values mean that shadows start closer near their caster surface
  47. * but will result in more shadowing artifacts (shadow acne). Larger values reduce shadow acne but caster may appear
  48. * as floating on air as nearby part of the shadow is cut off (peter paning).
  49. *
  50. * Default value is 0.5. Must be in range [0, 1].
  51. */
  52. void setShadowBias(float bias) { mShadowBias = std::max(std::min(bias, 1.0f), 0.0f); _markCoreDirty(); }
  53. /** @copydoc setShadowBias() */
  54. float getShadowBias() const { return mShadowBias; }
  55. /** Determines the color emitted by the light. */
  56. void setColor(const Color& color) { mColor = color; _markCoreDirty(); }
  57. /** @copydoc setColor() */
  58. Color getColor() const { return mColor; }
  59. /**
  60. * Range at which the light contribution fades out to zero. Use setUseAutoAttenuation to provide a radius
  61. * automatically dependant on light intensity. The radius will cut-off light contribution and therefore manually set
  62. * very small radius can end up being very physically incorrect.
  63. */
  64. void setAttenuationRadius(float radius);
  65. /** @copydoc setAttenuationRadius */
  66. float getAttenuationRadius() const { return mAttRadius; }
  67. /**
  68. * Radius of the light source. If non-zero then this light represents an area light, otherwise it is a punctual
  69. * light. Area lights have different attenuation then punctual lights, and their appearance in specular reflections
  70. * is realistic. Shape of the area light depends on light type:
  71. * - For directional light the shape is a disc projected on the hemisphere on the sky. This parameter
  72. * represents angular radius (in degrees) of the disk and should be very small (think of how much space the Sun
  73. * takes on the sky - roughly 0.25 degree radius).
  74. * - For radial light the shape is a sphere and the source radius is the radius of the sphere.
  75. * - For spot lights the shape is a disc oriented in the direction of the spot light and the source radius is the
  76. * radius of the disc.
  77. */
  78. void setSourceRadius(float radius);
  79. /** @copydoc setSourceRadius */
  80. float getSourceRadius() const { return mSourceRadius; }
  81. /**
  82. * If enabled the attenuation radius will automatically be controlled in order to provide reasonable light radius,
  83. * depending on its intensity.
  84. */
  85. void setUseAutoAttenuation(bool enabled);
  86. /** @copydoc setUseAutoAttenuation */
  87. bool getUseAutoAttenuation() const { return mAutoAttenuation; }
  88. /**
  89. * Determines the power of the light source. This will be luminous flux for radial & spot lights,
  90. * luminance for directional lights with no area, and illuminance for directional lights with area (non-zero source
  91. * radius).
  92. */
  93. void setIntensity(float intensity);
  94. /** @copydoc setIntensity */
  95. float getIntensity() const { return mIntensity; }
  96. /** Determines the total angle covered by a spot light. */
  97. void setSpotAngle(const Degree& spotAngle) { mSpotAngle = spotAngle; _markCoreDirty(); updateBounds(); }
  98. /** @copydoc setSpotAngle */
  99. Degree getSpotAngle() const { return mSpotAngle; }
  100. /**
  101. * Determines the falloff angle covered by a spot light. Falloff angle determines at what point does light intensity
  102. * starts quadratically falling off as the angle approaches the total spot angle.
  103. */
  104. void setSpotFalloffAngle(const Degree& spotFallofAngle)
  105. { mSpotFalloffAngle = spotFallofAngle; _markCoreDirty(); updateBounds(); }
  106. /** @copydoc setSpotFalloffAngle */
  107. Degree getSpotFalloffAngle() const { return mSpotFalloffAngle; }
  108. /** Returns world space bounds that completely encompass the light's area of influence. */
  109. Sphere getBounds() const { return mBounds; }
  110. /**
  111. * Returns the luminance of the light source. This is the value that should be used in lighting equations.
  112. *
  113. * @note
  114. * For point light sources this method returns luminous intensity and not luminance. We use the same method for both
  115. * as a convenience since in either case its used as a measure of intensity in lighting equations.
  116. */
  117. float getLuminance() const;
  118. protected:
  119. /** Updates the internal bounds for the light. Call this whenever a property affecting the bounds changes. */
  120. void updateBounds();
  121. /** Calculates maximum light range based on light intensity. */
  122. void updateAttenuationRange();
  123. LightType mType; /**< Type of light that determines how are the rest of the parameters interpreted. */
  124. bool mCastsShadows; /**< Determines whether the light casts shadows. */
  125. Color mColor; /**< Color of the light. */
  126. float mAttRadius; /**< Radius at which light intensity falls off to zero. */
  127. float mSourceRadius; /**< Radius of the light source. If > 0 the light is an area light. */
  128. float mIntensity; /**< Power of the light source. @see setIntensity. */
  129. Degree mSpotAngle; /**< Total angle covered by a spot light. */
  130. Degree mSpotFalloffAngle; /**< Spot light angle at which falloff starts. Must be smaller than total angle. */
  131. Sphere mBounds; /**< Sphere that bounds the light area of influence. */
  132. bool mAutoAttenuation; /**< Determines is attenuation radius is automatically determined. */
  133. float mShadowBias; /**< See setShadowBias() */
  134. };
  135. /** @} */
  136. /** @addtogroup Renderer-Internal
  137. * @{
  138. */
  139. namespace ct { class Light; }
  140. /** Illuminates a portion of the scene covered by the light. */
  141. class BS_CORE_EXPORT Light : public IReflectable, public CoreObject, public LightBase
  142. {
  143. public:
  144. /** Retrieves an implementation of the light usable only from the core thread. */
  145. SPtr<ct::Light> getCore() const;
  146. /**
  147. * Creates a new light with provided settings.
  148. *
  149. * @param[in] type Type of light that determines how are the rest of the parameters interpreted.
  150. * @param[in] color Color of the light.
  151. * @param[in] intensity Power of the light source. This will be luminous flux for radial & spot lights,
  152. * luminance for directional lights with no area, and illuminance for directional
  153. * lights with area (non-zero source radius).
  154. * @param[in] attRadius Radius at which light's influence fades out to zero.
  155. * @param[in] castsShadows Determines whether the light casts shadows.
  156. * @param[in] spotAngle Total angle covered by a spot light.
  157. * @param[in] spotFalloffAngle Spot light angle at which falloff starts. Must be smaller than total angle.
  158. */
  159. static SPtr<Light> create(LightType type = LightType::Radial, Color color = Color::White,
  160. float intensity = 100.0f, float attRadius = 10.0f, bool castsShadows = false,
  161. Degree spotAngle = Degree(45), Degree spotFalloffAngle = Degree(40));
  162. protected:
  163. Light(LightType type, Color color, float intensity, float attRadius, float srcRadius,
  164. bool castsShadows, Degree spotAngle, Degree spotFalloffAngle);
  165. /** @copydoc CoreObject::createCore */
  166. SPtr<ct::CoreObject> createCore() const override;
  167. /** @copydoc LightBase::_markCoreDirty */
  168. void _markCoreDirty(ActorDirtyFlag flag = ActorDirtyFlag::Everything) override;
  169. /** @copydoc CoreObject::syncToCore */
  170. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  171. /** Creates a light with without initializing it. Used for serialization. */
  172. static SPtr<Light> createEmpty();
  173. /************************************************************************/
  174. /* RTTI */
  175. /************************************************************************/
  176. public:
  177. friend class LightRTTI;
  178. static RTTITypeBase* getRTTIStatic();
  179. RTTITypeBase* getRTTI() const override;
  180. protected:
  181. Light(); // Serialization only
  182. };
  183. namespace ct
  184. {
  185. /** Core thread usable version of bs::Light. */
  186. class BS_CORE_EXPORT Light : public CoreObject, public LightBase
  187. {
  188. public:
  189. ~Light();
  190. /** Sets an ID that can be used for uniquely identifying this object by the renderer. */
  191. void setRendererId(UINT32 id) { mRendererId = id; }
  192. /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */
  193. UINT32 getRendererId() const { return mRendererId; }
  194. static const UINT32 LIGHT_CONE_NUM_SIDES;
  195. static const UINT32 LIGHT_CONE_NUM_SLICES;
  196. protected:
  197. friend class bs::Light;
  198. Light(LightType type, Color color, float intensity, float attRadius, float srcRadius, bool castsShadows,
  199. Degree spotAngle, Degree spotFalloffAngle);
  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. }