ReflectionProbeComponent.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (C) 2009-2022, 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/Components/ReflectionProbeComponent.h>
  6. #include <AnKi/Scene/Components/MoveComponent.h>
  7. #include <AnKi/Scene/SceneGraph.h>
  8. #include <AnKi/Scene/SceneNode.h>
  9. #include <AnKi/Core/ConfigSet.h>
  10. namespace anki {
  11. ReflectionProbeComponent::ReflectionProbeComponent(SceneNode* node)
  12. : SceneComponent(node, getStaticClassId())
  13. , m_uuid(node->getSceneGraph().getNewUuid())
  14. , m_spatial(this)
  15. {
  16. m_worldPos = node->getWorldTransform().getOrigin().xyz();
  17. for(U32 i = 0; i < 6; ++i)
  18. {
  19. m_frustums[i].init(FrustumType::kPerspective, &node->getMemoryPool());
  20. m_frustums[i].setPerspective(kClusterObjectFrustumNearPlane, 100.0f, kPi / 2.0f, kPi / 2.0f);
  21. m_frustums[i].setWorldTransform(
  22. Transform(m_worldPos.xyz0(), Frustum::getOmnidirectionalFrustumRotations()[i], 1.0f));
  23. m_frustums[i].setShadowCascadeCount(1);
  24. m_frustums[i].update();
  25. }
  26. }
  27. ReflectionProbeComponent::~ReflectionProbeComponent()
  28. {
  29. }
  30. Error ReflectionProbeComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  31. {
  32. const Bool moved = info.m_node->movedThisFrame();
  33. const Bool shapeUpdated = m_dirty;
  34. m_dirty = false;
  35. updated = moved || shapeUpdated;
  36. if(moved) [[unlikely]]
  37. {
  38. m_worldPos = info.m_node->getWorldTransform().getOrigin().xyz();
  39. for(U32 i = 0; i < 6; ++i)
  40. {
  41. m_frustums[i].setWorldTransform(
  42. Transform(m_worldPos.xyz0(), Frustum::getOmnidirectionalFrustumRotations()[i], 1.0f));
  43. }
  44. }
  45. if(shapeUpdated) [[unlikely]]
  46. {
  47. F32 effectiveDistance = max(m_halfSize.x(), m_halfSize.y());
  48. effectiveDistance = max(effectiveDistance, m_halfSize.z());
  49. effectiveDistance =
  50. max(effectiveDistance, getExternalSubsystems(*info.m_node).m_config->getSceneProbeEffectiveDistance());
  51. const F32 shadowCascadeDistance = min(
  52. effectiveDistance, getExternalSubsystems(*info.m_node).m_config->getSceneProbeShadowEffectiveDistance());
  53. for(U32 i = 0; i < 6; ++i)
  54. {
  55. m_frustums[i].setFar(effectiveDistance);
  56. m_frustums[i].setShadowCascadeDistance(0, shadowCascadeDistance);
  57. // Add something really far to force LOD 0 to be used. The importing tools create LODs with holes some times
  58. // and that causes the sky to bleed to GI rendering
  59. m_frustums[i].setLodDistances({effectiveDistance - 3.0f * kEpsilonf, effectiveDistance - 2.0f * kEpsilonf,
  60. effectiveDistance - 1.0f * kEpsilonf});
  61. }
  62. }
  63. if(updated) [[unlikely]]
  64. {
  65. // Set a new UUID to force the renderer to update the probe
  66. m_uuid = info.m_node->getSceneGraph().getNewUuid();
  67. for(U32 i = 0; i < 6; ++i)
  68. {
  69. m_frustums[i].update();
  70. }
  71. Aabb aabbWorld(-m_halfSize + m_worldPos, m_halfSize + m_worldPos);
  72. m_spatial.setBoundingShape(aabbWorld);
  73. m_spatial.update(info.m_node->getSceneGraph().getOctree());
  74. }
  75. return Error::kNone;
  76. }
  77. } // end namespace anki