LightComponent.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. /// @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. m_otherDirty = true;
  43. }
  44. F32 getLightPower() const
  45. {
  46. return m_diffColor.xyz().dot(Vec3(0.30f, 0.59f, 0.11f));
  47. }
  48. void setRadius(F32 x)
  49. {
  50. m_point.m_radius = x;
  51. m_shapeDirty = true;
  52. }
  53. F32 getRadius() const
  54. {
  55. return m_point.m_radius;
  56. }
  57. void setDistance(F32 x)
  58. {
  59. m_spot.m_distance = x;
  60. m_shapeDirty = true;
  61. }
  62. F32 getDistance() const
  63. {
  64. return m_spot.m_distance;
  65. }
  66. void setInnerAngle(F32 ang)
  67. {
  68. m_spot.m_innerAngle = ang;
  69. m_shapeDirty = true;
  70. }
  71. F32 getInnerAngle() const
  72. {
  73. return m_spot.m_innerAngle;
  74. }
  75. void setOuterAngle(F32 ang)
  76. {
  77. m_spot.m_outerAngle = ang;
  78. m_shapeDirty = true;
  79. }
  80. F32 getOuterAngle() const
  81. {
  82. return m_spot.m_outerAngle;
  83. }
  84. Bool getShadowEnabled() const
  85. {
  86. return m_shadow;
  87. }
  88. void setShadowEnabled(const Bool x)
  89. {
  90. if(x != m_shadow)
  91. {
  92. m_shadow = x;
  93. m_shapeDirty = m_otherDirty = true;
  94. }
  95. }
  96. Vec3 getDirection() const
  97. {
  98. return -m_worldTransform.getRotation().getZAxis().xyz();
  99. }
  100. Vec3 getWorldPosition() const
  101. {
  102. return m_worldTransform.getOrigin().xyz();
  103. }
  104. const Mat4& getSpotLightViewProjectionMatrix() const
  105. {
  106. ANKI_ASSERT(m_type == LightComponentType::kSpot);
  107. return m_spot.m_viewProjMat;
  108. }
  109. const Mat3x4& getSpotLightViewMatrix() const
  110. {
  111. ANKI_ASSERT(m_type == LightComponentType::kSpot);
  112. return m_spot.m_viewMat;
  113. }
  114. /// Calculate some matrices for each cascade. For dir lights.
  115. /// @param cameraFrustum Who is looking at the light.
  116. /// @param cascadeDistances The distances of the cascades.
  117. /// @param cascadeProjMats View projection matrices for each cascade.
  118. /// @param cascadeViewMats View matrices for each cascade. Optional.
  119. void computeCascadeFrustums(const Frustum& cameraFrustum, ConstWeakArray<F32> cascadeDistances, WeakArray<Mat4> cascadeProjMats,
  120. WeakArray<Mat3x4> cascadeViewMats = {},
  121. WeakArray<Array<F32, U32(FrustumPlaneType::kCount)>> cascadePlanes = {}) const;
  122. U32 getUuid() const
  123. {
  124. ANKI_ASSERT(m_uuid);
  125. return m_uuid;
  126. }
  127. ANKI_INTERNAL void setShadowAtlasUvViewports(ConstWeakArray<Vec4> viewports);
  128. const GpuSceneArrays::Light::Allocation& getGpuSceneLightAllocation() const
  129. {
  130. return m_gpuSceneLight;
  131. }
  132. private:
  133. Vec4 m_diffColor = Vec4(0.5f);
  134. Transform m_worldTransform = Transform::getIdentity();
  135. class Point
  136. {
  137. public:
  138. F32 m_radius = 1.0f;
  139. };
  140. class Spot
  141. {
  142. public:
  143. Mat3x4 m_viewMat = Mat3x4::getIdentity();
  144. Mat4 m_viewProjMat = Mat4::getIdentity();
  145. F32 m_distance = 1.0f;
  146. F32 m_outerAngle = toRad(30.0f);
  147. F32 m_innerAngle = toRad(15.0f);
  148. };
  149. Point m_point;
  150. Spot m_spot;
  151. GpuSceneArrays::Light::Allocation m_gpuSceneLight;
  152. GpuSceneArrays::LightVisibleRenderablesHash::Allocation m_hash;
  153. Array<Vec4, 6> m_shadowAtlasUvViewports;
  154. U32 m_uuid = 0;
  155. LightComponentType m_type;
  156. U8 m_shadow : 1 = false;
  157. U8 m_shapeDirty : 1 = true;
  158. U8 m_otherDirty : 1 = true;
  159. U8 m_shadowAtlasUvViewportCount : 3 = 0;
  160. void update(SceneComponentUpdateInfo& info, Bool& updated) override;
  161. };
  162. /// @}
  163. } // end namespace anki