3
0

SimpleLODComponent.h 4.1 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 <AzCore/Component/Component.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/Component/TickBus.h>
  12. #include <AzCore/Asset/AssetCommon.h>
  13. #include <AzCore/Script/ScriptProperty.h>
  14. #include <AzCore/std/smart_ptr/unique_ptr.h>
  15. #include <Integration/Assets/MotionAsset.h>
  16. #include <Integration/ActorComponentBus.h>
  17. #include <AtomLyIntegration/CommonFeatures/Mesh/MeshComponentBus.h>
  18. namespace EMotionFX
  19. {
  20. namespace Integration
  21. {
  22. class SimpleLODComponent
  23. : public AZ::Component
  24. , private AZ::TickBus::Handler
  25. , private ActorComponentNotificationBus::Handler
  26. {
  27. public:
  28. friend class EditorSimpleLODComponent;
  29. AZ_COMPONENT(SimpleLODComponent, "{9380B039-EB03-4920-9F06-D90481E739E6}");
  30. /**
  31. * Configuration struct for procedural configuration of SimpleLODComponents.
  32. */
  33. struct Configuration
  34. {
  35. AZ_TYPE_INFO(Configuration, "{262470E5-57D8-4C45-8BB4-88EDFBC54D7E}");
  36. Configuration() = default;
  37. void Reset();
  38. // Generate the default value based on LOD level.
  39. void GenerateDefaultValue(size_t numLODs);
  40. bool GetEnableLodSampling();
  41. static void Reflect(AZ::ReflectContext* context);
  42. AZStd::vector<float> m_lodDistances; // LOD distances that decide which lod the actor should choose.
  43. AZStd::vector<float> m_lodSampleRates; // Per LOD sample rate.
  44. bool m_enableLodSampling = false; // Enable per LOD sampling rate. This will allow animation to sample at a lower rate for performance improvement.
  45. };
  46. SimpleLODComponent(const Configuration* config = nullptr);
  47. ~SimpleLODComponent();
  48. // AZ::Component interface implementation
  49. void Init() override;
  50. void Activate() override;
  51. void Deactivate() override;
  52. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  53. {
  54. provided.push_back(AZ_CRC("EMotionFXSimpleLODService", 0xa9b5f358));
  55. }
  56. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  57. {
  58. AZ_UNUSED(dependent);
  59. }
  60. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  61. {
  62. required.push_back(AZ_CRC("EMotionFXActorService", 0xd6e8f48d));
  63. }
  64. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  65. {
  66. incompatible.push_back(AZ_CRC("EMotionFXSimpleLODService", 0xa9b5f358));
  67. }
  68. static void Reflect(AZ::ReflectContext* context);
  69. private:
  70. // ActorComponentNotificationBus::Handler
  71. void OnActorInstanceCreated(EMotionFX::ActorInstance* actorInstance) override;
  72. void OnActorInstanceDestroyed(EMotionFX::ActorInstance* actorInstance) override;
  73. // AZ::TickBus::Handler
  74. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  75. static size_t GetLodByDistance(const AZStd::vector<float>& distances, float distance);
  76. static void UpdateLodLevelByDistance(EMotionFX::ActorInstance* actorInstance, const Configuration& configuration, AZ::EntityId entityId);
  77. Configuration m_configuration; // Component configuration.
  78. EMotionFX::ActorInstance* m_actorInstance; // Associated actor instance (retrieved from Actor Component).
  79. AZ::RPI::Cullable::LodType m_previousLodType = AZ::RPI::Cullable::LodType::Default;
  80. size_t m_previousLodLevel = 0;
  81. };
  82. } // namespace Integration
  83. } // namespace EMotionFX