GlobalIlluminationProbeNode.cpp 6.6 KB

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