CylinderShape.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/CylinderShapeComponentBus.h>
  13. namespace AZ
  14. {
  15. enum class RandomDistributionType : AZ::u32;
  16. }
  17. namespace AzFramework
  18. {
  19. class DebugDisplayRequests;
  20. }
  21. namespace LmbrCentral
  22. {
  23. struct ShapeDrawParams;
  24. class CylinderShape
  25. : public ShapeComponentRequestsBus::Handler
  26. , public CylinderShapeComponentRequestsBus::Handler
  27. , public AZ::TransformNotificationBus::Handler
  28. {
  29. public:
  30. AZ_CLASS_ALLOCATOR(CylinderShape, AZ::SystemAllocator)
  31. AZ_RTTI(CylinderShape, "{B45EFEF2-631F-43D3-B538-A3FE68350231}")
  32. static void Reflect(AZ::ReflectContext* context);
  33. void Activate(AZ::EntityId entityId);
  34. void Deactivate();
  35. void InvalidateCache(InvalidateShapeCacheReason reason);
  36. // ShapeComponentRequestsBus::Handler
  37. AZ::Crc32 GetShapeType() const override { return AZ_CRC_CE("Cylinder"); }
  38. bool IsPointInside(const AZ::Vector3& point) const override;
  39. float DistanceSquaredFromPoint(const AZ::Vector3& point) const override;
  40. AZ::Aabb GetEncompassingAabb() const override;
  41. void GetTransformAndLocalBounds(AZ::Transform& transform, AZ::Aabb& bounds) const override;
  42. AZ::Vector3 GenerateRandomPointInside(AZ::RandomDistributionType randomDistribution) const override;
  43. bool IntersectRay(const AZ::Vector3& src, const AZ::Vector3& dir, float& distance) const override;
  44. // CylinderShapeComponentRequestsBus::Handler
  45. const CylinderShapeConfig& GetCylinderConfiguration() const override { return m_cylinderShapeConfig; }
  46. void SetHeight(float height) override;
  47. void SetRadius(float radius) override;
  48. float GetHeight() const override;
  49. float GetRadius() const override;
  50. // AZ::TransformNotificationBus::Handler
  51. void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override;
  52. void SetCylinderConfiguration(const CylinderShapeConfig& cylinderShapeConfig) { m_cylinderShapeConfig = cylinderShapeConfig; }
  53. const AZ::Transform& GetCurrentTransform() const { return m_currentTransform; }
  54. protected:
  55. friend class EditorCylinderShapeComponent;
  56. CylinderShapeConfig& ModifyConfiguration() { return m_cylinderShapeConfig; }
  57. private:
  58. /// Runtime data - cache potentially expensive operations.
  59. class CylinderIntersectionDataCache
  60. : public IntersectionTestDataCache<CylinderShapeConfig>
  61. {
  62. void UpdateIntersectionParamsImpl(
  63. const AZ::Transform& currentTransform, const CylinderShapeConfig& configuration,
  64. const AZ::Vector3& currentNonUniformScale = AZ::Vector3::CreateOne()) override;
  65. friend class CylinderShape;
  66. AZ::Vector3 m_baseCenterPoint; ///< The center point of the cylinder.
  67. AZ::Vector3 m_axisVector; ///< A vector along the axis of this cylinder scaled to the height of the cylinder.
  68. float m_height = 0.0f; ///< Height of the cylinder (including entity scale).
  69. float m_radius = 0.0f; ///< Radius of the cylinder (including entity scale).
  70. };
  71. CylinderShapeConfig m_cylinderShapeConfig; ///< Underlying cylinder configuration.
  72. mutable CylinderIntersectionDataCache m_intersectionDataCache; ///< Caches transient intersection data.
  73. AZ::Transform m_currentTransform; ///< Caches the current World transform.
  74. AZ::EntityId m_entityId; ///< The Id of the entity the shape is attached to.
  75. mutable AZStd::shared_mutex m_mutex; ///< Mutex to allow multiple readers but single writer for efficient thread safety
  76. };
  77. void DrawCylinderShape(
  78. const ShapeDrawParams& shapeDrawParams, const CylinderShapeConfig& cylinderShapeConfig,
  79. AzFramework::DebugDisplayRequests& debugDisplay);
  80. } // namespace LmbrCentral