| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- // 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/ReflectionProbeNode.h>
- #include <AnKi/Scene/Components/ReflectionProbeComponent.h>
- #include <AnKi/Scene/Components/MoveComponent.h>
- #include <AnKi/Scene/Components/FrustumComponent.h>
- #include <AnKi/Scene/Components/SpatialComponent.h>
- #include <AnKi/Scene/SceneGraph.h>
- #include <AnKi/Renderer/LightShading.h>
- #include <AnKi/Shaders/Include/ClusteredShadingTypes.h>
- namespace anki
- {
- const FrustumComponentVisibilityTestFlag FRUSTUM_TEST_FLAGS =
- FrustumComponentVisibilityTestFlag::RENDER_COMPONENTS | FrustumComponentVisibilityTestFlag::LIGHT_COMPONENTS
- | FrustumComponentVisibilityTestFlag::DIRECTIONAL_LIGHT_SHADOWS_1_CASCADE;
- /// Feedback component
- class ReflectionProbeNode::MoveFeedbackComponent : public SceneComponent
- {
- ANKI_SCENE_COMPONENT(ReflectionProbeNode::MoveFeedbackComponent)
- public:
- MoveFeedbackComponent(SceneNode* node)
- : SceneComponent(node, getStaticClassId(), true)
- {
- }
- Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
- {
- updated = false;
- MoveComponent& move = node.getFirstComponentOfType<MoveComponent>();
- if(move.getTimestamp() == node.getGlobalTimestamp())
- {
- // Move updated
- ReflectionProbeNode& dnode = static_cast<ReflectionProbeNode&>(node);
- dnode.onMoveUpdate(move);
- }
- return Error::NONE;
- }
- };
- ANKI_SCENE_COMPONENT_STATICS(ReflectionProbeNode::MoveFeedbackComponent)
- /// Feedback component
- class ReflectionProbeNode::ShapeFeedbackComponent : public SceneComponent
- {
- ANKI_SCENE_COMPONENT(ReflectionProbeNode::ShapeFeedbackComponent)
- public:
- ShapeFeedbackComponent(SceneNode* node)
- : SceneComponent(node, getStaticClassId(), true)
- {
- }
- Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
- {
- updated = false;
- ReflectionProbeComponent& reflc = node.getFirstComponentOfType<ReflectionProbeComponent>();
- if(reflc.getTimestamp() == node.getGlobalTimestamp())
- {
- ReflectionProbeNode& dnode = static_cast<ReflectionProbeNode&>(node);
- dnode.onShapeUpdate(reflc);
- }
- return Error::NONE;
- }
- };
- ANKI_SCENE_COMPONENT_STATICS(ReflectionProbeNode::ShapeFeedbackComponent)
- ReflectionProbeNode::ReflectionProbeNode(SceneGraph* scene, CString name)
- : SceneNode(scene, name)
- {
- // Move component first
- newComponent<MoveComponent>();
- // Feedback component
- newComponent<MoveFeedbackComponent>();
- // The frustum components
- const F32 ang = toRad(90.0f);
- Mat3 rot;
- rot = Mat3(Euler(0.0f, -PI / 2.0f, 0.0f)) * Mat3(Euler(0.0f, 0.0f, PI));
- m_frustumTransforms[0].setRotation(Mat3x4(Vec3(0.0f), rot));
- rot = Mat3(Euler(0.0f, PI / 2.0f, 0.0f)) * Mat3(Euler(0.0f, 0.0f, PI));
- m_frustumTransforms[1].setRotation(Mat3x4(Vec3(0.0f), rot));
- rot = Mat3(Euler(PI / 2.0f, 0.0f, 0.0f));
- m_frustumTransforms[2].setRotation(Mat3x4(Vec3(0.0f), rot));
- rot = Mat3(Euler(-PI / 2.0f, 0.0f, 0.0f));
- m_frustumTransforms[3].setRotation(Mat3x4(Vec3(0.0f), rot));
- rot = Mat3(Euler(0.0f, PI, 0.0f)) * Mat3(Euler(0.0f, 0.0f, PI));
- m_frustumTransforms[4].setRotation(Mat3x4(Vec3(0.0f), rot));
- rot = Mat3(Euler(0.0f, 0.0f, PI));
- m_frustumTransforms[5].setRotation(Mat3x4(Vec3(0.0f), rot));
- for(U i = 0; i < 6; ++i)
- {
- m_frustumTransforms[i].setOrigin(Vec4(0.0f));
- m_frustumTransforms[i].setScale(1.0f);
- FrustumComponent* frc = newComponent<FrustumComponent>();
- frc->setFrustumType(FrustumType::PERSPECTIVE);
- frc->setPerspective(CLUSTER_OBJECT_FRUSTUM_NEAR_PLANE, 10.0f, ang, ang);
- frc->setWorldTransform(m_frustumTransforms[i]);
- frc->setEnabledVisibilityTests(FrustumComponentVisibilityTestFlag::NONE);
- frc->setEffectiveShadowDistance(getSceneGraph().getConfig().m_reflectionProbeShadowEffectiveDistance);
- }
- // Reflection probe comp
- newComponent<ReflectionProbeComponent>();
- // Feedback
- newComponent<ShapeFeedbackComponent>();
- // Spatial component
- SpatialComponent* spatialc = newComponent<SpatialComponent>();
- spatialc->setUpdateOctreeBounds(false);
- }
- ReflectionProbeNode::~ReflectionProbeNode()
- {
- }
- void ReflectionProbeNode::onMoveUpdate(MoveComponent& move)
- {
- // Update frustum components
- U count = 0;
- iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& frc) {
- Transform trf = m_frustumTransforms[count];
- trf.setOrigin(move.getWorldTransform().getOrigin());
- frc.setWorldTransform(trf);
- ++count;
- });
- ANKI_ASSERT(count == 6);
- // Update the spatial comp
- SpatialComponent& sp = getFirstComponentOfType<SpatialComponent>();
- sp.setSpatialOrigin(move.getWorldTransform().getOrigin().xyz());
- // Update the refl comp
- ReflectionProbeComponent& reflc = getFirstComponentOfType<ReflectionProbeComponent>();
- reflc.setWorldPosition(move.getWorldTransform().getOrigin().xyz());
- }
- void ReflectionProbeNode::onShapeUpdate(ReflectionProbeComponent& reflc)
- {
- const Vec3 halfProbeSize = reflc.getBoxVolumeSize() / 2.0f;
- F32 effectiveDistance = max(halfProbeSize.x(), halfProbeSize.y());
- effectiveDistance = max(effectiveDistance, halfProbeSize.z());
- effectiveDistance = max(effectiveDistance, getSceneGraph().getConfig().m_reflectionProbeEffectiveDistance);
- // Update frustum components
- iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& frc) { frc.setFar(effectiveDistance); });
- // Update the spatial comp
- SpatialComponent& sp = getFirstComponentOfType<SpatialComponent>();
- sp.setAabbWorldSpace(reflc.getAabbWorldSpace());
- }
- Error ReflectionProbeNode::frameUpdate(Second prevUpdateTime, Second crntTime)
- {
- // Check the reflection probe component and if it's marked for rendering enable the frustum components
- const ReflectionProbeComponent& reflc = getFirstComponentOfType<ReflectionProbeComponent>();
- const FrustumComponentVisibilityTestFlag testFlags =
- reflc.getMarkedForRendering() ? FRUSTUM_TEST_FLAGS : FrustumComponentVisibilityTestFlag::NONE;
- iterateComponentsOfType<FrustumComponent>(
- [testFlags](FrustumComponent& frc) { frc.setEnabledVisibilityTests(testFlags); });
- return Error::NONE;
- }
- } // end namespace anki
|