Light.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Graphics/Drawable.h"
  6. #include "../GraphicsAPI/Texture.h"
  7. #include "../Math/Color.h"
  8. #include "../Math/Frustum.h"
  9. namespace Urho3D
  10. {
  11. class Camera;
  12. struct LightBatchQueue;
  13. /// %Light types.
  14. enum LightType
  15. {
  16. LIGHT_DIRECTIONAL = 0,
  17. LIGHT_SPOT,
  18. LIGHT_POINT
  19. };
  20. inline constexpr float SHADOW_MIN_QUANTIZE = 0.1f;
  21. inline constexpr float SHADOW_MIN_VIEW = 1.0f;
  22. inline constexpr i32 MAX_LIGHT_SPLITS = 6;
  23. #ifdef DESKTOP_GRAPHICS
  24. inline constexpr i32 MAX_CASCADE_SPLITS = 4;
  25. #else
  26. inline constexpr i32 MAX_CASCADE_SPLITS = 1;
  27. #endif
  28. /// Depth bias parameters. Used both by lights (for shadow mapping) and materials.
  29. struct URHO3D_API BiasParameters
  30. {
  31. /// Construct undefined.
  32. BiasParameters() = default;
  33. /// Construct with initial values.
  34. BiasParameters(float constantBias, float slopeScaledBias, float normalOffset = 0.0f) :
  35. constantBias_(constantBias),
  36. slopeScaledBias_(slopeScaledBias),
  37. normalOffset_(normalOffset)
  38. {
  39. }
  40. /// Validate parameters.
  41. void Validate();
  42. /// Constant bias.
  43. float constantBias_;
  44. /// Slope scaled bias.
  45. float slopeScaledBias_;
  46. /// Normal offset multiplier.
  47. float normalOffset_;
  48. };
  49. /// Cascaded shadow map parameters.
  50. /// @pod
  51. struct URHO3D_API CascadeParameters
  52. {
  53. /// Construct undefined.
  54. CascadeParameters() = default;
  55. /// Construct with initial values.
  56. CascadeParameters(float split1, float split2, float split3, float split4, float fadeStart, float biasAutoAdjust = 1.0f) :
  57. fadeStart_(fadeStart),
  58. biasAutoAdjust_(biasAutoAdjust)
  59. {
  60. splits_[0] = split1;
  61. splits_[1] = split2;
  62. splits_[2] = split3;
  63. splits_[3] = split4;
  64. }
  65. /// Validate parameters.
  66. void Validate();
  67. /// Return shadow maximum range.
  68. float GetShadowRange() const
  69. {
  70. float ret = 0.0f;
  71. for (unsigned i = 0; i < MAX_CASCADE_SPLITS; ++i)
  72. ret = Max(ret, splits_[i]);
  73. return ret;
  74. }
  75. /// Far clip values of the splits.
  76. Vector4 splits_;
  77. /// The point relative to the total shadow range where shadow fade begins (0.0 - 1.0).
  78. float fadeStart_{};
  79. /// Automatic depth bias adjustment strength.
  80. float biasAutoAdjust_{};
  81. };
  82. /// Shadow map focusing parameters.
  83. /// @pod
  84. struct URHO3D_API FocusParameters
  85. {
  86. /// Construct undefined.
  87. FocusParameters() = default;
  88. /// Construct with initial values.
  89. FocusParameters(bool focus, bool nonUniform, bool autoSize, float quantize, float minView) :
  90. focus_(focus),
  91. nonUniform_(nonUniform),
  92. autoSize_(autoSize),
  93. quantize_(quantize),
  94. minView_(minView)
  95. {
  96. }
  97. /// Validate parameters.
  98. void Validate();
  99. /// Focus flag.
  100. bool focus_;
  101. /// Non-uniform focusing flag.
  102. bool nonUniform_;
  103. /// Auto-size (reduce resolution when far away) flag.
  104. bool autoSize_;
  105. /// Focus quantization.
  106. float quantize_;
  107. /// Minimum view size.
  108. float minView_;
  109. };
  110. /// %Light component.
  111. class URHO3D_API Light : public Drawable
  112. {
  113. URHO3D_OBJECT(Light, Drawable);
  114. public:
  115. /// Construct.
  116. explicit Light(Context* context);
  117. /// Destruct.
  118. ~Light() override;
  119. /// Register object factory. Drawable must be registered first.
  120. /// @nobind
  121. static void RegisterObject(Context* context);
  122. /// Process octree raycast. May be called from a worker thread.
  123. void ProcessRayQuery(const RayOctreeQuery& query, Vector<RayQueryResult>& results) override;
  124. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  125. void UpdateBatches(const FrameInfo& frame) override;
  126. /// Visualize the component as debug geometry.
  127. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) override;
  128. /// Set light type.
  129. /// @property
  130. void SetLightType(LightType type);
  131. /// Set vertex lighting mode.
  132. /// @property
  133. void SetPerVertex(bool enable);
  134. /// Set color.
  135. /// @property
  136. void SetColor(const Color& color);
  137. /// Set temperature of the light in Kelvin. Modulates the light color when "use physical values" is enabled.
  138. /// @property
  139. void SetTemperature(float temperature);
  140. /// Set area light radius. Greater than zero activates area light mode. Works only with PBR shaders.
  141. /// @property
  142. void SetRadius(float radius);
  143. /// Set tube area light length. Works only with PBR shaders.
  144. /// @property
  145. void SetLength(float length);
  146. /// Set use physical light values.
  147. /// @property
  148. void SetUsePhysicalValues(bool enable);
  149. /// Set specular intensity. Zero disables specular calculations.
  150. /// @property
  151. void SetSpecularIntensity(float intensity);
  152. /// Set light brightness multiplier. Both the color and specular intensity are multiplied with this. When "use physical values" is enabled, the value is specified in lumens.
  153. /// @property
  154. void SetBrightness(float brightness);
  155. /// Set range.
  156. /// @property
  157. void SetRange(float range);
  158. /// Set spotlight field of view.
  159. /// @property
  160. void SetFov(float fov);
  161. /// Set spotlight aspect ratio.
  162. /// @property
  163. void SetAspectRatio(float aspectRatio);
  164. /// Set fade out start distance.
  165. /// @property
  166. void SetFadeDistance(float distance);
  167. /// Set shadow fade out start distance. Only has effect if shadow distance is also non-zero.
  168. /// @property
  169. void SetShadowFadeDistance(float distance);
  170. /// Set shadow depth bias parameters.
  171. /// @property
  172. void SetShadowBias(const BiasParameters& parameters);
  173. /// Set directional light cascaded shadow parameters.
  174. /// @property
  175. void SetShadowCascade(const CascadeParameters& parameters);
  176. /// Set shadow map focusing parameters.
  177. /// @property
  178. void SetShadowFocus(const FocusParameters& parameters);
  179. /// Set light intensity in shadow between 0.0 - 1.0. 0.0 (the default) gives fully dark shadows.
  180. /// @property
  181. void SetShadowIntensity(float intensity);
  182. /// Set shadow resolution between 0.25 - 1.0. Determines the shadow map to use.
  183. /// @property
  184. void SetShadowResolution(float resolution);
  185. /// Set shadow camera near/far clip distance ratio for spot and point lights. Does not affect directional lights, since they are orthographic and have near clip 0.
  186. /// @property
  187. void SetShadowNearFarRatio(float nearFarRatio);
  188. /// Set maximum shadow extrusion for directional lights. The actual extrusion will be the smaller of this and camera far clip. Default 1000.
  189. /// @property
  190. void SetShadowMaxExtrusion(float extrusion);
  191. /// Set range attenuation texture.
  192. /// @property
  193. void SetRampTexture(Texture* texture);
  194. /// Set spotlight attenuation texture.
  195. /// @property
  196. void SetShapeTexture(Texture* texture);
  197. /// Return light type.
  198. /// @property
  199. LightType GetLightType() const { return lightType_; }
  200. /// Return vertex lighting mode.
  201. /// @property
  202. bool GetPerVertex() const { return perVertex_; }
  203. /// Return color.
  204. /// @property
  205. const Color& GetColor() const { return color_; }
  206. /// Return the temperature of the light in Kelvin.
  207. /// @property
  208. float GetTemperature() const { return temperature_; }
  209. /// Return area light mode radius. Works only with PBR shaders.
  210. /// @property
  211. float GetRadius() const { return lightRad_; }
  212. /// Return area tube light length. Works only with PBR shaders.
  213. /// @property
  214. float GetLength() const { return lightLength_; }
  215. /// Return if light uses temperature and brightness in lumens.
  216. /// @property
  217. bool GetUsePhysicalValues() const { return usePhysicalValues_; }
  218. /// Return the color value of the temperature in Kelvin.
  219. /// @property
  220. Color GetColorFromTemperature() const;
  221. /// Return specular intensity.
  222. /// @property
  223. float GetSpecularIntensity() const { return specularIntensity_; }
  224. /// Return brightness multiplier. Specified in lumens when "use physical values" is enabled.
  225. /// @property
  226. float GetBrightness() const { return brightness_; }
  227. /// Return effective color, multiplied by brightness and affected by temperature when "use physical values" is enabled. Alpha is always 1 so that can compare against the default black color to detect a light with no effect.
  228. /// @property
  229. Color GetEffectiveColor() const;
  230. /// Return effective specular intensity, multiplied by absolute value of brightness.
  231. /// @property
  232. float GetEffectiveSpecularIntensity() const { return specularIntensity_ * Abs(brightness_); }
  233. /// Return range.
  234. /// @property
  235. float GetRange() const { return range_; }
  236. /// Return spotlight field of view.
  237. /// @property
  238. float GetFov() const { return fov_; }
  239. /// Return spotlight aspect ratio.
  240. /// @property
  241. float GetAspectRatio() const { return aspectRatio_; }
  242. /// Return fade start distance.
  243. /// @property
  244. float GetFadeDistance() const { return fadeDistance_; }
  245. /// Return shadow fade start distance.
  246. /// @property
  247. float GetShadowFadeDistance() const { return shadowFadeDistance_; }
  248. /// Return shadow depth bias parameters.
  249. /// @property
  250. const BiasParameters& GetShadowBias() const { return shadowBias_; }
  251. /// Return directional light cascaded shadow parameters.
  252. /// @property
  253. const CascadeParameters& GetShadowCascade() const { return shadowCascade_; }
  254. /// Return shadow map focus parameters.
  255. /// @property
  256. const FocusParameters& GetShadowFocus() const { return shadowFocus_; }
  257. /// Return light intensity in shadow.
  258. /// @property
  259. float GetShadowIntensity() const { return shadowIntensity_; }
  260. /// Return shadow resolution.
  261. /// @property
  262. float GetShadowResolution() const { return shadowResolution_; }
  263. /// Return shadow camera near/far clip distance ratio.
  264. /// @property
  265. float GetShadowNearFarRatio() const { return shadowNearFarRatio_; }
  266. /// Return maximum shadow extrusion distance for directional lights.
  267. /// @property
  268. float GetShadowMaxExtrusion() const { return shadowMaxExtrusion_; }
  269. /// Return range attenuation texture.
  270. /// @property
  271. Texture* GetRampTexture() const { return rampTexture_; }
  272. /// Return spotlight attenuation texture.
  273. /// @property
  274. Texture* GetShapeTexture() const { return shapeTexture_; }
  275. /// Return spotlight frustum.
  276. /// @property
  277. Frustum GetFrustum() const;
  278. /// Return spotlight frustum in the specified view space.
  279. Frustum GetViewSpaceFrustum(const Matrix3x4& view) const;
  280. /// Return number of shadow map cascade splits for a directional light, considering also graphics API limitations.
  281. /// @property
  282. i32 GetNumShadowSplits() const;
  283. /// Return whether light has negative (darkening) color.
  284. /// @property
  285. bool IsNegative() const { return GetEffectiveColor().SumRGB() < 0.0f; }
  286. /// Set sort value based on intensity and view distance.
  287. void SetIntensitySortValue(float distance);
  288. /// Set sort value based on overall intensity over a bounding box.
  289. void SetIntensitySortValue(const BoundingBox& box);
  290. /// Set light queue used for this light. Called by View.
  291. void SetLightQueue(LightBatchQueue* queue);
  292. /// Return light volume model transform.
  293. const Matrix3x4& GetVolumeTransform(Camera* camera);
  294. /// Return light queue. Called by View.
  295. LightBatchQueue* GetLightQueue() const { return lightQueue_; }
  296. /// Return a divisor value based on intensity for calculating the sort value.
  297. float GetIntensityDivisor(float attenuation = 1.0f) const
  298. {
  299. return Max(GetEffectiveColor().SumRGB(), 0.0f) * attenuation + M_EPSILON;
  300. }
  301. /// Set ramp texture attribute.
  302. void SetRampTextureAttr(const ResourceRef& value);
  303. /// Set shape texture attribute.
  304. void SetShapeTextureAttr(const ResourceRef& value);
  305. /// Return ramp texture attribute.
  306. ResourceRef GetRampTextureAttr() const;
  307. /// Return shape texture attribute.
  308. ResourceRef GetShapeTextureAttr() const;
  309. /// Return a transform for deferred fullscreen quad (directional light) rendering.
  310. static Matrix3x4 GetFullscreenQuadTransform(Camera* camera);
  311. protected:
  312. /// Recalculate the world-space bounding box.
  313. void OnWorldBoundingBoxUpdate() override;
  314. private:
  315. /// Validate shadow focus.
  316. void ValidateShadowFocus() { shadowFocus_.Validate(); }
  317. /// Validate shadow cascade.
  318. void ValidateShadowCascade() { shadowCascade_.Validate(); }
  319. /// Validate shadow bias.
  320. void ValidateShadowBias() { shadowBias_.Validate(); }
  321. /// Light type.
  322. LightType lightType_;
  323. /// Color.
  324. Color color_;
  325. /// Light temperature.
  326. float temperature_;
  327. /// Radius of the light source. If above 0 it will turn the light into an area light. Works only with PBR shaders.
  328. float lightRad_;
  329. /// Length of the light source. If above 0 and radius is above 0 it will create a tube light. Works only with PBR shaders.
  330. float lightLength_;
  331. /// Shadow depth bias parameters.
  332. BiasParameters shadowBias_;
  333. /// Directional light cascaded shadow parameters.
  334. CascadeParameters shadowCascade_;
  335. /// Shadow map focus parameters.
  336. FocusParameters shadowFocus_;
  337. /// Custom world transform for the light volume.
  338. Matrix3x4 volumeTransform_;
  339. /// Range attenuation texture.
  340. SharedPtr<Texture> rampTexture_;
  341. /// Spotlight attenuation texture.
  342. SharedPtr<Texture> shapeTexture_;
  343. /// Light queue.
  344. LightBatchQueue* lightQueue_;
  345. /// Specular intensity.
  346. float specularIntensity_;
  347. /// Brightness multiplier.
  348. float brightness_;
  349. /// Range.
  350. float range_;
  351. /// Spotlight field of view.
  352. float fov_;
  353. /// Spotlight aspect ratio.
  354. float aspectRatio_;
  355. /// Fade start distance.
  356. float fadeDistance_;
  357. /// Shadow fade start distance.
  358. float shadowFadeDistance_;
  359. /// Light intensity in shadow.
  360. float shadowIntensity_;
  361. /// Shadow resolution.
  362. float shadowResolution_;
  363. /// Shadow camera near/far clip distance ratio.
  364. float shadowNearFarRatio_;
  365. /// Directional shadow max. extrusion distance.
  366. float shadowMaxExtrusion_;
  367. /// Per-vertex lighting flag.
  368. bool perVertex_;
  369. /// Use physical light values flag.
  370. bool usePhysicalValues_;
  371. };
  372. inline bool CompareLights(Light* lhs, Light* rhs)
  373. {
  374. // When sorting lights, give priority to per-vertex lights, so that vertex lit base pass can be evaluated first
  375. if (lhs->GetPerVertex() != rhs->GetPerVertex())
  376. return lhs->GetPerVertex();
  377. else
  378. return lhs->GetSortValue() < rhs->GetSortValue();
  379. }
  380. }