ReflectionProbeNode.cpp 6.0 KB

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