SkinnedMeshContainer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <ProceduralSkinnedMesh.h>
  10. #include <AzCore/std/containers/vector.h>
  11. #include <AzCore/Math/Transform.h>
  12. #include <Atom/Feature/SkinnedMesh/SkinnedMeshFeatureProcessorInterface.h>
  13. #include <Atom/Feature/SkinnedMesh/SkinnedMeshOutputStreamManagerInterface.h>
  14. #include <Atom/Feature/SkinnedMesh/SkinnedMeshFeatureProcessorBus.h>
  15. #include <Atom/Feature/Mesh/MeshFeatureProcessorInterface.h>
  16. namespace AtomSampleViewer
  17. {
  18. //! Helper class used by the AtomSampleViewer examples.
  19. //! Stores a list of skinned meshes and will automatically release them upon destruction of the container.
  20. //! The skinned mesh input buffers are generated using the ProceduralSkinnedMesh class, so that you can easily create
  21. //! an arbitrary number of skinned meshes with arbitrary complexity such as vertex count and bone count.
  22. //! Currently supports 1 lod per skinned mesh, one sub-mesh per lod, and 1-4 influences per vertex.
  23. //! Currently supports a 1-1 mapping of skinned mesh inputs to skinned mesh instances.
  24. class SkinnedMeshContainer
  25. : private AZ::Render::SkinnedMeshOutputStreamNotificationBus::Handler
  26. {
  27. public:
  28. struct SkinnedMesh
  29. {
  30. ProceduralSkinnedMesh m_proceduralSkinnedMesh;
  31. AZStd::intrusive_ptr<AZ::Render::SkinnedMeshInputBuffers> m_skinnedMeshInputBuffers = nullptr;
  32. uint32_t m_useCount = 0;
  33. };
  34. struct RenderData
  35. {
  36. AZ::Transform m_rootTransform = AZ::Transform::CreateIdentity();
  37. AZ::Render::SkinnedMeshFeatureProcessorInterface::SkinnedMeshHandle m_skinnedMeshHandle;
  38. AZStd::intrusive_ptr<AZ::Render::SkinnedMeshInstance> m_skinnedMeshInstance = nullptr;
  39. AZ::Data::Instance<AZ::RPI::Buffer> m_boneTransformBuffer = nullptr;
  40. AZStd::shared_ptr<AZ::Render::MeshFeatureProcessorInterface::MeshHandle> m_meshHandle;
  41. };
  42. SkinnedMeshContainer(AZ::Render::SkinnedMeshFeatureProcessorInterface* skinnedMeshFeatureProcessor, AZ::Render::MeshFeatureProcessorInterface* meshFeatureProcessor, const SkinnedMeshConfig& config);
  43. AZ_DISABLE_COPY(SkinnedMeshContainer);
  44. ~SkinnedMeshContainer();
  45. void SetActiveSkinnedMeshCount(uint32_t activeSkinnedMeshCount);
  46. uint32_t GetMaxSkinnedMeshes() const { return aznumeric_cast<uint32_t>(m_skinnedMeshes.size()); }
  47. uint32_t GetActiveSkinnedMeshCount() const { return m_activeSkinnedMeshCount; }
  48. SkinnedMeshConfig GetSkinnedMeshConfig() const;
  49. void SetSkinnedMeshConfig(const SkinnedMeshConfig& skinnedMeshConfig);
  50. void UpdateAnimation(float time, bool useOutOfSyncBoneAnimation);
  51. void DrawBones();
  52. private:
  53. void SetupSkinnedMeshes();
  54. void SetupNewSkinnedMesh(SkinnedMeshConfig& skinnedMeshConfig);
  55. void AcquireSkinnedMesh(uint32_t i);
  56. void CreateInstance(uint32_t i);
  57. void ReleaseSkinnedMesh(uint32_t i);
  58. // SkinnedMeshOutputStreamNotificationBus::Handler overrides
  59. void OnSkinnedMeshOutputStreamMemoryAvailable() override;
  60. AZStd::vector<SkinnedMesh> m_skinnedMeshes;
  61. AZStd::vector<RenderData> m_skinnedMeshInstances;
  62. AZ::Render::MeshFeatureProcessorInterface* m_meshFeatureProcessor = nullptr;
  63. AZ::Render::SkinnedMeshFeatureProcessorInterface* m_skinnedMeshFeatureProcessor = nullptr;
  64. uint32_t m_activeSkinnedMeshCount = 0;
  65. SkinnedMeshConfig m_skinnedMeshConfig;
  66. AZStd::queue<uint32_t> m_instancesOutOfMemory;
  67. };
  68. } // namespace AtomSampleViewer