GlobalIlluminationProbeComponent.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /// Global illumination probe component. It's an axis aligned box divided into cells.
  14. class GlobalIlluminationProbeComponent : public SceneComponent
  15. {
  16. ANKI_SCENE_COMPONENT(GlobalIlluminationProbeComponent)
  17. public:
  18. GlobalIlluminationProbeComponent(SceneNode* node);
  19. /// Set the bounding box size.
  20. void setBoxVolumeSize(const Vec3& sizeXYZ)
  21. {
  22. m_halfBoxSize = sizeXYZ / 2.0f;
  23. updateMembers();
  24. m_shapeDirty = true;
  25. }
  26. Vec3 getBoxVolumeSize() const
  27. {
  28. return m_halfBoxSize * 2.0f;
  29. }
  30. Aabb getAabbWorldSpace() const
  31. {
  32. return Aabb(-m_halfBoxSize + m_worldPosition, m_halfBoxSize + m_worldPosition);
  33. }
  34. /// Set the cell size in meters.
  35. void setCellSize(F32 cellSize)
  36. {
  37. ANKI_ASSERT(cellSize > 0.0f);
  38. m_cellSize = cellSize;
  39. updateMembers();
  40. m_shapeDirty = true;
  41. }
  42. F32 getCellSize() const
  43. {
  44. return m_cellSize;
  45. }
  46. F32 getFadeDistance() const
  47. {
  48. return m_fadeDistance;
  49. }
  50. void setFadeDistance(F32 dist)
  51. {
  52. m_fadeDistance = max(0.0f, dist);
  53. }
  54. /// Returns true if it's marked for update this frame.
  55. Bool getMarkedForRendering() const
  56. {
  57. return m_markedForRendering;
  58. }
  59. /// Get the cell position that will be rendered this frame.
  60. Vec3 getRenderPosition() const
  61. {
  62. ANKI_ASSERT(m_renderPosition > -m_halfBoxSize + m_worldPosition
  63. && m_renderPosition < m_halfBoxSize + m_worldPosition);
  64. ANKI_ASSERT(m_markedForRendering);
  65. return m_renderPosition;
  66. }
  67. void setupGlobalIlluminationProbeQueueElement(GlobalIlluminationProbeQueueElement& el)
  68. {
  69. el.m_uuid = m_uuid;
  70. el.m_feedbackCallback = giProbeQueueElementFeedbackCallback;
  71. el.m_feedbackCallbackUserData = this;
  72. el.m_debugDrawCallback = [](RenderQueueDrawContext& ctx, ConstWeakArray<void*> userData) {
  73. ANKI_ASSERT(userData.getSize() == 1);
  74. static_cast<const GlobalIlluminationProbeComponent*>(userData[0])->draw(ctx);
  75. };
  76. el.m_debugDrawCallbackUserData = this;
  77. el.m_renderQueues = {};
  78. el.m_aabbMin = -m_halfBoxSize + m_worldPosition;
  79. el.m_aabbMax = m_halfBoxSize + m_worldPosition;
  80. el.m_cellCounts = m_cellCounts;
  81. el.m_totalCellCount = m_cellCounts.x() * m_cellCounts.y() * m_cellCounts.z();
  82. el.m_cellSizes = (m_halfBoxSize * 2.0f) / Vec3(m_cellCounts);
  83. el.m_fadeDistance = m_fadeDistance;
  84. }
  85. void setWorldPosition(const Vec3& pos)
  86. {
  87. m_worldPosition = pos;
  88. m_shapeDirty = true;
  89. }
  90. ANKI_USE_RESULT Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override
  91. {
  92. updated = m_shapeDirty;
  93. m_shapeDirty = false;
  94. return Error::NONE;
  95. }
  96. private:
  97. SceneNode* m_node;
  98. U64 m_uuid;
  99. Vec3 m_halfBoxSize = Vec3(0.5f);
  100. Vec3 m_worldPosition = Vec3(0.0f);
  101. Vec3 m_renderPosition = Vec3(0.0f);
  102. UVec3 m_cellCounts = UVec3(2u);
  103. F32 m_cellSize = 4.0f; ///< Cell size in meters.
  104. F32 m_fadeDistance = 0.2f;
  105. Bool m_markedForRendering : 1;
  106. Bool m_shapeDirty : 1;
  107. ImageResourcePtr m_debugImage;
  108. static void giProbeQueueElementFeedbackCallback(Bool fillRenderQueuesOnNextFrame, void* userData,
  109. const Vec4& eyeWorldPosition)
  110. {
  111. ANKI_ASSERT(userData);
  112. GlobalIlluminationProbeComponent& self = *static_cast<GlobalIlluminationProbeComponent*>(userData);
  113. ANKI_ASSERT(!(fillRenderQueuesOnNextFrame
  114. && (eyeWorldPosition.xyz() < -self.m_halfBoxSize + self.m_worldPosition
  115. || eyeWorldPosition.xyz() > self.m_halfBoxSize + self.m_worldPosition)));
  116. self.m_markedForRendering = fillRenderQueuesOnNextFrame;
  117. self.m_renderPosition = eyeWorldPosition.xyz();
  118. }
  119. /// Recalc come values.
  120. void updateMembers()
  121. {
  122. const Vec3 dist = m_halfBoxSize * 2.0f;
  123. m_cellCounts = UVec3(dist / m_cellSize);
  124. m_cellCounts = m_cellCounts.max(UVec3(1));
  125. }
  126. void draw(RenderQueueDrawContext& ctx) const;
  127. };
  128. /// @}
  129. } // end namespace anki