2
0

BoundsTestComponent.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <Tests/BoundsTestComponent.h>
  9. #include <AzCore/Math/Obb.h>
  10. #include <AzCore/Math/IntersectSegment.h>
  11. #include <AzToolsFramework/ViewportSelection/EditorSelectionUtil.h>
  12. namespace UnitTest
  13. {
  14. AZ::Aabb BoundsTestComponent::GetEditorSelectionBoundsViewport([[maybe_unused]] const AzFramework::ViewportInfo& viewportInfo)
  15. {
  16. return GetWorldBounds();
  17. }
  18. bool BoundsTestComponent::EditorSelectionIntersectRayViewport(
  19. [[maybe_unused]] const AzFramework::ViewportInfo& viewportInfo, const AZ::Vector3& src, const AZ::Vector3& dir, float& distance)
  20. {
  21. return AzToolsFramework::AabbIntersectRay(src, dir, GetWorldBounds(), distance);
  22. }
  23. bool BoundsTestComponent::SupportsEditorRayIntersect()
  24. {
  25. return true;
  26. }
  27. void BoundsTestComponent::Reflect(AZ::ReflectContext* context)
  28. {
  29. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  30. {
  31. serializeContext->Class<BoundsTestComponent, EditorComponentBase>()->Version(1);
  32. }
  33. }
  34. void BoundsTestComponent::Activate()
  35. {
  36. AzFramework::BoundsRequestBus::Handler::BusConnect(GetEntityId());
  37. AzToolsFramework::EditorComponentSelectionRequestsBus::Handler::BusConnect(GetEntityId());
  38. // default local bounds to unit cube
  39. m_localBounds = AZ::Aabb::CreateFromMinMax(AZ::Vector3(-0.5f), AZ::Vector3(0.5f));
  40. }
  41. void BoundsTestComponent::Deactivate()
  42. {
  43. AzToolsFramework::EditorComponentSelectionRequestsBus::Handler::BusDisconnect();
  44. AzFramework::BoundsRequestBus::Handler::BusDisconnect();
  45. }
  46. AZ::Aabb BoundsTestComponent::GetWorldBounds() const
  47. {
  48. AZ::Transform worldFromLocal = AZ::Transform::CreateIdentity();
  49. AZ::TransformBus::EventResult(worldFromLocal, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  50. return GetLocalBounds().GetTransformedAabb(worldFromLocal);
  51. }
  52. AZ::Aabb BoundsTestComponent::GetLocalBounds() const
  53. {
  54. return m_localBounds;
  55. }
  56. void RenderGeometryIntersectionTestComponent::Reflect(AZ::ReflectContext* context)
  57. {
  58. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  59. {
  60. serializeContext->Class<RenderGeometryIntersectionTestComponent, BoundsTestComponent>()->Version(1);
  61. }
  62. }
  63. void RenderGeometryIntersectionTestComponent::Activate()
  64. {
  65. BoundsTestComponent::Activate();
  66. const AZ::EntityId entityId = GetEntityId();
  67. AzFramework::EntityContextId contextId = AzFramework::EntityContextId::CreateNull();
  68. AzFramework::EntityIdContextQueryBus::EventResult(contextId, entityId, &AzFramework::EntityIdContextQueries::GetOwningContextId);
  69. AzFramework::RenderGeometry::IntersectionRequestBus::Handler::BusConnect({entityId, contextId});
  70. }
  71. void RenderGeometryIntersectionTestComponent::Deactivate()
  72. {
  73. AzFramework::RenderGeometry::IntersectionRequestBus::Handler::BusDisconnect();
  74. BoundsTestComponent::Deactivate();
  75. }
  76. AzFramework::RenderGeometry::RayResult RenderGeometryIntersectionTestComponent::RenderGeometryIntersect(
  77. const AzFramework::RenderGeometry::RayRequest& ray) const
  78. {
  79. AZ::Transform worldFromLocal = AZ::Transform::CreateIdentity();
  80. AZ::TransformBus::EventResult(worldFromLocal, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  81. AzFramework::RenderGeometry::RayResult rayResult;
  82. float t = 0.0f;
  83. const AZ::Obb obb = GetLocalBounds().GetTransformedObb(worldFromLocal);
  84. const AZ::Vector3 rayDirection = ray.m_endWorldPosition - ray.m_startWorldPosition;
  85. if (AZ::Intersect::IntersectRayObb(ray.m_startWorldPosition, rayDirection, obb, t))
  86. {
  87. rayResult.m_worldPosition = ray.m_startWorldPosition + rayDirection * t;
  88. rayResult.m_entityAndComponent = AZ::EntityComponentIdPair(GetEntityId(), GetId());
  89. rayResult.m_distance = t;
  90. rayResult.m_uv = AZ::Vector2::CreateZero();
  91. rayResult.m_worldNormal = AZ::Vector3::CreateZero();
  92. }
  93. return rayResult;
  94. }
  95. } // namespace UnitTest