FogDensityComponent.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include <AnKi/Scene/Components/FogDensityComponent.h>
  6. #include <AnKi/Scene/SceneNode.h>
  7. #include <AnKi/Scene/SceneGraph.h>
  8. #include <AnKi/Core/GpuMemory/GpuSceneBuffer.h>
  9. namespace anki {
  10. FogDensityComponent::FogDensityComponent(SceneNode* node)
  11. : SceneComponent(node, getStaticClassId())
  12. , m_spatial(this)
  13. {
  14. m_gpuSceneIndex = GpuSceneContiguousArrays::getSingleton().allocate(GpuSceneContiguousArrayType::kFogDensityVolumes);
  15. }
  16. FogDensityComponent ::~FogDensityComponent()
  17. {
  18. m_spatial.removeFromOctree(SceneGraph::getSingleton().getOctree());
  19. }
  20. Error FogDensityComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  21. {
  22. updated = m_dirty || info.m_node->movedThisFrame();
  23. if(updated)
  24. {
  25. m_dirty = false;
  26. m_worldPos = info.m_node->getWorldTransform().getOrigin().xyz();
  27. if(m_isBox)
  28. {
  29. const Aabb aabb(m_aabbMin + m_worldPos, m_aabbMax + m_worldPos);
  30. m_spatial.setBoundingShape(aabb);
  31. }
  32. else
  33. {
  34. const Sphere sphere(m_worldPos, m_sphereRadius);
  35. m_spatial.setBoundingShape(sphere);
  36. }
  37. // Upload to the GPU scene
  38. GpuSceneFogDensityVolume gpuVolume;
  39. if(m_isBox)
  40. {
  41. gpuVolume.m_aabbMinOrSphereCenter = m_aabbMin.xyz();
  42. gpuVolume.m_aabbMaxOrSphereRadius = m_aabbMax.xyz();
  43. }
  44. else
  45. {
  46. gpuVolume.m_aabbMaxOrSphereRadius = Vec3(m_sphereRadius);
  47. gpuVolume.m_aabbMinOrSphereCenter = m_worldPos.xyz();
  48. }
  49. gpuVolume.m_isBox = m_isBox;
  50. gpuVolume.m_density = m_density;
  51. GpuSceneMicroPatcher::getSingleton().newCopy(*info.m_framePool, m_gpuSceneIndex.getOffsetInGpuScene(), gpuVolume);
  52. }
  53. const Bool spatialUpdated = m_spatial.update(SceneGraph::getSingleton().getOctree());
  54. updated = updated || spatialUpdated;
  55. return Error::kNone;
  56. }
  57. } // end namespace anki