LightComponent.h 4.9 KB

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