2
0

Light.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // Copyright (c) 2008-2013 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)
  44. static const int MAX_CASCADE_SPLITS = 4;
  45. #else
  46. static const int MAX_CASCADE_SPLITS = 2;
  47. #endif
  48. /// Shadow depth bias parameters.
  49. struct 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 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) :
  77. fadeStart_(fadeStart)
  78. {
  79. splits_[0] = split1;
  80. splits_[1] = split2;
  81. #if !defined(ANDROID) && !defined(IOS)
  82. splits_[2] = split3;
  83. splits_[3] = split4;
  84. #endif
  85. }
  86. /// Validate parameters.
  87. void Validate();
  88. /// Return shadow maximum range.
  89. float GetShadowRange() const
  90. {
  91. float ret = 0.0f;
  92. for (unsigned i = 0; i < MAX_CASCADE_SPLITS; ++i)
  93. ret = Max(ret, splits_[i]);
  94. return ret;
  95. }
  96. /// Near clip of first split.
  97. float start_;
  98. /// Far clip values of the splits.
  99. float splits_[MAX_CASCADE_SPLITS];
  100. /// The point relative to final split far clip where fade out begins. (0.0 - 1.0)
  101. float fadeStart_;
  102. };
  103. /// Shadow map focusing parameters.
  104. struct FocusParameters
  105. {
  106. /// Construct undefined.
  107. FocusParameters()
  108. {
  109. }
  110. /// Construct with initial values.
  111. FocusParameters(bool focus, bool nonUniform, bool autoSize, float quantize, float minView) :
  112. focus_(focus),
  113. nonUniform_(nonUniform),
  114. autoSize_(autoSize),
  115. quantize_(quantize),
  116. minView_(minView)
  117. {
  118. }
  119. /// Validate parameters.
  120. void Validate();
  121. /// Focus flag.
  122. bool focus_;
  123. /// Non-uniform focusing flag.
  124. bool nonUniform_;
  125. /// Auto-size (reduce resolution when far away) flag.
  126. bool autoSize_;
  127. /// Focus quantization.
  128. float quantize_;
  129. /// Minimum view size.
  130. float minView_;
  131. };
  132. /// %Light component.
  133. class Light : public Drawable
  134. {
  135. OBJECT(Light);
  136. public:
  137. /// Construct.
  138. Light(Context* context);
  139. /// Destruct.
  140. virtual ~Light();
  141. /// Register object factory. Drawable must be registered first.
  142. static void RegisterObject(Context* context);
  143. /// Handle attribute change.
  144. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  145. /// Process octree raycast. May be called from a worker thread.
  146. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  147. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  148. virtual void UpdateBatches(const FrameInfo& frame);
  149. /// Visualize the component as debug geometry.
  150. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  151. /// Set light type.
  152. void SetLightType(LightType type);
  153. /// Set vertex lighting mode.
  154. void SetPerVertex(bool enable);
  155. /// Set color.
  156. void SetColor(const Color& color);
  157. /// Set specular intensity.
  158. void SetSpecularIntensity(float intensity);
  159. /// Set range.
  160. void SetRange(float range);
  161. /// Set spotlight field of view.
  162. void SetFov(float fov);
  163. /// Set spotlight aspect ratio.
  164. void SetAspectRatio(float aspectRatio);
  165. /// Set fade out start distance.
  166. void SetFadeDistance(float distance);
  167. /// Set shadow fade out start distance. Only has effect if shadow distance is also non-zero.
  168. void SetShadowFadeDistance(float distance);
  169. /// Set shadow depth bias parameters.
  170. void SetShadowBias(const BiasParameters& parameters);
  171. /// Set directional light cascaded shadow parameters.
  172. void SetShadowCascade(const CascadeParameters& parameters);
  173. /// Set shadow map focusing parameters.
  174. void SetShadowFocus(const FocusParameters& parameters);
  175. /// Set shadow intensity between 0.0 - 1.0. 0.0 (the default) gives fully dark shadows.
  176. void SetShadowIntensity(float intensity);
  177. /// Set shadow resolution between 0.25 - 1.0. Determines the shadow map to use.
  178. void SetShadowResolution(float resolution);
  179. /// Set shadow camera near/far clip distance ratio.
  180. void SetShadowNearFarRatio(float nearFarRatio);
  181. /// Set range attenuation texture.
  182. void SetRampTexture(Texture* texture);
  183. /// Set spotlight attenuation texture.
  184. void SetShapeTexture(Texture* texture);
  185. /// Return light type.
  186. LightType GetLightType() const { return lightType_; }
  187. /// Return vertex lighting mode.
  188. bool GetPerVertex() const { return perVertex_; }
  189. /// Return color.
  190. const Color& GetColor() const { return color_; }
  191. /// Return specular intensity.
  192. float GetSpecularIntensity() const { return specularIntensity_; }
  193. /// Return range.
  194. float GetRange() const { return range_; }
  195. /// Return spotlight field of view.
  196. float GetFov() const { return fov_; }
  197. /// Return spotlight aspect ratio.
  198. float GetAspectRatio() const { return aspectRatio_; }
  199. /// Return fade start distance.
  200. float GetFadeDistance() const { return fadeDistance_; }
  201. /// Return shadow fade start distance.
  202. float GetShadowFadeDistance() const { return shadowFadeDistance_; }
  203. /// Return shadow depth bias parameters.
  204. const BiasParameters& GetShadowBias() const { return shadowBias_; }
  205. /// Return directional light cascaded shadow parameters.
  206. const CascadeParameters& GetShadowCascade() const { return shadowCascade_; }
  207. /// Return shadow map focus parameters.
  208. const FocusParameters& GetShadowFocus() const { return shadowFocus_; }
  209. /// Return shadow intensity.
  210. float GetShadowIntensity() const { return shadowIntensity_; }
  211. /// Return shadow resolution.
  212. float GetShadowResolution() const { return shadowResolution_; }
  213. /// Return shadow camera near/far clip distance ratio.
  214. float GetShadowNearFarRatio() const { return shadowNearFarRatio_; }
  215. /// Return range attenuation texture.
  216. Texture* GetRampTexture() const { return rampTexture_; }
  217. /// Return spotlight attenuation texture.
  218. Texture* GetShapeTexture() const { return shapeTexture_; }
  219. /// Return spotlight frustum.
  220. Frustum GetFrustum() const;
  221. /// Set sort value based on intensity and view distance.
  222. void SetIntensitySortValue(float distance);
  223. /// Set sort value based on overall intensity over a bounding box.
  224. void SetIntensitySortValue(const BoundingBox& box);
  225. /// Set light queue used for this light. Called by View.
  226. void SetLightQueue(LightBatchQueue* queue);
  227. /// Return directional light quad transform for either near or far split.
  228. Matrix3x4 GetDirLightTransform(Camera* camera, bool getNearQuad = false);
  229. /// Return light volume model transform.
  230. const Matrix3x4& GetVolumeTransform(Camera* camera);
  231. /// Return light queue. Called by View.
  232. LightBatchQueue* GetLightQueue() const { return lightQueue_; }
  233. /// Set ramp texture attribute.
  234. void SetRampTextureAttr(ResourceRef value);
  235. /// Set shape texture attribute.
  236. void SetShapeTextureAttr(ResourceRef value);
  237. /// Return ramp texture attribute.
  238. ResourceRef GetRampTextureAttr() const;
  239. /// Return shape texture attribute.
  240. ResourceRef GetShapeTextureAttr() const;
  241. protected:
  242. /// Recalculate the world-space bounding box.
  243. virtual void OnWorldBoundingBoxUpdate();
  244. private:
  245. /// Light type.
  246. LightType lightType_;
  247. /// Color.
  248. Color color_;
  249. /// Shadow depth bias parameters.
  250. BiasParameters shadowBias_;
  251. /// Directional light cascaded shadow parameters.
  252. CascadeParameters shadowCascade_;
  253. /// Shadow map focus parameters.
  254. FocusParameters shadowFocus_;
  255. /// Custom world transform for the light volume.
  256. Matrix3x4 volumeTransform_;
  257. /// Range attenuation texture.
  258. SharedPtr<Texture> rampTexture_;
  259. /// Spotlight attenuation texture.
  260. SharedPtr<Texture> shapeTexture_;
  261. /// Light queue.
  262. LightBatchQueue* lightQueue_;
  263. /// Specular intensity.
  264. float specularIntensity_;
  265. /// Range.
  266. float range_;
  267. /// Spotlight field of view.
  268. float fov_;
  269. /// Spotlight aspect ratio.
  270. float aspectRatio_;
  271. /// Fade start distance.
  272. float fadeDistance_;
  273. /// Shadow fade start distance.
  274. float shadowFadeDistance_;
  275. /// Shadow intensity.
  276. float shadowIntensity_;
  277. /// Shadow resolution.
  278. float shadowResolution_;
  279. /// Shadow camera near/far clip distance ratio.
  280. float shadowNearFarRatio_;
  281. /// Per-vertex lighting flag.
  282. bool perVertex_;
  283. };
  284. }