ReflectionProbeComponent.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #pragma once
  6. #include <AnKi/Scene/Components/SceneComponent.h>
  7. #include <AnKi/Renderer/RenderQueue.h>
  8. #include <AnKi/Collision/Aabb.h>
  9. namespace anki
  10. {
  11. /// @addtogroup scene
  12. /// @{
  13. /// Reflection probe component.
  14. class ReflectionProbeComponent : public SceneComponent
  15. {
  16. ANKI_SCENE_COMPONENT(ReflectionProbeComponent)
  17. public:
  18. ReflectionProbeComponent(SceneNode* node);
  19. ~ReflectionProbeComponent();
  20. /// Set the local size of the probe volume.
  21. void setBoxVolumeSize(const Vec3& sizeXYZ)
  22. {
  23. m_halfSize = sizeXYZ / 2.0f;
  24. m_markedForUpdate = true;
  25. }
  26. Vec3 getBoxVolumeSize() const
  27. {
  28. return m_halfSize * 2.0f;
  29. }
  30. Vec3 getWorldPosition() const
  31. {
  32. return m_worldPos;
  33. }
  34. void setWorldPosition(const Vec3& pos)
  35. {
  36. m_worldPos = pos;
  37. m_markedForUpdate = true;
  38. }
  39. Aabb getAabbWorldSpace() const
  40. {
  41. return Aabb(-m_halfSize + m_worldPos, m_halfSize + m_worldPos);
  42. }
  43. Bool getMarkedForRendering() const
  44. {
  45. return m_markedForRendering;
  46. }
  47. void setupReflectionProbeQueueElement(ReflectionProbeQueueElement& el) const
  48. {
  49. el.m_feedbackCallback = reflectionProbeQueueElementFeedbackCallback;
  50. el.m_feedbackCallbackUserData = const_cast<ReflectionProbeComponent*>(this);
  51. el.m_uuid = m_uuid;
  52. el.m_worldPosition = m_worldPos;
  53. el.m_aabbMin = -m_halfSize + m_worldPos;
  54. el.m_aabbMax = m_halfSize + m_worldPos;
  55. el.m_textureArrayIndex = MAX_U32;
  56. el.m_debugDrawCallback = [](RenderQueueDrawContext& ctx, ConstWeakArray<void*> userData) {
  57. ANKI_ASSERT(userData.getSize() == 1);
  58. static_cast<const ReflectionProbeComponent*>(userData[0])->draw(ctx);
  59. };
  60. el.m_debugDrawCallbackUserData = this;
  61. }
  62. Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
  63. {
  64. updated = m_markedForUpdate;
  65. m_markedForUpdate = false;
  66. return Error::NONE;
  67. }
  68. private:
  69. SceneNode* m_node = nullptr;
  70. U64 m_uuid = 0;
  71. Vec3 m_worldPos = Vec3(0.0f);
  72. Vec3 m_halfSize = Vec3(1.0f);
  73. Bool m_markedForRendering : 1;
  74. Bool m_markedForUpdate : 1;
  75. ImageResourcePtr m_debugImage;
  76. static void reflectionProbeQueueElementFeedbackCallback(Bool fillRenderQueuesOnNextFrame, void* userData)
  77. {
  78. ANKI_ASSERT(userData);
  79. static_cast<ReflectionProbeComponent*>(userData)->m_markedForRendering = fillRenderQueuesOnNextFrame;
  80. }
  81. void draw(RenderQueueDrawContext& ctx) const;
  82. };
  83. /// @}
  84. } // end namespace anki