Light.h 11 KB

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