BsLight.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 offset at which the shadows are rendered from the shadow caster. Bias value of 0 means
  47. * the shadow will be renderered exactly at the casters position. If your geometry has thin areas this will
  48. * produce an artifact called shadow acne, in which case you can increase the shadow bias value to eliminate it.
  49. * Note that increasing the shadow bias will on the other hand make the shadow be offset from the caster and may
  50. * make the caster appear as if floating (Peter Panning artifact). Neither is perfect, so it is preferable to ensure
  51. * all your geometry has thickness and keep the bias at zero, or even at negative values.
  52. *
  53. * Default value is 0.5. Should be in rough range [-1, 1].
  54. */
  55. void setShadowBias(float bias) { mShadowBias = bias; _markCoreDirty(); }
  56. /** @copydoc setShadowBias() */
  57. float getShadowBias() const { return mShadowBias; }
  58. /** Determines the color emitted by the light. */
  59. void setColor(const Color& color) { mColor = color; _markCoreDirty(); }
  60. /** @copydoc setColor() */
  61. Color getColor() const { return mColor; }
  62. /**
  63. * Range at which the light contribution fades out to zero. Use setUseAutoAttenuation to provide a radius
  64. * automatically dependant on light intensity. The radius will cut-off light contribution and therefore manually set
  65. * very small radius can end up being very physically incorrect.
  66. */
  67. void setAttenuationRadius(float radius);
  68. /** @copydoc setAttenuationRadius */
  69. float getAttenuationRadius() const { return mAttRadius; }
  70. /**
  71. * Radius of the light source. If non-zero then this light represents an area light, otherwise it is a punctual
  72. * light. Area lights have different attenuation then punctual lights, and their appearance in specular reflections
  73. * is realistic. Shape of the area light depends on light type:
  74. * - For directional light the shape is a disc projected on the hemisphere on the sky. This parameter
  75. * represents angular radius (in degrees) of the disk and should be very small (think of how much space the Sun
  76. * takes on the sky - roughly 0.25 degree radius).
  77. * - For radial light the shape is a sphere and the source radius is the radius of the sphere.
  78. * - For spot lights the shape is a disc oriented in the direction of the spot light and the source radius is the
  79. * radius of the disc.
  80. */
  81. void setSourceRadius(float radius);
  82. /** @copydoc setSourceRadius */
  83. float getSourceRadius() const { return mSourceRadius; }
  84. /**
  85. * If enabled the attenuation radius will automatically be controlled in order to provide reasonable light radius,
  86. * depending on its intensity.
  87. */
  88. void setUseAutoAttenuation(bool enabled);
  89. /** @copydoc setUseAutoAttenuation */
  90. bool getUseAutoAttenuation() const { return mAutoAttenuation; }
  91. /**
  92. * Determines the power of the light source. This will be luminous flux for radial & spot lights,
  93. * luminance for directional lights with no area, and illuminance for directional lights with area (non-zero source
  94. * radius).
  95. */
  96. void setIntensity(float intensity);
  97. /** @copydoc setIntensity */
  98. float getIntensity() const { return mIntensity; }
  99. /** Determines the total angle covered by a spot light. */
  100. void setSpotAngle(const Degree& spotAngle) { mSpotAngle = spotAngle; _markCoreDirty(); updateBounds(); }
  101. /** @copydoc setSpotAngle */
  102. Degree getSpotAngle() const { return mSpotAngle; }
  103. /**
  104. * Determines the falloff angle covered by a spot light. Falloff angle determines at what point does light intensity
  105. * starts quadratically falling off as the angle approaches the total spot angle.
  106. */
  107. void setSpotFalloffAngle(const Degree& spotFallofAngle)
  108. { mSpotFalloffAngle = spotFallofAngle; _markCoreDirty(); updateBounds(); }
  109. /** @copydoc setSpotFalloffAngle */
  110. Degree getSpotFalloffAngle() const { return mSpotFalloffAngle; }
  111. /** Returns world space bounds that completely encompass the light's area of influence. */
  112. Sphere getBounds() const { return mBounds; }
  113. /**
  114. * Returns the luminance of the light source. This is the value that should be used in lighting equations.
  115. *
  116. * @note
  117. * For point light sources this method returns luminous intensity and not luminance. We use the same method for both
  118. * as a convenience since in either case its used as a measure of intensity in lighting equations.
  119. */
  120. float getLuminance() const;
  121. protected:
  122. /** Updates the internal bounds for the light. Call this whenever a property affecting the bounds changes. */
  123. void updateBounds();
  124. /** Calculates maximum light range based on light intensity. */
  125. void updateAttenuationRange();
  126. LightType mType; /**< Type of light that determines how are the rest of the parameters interpreted. */
  127. bool mCastsShadows; /**< Determines whether the light casts shadows. */
  128. Color mColor; /**< Color of the light. */
  129. float mAttRadius; /**< Radius at which light intensity falls off to zero. */
  130. float mSourceRadius; /**< Radius of the light source. If > 0 the light is an area light. */
  131. float mIntensity; /**< Power of the light source. @see setIntensity. */
  132. Degree mSpotAngle; /**< Total angle covered by a spot light. */
  133. Degree mSpotFalloffAngle; /**< Spot light angle at which falloff starts. Must be smaller than total angle. */
  134. Sphere mBounds; /**< Sphere that bounds the light area of influence. */
  135. bool mAutoAttenuation; /**< Determines is attenuation radius is automatically determined. */
  136. float mShadowBias; /**< See setShadowBias() */
  137. };
  138. /** @} */
  139. /** @addtogroup Renderer-Internal
  140. * @{
  141. */
  142. namespace ct { class Light; }
  143. /** Illuminates a portion of the scene covered by the light. */
  144. class BS_CORE_EXPORT Light : public IReflectable, public CoreObject, public LightBase
  145. {
  146. public:
  147. /** Retrieves an implementation of the light usable only from the core thread. */
  148. SPtr<ct::Light> getCore() const;
  149. /**
  150. * Creates a new light with provided settings.
  151. *
  152. * @param[in] type Type of light that determines how are the rest of the parameters interpreted.
  153. * @param[in] color Color of the light.
  154. * @param[in] intensity Power of the light source. This will be luminous flux for radial & spot lights,
  155. * luminance for directional lights with no area, and illuminance for directional
  156. * lights with area (non-zero source radius).
  157. * @param[in] attRadius Radius at which light's influence fades out to zero.
  158. * @param[in] castsShadows Determines whether the light casts shadows.
  159. * @param[in] spotAngle Total angle covered by a spot light.
  160. * @param[in] spotFalloffAngle Spot light angle at which falloff starts. Must be smaller than total angle.
  161. */
  162. static SPtr<Light> create(LightType type = LightType::Radial, Color color = Color::White,
  163. float intensity = 100.0f, float attRadius = 10.0f, bool castsShadows = false,
  164. Degree spotAngle = Degree(45), Degree spotFalloffAngle = Degree(40));
  165. protected:
  166. Light(LightType type, Color color, float intensity, float attRadius, float srcRadius,
  167. bool castsShadows, Degree spotAngle, Degree spotFalloffAngle);
  168. /** @copydoc CoreObject::createCore */
  169. SPtr<ct::CoreObject> createCore() const override;
  170. /** @copydoc LightBase::_markCoreDirty */
  171. void _markCoreDirty(ActorDirtyFlag flag = ActorDirtyFlag::Everything) override;
  172. /** @copydoc CoreObject::syncToCore */
  173. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  174. /** Creates a light with without initializing it. Used for serialization. */
  175. static SPtr<Light> createEmpty();
  176. /************************************************************************/
  177. /* RTTI */
  178. /************************************************************************/
  179. public:
  180. friend class LightRTTI;
  181. static RTTITypeBase* getRTTIStatic();
  182. RTTITypeBase* getRTTI() const override;
  183. protected:
  184. Light(); // Serialization only
  185. };
  186. namespace ct
  187. {
  188. /** Core thread usable version of bs::Light. */
  189. class BS_CORE_EXPORT Light : public CoreObject, public LightBase
  190. {
  191. public:
  192. ~Light();
  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. static const UINT32 LIGHT_CONE_NUM_SIDES;
  198. static const UINT32 LIGHT_CONE_NUM_SLICES;
  199. protected:
  200. friend class bs::Light;
  201. Light(LightType type, Color color, float intensity, float attRadius, float srcRadius, bool castsShadows,
  202. Degree spotAngle, Degree spotFalloffAngle);
  203. /** @copydoc CoreObject::initialize */
  204. void initialize() override;
  205. /** @copydoc CoreObject::syncToCore */
  206. void syncToCore(const CoreSyncData& data) override;
  207. UINT32 mRendererId;
  208. };
  209. }
  210. /** @} */
  211. }