LightComponent.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright (C) 2009-2023, 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/Spatial.h>
  8. #include <AnKi/Math.h>
  9. #include <AnKi/Renderer/RenderQueue.h>
  10. namespace anki {
  11. // Forward
  12. class Frustum;
  13. /// @addtogroup scene
  14. /// @{
  15. enum class LightComponentType : U8
  16. {
  17. kPoint,
  18. kSpot,
  19. kDirectional, ///< Basically the sun.
  20. kCount,
  21. kFirst = 0
  22. };
  23. /// Light component. Contains all the info of lights.
  24. class LightComponent : public SceneComponent
  25. {
  26. ANKI_SCENE_COMPONENT(LightComponent)
  27. public:
  28. LightComponent(SceneNode* node);
  29. ~LightComponent();
  30. void setLightComponentType(LightComponentType type);
  31. LightComponentType getLightComponentType() const
  32. {
  33. return m_type;
  34. }
  35. const Vec4& getDiffuseColor() const
  36. {
  37. return m_diffColor;
  38. }
  39. void setDiffuseColor(const Vec4& x)
  40. {
  41. m_diffColor = x;
  42. }
  43. void setRadius(F32 x)
  44. {
  45. m_point.m_radius = x;
  46. m_shapeUpdated = true;
  47. }
  48. F32 getRadius() const
  49. {
  50. return m_point.m_radius;
  51. }
  52. void setDistance(F32 x)
  53. {
  54. m_spot.m_distance = x;
  55. m_shapeUpdated = true;
  56. }
  57. F32 getDistance() const
  58. {
  59. return m_spot.m_distance;
  60. }
  61. void setInnerAngle(F32 ang)
  62. {
  63. m_spot.m_innerAngle = ang;
  64. m_shapeUpdated = true;
  65. }
  66. F32 getInnerAngle() const
  67. {
  68. return m_spot.m_innerAngle;
  69. }
  70. void setOuterAngle(F32 ang)
  71. {
  72. m_spot.m_outerAngle = ang;
  73. m_shapeUpdated = true;
  74. }
  75. F32 getOuterAngle() const
  76. {
  77. return m_spot.m_outerAngle;
  78. }
  79. Bool getShadowEnabled() const
  80. {
  81. return m_shadow;
  82. }
  83. void setShadowEnabled(const Bool x)
  84. {
  85. m_shadow = x;
  86. m_shapeUpdated = true;
  87. }
  88. ANKI_INTERNAL WeakArray<Frustum> getFrustums() const
  89. {
  90. ANKI_ASSERT(m_shadow);
  91. return WeakArray<Frustum>(m_frustums, m_frustumCount);
  92. }
  93. void setupPointLightQueueElement(PointLightQueueElement& el) const
  94. {
  95. ANKI_ASSERT(m_type == LightComponentType::kPoint);
  96. el.m_uuid = m_uuid;
  97. el.m_worldPosition = m_worldTransform.getOrigin().xyz();
  98. el.m_radius = m_point.m_radius;
  99. el.m_diffuseColor = m_diffColor.xyz();
  100. el.m_shadowLayer = kMaxU8;
  101. el.m_index = m_gpuSceneLightIndex.get();
  102. }
  103. void setupSpotLightQueueElement(SpotLightQueueElement& el) const
  104. {
  105. ANKI_ASSERT(m_type == LightComponentType::kSpot);
  106. el.m_uuid = m_uuid;
  107. el.m_worldTransform = Mat4(m_worldTransform);
  108. el.m_textureMatrix = m_spot.m_textureMat;
  109. el.m_distance = m_spot.m_distance;
  110. el.m_outerAngle = m_spot.m_outerAngle;
  111. el.m_innerAngle = m_spot.m_innerAngle;
  112. el.m_diffuseColor = m_diffColor.xyz();
  113. el.m_edgePoints = m_spot.m_edgePointsWspace;
  114. el.m_shadowLayer = kMaxU8;
  115. el.m_index = m_gpuSceneLightIndex.get();
  116. }
  117. /// Setup a directional queue element.
  118. /// @param[in] cameraFrustum The frustum that is looking that directional light. Used to calculate the cascades.
  119. /// @param[out] el The queue element to fill out.
  120. /// @param[out] cascadeFrustums Fill those frustums as well. The size of this array is the count of the cascades.
  121. void setupDirectionalLightQueueElement(const Frustum& cameraFrustum, DirectionalLightQueueElement& el,
  122. WeakArray<Frustum> cascadeFrustums) const;
  123. private:
  124. U64 m_uuid;
  125. Vec4 m_diffColor = Vec4(0.5f);
  126. Transform m_worldTransform = Transform::getIdentity();
  127. class Point
  128. {
  129. public:
  130. F32 m_radius = 1.0f;
  131. };
  132. class Spot
  133. {
  134. public:
  135. Mat4 m_textureMat = Mat4::getIdentity();
  136. F32 m_distance = 1.0f;
  137. F32 m_outerAngle = toRad(30.0f);
  138. F32 m_innerAngle = toRad(15.0f);
  139. Array<Vec3, 4> m_edgePointsWspace = {};
  140. };
  141. class Dir
  142. {
  143. public:
  144. Vec3 m_sceneMin = Vec3(-1.0f);
  145. Vec3 m_sceneMax = Vec3(1.0f);
  146. };
  147. Point m_point;
  148. Spot m_spot;
  149. Dir m_dir;
  150. Spatial m_spatial;
  151. Frustum* m_frustums = nullptr;
  152. GpuSceneContiguousArrayIndex m_gpuSceneLightIndex;
  153. LightComponentType m_type;
  154. U8 m_shadow : 1 = false;
  155. U8 m_shapeUpdated : 1 = true;
  156. U8 m_typeChanged : 1 = true;
  157. U8 m_frustumCount : 4 = 0; ///< The size of m_frustums array.
  158. Error update(SceneComponentUpdateInfo& info, Bool& updated);
  159. };
  160. /// @}
  161. } // end namespace anki