LightComponent.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright (C) 2009-2021, 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/Math.h>
  7. #include <AnKi/Renderer/RenderQueue.h>
  8. #include <AnKi/Scene/Components/SceneComponent.h>
  9. namespace anki {
  10. /// @addtogroup scene
  11. /// @{
  12. enum class LightComponentType : U8
  13. {
  14. POINT,
  15. SPOT,
  16. DIRECTIONAL, ///< Basically the sun.
  17. COUNT,
  18. FIRST = 0
  19. };
  20. /// Light component. Contains all the info of lights.
  21. class LightComponent : public SceneComponent
  22. {
  23. ANKI_SCENE_COMPONENT(LightComponent)
  24. public:
  25. LightComponent(SceneNode* node);
  26. ~LightComponent()
  27. {
  28. }
  29. void setLightComponentType(LightComponentType type)
  30. {
  31. ANKI_ASSERT(type >= LightComponentType::FIRST && type < LightComponentType::COUNT);
  32. m_type = type;
  33. m_markedForUpdate = true;
  34. }
  35. LightComponentType getLightComponentType() const
  36. {
  37. return m_type;
  38. }
  39. void setWorldTransform(const Transform& trf)
  40. {
  41. m_worldtransform = trf;
  42. m_markedForUpdate = true;
  43. }
  44. const Transform& getWorldTransform() const
  45. {
  46. return m_worldtransform;
  47. }
  48. const Vec4& getDiffuseColor() const
  49. {
  50. return m_diffColor;
  51. }
  52. void setDiffuseColor(const Vec4& x)
  53. {
  54. m_diffColor = x;
  55. }
  56. void setRadius(F32 x)
  57. {
  58. m_point.m_radius = x;
  59. m_markedForUpdate = true;
  60. }
  61. F32 getRadius() const
  62. {
  63. return m_point.m_radius;
  64. }
  65. void setDistance(F32 x)
  66. {
  67. m_spot.m_distance = x;
  68. m_markedForUpdate = true;
  69. }
  70. F32 getDistance() const
  71. {
  72. return m_spot.m_distance;
  73. }
  74. void setInnerAngle(F32 ang)
  75. {
  76. m_spot.m_innerAngleCos = cos(ang / 2.0f);
  77. m_spot.m_innerAngle = ang;
  78. m_markedForUpdate = true;
  79. }
  80. F32 getInnerAngleCos() const
  81. {
  82. return m_spot.m_innerAngleCos;
  83. }
  84. F32 getInnerAngle() const
  85. {
  86. return m_spot.m_innerAngle;
  87. }
  88. void setOuterAngle(F32 ang)
  89. {
  90. m_spot.m_outerAngleCos = cos(ang / 2.0f);
  91. m_spot.m_outerAngle = ang;
  92. m_markedForUpdate = true;
  93. }
  94. F32 getOuterAngle() const
  95. {
  96. return m_spot.m_outerAngle;
  97. }
  98. F32 getOuterAngleCos() const
  99. {
  100. return m_spot.m_outerAngleCos;
  101. }
  102. Bool getShadowEnabled() const
  103. {
  104. return m_shadow;
  105. }
  106. void setShadowEnabled(const Bool x)
  107. {
  108. m_shadow = x;
  109. }
  110. ANKI_USE_RESULT Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override;
  111. void setupPointLightQueueElement(PointLightQueueElement& el) const
  112. {
  113. ANKI_ASSERT(m_type == LightComponentType::POINT);
  114. el.m_uuid = m_uuid;
  115. el.m_worldPosition = m_worldtransform.getOrigin().xyz();
  116. el.m_radius = m_point.m_radius;
  117. el.m_diffuseColor = m_diffColor.xyz();
  118. el.m_debugDrawCallback = [](RenderQueueDrawContext& ctx, ConstWeakArray<void*> userData) {
  119. ANKI_ASSERT(userData.getSize() == 1);
  120. static_cast<const LightComponent*>(userData[0])->draw(ctx);
  121. };
  122. el.m_debugDrawCallbackUserData = this;
  123. el.m_shadowLayer = MAX_U8;
  124. }
  125. void setupSpotLightQueueElement(SpotLightQueueElement& el) const
  126. {
  127. ANKI_ASSERT(m_type == LightComponentType::SPOT);
  128. el.m_uuid = m_uuid;
  129. el.m_worldTransform = Mat4(m_worldtransform);
  130. el.m_textureMatrix = m_spot.m_textureMat;
  131. el.m_distance = m_spot.m_distance;
  132. el.m_outerAngle = m_spot.m_outerAngle;
  133. el.m_innerAngle = m_spot.m_innerAngle;
  134. el.m_diffuseColor = m_diffColor.xyz();
  135. el.m_edgePoints = m_spot.m_edgePointsWspace;
  136. el.m_debugDrawCallback = [](RenderQueueDrawContext& ctx, ConstWeakArray<void*> userData) {
  137. ANKI_ASSERT(userData.getSize() == 1);
  138. static_cast<const LightComponent*>(userData[0])->draw(ctx);
  139. };
  140. el.m_debugDrawCallbackUserData = this;
  141. el.m_shadowLayer = MAX_U8;
  142. }
  143. /// Setup a directional queue element.
  144. /// @param[in] frustumComp The frustum that is looking that directional light. Used to calculate the cascades.
  145. /// @param[out] el The queue element to fill out.
  146. /// @param[out] cascadeFrustumComponents Fill those frustums as well. The size of this array is the count of the
  147. /// cascades.
  148. void setupDirectionalLightQueueElement(const FrustumComponent& frustumComp, DirectionalLightQueueElement& el,
  149. WeakArray<FrustumComponent> cascadeFrustumComponents) const;
  150. private:
  151. SceneNode* m_node = nullptr;
  152. U64 m_uuid;
  153. Vec4 m_diffColor = Vec4(0.5f);
  154. Transform m_worldtransform = Transform::getIdentity();
  155. class Point
  156. {
  157. public:
  158. F32 m_radius = 1.0f;
  159. };
  160. class Spot
  161. {
  162. public:
  163. Mat4 m_textureMat = Mat4::getIdentity();
  164. F32 m_distance = 1.0f;
  165. F32 m_outerAngle = toRad(30.0f);
  166. F32 m_innerAngle = toRad(15.0f);
  167. F32 m_outerAngleCos = cos(m_outerAngle / 2.0f);
  168. F32 m_innerAngleCos = cos(m_innerAngle / 2.0f);
  169. Array<Vec3, 4> m_edgePointsWspace = {};
  170. };
  171. class Dir
  172. {
  173. public:
  174. Vec3 m_sceneMin = Vec3(-1.0f);
  175. Vec3 m_sceneMax = Vec3(1.0f);
  176. };
  177. Point m_point;
  178. Spot m_spot;
  179. Dir m_dir;
  180. ImageResourcePtr m_pointDebugImage;
  181. ImageResourcePtr m_spotDebugImage;
  182. LightComponentType m_type;
  183. U8 m_shadow : 1;
  184. U8 m_markedForUpdate : 1;
  185. void draw(RenderQueueDrawContext& ctx) const;
  186. };
  187. /// @}
  188. } // end namespace anki