ReflectionProbeNode.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/ReflectionProbeNode.h>
  6. #include <AnKi/Scene/Components/ReflectionProbeComponent.h>
  7. #include <AnKi/Scene/Components/MoveComponent.h>
  8. #include <AnKi/Scene/Components/FrustumComponent.h>
  9. #include <AnKi/Scene/Components/SpatialComponent.h>
  10. #include <AnKi/Core/ConfigSet.h>
  11. #include <AnKi/Shaders/Include/ClusteredShadingTypes.h>
  12. namespace anki {
  13. const FrustumComponentVisibilityTestFlag FRUSTUM_TEST_FLAGS =
  14. FrustumComponentVisibilityTestFlag::RENDER_COMPONENTS | FrustumComponentVisibilityTestFlag::LIGHT_COMPONENTS
  15. | FrustumComponentVisibilityTestFlag::DIRECTIONAL_LIGHT_SHADOWS_1_CASCADE;
  16. /// Feedback component
  17. class ReflectionProbeNode::MoveFeedbackComponent : public SceneComponent
  18. {
  19. ANKI_SCENE_COMPONENT(ReflectionProbeNode::MoveFeedbackComponent)
  20. public:
  21. MoveFeedbackComponent(SceneNode* node)
  22. : SceneComponent(node, getStaticClassId(), true)
  23. {
  24. }
  25. Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
  26. {
  27. updated = false;
  28. MoveComponent& move = node.getFirstComponentOfType<MoveComponent>();
  29. if(move.getTimestamp() == node.getGlobalTimestamp())
  30. {
  31. // Move updated
  32. ReflectionProbeNode& dnode = static_cast<ReflectionProbeNode&>(node);
  33. dnode.onMoveUpdate(move);
  34. }
  35. return Error::NONE;
  36. }
  37. };
  38. ANKI_SCENE_COMPONENT_STATICS(ReflectionProbeNode::MoveFeedbackComponent)
  39. /// Feedback component
  40. class ReflectionProbeNode::ShapeFeedbackComponent : public SceneComponent
  41. {
  42. ANKI_SCENE_COMPONENT(ReflectionProbeNode::ShapeFeedbackComponent)
  43. public:
  44. ShapeFeedbackComponent(SceneNode* node)
  45. : SceneComponent(node, getStaticClassId(), true)
  46. {
  47. }
  48. Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
  49. {
  50. updated = false;
  51. ReflectionProbeComponent& reflc = node.getFirstComponentOfType<ReflectionProbeComponent>();
  52. if(reflc.getTimestamp() == node.getGlobalTimestamp())
  53. {
  54. ReflectionProbeNode& dnode = static_cast<ReflectionProbeNode&>(node);
  55. dnode.onShapeUpdate(reflc);
  56. }
  57. return Error::NONE;
  58. }
  59. };
  60. ANKI_SCENE_COMPONENT_STATICS(ReflectionProbeNode::ShapeFeedbackComponent)
  61. ReflectionProbeNode::ReflectionProbeNode(SceneGraph* scene, CString name)
  62. : SceneNode(scene, name)
  63. {
  64. // Move component first
  65. newComponent<MoveComponent>();
  66. // Feedback component
  67. newComponent<MoveFeedbackComponent>();
  68. // The frustum components
  69. const F32 ang = toRad(90.0f);
  70. Mat3 rot;
  71. rot = Mat3(Euler(0.0f, -PI / 2.0f, 0.0f)) * Mat3(Euler(0.0f, 0.0f, PI));
  72. m_frustumTransforms[0].setRotation(Mat3x4(Vec3(0.0f), rot));
  73. rot = Mat3(Euler(0.0f, PI / 2.0f, 0.0f)) * Mat3(Euler(0.0f, 0.0f, PI));
  74. m_frustumTransforms[1].setRotation(Mat3x4(Vec3(0.0f), rot));
  75. rot = Mat3(Euler(PI / 2.0f, 0.0f, 0.0f));
  76. m_frustumTransforms[2].setRotation(Mat3x4(Vec3(0.0f), rot));
  77. rot = Mat3(Euler(-PI / 2.0f, 0.0f, 0.0f));
  78. m_frustumTransforms[3].setRotation(Mat3x4(Vec3(0.0f), rot));
  79. rot = Mat3(Euler(0.0f, PI, 0.0f)) * Mat3(Euler(0.0f, 0.0f, PI));
  80. m_frustumTransforms[4].setRotation(Mat3x4(Vec3(0.0f), rot));
  81. rot = Mat3(Euler(0.0f, 0.0f, PI));
  82. m_frustumTransforms[5].setRotation(Mat3x4(Vec3(0.0f), rot));
  83. for(U i = 0; i < 6; ++i)
  84. {
  85. m_frustumTransforms[i].setOrigin(Vec4(0.0f));
  86. m_frustumTransforms[i].setScale(1.0f);
  87. FrustumComponent* frc = newComponent<FrustumComponent>();
  88. frc->setFrustumType(FrustumType::PERSPECTIVE);
  89. frc->setPerspective(CLUSTER_OBJECT_FRUSTUM_NEAR_PLANE, 10.0f, ang, ang);
  90. frc->setWorldTransform(m_frustumTransforms[i]);
  91. frc->setEnabledVisibilityTests(FrustumComponentVisibilityTestFlag::NONE);
  92. frc->setEffectiveShadowDistance(getConfig().getSceneReflectionProbeShadowEffectiveDistance());
  93. }
  94. // Reflection probe comp
  95. newComponent<ReflectionProbeComponent>();
  96. // Feedback
  97. newComponent<ShapeFeedbackComponent>();
  98. // Spatial component
  99. SpatialComponent* spatialc = newComponent<SpatialComponent>();
  100. spatialc->setUpdateOctreeBounds(false);
  101. }
  102. ReflectionProbeNode::~ReflectionProbeNode()
  103. {
  104. }
  105. void ReflectionProbeNode::onMoveUpdate(MoveComponent& move)
  106. {
  107. // Update frustum components
  108. U count = 0;
  109. iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& frc) {
  110. Transform trf = m_frustumTransforms[count];
  111. trf.setOrigin(move.getWorldTransform().getOrigin());
  112. frc.setWorldTransform(trf);
  113. ++count;
  114. });
  115. ANKI_ASSERT(count == 6);
  116. // Update the spatial comp
  117. SpatialComponent& sp = getFirstComponentOfType<SpatialComponent>();
  118. sp.setSpatialOrigin(move.getWorldTransform().getOrigin().xyz());
  119. // Update the refl comp
  120. ReflectionProbeComponent& reflc = getFirstComponentOfType<ReflectionProbeComponent>();
  121. reflc.setWorldPosition(move.getWorldTransform().getOrigin().xyz());
  122. }
  123. void ReflectionProbeNode::onShapeUpdate(ReflectionProbeComponent& reflc)
  124. {
  125. const Vec3 halfProbeSize = reflc.getBoxVolumeSize() / 2.0f;
  126. F32 effectiveDistance = max(halfProbeSize.x(), halfProbeSize.y());
  127. effectiveDistance = max(effectiveDistance, halfProbeSize.z());
  128. effectiveDistance = max(effectiveDistance, getConfig().getSceneReflectionProbeEffectiveDistance());
  129. // Update frustum components
  130. iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& frc) {
  131. frc.setFar(effectiveDistance);
  132. });
  133. // Update the spatial comp
  134. SpatialComponent& sp = getFirstComponentOfType<SpatialComponent>();
  135. sp.setAabbWorldSpace(reflc.getAabbWorldSpace());
  136. }
  137. Error ReflectionProbeNode::frameUpdate(Second prevUpdateTime, Second crntTime)
  138. {
  139. // Check the reflection probe component and if it's marked for rendering enable the frustum components
  140. const ReflectionProbeComponent& reflc = getFirstComponentOfType<ReflectionProbeComponent>();
  141. const FrustumComponentVisibilityTestFlag testFlags =
  142. reflc.getMarkedForRendering() ? FRUSTUM_TEST_FLAGS : FrustumComponentVisibilityTestFlag::NONE;
  143. iterateComponentsOfType<FrustumComponent>([testFlags](FrustumComponent& frc) {
  144. frc.setEnabledVisibilityTests(testFlags);
  145. });
  146. return Error::NONE;
  147. }
  148. } // end namespace anki