LightNode.cpp 9.1 KB

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