2
0

ReflectionProbeComponent.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2009-2023, 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. m_gpuSceneOffset = U32(node->getSceneGraph().getAllGpuSceneContiguousArrays().allocate(
  27. GpuSceneContiguousArrayType::kReflectionProbes));
  28. }
  29. ReflectionProbeComponent::~ReflectionProbeComponent()
  30. {
  31. }
  32. Error ReflectionProbeComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  33. {
  34. const Bool moved = info.m_node->movedThisFrame();
  35. const Bool shapeUpdated = m_dirty;
  36. m_dirty = false;
  37. updated = moved || shapeUpdated;
  38. if(updated) [[unlikely]]
  39. {
  40. m_worldPos = info.m_node->getWorldTransform().getOrigin().xyz();
  41. F32 effectiveDistance = max(m_halfSize.x(), m_halfSize.y());
  42. effectiveDistance = max(effectiveDistance, m_halfSize.z());
  43. effectiveDistance =
  44. max(effectiveDistance, getExternalSubsystems(*info.m_node).m_config->getSceneProbeEffectiveDistance());
  45. const F32 shadowCascadeDistance = min(
  46. effectiveDistance, getExternalSubsystems(*info.m_node).m_config->getSceneProbeShadowEffectiveDistance());
  47. for(U32 i = 0; i < 6; ++i)
  48. {
  49. m_frustums[i].setWorldTransform(
  50. Transform(m_worldPos.xyz0(), Frustum::getOmnidirectionalFrustumRotations()[i], 1.0f));
  51. m_frustums[i].setFar(effectiveDistance);
  52. m_frustums[i].setShadowCascadeDistance(0, shadowCascadeDistance);
  53. // Add something really far to force LOD 0 to be used. The importing tools create LODs with holes some times
  54. // and that causes the sky to bleed to GI rendering
  55. m_frustums[i].setLodDistances({effectiveDistance - 3.0f * kEpsilonf, effectiveDistance - 2.0f * kEpsilonf,
  56. effectiveDistance - 1.0f * kEpsilonf});
  57. }
  58. // Set a new UUID to force the renderer to update the probe
  59. m_uuid = info.m_node->getSceneGraph().getNewUuid();
  60. const Aabb aabbWorld(-m_halfSize + m_worldPos, m_halfSize + m_worldPos);
  61. m_spatial.setBoundingShape(aabbWorld);
  62. // Upload to the GPU scene
  63. GpuSceneReflectionProbe gpuProbe;
  64. gpuProbe.m_position = m_worldPos;
  65. gpuProbe.m_cubemapIndex = 0; // Unknown at this point
  66. gpuProbe.m_aabbMin = aabbWorld.getMin().xyz();
  67. gpuProbe.m_aabbMax = aabbWorld.getMax().xyz();
  68. getExternalSubsystems(*info.m_node)
  69. .m_gpuSceneMicroPatcher->newCopy(*info.m_framePool, m_gpuSceneOffset, sizeof(gpuProbe), &gpuProbe);
  70. }
  71. const Bool spatialUpdated = m_spatial.update(info.m_node->getSceneGraph().getOctree());
  72. updated = updated || spatialUpdated;
  73. for(U32 i = 0; i < 6; ++i)
  74. {
  75. const Bool frustumUpdated = m_frustums[i].update();
  76. updated = updated || frustumUpdated;
  77. }
  78. return Error::kNone;
  79. }
  80. void ReflectionProbeComponent::onDestroy(SceneNode& node)
  81. {
  82. m_spatial.removeFromOctree(node.getSceneGraph().getOctree());
  83. node.getSceneGraph().getAllGpuSceneContiguousArrays().deferredFree(GpuSceneContiguousArrayType::kParticleEmitters,
  84. m_gpuSceneOffset);
  85. }
  86. } // end namespace anki