Light.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Color.h"
  24. #include "Drawable.h"
  25. #include "Frustum.h"
  26. namespace Urho3D
  27. {
  28. class Camera;
  29. class Texture;
  30. class Texture2D;
  31. class TextureCube;
  32. struct LightBatchQueue;
  33. /// %Light types.
  34. enum LightType
  35. {
  36. LIGHT_DIRECTIONAL = 0,
  37. LIGHT_SPOT,
  38. LIGHT_POINT
  39. };
  40. static const float SHADOW_MIN_QUANTIZE = 0.1f;
  41. static const float SHADOW_MIN_VIEW = 1.0f;
  42. static const int MAX_LIGHT_SPLITS = 6;
  43. #if !defined(ANDROID) && !defined(IOS) && !defined(RASPI)
  44. static const int MAX_CASCADE_SPLITS = 4;
  45. #else
  46. static const int MAX_CASCADE_SPLITS = 1;
  47. #endif
  48. /// Shadow depth bias parameters.
  49. struct URHO3D_API BiasParameters
  50. {
  51. /// Construct undefined.
  52. BiasParameters()
  53. {
  54. }
  55. /// Construct with initial values.
  56. BiasParameters(float constantBias, float slopeScaledBias) :
  57. constantBias_(constantBias),
  58. slopeScaledBias_(slopeScaledBias)
  59. {
  60. }
  61. /// Validate parameters.
  62. void Validate();
  63. /// Constant bias.
  64. float constantBias_;
  65. /// Slope scaled bias.
  66. float slopeScaledBias_;
  67. };
  68. /// Cascaded shadow map parameters.
  69. struct URHO3D_API CascadeParameters
  70. {
  71. /// Construct undefined.
  72. CascadeParameters()
  73. {
  74. }
  75. /// Construct with initial values.
  76. CascadeParameters(float split1, float split2, float split3, float split4, float fadeStart, float biasAutoAdjust = 1.0f) :
  77. fadeStart_(fadeStart),
  78. biasAutoAdjust_(biasAutoAdjust)
  79. {
  80. splits_[0] = split1;
  81. splits_[1] = split2;
  82. splits_[2] = split3;
  83. splits_[3] = split4;
  84. }
  85. /// Validate parameters.
  86. void Validate();
  87. /// Return shadow maximum range.
  88. float GetShadowRange() const
  89. {
  90. float ret = 0.0f;
  91. for (unsigned i = 0; i < MAX_CASCADE_SPLITS; ++i)
  92. ret = Max(ret, splits_[i]);
  93. return ret;
  94. }
  95. /// Far clip values of the splits.
  96. float splits_[4];
  97. /// The point relative to the total shadow range where shadow fade begins (0.0 - 1.0)
  98. float fadeStart_;
  99. /// Automatic depth bias adjustment strength.
  100. float biasAutoAdjust_;
  101. };
  102. /// Shadow map focusing parameters.
  103. struct URHO3D_API FocusParameters
  104. {
  105. /// Construct undefined.
  106. FocusParameters()
  107. {
  108. }
  109. /// Construct with initial values.
  110. FocusParameters(bool focus, bool nonUniform, bool autoSize, float quantize, float minView) :
  111. focus_(focus),
  112. nonUniform_(nonUniform),
  113. autoSize_(autoSize),
  114. quantize_(quantize),
  115. minView_(minView)
  116. {
  117. }
  118. /// Validate parameters.
  119. void Validate();
  120. /// Focus flag.
  121. bool focus_;
  122. /// Non-uniform focusing flag.
  123. bool nonUniform_;
  124. /// Auto-size (reduce resolution when far away) flag.
  125. bool autoSize_;
  126. /// Focus quantization.
  127. float quantize_;
  128. /// Minimum view size.
  129. float minView_;
  130. };
  131. /// %Light component.
  132. class URHO3D_API Light : public Drawable
  133. {
  134. OBJECT(Light);
  135. public:
  136. /// Construct.
  137. Light(Context* context);
  138. /// Destruct.
  139. virtual ~Light();
  140. /// Register object factory. Drawable must be registered first.
  141. static void RegisterObject(Context* context);
  142. /// Handle attribute change.
  143. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  144. /// Process octree raycast. May be called from a worker thread.
  145. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  146. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  147. virtual void UpdateBatches(const FrameInfo& frame);
  148. /// Visualize the component as debug geometry.
  149. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  150. /// Set light type.
  151. void SetLightType(LightType type);
  152. /// Set vertex lighting mode.
  153. void SetPerVertex(bool enable);
  154. /// Set color.
  155. void SetColor(const Color& color);
  156. /// Set specular intensity. Zero disables specular calculations.
  157. void SetSpecularIntensity(float intensity);
  158. /// Set light brightness multiplier. Both the color and specular intensity are multiplied with this to get final values for rendering.
  159. void SetBrightness(float brightness);
  160. /// Set range.
  161. void SetRange(float range);
  162. /// Set spotlight field of view.
  163. void SetFov(float fov);
  164. /// Set spotlight aspect ratio.
  165. void SetAspectRatio(float aspectRatio);
  166. /// Set fade out start distance.
  167. void SetFadeDistance(float distance);
  168. /// Set shadow fade out start distance. Only has effect if shadow distance is also non-zero.
  169. void SetShadowFadeDistance(float distance);
  170. /// Set shadow depth bias parameters.
  171. void SetShadowBias(const BiasParameters& parameters);
  172. /// Set directional light cascaded shadow parameters.
  173. void SetShadowCascade(const CascadeParameters& parameters);
  174. /// Set shadow map focusing parameters.
  175. void SetShadowFocus(const FocusParameters& parameters);
  176. /// Set shadow intensity between 0.0 - 1.0. 0.0 (the default) gives fully dark shadows.
  177. void SetShadowIntensity(float intensity);
  178. /// Set shadow resolution between 0.25 - 1.0. Determines the shadow map to use.
  179. void SetShadowResolution(float resolution);
  180. /// Set shadow camera near/far clip distance ratio.
  181. void SetShadowNearFarRatio(float nearFarRatio);
  182. /// Set range attenuation texture.
  183. void SetRampTexture(Texture* texture);
  184. /// Set spotlight attenuation texture.
  185. void SetShapeTexture(Texture* texture);
  186. /// Return light type.
  187. LightType GetLightType() const { return lightType_; }
  188. /// Return vertex lighting mode.
  189. bool GetPerVertex() const { return perVertex_; }
  190. /// Return color.
  191. const Color& GetColor() const { return color_; }
  192. /// Return specular intensity.
  193. float GetSpecularIntensity() const { return specularIntensity_; }
  194. /// Return brightness multiplier.
  195. float GetBrightness() const { return brightness_; }
  196. /// Return effective color, multiplied by brightness. Do not multiply the alpha so that can compare against the default black color to detect a light with no effect.
  197. Color GetEffectiveColor() const { return Color(color_ * brightness_, 1.0f); }
  198. /// Return effective specular intensity, multiplied by absolute value of brightness.
  199. float GetEffectiveSpecularIntensity() const { return specularIntensity_ * Abs(brightness_); }
  200. /// Return range.
  201. float GetRange() const { return range_; }
  202. /// Return spotlight field of view.
  203. float GetFov() const { return fov_; }
  204. /// Return spotlight aspect ratio.
  205. float GetAspectRatio() const { return aspectRatio_; }
  206. /// Return fade start distance.
  207. float GetFadeDistance() const { return fadeDistance_; }
  208. /// Return shadow fade start distance.
  209. float GetShadowFadeDistance() const { return shadowFadeDistance_; }
  210. /// Return shadow depth bias parameters.
  211. const BiasParameters& GetShadowBias() const { return shadowBias_; }
  212. /// Return directional light cascaded shadow parameters.
  213. const CascadeParameters& GetShadowCascade() const { return shadowCascade_; }
  214. /// Return shadow map focus parameters.
  215. const FocusParameters& GetShadowFocus() const { return shadowFocus_; }
  216. /// Return shadow intensity.
  217. float GetShadowIntensity() const { return shadowIntensity_; }
  218. /// Return shadow resolution.
  219. float GetShadowResolution() const { return shadowResolution_; }
  220. /// Return shadow camera near/far clip distance ratio.
  221. float GetShadowNearFarRatio() const { return shadowNearFarRatio_; }
  222. /// Return range attenuation texture.
  223. Texture* GetRampTexture() const { return rampTexture_; }
  224. /// Return spotlight attenuation texture.
  225. Texture* GetShapeTexture() const { return shapeTexture_; }
  226. /// Return spotlight frustum.
  227. Frustum GetFrustum() const;
  228. /// Return number of shadow map cascade splits for a directional light, considering also graphics API limitations.
  229. int GetNumShadowSplits() const;
  230. /// Return whether light has negative (darkening) color.
  231. bool IsNegative() const { return GetEffectiveColor().SumRGB() < 0.0f; }
  232. /// Set sort value based on intensity and view distance.
  233. void SetIntensitySortValue(float distance);
  234. /// Set sort value based on overall intensity over a bounding box.
  235. void SetIntensitySortValue(const BoundingBox& box);
  236. /// Set light queue used for this light. Called by View.
  237. void SetLightQueue(LightBatchQueue* queue);
  238. /// Return directional light quad transform for either near or far split.
  239. Matrix3x4 GetDirLightTransform(Camera* camera, bool getNearQuad = false);
  240. /// Return light volume model transform.
  241. const Matrix3x4& GetVolumeTransform(Camera* camera);
  242. /// Return light queue. Called by View.
  243. LightBatchQueue* GetLightQueue() const { return lightQueue_; }
  244. /// Return a divisor value based on intensity for calculating the sort value.
  245. float GetIntensityDivisor(float attenuation = 1.0f) const { return Max(GetEffectiveColor().SumRGB(), 0.0f) * attenuation + M_EPSILON; }
  246. /// Set ramp texture attribute.
  247. void SetRampTextureAttr(ResourceRef value);
  248. /// Set shape texture attribute.
  249. void SetShapeTextureAttr(ResourceRef value);
  250. /// Return ramp texture attribute.
  251. ResourceRef GetRampTextureAttr() const;
  252. /// Return shape texture attribute.
  253. ResourceRef GetShapeTextureAttr() const;
  254. protected:
  255. /// Recalculate the world-space bounding box.
  256. virtual void OnWorldBoundingBoxUpdate();
  257. private:
  258. /// Light type.
  259. LightType lightType_;
  260. /// Color.
  261. Color color_;
  262. /// Shadow depth bias parameters.
  263. BiasParameters shadowBias_;
  264. /// Directional light cascaded shadow parameters.
  265. CascadeParameters shadowCascade_;
  266. /// Shadow map focus parameters.
  267. FocusParameters shadowFocus_;
  268. /// Custom world transform for the light volume.
  269. Matrix3x4 volumeTransform_;
  270. /// Range attenuation texture.
  271. SharedPtr<Texture> rampTexture_;
  272. /// Spotlight attenuation texture.
  273. SharedPtr<Texture> shapeTexture_;
  274. /// Light queue.
  275. LightBatchQueue* lightQueue_;
  276. /// Specular intensity.
  277. float specularIntensity_;
  278. /// Brightness multiplier.
  279. float brightness_;
  280. /// Range.
  281. float range_;
  282. /// Spotlight field of view.
  283. float fov_;
  284. /// Spotlight aspect ratio.
  285. float aspectRatio_;
  286. /// Fade start distance.
  287. float fadeDistance_;
  288. /// Shadow fade start distance.
  289. float shadowFadeDistance_;
  290. /// Shadow intensity.
  291. float shadowIntensity_;
  292. /// Shadow resolution.
  293. float shadowResolution_;
  294. /// Shadow camera near/far clip distance ratio.
  295. float shadowNearFarRatio_;
  296. /// Per-vertex lighting flag.
  297. bool perVertex_;
  298. };
  299. inline bool CompareLights(Light* lhs, Light* rhs)
  300. {
  301. // When sorting lights, give priority to per-vertex lights, so that vertex lit base pass can be evaluated first
  302. if (lhs->GetPerVertex() != rhs->GetPerVertex())
  303. return lhs->GetPerVertex();
  304. else
  305. return lhs->GetSortValue() < rhs->GetSortValue();
  306. }
  307. }