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