LightComponent.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Scene/Components/SceneComponent.h>
  7. #include <AnKi/Scene/GpuSceneArray.h>
  8. #include <AnKi/Math.h>
  9. #include <AnKi/Collision/Common.h>
  10. namespace anki {
  11. // Forward
  12. class Frustum;
  13. enum class LightComponentType : U8
  14. {
  15. kPoint,
  16. kSpot,
  17. kDirectional, // Basically the sun.
  18. kCount,
  19. kFirst = 0
  20. };
  21. inline constexpr Array<const Char*, U32(LightComponentType::kCount)> kLightComponentTypeNames = {"Point", "Spot", "Directional"};
  22. // Light component. Contains all the info of lights.
  23. class LightComponent : public SceneComponent
  24. {
  25. ANKI_SCENE_COMPONENT(LightComponent)
  26. public:
  27. LightComponent(SceneNode* node);
  28. ~LightComponent();
  29. void setLightComponentType(LightComponentType type);
  30. LightComponentType getLightComponentType() const
  31. {
  32. return m_type;
  33. }
  34. const Vec4& getDiffuseColor() const
  35. {
  36. return m_diffColor;
  37. }
  38. void setDiffuseColor(const Vec4& x)
  39. {
  40. m_diffColor = x;
  41. m_otherDirty = true;
  42. }
  43. F32 getLightPower() const
  44. {
  45. return m_diffColor.xyz.dot(Vec3(0.30f, 0.59f, 0.11f));
  46. }
  47. void setRadius(F32 x)
  48. {
  49. m_point.m_radius = x;
  50. m_shapeDirty = true;
  51. }
  52. F32 getRadius() const
  53. {
  54. return m_point.m_radius;
  55. }
  56. void setDistance(F32 x)
  57. {
  58. m_spot.m_distance = x;
  59. m_shapeDirty = true;
  60. }
  61. F32 getDistance() const
  62. {
  63. return m_spot.m_distance;
  64. }
  65. void setInnerAngle(F32 ang)
  66. {
  67. m_spot.m_innerAngle = ang;
  68. m_shapeDirty = true;
  69. }
  70. F32 getInnerAngle() const
  71. {
  72. return m_spot.m_innerAngle;
  73. }
  74. void setOuterAngle(F32 ang)
  75. {
  76. m_spot.m_outerAngle = ang;
  77. m_shapeDirty = true;
  78. }
  79. F32 getOuterAngle() const
  80. {
  81. return m_spot.m_outerAngle;
  82. }
  83. Bool getShadowEnabled() const
  84. {
  85. return m_shadow;
  86. }
  87. void setShadowEnabled(const Bool x)
  88. {
  89. if(x != m_shadow)
  90. {
  91. m_shadow = x;
  92. m_shapeDirty = m_otherDirty = true;
  93. }
  94. }
  95. Vec3 getDirection() const
  96. {
  97. return -m_worldTransform.getRotation().getZAxis().xyz;
  98. }
  99. Vec3 getWorldPosition() const
  100. {
  101. return m_worldTransform.getOrigin().xyz;
  102. }
  103. const Mat4& getSpotLightViewProjectionMatrix() const
  104. {
  105. ANKI_ASSERT(m_type == LightComponentType::kSpot);
  106. return m_spot.m_viewProjMat;
  107. }
  108. const Mat3x4& getSpotLightViewMatrix() const
  109. {
  110. ANKI_ASSERT(m_type == LightComponentType::kSpot);
  111. return m_spot.m_viewMat;
  112. }
  113. // Set the direction of the directional light by setting the date and hour.
  114. void setDirectionFromTimeOfDay(I32 month, I32 day, F32 hour)
  115. {
  116. ANKI_EXPECT(month >= 0 && month < 12);
  117. ANKI_EXPECT(day >= 0 && day < 31);
  118. ANKI_EXPECT(hour >= 0.0f && hour <= 24.0f);
  119. m_dir.m_month = clamp(month, 0, 11);
  120. m_dir.m_day = clamp(day, 0, 30);
  121. m_dir.m_hour = clamp(hour, 0.0f, 24.0f);
  122. m_shapeDirty = true;
  123. }
  124. // Get the fields which might or might not have come from the direction of the light
  125. void getTimeOfDay(I32& month, I32& day, F32& hour) const
  126. {
  127. month = m_dir.m_month;
  128. day = m_dir.m_day;
  129. hour = m_dir.m_hour;
  130. }
  131. // Calculate some matrices for each cascade. For dir lights.
  132. // cameraFrustum Who is looking at the light.
  133. // cascadeDistances The distances of the cascades.
  134. // cascadeProjMats View projection matrices for each cascade.
  135. // cascadeViewMats View matrices for each cascade. Optional.
  136. void computeCascadeFrustums(const Frustum& cameraFrustum, ConstWeakArray<F32> cascadeDistances, WeakArray<Mat4> cascadeProjMats,
  137. WeakArray<Mat3x4> cascadeViewMats = {},
  138. WeakArray<Array<F32, U32(FrustumPlaneType::kCount)>> cascadePlanes = {}) const;
  139. ANKI_INTERNAL void setShadowAtlasUvViewports(ConstWeakArray<Vec4> viewports);
  140. const GpuSceneArrays::Light::Allocation& getGpuSceneLightAllocation() const
  141. {
  142. return m_gpuSceneLight;
  143. }
  144. private:
  145. Vec4 m_diffColor = Vec4(0.5f);
  146. Transform m_worldTransform = Transform::getIdentity();
  147. class Point
  148. {
  149. public:
  150. F32 m_radius = 1.0f;
  151. };
  152. class Spot
  153. {
  154. public:
  155. Mat3x4 m_viewMat = Mat3x4::getIdentity();
  156. Mat4 m_viewProjMat = Mat4::getIdentity();
  157. F32 m_distance = 1.0f;
  158. F32 m_outerAngle = toRad(30.0f);
  159. F32 m_innerAngle = toRad(15.0f);
  160. };
  161. class Dir
  162. {
  163. public:
  164. I32 m_month = -1;
  165. I32 m_day = -1;
  166. F32 m_hour = -1.0;
  167. };
  168. Point m_point;
  169. Spot m_spot;
  170. Dir m_dir;
  171. GpuSceneArrays::Light::Allocation m_gpuSceneLight;
  172. GpuSceneArrays::LightVisibleRenderablesHash::Allocation m_hash;
  173. Array<Vec4, 6> m_shadowAtlasUvViewports;
  174. LightComponentType m_type;
  175. U8 m_shadow : 1 = false;
  176. U8 m_shapeDirty : 1 = true;
  177. U8 m_otherDirty : 1 = true;
  178. U8 m_shadowAtlasUvViewportCount : 3 = 0;
  179. void update(SceneComponentUpdateInfo& info, Bool& updated) override;
  180. Error serialize(SceneSerializer& serializer) override;
  181. };
  182. } // end namespace anki