DecalComponent.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #pragma once
  6. #include <AnKi/Scene/Components/SceneComponent.h>
  7. #include <AnKi/Scene/Spatial.h>
  8. #include <AnKi/Scene/GpuSceneArray.h>
  9. #include <AnKi/Resource/ImageAtlasResource.h>
  10. #include <AnKi/Collision/Obb.h>
  11. #include <AnKi/Renderer/RenderQueue.h>
  12. namespace anki {
  13. /// @addtogroup scene
  14. /// @{
  15. /// Decal component. Contains all the relevant info for a deferred decal.
  16. class DecalComponent : public SceneComponent
  17. {
  18. ANKI_SCENE_COMPONENT(DecalComponent)
  19. public:
  20. static constexpr U32 kAtlasSubImageMargin = 16;
  21. DecalComponent(SceneNode* node);
  22. ~DecalComponent();
  23. Bool isEnabled() const
  24. {
  25. return m_layers[LayerType::kDiffuse].m_bindlessTextureIndex != kMaxU32
  26. || m_layers[LayerType::kRoughnessMetalness].m_bindlessTextureIndex != kMaxU32;
  27. }
  28. void loadDiffuseImageResource(CString fname, F32 blendFactor)
  29. {
  30. setLayer(fname, blendFactor, LayerType::kDiffuse);
  31. }
  32. void loadRoughnessMetalnessImageResource(CString fname, F32 blendFactor)
  33. {
  34. setLayer(fname, blendFactor, LayerType::kRoughnessMetalness);
  35. }
  36. /// Update the internal structures.
  37. void setBoxVolumeSize(const Vec3& sizeXYZ)
  38. {
  39. m_boxSize = sizeXYZ;
  40. m_dirty = true;
  41. }
  42. const Vec3& getBoxVolumeSize() const
  43. {
  44. return m_boxSize;
  45. }
  46. void setupDecalQueueElement(DecalQueueElement& el) const
  47. {
  48. ANKI_ASSERT(isEnabled());
  49. el.m_diffuseBindlessTextureIndex = m_layers[LayerType::kDiffuse].m_bindlessTextureIndex;
  50. el.m_roughnessMetalnessBindlessTextureIndex = m_layers[LayerType::kRoughnessMetalness].m_bindlessTextureIndex;
  51. el.m_diffuseBlendFactor = m_layers[LayerType::kDiffuse].m_blendFactor;
  52. el.m_roughnessMetalnessBlendFactor = m_layers[LayerType::kRoughnessMetalness].m_blendFactor;
  53. el.m_textureMatrix = m_biasProjViewMat;
  54. el.m_obbCenter = m_obb.getCenter().xyz();
  55. el.m_obbExtend = m_obb.getExtend().xyz();
  56. el.m_obbRotation = m_obb.getRotation().getRotationPart();
  57. el.m_index = m_gpuSceneDecal.getIndex();
  58. }
  59. private:
  60. enum class LayerType : U8
  61. {
  62. kDiffuse,
  63. kRoughnessMetalness,
  64. kCount
  65. };
  66. class Layer
  67. {
  68. public:
  69. ImageResourcePtr m_image;
  70. F32 m_blendFactor = 0.0f;
  71. U32 m_bindlessTextureIndex = kMaxU32;
  72. };
  73. Spatial m_spatial;
  74. Array<Layer, U(LayerType::kCount)> m_layers;
  75. Mat4 m_biasProjViewMat = Mat4::getIdentity();
  76. Vec3 m_boxSize = Vec3(1.0f);
  77. Obb m_obb = Obb(Vec4(0.0f), Mat3x4::getIdentity(), Vec4(0.5f, 0.5f, 0.5f, 0.0f));
  78. GpuSceneArrays::Decal::Allocation m_gpuSceneDecal;
  79. Bool m_dirty = true;
  80. void setLayer(CString fname, F32 blendFactor, LayerType type);
  81. Error update(SceneComponentUpdateInfo& info, Bool& updated);
  82. };
  83. /// @}
  84. } // end namespace anki