GlobalIlluminationProbeComponent.h 4.0 KB

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