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