| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <AnKi/Scene/LightNode.h>
- #include <AnKi/Scene/SceneGraph.h>
- #include <AnKi/Scene/Components/LensFlareComponent.h>
- #include <AnKi/Scene/Components/MoveComponent.h>
- #include <AnKi/Scene/Components/SpatialComponent.h>
- #include <AnKi/Scene/Components/FrustumComponent.h>
- #include <AnKi/Shaders/Include/ClusteredShadingTypes.h>
- namespace anki {
- /// Feedback component.
- class LightNode::OnMovedFeedbackComponent : public SceneComponent
- {
- ANKI_SCENE_COMPONENT(LightNode::OnMovedFeedbackComponent)
- public:
- OnMovedFeedbackComponent(SceneNode* node)
- : SceneComponent(node, getStaticClassId(), true)
- {
- }
- Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
- {
- updated = false;
- const MoveComponent& move = node.getComponentAt<MoveComponent>(0);
- if(move.getTimestamp() == node.getGlobalTimestamp())
- {
- // Move updated
- static_cast<LightNode&>(node).onMoved(move);
- }
- return Error::NONE;
- }
- };
- ANKI_SCENE_COMPONENT_STATICS(LightNode::OnMovedFeedbackComponent)
- /// Feedback component.
- class LightNode::OnLightShapeUpdatedFeedbackComponent : public SceneComponent
- {
- ANKI_SCENE_COMPONENT(LightNode::OnLightShapeUpdatedFeedbackComponent)
- public:
- OnLightShapeUpdatedFeedbackComponent(SceneNode* node)
- : SceneComponent(node, getStaticClassId(), true)
- {
- }
- Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
- {
- updated = false;
- LightComponent& light = node.getFirstComponentOfType<LightComponent>();
- if(light.getTimestamp() == node.getGlobalTimestamp())
- {
- // Shape updated
- static_cast<LightNode&>(node).onLightShapeUpdated(light);
- }
- return Error::NONE;
- }
- };
- ANKI_SCENE_COMPONENT_STATICS(LightNode::OnLightShapeUpdatedFeedbackComponent)
- LightNode::LightNode(SceneGraph* scene, CString name)
- : SceneNode(scene, name)
- {
- }
- LightNode::~LightNode()
- {
- }
- void LightNode::frameUpdateCommon()
- {
- // Update frustum comps shadow info
- const LightComponent& lc = getFirstComponentOfType<LightComponent>();
- const Bool castsShadow = lc.getShadowEnabled();
- iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& frc) {
- if(castsShadow)
- {
- frc.setEnabledVisibilityTests(FrustumComponentVisibilityTestFlag::SHADOW_CASTERS);
- }
- else
- {
- frc.setEnabledVisibilityTests(FrustumComponentVisibilityTestFlag::NONE);
- }
- });
- }
- void LightNode::onMoveUpdateCommon(const MoveComponent& move)
- {
- // Update the spatial
- SpatialComponent& sp = getFirstComponentOfType<SpatialComponent>();
- sp.setSpatialOrigin(move.getWorldTransform().getOrigin().xyz());
- // Update the lens flare
- LensFlareComponent& lf = getFirstComponentOfType<LensFlareComponent>();
- lf.setWorldPosition(move.getWorldTransform().getOrigin().xyz());
- // Update light component
- getFirstComponentOfType<LightComponent>().setWorldTransform(move.getWorldTransform());
- }
- PointLightNode::PointLightNode(SceneGraph* scene, CString name)
- : LightNode(scene, name)
- {
- newComponent<MoveComponent>();
- newComponent<OnMovedFeedbackComponent>();
- LightComponent* lc = newComponent<LightComponent>();
- lc->setLightComponentType(LightComponentType::POINT);
- newComponent<LensFlareComponent>();
- newComponent<OnLightShapeUpdatedFeedbackComponent>();
- newComponent<SpatialComponent>();
- }
- PointLightNode::~PointLightNode()
- {
- m_shadowData.destroy(getAllocator());
- }
- void PointLightNode::onMoved(const MoveComponent& move)
- {
- onMoveUpdateCommon(move);
- // Update the frustums
- U32 count = 0;
- iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fr) {
- Transform trf = m_shadowData[count].m_localTrf;
- trf.setOrigin(move.getWorldTransform().getOrigin());
- fr.setWorldTransform(trf);
- ++count;
- });
- }
- void PointLightNode::onLightShapeUpdated(LightComponent& light)
- {
- iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fr) {
- fr.setFar(light.getRadius());
- });
- SpatialComponent& spatialc = getFirstComponentOfType<SpatialComponent>();
- spatialc.setSphereWorldSpace(Sphere(light.getWorldTransform().getOrigin(), light.getRadius()));
- }
- Error PointLightNode::frameUpdate(Second prevUpdateTime, Second crntTime)
- {
- // Lazily init
- const LightComponent& lightc = getFirstComponentOfType<LightComponent>();
- if(lightc.getShadowEnabled() && m_shadowData.isEmpty())
- {
- m_shadowData.create(getAllocator(), 6);
- const F32 ang = toRad(90.0f);
- const F32 dist = lightc.getRadius();
- const F32 zNear = CLUSTER_OBJECT_FRUSTUM_NEAR_PLANE;
- Mat3 rot;
- rot = Mat3(Euler(0.0, -PI / 2.0, 0.0)) * Mat3(Euler(0.0, 0.0, PI));
- m_shadowData[0].m_localTrf.setRotation(Mat3x4(Vec3(0.0f), rot));
- rot = Mat3(Euler(0.0, PI / 2.0, 0.0)) * Mat3(Euler(0.0, 0.0, PI));
- m_shadowData[1].m_localTrf.setRotation(Mat3x4(Vec3(0.0f), rot));
- rot = Mat3(Euler(PI / 2.0, 0.0, 0.0));
- m_shadowData[2].m_localTrf.setRotation(Mat3x4(Vec3(0.0f), rot));
- rot = Mat3(Euler(-PI / 2.0, 0.0, 0.0));
- m_shadowData[3].m_localTrf.setRotation(Mat3x4(Vec3(0.0f), rot));
- rot = Mat3(Euler(0.0, PI, 0.0)) * Mat3(Euler(0.0, 0.0, PI));
- m_shadowData[4].m_localTrf.setRotation(Mat3x4(Vec3(0.0f), rot));
- rot = Mat3(Euler(0.0, 0.0, PI));
- m_shadowData[5].m_localTrf.setRotation(Mat3x4(Vec3(0.0f), rot));
- const Vec4& origin = getFirstComponentOfType<MoveComponent>().getWorldTransform().getOrigin();
- for(U32 i = 0; i < 6; i++)
- {
- Transform trf = m_shadowData[i].m_localTrf;
- trf.setOrigin(origin);
- FrustumComponent* frc = newComponent<FrustumComponent>();
- frc->setFrustumType(FrustumType::PERSPECTIVE);
- frc->setPerspective(zNear, dist, ang, ang);
- frc->setWorldTransform(trf);
- }
- }
- frameUpdateCommon();
- return Error::NONE;
- }
- class SpotLightNode::OnFrustumUpdatedFeedbackComponent : public SceneComponent
- {
- ANKI_SCENE_COMPONENT(SpotLightNode::OnFrustumUpdatedFeedbackComponent)
- public:
- OnFrustumUpdatedFeedbackComponent(SceneNode* node)
- : SceneComponent(node, getStaticClassId(), true)
- {
- }
- Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
- {
- updated = false;
- FrustumComponent& frc = node.getFirstComponentOfType<FrustumComponent>();
- if(frc.getTimestamp() == node.getGlobalTimestamp())
- {
- // Shape updated
- static_cast<SpotLightNode&>(node).onFrustumUpdated(frc);
- }
- return Error::NONE;
- }
- };
- ANKI_SCENE_COMPONENT_STATICS(SpotLightNode::OnFrustumUpdatedFeedbackComponent)
- SpotLightNode::SpotLightNode(SceneGraph* scene, CString name)
- : LightNode(scene, name)
- {
- newComponent<MoveComponent>();
- newComponent<OnMovedFeedbackComponent>();
- LightComponent* lc = newComponent<LightComponent>();
- lc->setLightComponentType(LightComponentType::SPOT);
- newComponent<LensFlareComponent>();
- newComponent<OnLightShapeUpdatedFeedbackComponent>();
- FrustumComponent* fr = newComponent<FrustumComponent>();
- fr->setFrustumType(FrustumType::PERSPECTIVE);
- fr->setEnabledVisibilityTests(FrustumComponentVisibilityTestFlag::NONE);
- newComponent<OnFrustumUpdatedFeedbackComponent>();
- newComponent<SpatialComponent>();
- }
- void SpotLightNode::onMoved(const MoveComponent& move)
- {
- // Update the frustums
- iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fr) {
- fr.setWorldTransform(move.getWorldTransform());
- });
- onMoveUpdateCommon(move);
- }
- void SpotLightNode::onLightShapeUpdated(LightComponent& light)
- {
- FrustumComponent& frc = getFirstComponentOfType<FrustumComponent>();
- frc.setPerspective(CLUSTER_OBJECT_FRUSTUM_NEAR_PLANE, light.getDistance(), light.getOuterAngle(),
- light.getOuterAngle());
- }
- void SpotLightNode::onFrustumUpdated(FrustumComponent& frc)
- {
- SpatialComponent& sp = getFirstComponentOfType<SpatialComponent>();
- sp.setConvexHullWorldSpace(frc.getPerspectiveBoundingShapeWorldSpace());
- }
- Error SpotLightNode::frameUpdate(Second prevUpdateTime, Second crntTime)
- {
- frameUpdateCommon();
- return Error::NONE;
- }
- class DirectionalLightNode::FeedbackComponent : public SceneComponent
- {
- ANKI_SCENE_COMPONENT(DirectionalLightNode::FeedbackComponent)
- public:
- FeedbackComponent(SceneNode* node)
- : SceneComponent(node, getStaticClassId(), true)
- {
- }
- Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
- {
- const MoveComponent& move = node.getComponentAt<MoveComponent>(0);
- if(move.getTimestamp() == node.getGlobalTimestamp())
- {
- // Move updated
- LightComponent& lightc = node.getFirstComponentOfType<LightComponent>();
- lightc.setWorldTransform(move.getWorldTransform());
- SpatialComponent& spatialc = node.getFirstComponentOfType<SpatialComponent>();
- spatialc.setSpatialOrigin(move.getWorldTransform().getOrigin().xyz());
- }
- return Error::NONE;
- }
- };
- ANKI_SCENE_COMPONENT_STATICS(DirectionalLightNode::FeedbackComponent)
- DirectionalLightNode::DirectionalLightNode(SceneGraph* scene, CString name)
- : SceneNode(scene, name)
- {
- newComponent<MoveComponent>();
- newComponent<FeedbackComponent>();
- LightComponent* lc = newComponent<LightComponent>();
- lc->setLightComponentType(LightComponentType::DIRECTIONAL);
- SpatialComponent* spatialc = newComponent<SpatialComponent>();
- // Make the spatial always visible
- spatialc->setAlwaysVisible(true);
- }
- } // end namespace anki
|