ParticleEmitter2Component.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/RenderStateBucket.h>
  8. #include <AnKi/Scene/GpuSceneArray.h>
  9. #include <AnKi/Resource/ParticleEmitterResource2.h>
  10. #include <AnKi/GpuMemory/UnifiedGeometryBuffer.h>
  11. #include <AnKi/Collision/Aabb.h>
  12. #include <AnKi/Util/WeakArray.h>
  13. namespace anki {
  14. enum class ParticleGeometryType : U8
  15. {
  16. kQuad,
  17. kMeshComponent,
  18. kCount,
  19. kFirst = 0
  20. };
  21. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(ParticleGeometryType)
  22. inline constexpr Array<const Char*, U32(ParticleGeometryType::kCount)> kParticleEmitterGeometryTypeName = {"Quad", "MeshComponent"};
  23. // Contains a particle emitter resource and maybe connects to a mesh component
  24. class ParticleEmitter2Component : public SceneComponent
  25. {
  26. ANKI_SCENE_COMPONENT(ParticleEmitter2Component)
  27. public:
  28. ParticleEmitter2Component(SceneNode* node);
  29. ~ParticleEmitter2Component();
  30. ParticleEmitter2Component& setParticleEmitterFilename(CString filename);
  31. CString getParticleEmitterFilename() const;
  32. Bool hasParticleEmitterResource() const
  33. {
  34. return !!m_resource;
  35. }
  36. ParticleEmitter2Component& setParticleGeometryType(ParticleGeometryType type)
  37. {
  38. if(type != m_geomType && ANKI_EXPECT(type < ParticleGeometryType::kCount))
  39. {
  40. m_geomType = type;
  41. m_anyDirty = true;
  42. }
  43. return *this;
  44. }
  45. ParticleGeometryType getParticleGeometryType() const
  46. {
  47. return m_geomType;
  48. }
  49. Bool isValid() const;
  50. ANKI_INTERNAL U32 getGpuSceneMeshLodIndex(U32 submeshIdx) const;
  51. ANKI_INTERNAL U32 getGpuSceneParticleEmitter2Index() const
  52. {
  53. ANKI_ASSERT(isValid());
  54. return m_gpuScene.m_gpuSceneParticleEmitter.getIndex();
  55. }
  56. // Return true if the above indices changed this frame
  57. ANKI_INTERNAL Bool gpuSceneReallocationsThisFrame() const
  58. {
  59. ANKI_ASSERT(isValid());
  60. return m_gpuSceneReallocationsThisFrame;
  61. }
  62. // The renderer will call it to update the bounds of the emitter
  63. ANKI_INTERNAL void updateBoundingVolume(Vec3 min, Vec3 max)
  64. {
  65. ANKI_ASSERT(min < max);
  66. m_boundingVolume = {min, max};
  67. }
  68. ANKI_INTERNAL Array<Vec3, 2> getBoundingVolume() const
  69. {
  70. ANKI_ASSERT(isValid());
  71. return m_boundingVolume;
  72. }
  73. ANKI_INTERNAL ParticleEmitterResource2& getParticleEmitterResource() const
  74. {
  75. ANKI_ASSERT(isValid());
  76. return *m_resource;
  77. }
  78. ANKI_INTERNAL F32 getDt() const
  79. {
  80. ANKI_ASSERT(isValid());
  81. return m_dt;
  82. }
  83. private:
  84. class ParticleEmitterQuadGeometry;
  85. ParticleEmitterResource2Ptr m_resource;
  86. MeshComponent* m_meshComponent = nullptr;
  87. class
  88. {
  89. public:
  90. Array<GpuSceneBufferAllocation, U32(ParticleProperty::kCount)> m_particleStreams;
  91. GpuSceneBufferAllocation m_aliveParticleIndices;
  92. GpuSceneBufferAllocation m_anKiParticleEmitterProperties;
  93. GpuSceneArrays::ParticleEmitter2::Allocation m_gpuSceneParticleEmitter;
  94. } m_gpuScene;
  95. Array<Vec3, 2> m_boundingVolume = {Vec3(-0.5f), Vec3(0.5f)};
  96. F32 m_dt = 0.0f;
  97. ParticleGeometryType m_geomType = ParticleGeometryType::kQuad;
  98. Bool m_anyDirty = true;
  99. Bool m_gpuSceneReallocationsThisFrame = true; // Only tracks the memory shared externally
  100. void update(SceneComponentUpdateInfo& info, Bool& updated) override;
  101. Error serialize(SceneSerializer& serializer) override;
  102. void onOtherComponentRemovedOrAdded(SceneComponent* other, Bool added) override;
  103. };
  104. } // end namespace anki