LightNode.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include <AnKi/Scene/LightNode.h>
  6. #include <AnKi/Scene/SceneGraph.h>
  7. #include <AnKi/Scene/Components/LensFlareComponent.h>
  8. #include <AnKi/Scene/Components/MoveComponent.h>
  9. #include <AnKi/Scene/Components/SpatialComponent.h>
  10. #include <AnKi/Scene/Components/FrustumComponent.h>
  11. #include <AnKi/Shaders/Include/ClusteredShadingTypes.h>
  12. namespace anki
  13. {
  14. /// Feedback component.
  15. class LightNode::OnMovedFeedbackComponent : public SceneComponent
  16. {
  17. ANKI_SCENE_COMPONENT(LightNode::OnMovedFeedbackComponent)
  18. public:
  19. OnMovedFeedbackComponent(SceneNode* node)
  20. : SceneComponent(node, getStaticClassId(), true)
  21. {
  22. }
  23. Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
  24. {
  25. updated = false;
  26. const MoveComponent& move = node.getComponentAt<MoveComponent>(0);
  27. if(move.getTimestamp() == node.getGlobalTimestamp())
  28. {
  29. // Move updated
  30. static_cast<LightNode&>(node).onMoved(move);
  31. }
  32. return Error::NONE;
  33. }
  34. };
  35. ANKI_SCENE_COMPONENT_STATICS(LightNode::OnMovedFeedbackComponent)
  36. /// Feedback component.
  37. class LightNode::OnLightShapeUpdatedFeedbackComponent : public SceneComponent
  38. {
  39. ANKI_SCENE_COMPONENT(LightNode::OnLightShapeUpdatedFeedbackComponent)
  40. public:
  41. OnLightShapeUpdatedFeedbackComponent(SceneNode* node)
  42. : SceneComponent(node, getStaticClassId(), true)
  43. {
  44. }
  45. Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
  46. {
  47. updated = false;
  48. LightComponent& light = node.getFirstComponentOfType<LightComponent>();
  49. if(light.getTimestamp() == node.getGlobalTimestamp())
  50. {
  51. // Shape updated
  52. static_cast<LightNode&>(node).onLightShapeUpdated(light);
  53. }
  54. return Error::NONE;
  55. }
  56. };
  57. ANKI_SCENE_COMPONENT_STATICS(LightNode::OnLightShapeUpdatedFeedbackComponent)
  58. LightNode::LightNode(SceneGraph* scene, CString name)
  59. : SceneNode(scene, name)
  60. {
  61. }
  62. LightNode::~LightNode()
  63. {
  64. }
  65. void LightNode::frameUpdateCommon()
  66. {
  67. // Update frustum comps shadow info
  68. const LightComponent& lc = getFirstComponentOfType<LightComponent>();
  69. const Bool castsShadow = lc.getShadowEnabled();
  70. iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& frc) {
  71. if(castsShadow)
  72. {
  73. frc.setEnabledVisibilityTests(FrustumComponentVisibilityTestFlag::SHADOW_CASTERS);
  74. }
  75. else
  76. {
  77. frc.setEnabledVisibilityTests(FrustumComponentVisibilityTestFlag::NONE);
  78. }
  79. });
  80. }
  81. void LightNode::onMoveUpdateCommon(const MoveComponent& move)
  82. {
  83. // Update the spatial
  84. SpatialComponent& sp = getFirstComponentOfType<SpatialComponent>();
  85. sp.setSpatialOrigin(move.getWorldTransform().getOrigin().xyz());
  86. // Update the lens flare
  87. LensFlareComponent& lf = getFirstComponentOfType<LensFlareComponent>();
  88. lf.setWorldPosition(move.getWorldTransform().getOrigin().xyz());
  89. // Update light component
  90. getFirstComponentOfType<LightComponent>().setWorldTransform(move.getWorldTransform());
  91. }
  92. PointLightNode::PointLightNode(SceneGraph* scene, CString name)
  93. : LightNode(scene, name)
  94. {
  95. newComponent<MoveComponent>();
  96. newComponent<OnMovedFeedbackComponent>();
  97. LightComponent* lc = newComponent<LightComponent>();
  98. lc->setLightComponentType(LightComponentType::POINT);
  99. newComponent<LensFlareComponent>();
  100. newComponent<OnLightShapeUpdatedFeedbackComponent>();
  101. newComponent<SpatialComponent>();
  102. }
  103. PointLightNode::~PointLightNode()
  104. {
  105. m_shadowData.destroy(getAllocator());
  106. }
  107. void PointLightNode::onMoved(const MoveComponent& move)
  108. {
  109. onMoveUpdateCommon(move);
  110. // Update the frustums
  111. U32 count = 0;
  112. iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fr) {
  113. Transform trf = m_shadowData[count].m_localTrf;
  114. trf.setOrigin(move.getWorldTransform().getOrigin());
  115. fr.setWorldTransform(trf);
  116. ++count;
  117. });
  118. }
  119. void PointLightNode::onLightShapeUpdated(LightComponent& light)
  120. {
  121. iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fr) { fr.setFar(light.getRadius()); });
  122. SpatialComponent& spatialc = getFirstComponentOfType<SpatialComponent>();
  123. spatialc.setSphereWorldSpace(Sphere(light.getWorldTransform().getOrigin(), light.getRadius()));
  124. }
  125. Error PointLightNode::frameUpdate(Second prevUpdateTime, Second crntTime)
  126. {
  127. // Lazily init
  128. const LightComponent& lightc = getFirstComponentOfType<LightComponent>();
  129. if(lightc.getShadowEnabled() && m_shadowData.isEmpty())
  130. {
  131. m_shadowData.create(getAllocator(), 6);
  132. const F32 ang = toRad(90.0f);
  133. const F32 dist = lightc.getRadius();
  134. const F32 zNear = CLUSTER_OBJECT_FRUSTUM_NEAR_PLANE;
  135. Mat3 rot;
  136. rot = Mat3(Euler(0.0, -PI / 2.0, 0.0)) * Mat3(Euler(0.0, 0.0, PI));
  137. m_shadowData[0].m_localTrf.setRotation(Mat3x4(Vec3(0.0f), rot));
  138. rot = Mat3(Euler(0.0, PI / 2.0, 0.0)) * Mat3(Euler(0.0, 0.0, PI));
  139. m_shadowData[1].m_localTrf.setRotation(Mat3x4(Vec3(0.0f), rot));
  140. rot = Mat3(Euler(PI / 2.0, 0.0, 0.0));
  141. m_shadowData[2].m_localTrf.setRotation(Mat3x4(Vec3(0.0f), rot));
  142. rot = Mat3(Euler(-PI / 2.0, 0.0, 0.0));
  143. m_shadowData[3].m_localTrf.setRotation(Mat3x4(Vec3(0.0f), rot));
  144. rot = Mat3(Euler(0.0, PI, 0.0)) * Mat3(Euler(0.0, 0.0, PI));
  145. m_shadowData[4].m_localTrf.setRotation(Mat3x4(Vec3(0.0f), rot));
  146. rot = Mat3(Euler(0.0, 0.0, PI));
  147. m_shadowData[5].m_localTrf.setRotation(Mat3x4(Vec3(0.0f), rot));
  148. const Vec4& origin = getFirstComponentOfType<MoveComponent>().getWorldTransform().getOrigin();
  149. for(U32 i = 0; i < 6; i++)
  150. {
  151. Transform trf = m_shadowData[i].m_localTrf;
  152. trf.setOrigin(origin);
  153. FrustumComponent* frc = newComponent<FrustumComponent>();
  154. frc->setFrustumType(FrustumType::PERSPECTIVE);
  155. frc->setPerspective(zNear, dist, ang, ang);
  156. frc->setWorldTransform(trf);
  157. }
  158. }
  159. frameUpdateCommon();
  160. return Error::NONE;
  161. }
  162. class SpotLightNode::OnFrustumUpdatedFeedbackComponent : public SceneComponent
  163. {
  164. ANKI_SCENE_COMPONENT(SpotLightNode::OnFrustumUpdatedFeedbackComponent)
  165. public:
  166. OnFrustumUpdatedFeedbackComponent(SceneNode* node)
  167. : SceneComponent(node, getStaticClassId(), true)
  168. {
  169. }
  170. Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
  171. {
  172. updated = false;
  173. FrustumComponent& frc = node.getFirstComponentOfType<FrustumComponent>();
  174. if(frc.getTimestamp() == node.getGlobalTimestamp())
  175. {
  176. // Shape updated
  177. static_cast<SpotLightNode&>(node).onFrustumUpdated(frc);
  178. }
  179. return Error::NONE;
  180. }
  181. };
  182. ANKI_SCENE_COMPONENT_STATICS(SpotLightNode::OnFrustumUpdatedFeedbackComponent)
  183. SpotLightNode::SpotLightNode(SceneGraph* scene, CString name)
  184. : LightNode(scene, name)
  185. {
  186. newComponent<MoveComponent>();
  187. newComponent<OnMovedFeedbackComponent>();
  188. LightComponent* lc = newComponent<LightComponent>();
  189. lc->setLightComponentType(LightComponentType::SPOT);
  190. newComponent<LensFlareComponent>();
  191. newComponent<OnLightShapeUpdatedFeedbackComponent>();
  192. FrustumComponent* fr = newComponent<FrustumComponent>();
  193. fr->setFrustumType(FrustumType::PERSPECTIVE);
  194. fr->setEnabledVisibilityTests(FrustumComponentVisibilityTestFlag::NONE);
  195. newComponent<OnFrustumUpdatedFeedbackComponent>();
  196. newComponent<SpatialComponent>();
  197. }
  198. void SpotLightNode::onMoved(const MoveComponent& move)
  199. {
  200. // Update the frustums
  201. iterateComponentsOfType<FrustumComponent>(
  202. [&](FrustumComponent& fr) { fr.setWorldTransform(move.getWorldTransform()); });
  203. onMoveUpdateCommon(move);
  204. }
  205. void SpotLightNode::onLightShapeUpdated(LightComponent& light)
  206. {
  207. FrustumComponent& frc = getFirstComponentOfType<FrustumComponent>();
  208. frc.setPerspective(CLUSTER_OBJECT_FRUSTUM_NEAR_PLANE, light.getDistance(), light.getOuterAngle(),
  209. light.getOuterAngle());
  210. }
  211. void SpotLightNode::onFrustumUpdated(FrustumComponent& frc)
  212. {
  213. SpatialComponent& sp = getFirstComponentOfType<SpatialComponent>();
  214. sp.setConvexHullWorldSpace(frc.getPerspectiveBoundingShapeWorldSpace());
  215. }
  216. Error SpotLightNode::frameUpdate(Second prevUpdateTime, Second crntTime)
  217. {
  218. frameUpdateCommon();
  219. return Error::NONE;
  220. }
  221. class DirectionalLightNode::FeedbackComponent : public SceneComponent
  222. {
  223. ANKI_SCENE_COMPONENT(DirectionalLightNode::FeedbackComponent)
  224. public:
  225. FeedbackComponent(SceneNode* node)
  226. : SceneComponent(node, getStaticClassId(), true)
  227. {
  228. }
  229. Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
  230. {
  231. const MoveComponent& move = node.getComponentAt<MoveComponent>(0);
  232. if(move.getTimestamp() == node.getGlobalTimestamp())
  233. {
  234. // Move updated
  235. LightComponent& lightc = node.getFirstComponentOfType<LightComponent>();
  236. lightc.setWorldTransform(move.getWorldTransform());
  237. SpatialComponent& spatialc = node.getFirstComponentOfType<SpatialComponent>();
  238. spatialc.setSpatialOrigin(move.getWorldTransform().getOrigin().xyz());
  239. }
  240. return Error::NONE;
  241. }
  242. };
  243. ANKI_SCENE_COMPONENT_STATICS(DirectionalLightNode::FeedbackComponent)
  244. DirectionalLightNode::DirectionalLightNode(SceneGraph* scene, CString name)
  245. : SceneNode(scene, name)
  246. {
  247. newComponent<MoveComponent>();
  248. newComponent<FeedbackComponent>();
  249. LightComponent* lc = newComponent<LightComponent>();
  250. lc->setLightComponentType(LightComponentType::DIRECTIONAL);
  251. SpatialComponent* spatialc = newComponent<SpatialComponent>();
  252. // Make the spatial always visible
  253. spatialc->setAlwaysVisible(true);
  254. }
  255. } // end namespace anki