AssetLoadTestComponent.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <EntityLatticeTestComponent.h>
  10. #include <Utils/ImGuiSidebar.h>
  11. #include <Utils/ImGuiAssetBrowser.h>
  12. #include <AzCore/Component/TickBus.h>
  13. namespace AtomSampleViewer
  14. {
  15. /*
  16. This test loads a configurable lattice of entities and swaps out each entity's model and material
  17. at given time steps. Each entity can have its assets swapped very rapidly (10ths of seconds).
  18. The assets that are applied to the entities are chosen from a configurable
  19. "allow-list" of models and materials. The allow-list is saved to the cache's user folder and
  20. loaded on startup. This makes it easy to chose "working" assets to use in the test vs more development
  21. assets that may not be working properly. It also allows you to build cases where we want
  22. to test instancing more than loading. UI to modify allow-list is a core part of this component.
  23. */
  24. class AssetLoadTestComponent final
  25. : public EntityLatticeTestComponent
  26. , public AZ::TickBus::Handler
  27. {
  28. using Base = EntityLatticeTestComponent;
  29. public:
  30. AZ_COMPONENT(AssetLoadTestComponent, "{30E6EE46-2CD5-4903-801F-56AE70A33656}", EntityLatticeTestComponent);
  31. static void Reflect(AZ::ReflectContext* context);
  32. AssetLoadTestComponent();
  33. //! AZ::Component overrides...
  34. void Activate() override;
  35. void Deactivate() override;
  36. private:
  37. AZ_DISABLE_COPY_MOVE(AssetLoadTestComponent);
  38. // CommonSampleComponentBase overrides...
  39. void OnAllAssetsReadyActivate() override;
  40. //! EntityLatticeTestComponent overrides...
  41. void PrepareCreateLatticeInstances(uint32_t instanceCount) override;
  42. void CreateLatticeInstance(const AZ::Transform& transform) override;
  43. void FinalizeLatticeInstances() override;
  44. void DestroyLatticeInstances() override;
  45. void DestroyHandles();
  46. AZ::Data::AssetId GetRandomModelId() const;
  47. AZ::Data::AssetId GetRandomMaterialId() const;
  48. void OnTick(float deltaTime, AZ::ScriptTimePoint scriptTime) override;
  49. struct ModelInstanceData
  50. {
  51. AZ::Transform m_transform;
  52. AZ::Data::AssetId m_modelAssetId;
  53. AZ::Data::AssetId m_materialAssetId;
  54. AZ::Render::MeshFeatureProcessorInterface::MeshHandle m_meshHandle;
  55. };
  56. ImGuiSidebar m_imguiSidebar;
  57. ImGuiAssetBrowser m_materialBrowser;
  58. ImGuiAssetBrowser m_modelBrowser;
  59. AZStd::vector<ModelInstanceData> m_modelInstanceData;
  60. struct Compare
  61. {
  62. bool operator()(const AZ::Data::Asset<AZ::RPI::MaterialAsset>& lhs, const AZ::Data::Asset<AZ::RPI::MaterialAsset>& rhs) const
  63. {
  64. if (lhs.GetId().m_guid == rhs.GetId().m_guid)
  65. {
  66. return lhs.GetId().m_subId > rhs.GetId().m_subId;
  67. }
  68. return lhs.GetId().m_guid > rhs.GetId().m_guid;
  69. }
  70. };
  71. using MaterialAssetSet = AZStd::set<AZ::Data::Asset<AZ::RPI::MaterialAsset>, Compare>;
  72. MaterialAssetSet m_cachedMaterials;
  73. uint32_t m_pinnedMaterialCount = 0;
  74. size_t m_lastPinnedModelCount = 0;
  75. float m_lastMaterialSwitchInSeconds = 0;
  76. float m_lastModelSwitchInSeconds = 0;
  77. float m_materialSwitchTimeInSeconds = 5.0f;
  78. float m_modelSwitchTimeInSeconds = 3.0f;
  79. bool m_materialSwitchEnabled = true;
  80. bool m_modelSwitchEnabled = true;
  81. bool m_updateTransformEnabled = false;
  82. };
  83. } // namespace AtomSampleViewer