DynamicMaterialTestComponent.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/ImGuiHistogramQueue.h>
  12. #include <Atom/RPI.Reflect/Material/MaterialPropertyDescriptor.h>
  13. #include <AzCore/Component/TickBus.h>
  14. #include <AzCore/std/functional.h>
  15. namespace AtomSampleViewer
  16. {
  17. //! This test loads a configurable lattice of entities, gives them all a unique Material instance, and
  18. //! changes a material property value every frame. UI to configure the size of the lattice is included.
  19. class DynamicMaterialTestComponent final
  20. : public EntityLatticeTestComponent
  21. , public AZ::TickBus::Handler
  22. {
  23. using Base = EntityLatticeTestComponent;
  24. public:
  25. AZ_COMPONENT(DynamicMaterialTestComponent, "{14E4AD00-BB56-4771-809F-1AF7BD3611D9}", EntityLatticeTestComponent);
  26. DynamicMaterialTestComponent();
  27. static void Reflect(AZ::ReflectContext* context);
  28. // AZ::Component overrides...
  29. void Activate() override;
  30. void Deactivate() override;
  31. private:
  32. AZ_DISABLE_COPY_MOVE(DynamicMaterialTestComponent);
  33. void InitMaterialConfigs();
  34. //! EntityLatticeTestComponent overrides...
  35. void PrepareCreateLatticeInstances(uint32_t instanceCount) override;
  36. void CreateLatticeInstance(const AZ::Transform& transform) override;
  37. void DestroyLatticeInstances() override;
  38. // AZ::TickBus::Handler overrides...
  39. void OnTick(float deltaTime, AZ::ScriptTimePoint scriptTime) override;
  40. void CompileWithTimer(AZ::Data::Instance<AZ::RPI::Material> material);
  41. void UpdateStandardPbrColors();
  42. void UpdateEmissiveMaterialIntensity();
  43. void CompileMaterials();
  44. ImGuiSidebar m_imguiSidebar;
  45. bool m_pause = false;
  46. struct MaterialConfig
  47. {
  48. AZStd::string m_name;
  49. AZ::Data::Asset<AZ::RPI::MaterialAsset> m_materialAsset;
  50. AZStd::function<void()> m_updateLatticeMaterials;
  51. };
  52. AZStd::vector<MaterialConfig> m_materialConfigs;
  53. int m_currentMaterialConfig;
  54. AZ::Data::Asset<AZ::RPI::ModelAsset> m_modelAsset;
  55. AZStd::vector<AZ::Render::MeshFeatureProcessorInterface::MeshHandle> m_meshHandles;
  56. AZStd::vector<AZ::Data::Instance<AZ::RPI::Material>> m_materials;
  57. static constexpr AZStd::size_t CompileTimerQueueSize = 30;
  58. ImGuiHistogramQueue m_compileTimer;
  59. bool m_waitingForMeshes = false;
  60. uint32_t m_loadedMeshCounter = 0;
  61. AZStd::vector<AZ::Render::MeshFeatureProcessorInterface::ModelChangedEvent::Handler> m_meshLoadEventHandlers;
  62. float m_currentTime = 0.0f;
  63. };
  64. } // namespace AtomSampleViewer