GlobalIlluminationProbeComponent.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (C) 2009-present, 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/Scene/Frustum.h>
  8. #include <AnKi/Scene/GpuSceneArray.h>
  9. #include <AnKi/Collision/Aabb.h>
  10. namespace anki {
  11. // Global illumination probe component. It's an axis aligned box divided into cells.
  12. class GlobalIlluminationProbeComponent : public SceneComponent
  13. {
  14. ANKI_SCENE_COMPONENT(GlobalIlluminationProbeComponent)
  15. public:
  16. GlobalIlluminationProbeComponent(SceneNode* node);
  17. ~GlobalIlluminationProbeComponent();
  18. // Set the cell size in meters.
  19. void setCellSize(F32 cellSize)
  20. {
  21. if(ANKI_EXPECT(cellSize > 0.0f) && m_cellSize != cellSize)
  22. {
  23. m_cellSize = cellSize;
  24. m_dirty = true;
  25. }
  26. }
  27. F32 getCellSize() const
  28. {
  29. return m_cellSize;
  30. }
  31. F32 getFadeDistance() const
  32. {
  33. return m_fadeDistance;
  34. }
  35. void setFadeDistance(F32 dist)
  36. {
  37. if(ANKI_EXPECT(dist > 0.0f) && m_fadeDistance != dist)
  38. {
  39. m_fadeDistance = dist;
  40. m_dirty = true;
  41. }
  42. }
  43. ANKI_INTERNAL Vec3 getBoxVolumeSize() const
  44. {
  45. return m_halfSize * 2.0f;
  46. }
  47. // Check if any of the probe's cells need to be re-rendered.
  48. Bool getCellsNeedsRefresh() const
  49. {
  50. return m_cellsRefreshedCount < m_totalCellCount;
  51. }
  52. ANKI_INTERNAL U32 getNextCellForRefresh() const
  53. {
  54. ANKI_ASSERT(getCellsNeedsRefresh());
  55. return m_cellsRefreshedCount;
  56. }
  57. // Add to the number of texels that got refreshed this frame.
  58. ANKI_INTERNAL void incrementRefreshedCells(U32 cellCount)
  59. {
  60. ANKI_ASSERT(getCellsNeedsRefresh());
  61. m_cellsRefreshedCount += cellCount;
  62. ANKI_ASSERT(m_cellsRefreshedCount <= m_totalCellCount);
  63. m_dirty = true;
  64. }
  65. // The radius around the probe's center that can infuence the rendering of the env texture.
  66. ANKI_INTERNAL F32 getRenderRadius() const;
  67. ANKI_INTERNAL F32 getShadowsRenderRadius() const;
  68. ANKI_INTERNAL const Vec3& getWorldPosition() const
  69. {
  70. return m_worldPos;
  71. }
  72. ANKI_INTERNAL const UVec3& getCellCountsPerDimension() const
  73. {
  74. return m_cellCounts;
  75. }
  76. ANKI_INTERNAL U32 getCellCount() const
  77. {
  78. return m_totalCellCount;
  79. }
  80. ANKI_INTERNAL Texture& getVolumeTexture() const
  81. {
  82. return *m_volTex;
  83. }
  84. ANKI_INTERNAL const GpuSceneArrays::GlobalIlluminationProbe::Allocation& getGpuSceneAllocation() const
  85. {
  86. return m_gpuSceneProbe;
  87. }
  88. private:
  89. Vec3 m_halfSize = Vec3(0.5f);
  90. Vec3 m_worldPos = Vec3(0.0f);
  91. UVec3 m_cellCounts = UVec3(2u);
  92. U32 m_totalCellCount = 8u;
  93. F32 m_cellSize = 4.0f; // Cell size in meters.
  94. F32 m_fadeDistance = 0.2f;
  95. TexturePtr m_volTex;
  96. U32 m_volTexBindlessIdx = 0;
  97. GpuSceneArrays::GlobalIlluminationProbe::Allocation m_gpuSceneProbe;
  98. ShaderProgramResourcePtr m_clearTextureProg;
  99. U32 m_cellsRefreshedCount = 0;
  100. Bool m_dirty = true;
  101. void update(SceneComponentUpdateInfo& info, Bool& updated) override;
  102. Error serialize(SceneSerializer& serializer) override;
  103. };
  104. } // end namespace anki