DiskShape.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/TransformBus.h>
  10. #include <AzCore/std/parallel/shared_mutex.h>
  11. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  12. #include <LmbrCentral/Shape/DiskShapeComponentBus.h>
  13. namespace AzFramework
  14. {
  15. class DebugDisplayRequests;
  16. }
  17. namespace LmbrCentral
  18. {
  19. struct ShapeDrawParams;
  20. //! Provide DiskShape functionality.
  21. class DiskShape
  22. : public ShapeComponentRequestsBus::Handler
  23. , public DiskShapeComponentRequestBus::Handler
  24. , public AZ::TransformNotificationBus::Handler
  25. {
  26. public:
  27. AZ_CLASS_ALLOCATOR(DiskShape, AZ::SystemAllocator)
  28. AZ_RTTI(DiskShape, "{21E75068-3E05-4DD2-981A-DAEB0B1A9BC4}")
  29. static void Reflect(AZ::ReflectContext* context);
  30. void Activate(AZ::EntityId entityId);
  31. void Deactivate();
  32. void InvalidateCache(InvalidateShapeCacheReason reason);
  33. // ShapeComponentRequestsBus
  34. AZ::Crc32 GetShapeType() const override { return AZ_CRC_CE("DiskShape"); }
  35. AZ::Aabb GetEncompassingAabb() const override;
  36. void GetTransformAndLocalBounds(AZ::Transform& transform, AZ::Aabb& bounds) const override;
  37. bool IsPointInside(const AZ::Vector3& point) const override;
  38. float DistanceSquaredFromPoint(const AZ::Vector3& point) const override;
  39. bool IntersectRay(const AZ::Vector3& src, const AZ::Vector3& dir, float& distance) const override;
  40. // DiskShapeComponentRequestBus
  41. const DiskShapeConfig& GetDiskConfiguration() const override;
  42. void SetRadius(float radius) override;
  43. float GetRadius() const override;
  44. const AZ::Vector3& GetNormal() const override;
  45. // AZ::TransformNotificationBus
  46. void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override;
  47. void SetDiskConfiguration(const DiskShapeConfig& diskShapeConfig);
  48. const AZ::Transform& GetCurrentTransform() const;
  49. protected:
  50. friend class EditorDiskShapeComponent;
  51. ShapeComponentConfig& ModifyShapeComponent();
  52. private:
  53. //! Runtime data - cache potentially expensive operations.
  54. class DiskIntersectionDataCache
  55. : public IntersectionTestDataCache<DiskShapeConfig>
  56. {
  57. void UpdateIntersectionParamsImpl(
  58. const AZ::Transform& currentTransform, const DiskShapeConfig& configuration,
  59. const AZ::Vector3& currentNonUniformScale = AZ::Vector3::CreateOne()) override;
  60. friend class DiskShape;
  61. AZ::Vector3 m_position; ///< Position of the center of the disk.
  62. AZ::Vector3 m_normal; ///< normal of the disk.
  63. float m_radius = 0.0f; ///< Radius of the disk (including entity scale).
  64. };
  65. DiskShapeConfig m_diskShapeConfig; ///< Underlying disk configuration.
  66. mutable DiskIntersectionDataCache m_intersectionDataCache; ///< Caches transient intersection data.
  67. AZ::Transform m_currentTransform; ///< Caches the current world transform.
  68. AZ::EntityId m_entityId; ///< The Id of the entity the shape is attached to.
  69. mutable AZStd::shared_mutex m_mutex; ///< Mutex to allow multiple readers but single writer for efficient thread safety
  70. };
  71. void DrawDiskShape(
  72. const ShapeDrawParams& shapeDrawParams, const DiskShapeConfig& diskShapeConfig,
  73. AzFramework::DebugDisplayRequests& debugDisplay);
  74. } // namespace LmbrCentral