SurfaceDataMeshComponent.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/Asset/AssetCommon.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/NonUniformScaleBus.h>
  12. #include <AzCore/Component/TickBus.h>
  13. #include <AzCore/Component/TransformBus.h>
  14. #include <AzCore/std/containers/unordered_map.h>
  15. #include <AzCore/std/parallel/shared_mutex.h>
  16. #include <AtomLyIntegration/CommonFeatures/Mesh/MeshComponentBus.h>
  17. #include <SurfaceData/SurfaceDataProviderRequestBus.h>
  18. #include <SurfaceData/SurfaceDataTypes.h>
  19. namespace LmbrCentral
  20. {
  21. template<typename, typename>
  22. class EditorWrappedComponentBase;
  23. }
  24. namespace SurfaceData
  25. {
  26. constexpr float s_rayAABBHeightPadding = 0.1f;
  27. class SurfaceDataMeshConfig
  28. : public AZ::ComponentConfig
  29. {
  30. public:
  31. AZ_CLASS_ALLOCATOR(SurfaceDataMeshConfig, AZ::SystemAllocator);
  32. AZ_RTTI(SurfaceDataMeshConfig, "{764C602E-7CA8-4BCC-AB2D-3E46623B3A20}", AZ::ComponentConfig);
  33. static void Reflect(AZ::ReflectContext* context);
  34. SurfaceTagVector m_tags;
  35. };
  36. class SurfaceDataMeshComponent
  37. : public AZ::Component
  38. , public AZ::TickBus::Handler
  39. , public AZ::TransformNotificationBus::Handler
  40. , public AZ::Render::MeshComponentNotificationBus::Handler
  41. , public SurfaceDataProviderRequestBus::Handler
  42. {
  43. public:
  44. template<typename, typename> friend class LmbrCentral::EditorWrappedComponentBase;
  45. AZ_COMPONENT(SurfaceDataMeshComponent, "{F8915F34-BE8B-40B4-B7E8-01EBF3DA1C95}");
  46. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  47. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  48. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  49. static void Reflect(AZ::ReflectContext* context);
  50. SurfaceDataMeshComponent(const SurfaceDataMeshConfig& configuration);
  51. SurfaceDataMeshComponent();
  52. ~SurfaceDataMeshComponent() = default;
  53. //////////////////////////////////////////////////////////////////////////
  54. // AZ::Component interface implementation
  55. void Activate() override;
  56. void Deactivate() override;
  57. bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override;
  58. bool WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const override;
  59. //////////////////////////////////////////////////////////////////////////
  60. // MeshComponentNotificationBus
  61. void OnModelReady(const AZ::Data::Asset<AZ::RPI::ModelAsset>& modelAsset, const AZ::Data::Instance<AZ::RPI::Model>& model) override;
  62. //////////////////////////////////////////////////////////////////////////
  63. // TransformNotificationBus
  64. void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override;
  65. ////////////////////////////////////////////////////////////////////////
  66. // AZ::TickBus
  67. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  68. ////////////////////////////////////////////////////////////////////////
  69. // SurfaceDataProviderRequestBus
  70. void GetSurfacePoints(const AZ::Vector3& inPosition, SurfacePointList& surfacePointList) const override;
  71. void GetSurfacePointsFromList(AZStd::span<const AZ::Vector3> inPositions, SurfacePointList& surfacePointList) const override;
  72. private:
  73. void UpdateMeshData();
  74. void OnCompositionChanged();
  75. AZ::Aabb GetSurfaceAabb() const;
  76. SurfaceTagVector GetSurfaceTags() const;
  77. AZ::NonUniformScaleChangedEvent::Handler m_nonUniformScaleChangedHandler; ///< Responds to changes in non-uniform scale.
  78. SurfaceDataMeshConfig m_configuration;
  79. SurfaceDataRegistryHandle m_providerHandle = InvalidSurfaceDataRegistryHandle;
  80. // cached data
  81. AZStd::atomic_bool m_refresh{ false };
  82. mutable AZStd::shared_mutex m_cacheMutex;
  83. AZ::Data::Asset<AZ::Data::AssetData> m_meshAssetData;
  84. AZ::Transform m_meshWorldTM = AZ::Transform::CreateIdentity();
  85. AZ::Transform m_meshWorldTMInverse = AZ::Transform::CreateIdentity();
  86. AZ::Vector3 m_meshNonUniformScale = AZ::Vector3::CreateOne();
  87. AZ::Aabb m_meshBounds = AZ::Aabb::CreateNull();
  88. SurfaceTagWeights m_newPointWeights;
  89. };
  90. }