Update intersect behavior for positioning entities in the viewport (#5906)

* update intersect behavior for positioning entities in the viewport and restore SurfaceManipulator

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>

* tests for surface manipulator from EditorTransformComponentSelection

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>

* update comments in tests and remove #pragma optimize('', off)@

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>

* add new file for FindClosestPickIntersection tests

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>

* add remaining tests for surface manipulator and snap fixes

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>

* some small updates and polish before PR

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>

* small updates following PR feedback

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>
This commit is contained in:
Tom Hulton-Harrop
2021-11-30 15:07:40 +00:00
committed by GitHub
parent 75d6cb2527
commit 6908a3e945
31 changed files with 811 additions and 280 deletions
@@ -8,6 +8,8 @@
#include <Tests/BoundsTestComponent.h>
#include <AzCore/Math/Obb.h>
#include <AzCore/Math/IntersectSegment.h>
#include <AzToolsFramework/ViewportSelection/EditorSelectionUtil.h>
namespace UnitTest
@@ -62,4 +64,51 @@ namespace UnitTest
{
return m_localBounds;
}
void RenderGeometryIntersectionTestComponent::Reflect(AZ::ReflectContext* context)
{
if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
{
serializeContext->Class<RenderGeometryIntersectionTestComponent, BoundsTestComponent>()->Version(1);
}
}
void RenderGeometryIntersectionTestComponent::Activate()
{
BoundsTestComponent::Activate();
const AZ::EntityId entityId = GetEntityId();
AzFramework::EntityContextId contextId = AzFramework::EntityContextId::CreateNull();
AzFramework::EntityIdContextQueryBus::EventResult(contextId, entityId, &AzFramework::EntityIdContextQueries::GetOwningContextId);
AzFramework::RenderGeometry::IntersectionRequestBus::Handler::BusConnect({entityId, contextId});
}
void RenderGeometryIntersectionTestComponent::Deactivate()
{
AzFramework::RenderGeometry::IntersectionRequestBus::Handler::BusDisconnect();
BoundsTestComponent::Deactivate();
}
AzFramework::RenderGeometry::RayResult RenderGeometryIntersectionTestComponent::RenderGeometryIntersect(
const AzFramework::RenderGeometry::RayRequest& ray)
{
AZ::Transform worldFromLocal = AZ::Transform::CreateIdentity();
AZ::TransformBus::EventResult(worldFromLocal, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
AzFramework::RenderGeometry::RayResult rayResult;
float t = 0.0f;
const AZ::Obb obb = GetLocalBounds().GetTransformedObb(worldFromLocal);
const AZ::Vector3 rayDirection = ray.m_endWorldPosition - ray.m_startWorldPosition;
if (AZ::Intersect::IntersectRayObb(ray.m_startWorldPosition, rayDirection, obb, t))
{
rayResult.m_worldPosition = ray.m_startWorldPosition + rayDirection * t;
rayResult.m_entityAndComponent = AZ::EntityComponentIdPair(GetEntityId(), GetId());
rayResult.m_distance = t;
rayResult.m_uv = AZ::Vector2::CreateZero();
rayResult.m_worldNormal = AZ::Vector3::CreateZero();
}
return rayResult;
}
} // namespace UnitTest
@@ -8,6 +8,7 @@
#pragma once
#include <AzFramework/Render/GeometryIntersectionBus.h>
#include <AzFramework/Visibility/BoundsBus.h>
#include <AzToolsFramework/API/ComponentEntitySelectionBus.h>
#include <AzToolsFramework/ToolsComponents/EditorComponentBase.h>
@@ -44,4 +45,21 @@ namespace UnitTest
AZ::Aabb m_localBounds; //!< Local bounds that can be modified for certain tests (defaults to unit cube).
};
class RenderGeometryIntersectionTestComponent
: public BoundsTestComponent
, public AzFramework::RenderGeometry::IntersectionRequestBus::Handler
{
public:
AZ_EDITOR_COMPONENT(RenderGeometryIntersectionTestComponent, "{6F46B5BF-60DF-4BDD-9BA7-9658E85B99C2}", BoundsTestComponent);
static void Reflect(AZ::ReflectContext* context);
// AZ::Component overrides ...
void Activate() override;
void Deactivate() override;
// IntersectionRequestBus overrides ...
AzFramework::RenderGeometry::RayResult RenderGeometryIntersect(const AzFramework::RenderGeometry::RayRequest& ray) override;
};
} // namespace UnitTest
@@ -1663,7 +1663,7 @@ namespace UnitTest
const AZ::Transform finalEntityTransform = AzToolsFramework::GetWorldTransform(m_entityId1);
// ensure final world positions match
EXPECT_TRUE(finalEntityTransform.IsClose(finalTransformWorld, 0.01f));
EXPECT_THAT(finalEntityTransform, IsCloseTolerance(finalTransformWorld, 0.01f));
}
TEST_F(EditorTransformComponentSelectionManipulatorTestFixture, TranslatingEntityWithLinearManipulatorNotifiesOnEntityTransformChanged)
@@ -2973,4 +2973,176 @@ namespace UnitTest
EXPECT_THAT(hoveredEntityIdAccentRemoved, IsTrue());
EXPECT_THAT(hoveredEntityEntityId, Eq(AZ::EntityId(12345)));
}
class EditorTransformComponentSelectionRenderGeometryIntersectionFixture : public ToolsApplicationFixture
{
public:
void SetUpEditorFixtureImpl() override
{
auto* app = GetApplication();
// register a simple component implementing BoundsRequestBus and EditorComponentSelectionRequestsBus
app->RegisterComponentDescriptor(BoundsTestComponent::CreateDescriptor());
// register a component implementing RenderGeometry::IntersectionRequestBus
app->RegisterComponentDescriptor(RenderGeometryIntersectionTestComponent::CreateDescriptor());
auto createEntityWithGeometryIntersectionFn = [](const char* entityName)
{
AZ::Entity* entity = nullptr;
AZ::EntityId entityId = CreateDefaultEditorEntity(entityName, &entity);
entity->Deactivate();
entity->CreateComponent<RenderGeometryIntersectionTestComponent>();
entity->Activate();
return entityId;
};
m_entityIdGround = createEntityWithGeometryIntersectionFn("Entity1");
m_entityIdBox = createEntityWithGeometryIntersectionFn("Entity2");
if (auto* ground = AzToolsFramework::GetEntityById(m_entityIdGround)->FindComponent<RenderGeometryIntersectionTestComponent>())
{
ground->m_localBounds = AZ::Aabb::CreateFromMinMax(AZ::Vector3(-10.0f, -10.0f, -0.5f), AZ::Vector3(10.0f, 10.0f, 0.5f));
}
AzToolsFramework::SetWorldTransform(m_entityIdGround, AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, 10.0f, 5.0f)));
if (auto* box = AzToolsFramework::GetEntityById(m_entityIdBox)->FindComponent<RenderGeometryIntersectionTestComponent>())
{
box->m_localBounds = AZ::Aabb::CreateFromMinMax(AZ::Vector3(-0.5f), AZ::Vector3(0.5f));
}
AzToolsFramework::SetWorldTransform(
m_entityIdBox,
AZ::Transform::CreateFromMatrix3x3AndTranslation(
AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(45.0f)), AZ::Vector3(0.0f, 10.0f, 7.0f)));
}
AZ::EntityId m_entityIdGround;
AZ::EntityId m_entityIdBox;
};
using EditorTransformComponentSelectionRenderGeometryIntersectionManipulatorFixture =
IndirectCallManipulatorViewportInteractionFixtureMixin<EditorTransformComponentSelectionRenderGeometryIntersectionFixture>;
TEST_F(
EditorTransformComponentSelectionRenderGeometryIntersectionManipulatorFixture, BoxCanBePlacedOnMeshSurfaceUsingSurfaceManipulator)
{
// camera (go to position format) - 0.00, 20.00, 12.00, -35.00, -180.00
m_cameraState.m_viewportSize = AZ::Vector2(1280.0f, 720.0f);
AzFramework::SetCameraTransform(
m_cameraState,
AZ::Transform::CreateFromMatrix3x3AndTranslation(
AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(-180.0f)) * AZ::Matrix3x3::CreateRotationX(AZ::DegToRad(-35.0f)),
AZ::Vector3(0.0f, 20.0f, 12.0f)));
// the initial starting position of the entity
const auto initialTransformWorld = AzToolsFramework::GetWorldTransform(m_entityIdBox);
// where the entity should end up (snapped to the larger ground surface)
const auto finalTransformWorld =
AZ::Transform::CreateFromQuaternionAndTranslation(initialTransformWorld.GetRotation(), AZ::Vector3(2.5f, 12.5f, 5.5f));
// calculate the position in screen space of the initial position of the entity
const auto initialPositionScreen = AzFramework::WorldToScreen(initialTransformWorld.GetTranslation(), m_cameraState);
// calculate the position in screen space of the final position of the entity
const auto finalPositionScreen = AzFramework::WorldToScreen(finalTransformWorld.GetTranslation(), m_cameraState);
// select the entity (this will cause the manipulators to appear in EditorTransformComponentSelection)
AzToolsFramework::SelectEntity(m_entityIdBox);
// press and drag the mouse (starting where the surface manipulator is)
m_actionDispatcher->CameraState(m_cameraState)
->MousePosition(initialPositionScreen)
->MouseLButtonDown()
->MousePosition(finalPositionScreen)
->MouseLButtonUp();
// read back the position of the entity now
const AZ::Transform finalEntityTransform = AzToolsFramework::GetWorldTransform(m_entityIdBox);
// ensure final world positions match
EXPECT_THAT(finalEntityTransform, IsCloseTolerance(finalTransformWorld, 0.01f));
}
TEST_F(
EditorTransformComponentSelectionRenderGeometryIntersectionManipulatorFixture,
SurfaceManipulatorFollowsMouseAtDefaultEditorDistanceFromCameraWhenNoMeshIntersection)
{
// camera (go to position format) - 0.00, 25.00, 12.00, 0.00, -180.00
m_cameraState.m_viewportSize = AZ::Vector2(1280.0f, 720.0f);
AzFramework::SetCameraTransform(
m_cameraState,
AZ::Transform::CreateFromMatrix3x3AndTranslation(
AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(-180.0f)), AZ::Vector3(0.0f, 25.0f, 12.0f)));
// the initial starting position of the entity
const auto initialTransformWorld = AzToolsFramework::GetWorldTransform(m_entityIdBox);
// where the entity should end up (default distance away from the camera/near clip under where the mouse is)
const auto finalTransformWorld =
AZ::Transform::CreateFromQuaternionAndTranslation(initialTransformWorld.GetRotation(), AZ::Vector3(0.0f, 14.9f, 12.0f));
// calculate the position in screen space of the initial position of the entity
const auto initialPositionScreen = AzFramework::WorldToScreen(initialTransformWorld.GetTranslation(), m_cameraState);
// calculate the position in screen space of the final position of the entity
const auto finalPositionScreen = AzFramework::WorldToScreen(finalTransformWorld.GetTranslation(), m_cameraState);
// select the entity (this will cause the manipulators to appear in EditorTransformComponentSelection)
AzToolsFramework::SelectEntity(m_entityIdBox);
// press and drag the mouse (starting where the surface manipulator is)
m_actionDispatcher->CameraState(m_cameraState)
->MousePosition(initialPositionScreen)
->MouseLButtonDown()
->MousePosition(finalPositionScreen)
->MouseLButtonUp();
// read back the position of the entity now
const AZ::Transform finalEntityTransform = AzToolsFramework::GetWorldTransform(m_entityIdBox);
const auto viewportRay = AzToolsFramework::ViewportInteraction::ViewportScreenToWorldRay(m_cameraState, initialPositionScreen);
const auto distanceAway = (finalEntityTransform.GetTranslation() - viewportRay.origin).GetLength();
// ensure final world positions match
EXPECT_THAT(finalEntityTransform, IsCloseTolerance(finalTransformWorld, 0.01f));
// ensure distance away is what we expect
EXPECT_NEAR(distanceAway, AzToolsFramework::GetDefaultEntityPlacementDistance(), 0.001f);
}
TEST_F(
EditorTransformComponentSelectionRenderGeometryIntersectionManipulatorFixture,
MiddleMouseButtonWithShiftAndCtrlHeldOnMeshSurfaceWillSnapSelectedEntityToIntersectionPoint)
{
// camera (go to position format) - 21.00, 8.00, 11.00, -22.00, 150.00
m_cameraState.m_viewportSize = AZ::Vector2(1280.0f, 720.0f);
AzFramework::SetCameraTransform(
m_cameraState,
AZ::Transform::CreateFromMatrix3x3AndTranslation(
AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(150.0f)) * AZ::Matrix3x3::CreateRotationX(AZ::DegToRad(-22.0f)),
AZ::Vector3(21.0f, 8.0f, 11.0f)));
// position the ground entity
AzToolsFramework::SetWorldTransform(
m_entityIdGround,
AZ::Transform::CreateFromMatrix3x3AndTranslation(
AZ::Matrix3x3::CreateRotationY(AZ::DegToRad(40.0f)) * AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(60.0f)),
AZ::Vector3(14.0f, -6.0f, 5.0f)));
// select the other entity (a 1x1x1 box)
AzToolsFramework::SelectEntity(m_entityIdBox);
// expected world position (value taken from editor scenario)
const auto expectedWorldPosition = AZ::Vector3(13.606657f, -2.6753534f, 5.9827675f);
const auto screenPosition = AzFramework::WorldToScreen(expectedWorldPosition, m_cameraState);
// perform snap action
m_actionDispatcher->CameraState(m_cameraState)
->MousePosition(screenPosition)
->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Shift)
->MouseMButtonDown();
// read back the current entity transform after placement
const AZ::Transform finalEntityTransform = AzToolsFramework::GetWorldTransform(m_entityIdBox);
EXPECT_THAT(finalEntityTransform.GetTranslation(), IsCloseTolerance(expectedWorldPosition, 0.01f));
}
} // namespace UnitTest
@@ -10,22 +10,23 @@
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/UnitTest/TestTypes.h>
#include <AzFramework/Viewport/ViewportScreen.h>
#include <AzManipulatorTestFramework/AzManipulatorTestFramework.h>
#include <AzManipulatorTestFramework/AzManipulatorTestFrameworkTestHelpers.h>
#include <AzManipulatorTestFramework/AzManipulatorTestFrameworkUtils.h>
#include <AzManipulatorTestFramework/ImmediateModeActionDispatcher.h>
#include <AzManipulatorTestFramework/IndirectManipulatorViewportInteraction.h>
#include <AzTest/AzTest.h>
#include <AzToolsFramework/Application/ToolsApplication.h>
#include <AzToolsFramework/Entity/EditorEntityHelpers.h>
#include <AzToolsFramework/Manipulators/EditorVertexSelection.h>
#include <AzToolsFramework/Manipulators/HoverSelection.h>
#include <AzToolsFramework/Manipulators/ManipulatorManager.h>
#include <AzToolsFramework/Manipulators/TranslationManipulators.h>
#include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
#include <AzToolsFramework/ViewportSelection/EditorInteractionSystemViewportSelectionRequestBus.h>
#include <AzManipulatorTestFramework/AzManipulatorTestFramework.h>
#include <AzManipulatorTestFramework/AzManipulatorTestFrameworkUtils.h>
#include <AzManipulatorTestFramework/AzManipulatorTestFrameworkTestHelpers.h>
#include <AzManipulatorTestFramework/IndirectManipulatorViewportInteraction.h>
#include <AzManipulatorTestFramework/ImmediateModeActionDispatcher.h>
#include <Tests/Utils/Printers.h>
using namespace AzToolsFramework;
#include <Tests/BoundsTestComponent.h>
namespace UnitTest
{
@@ -42,20 +43,65 @@ namespace UnitTest
void Disconnect();
// FixedVerticesRequestBus/VariableVerticesRequestBus ...
bool GetVertex(size_t index, AZ::Vector3& vertex) const override { return m_vertexContainer.GetVertex(index, vertex); }
bool UpdateVertex(size_t index, const AZ::Vector3& vertex) override { return m_vertexContainer.UpdateVertex(index, vertex); };
void AddVertex(const AZ::Vector3& vertex) override { m_vertexContainer.AddVertex(vertex); }
bool InsertVertex(size_t index, const AZ::Vector3& vertex) override { return m_vertexContainer.InsertVertex(index, vertex); }
bool RemoveVertex(size_t index) override { return m_vertexContainer.RemoveVertex(index); }
void SetVertices(const AZStd::vector<AZ::Vector3>& vertices) override { m_vertexContainer.SetVertices(vertices); };
void ClearVertices() override { m_vertexContainer.Clear(); }
size_t Size() const override { return m_vertexContainer.Size(); }
bool Empty() const override { return m_vertexContainer.Empty(); }
bool GetVertex(size_t index, AZ::Vector3& vertex) const override;
bool UpdateVertex(size_t index, const AZ::Vector3& vertex) override;
void AddVertex(const AZ::Vector3& vertex) override;
bool InsertVertex(size_t index, const AZ::Vector3& vertex) override;
bool RemoveVertex(size_t index) override;
void SetVertices(const AZStd::vector<AZ::Vector3>& vertices) override;
void ClearVertices() override;
size_t Size() const override;
bool Empty() const override;
private:
AZ::VertexContainer<AZ::Vector3> m_vertexContainer;
};
bool TestVariableVerticesVertexContainer::GetVertex(size_t index, AZ::Vector3& vertex) const
{
return m_vertexContainer.GetVertex(index, vertex);
}
bool TestVariableVerticesVertexContainer::UpdateVertex(size_t index, const AZ::Vector3& vertex)
{
return m_vertexContainer.UpdateVertex(index, vertex);
}
void TestVariableVerticesVertexContainer::AddVertex(const AZ::Vector3& vertex)
{
m_vertexContainer.AddVertex(vertex);
}
bool TestVariableVerticesVertexContainer::InsertVertex(size_t index, const AZ::Vector3& vertex)
{
return m_vertexContainer.InsertVertex(index, vertex);
}
bool TestVariableVerticesVertexContainer::RemoveVertex(size_t index)
{
return m_vertexContainer.RemoveVertex(index);
}
void TestVariableVerticesVertexContainer::SetVertices(const AZStd::vector<AZ::Vector3>& vertices)
{
m_vertexContainer.SetVertices(vertices);
}
void TestVariableVerticesVertexContainer::ClearVertices()
{
m_vertexContainer.Clear();
}
size_t TestVariableVerticesVertexContainer::Size() const
{
return m_vertexContainer.Size();
}
bool TestVariableVerticesVertexContainer::Empty() const
{
return m_vertexContainer.Empty();
}
void TestVariableVerticesVertexContainer::Connect(const AZ::EntityId entityId)
{
AZ::VariableVerticesRequestBus<AZ::Vector3>::Handler::BusConnect(entityId);
@@ -68,17 +114,18 @@ namespace UnitTest
AZ::VariableVerticesRequestBus<AZ::Vector3>::Handler::BusDisconnect();
}
class TestEditorVertexSelectionVariable
: public EditorVertexSelectionVariable<AZ::Vector3>
class TestEditorVertexSelectionVariable : public AzToolsFramework::EditorVertexSelectionVariable<AZ::Vector3>
{
public:
AZ_CLASS_ALLOCATOR(TestEditorVertexSelectionVariable, AZ::SystemAllocator, 0)
void ShowVertexDeletionWarning() override { /*noop*/ }
void ShowVertexDeletionWarning() override
{
// noop
}
};
class EditorVertexSelectionFixture
: public ToolsApplicationFixture
class EditorVertexSelectionFixture : public ToolsApplicationFixture
{
public:
void SetUpEditorFixtureImpl() override
@@ -112,26 +159,25 @@ namespace UnitTest
void EditorVertexSelectionFixture::RecreateVertexSelection()
{
namespace aztf = AzToolsFramework;
m_vertexSelection.Create(
AZ::EntityComponentIdPair(m_entityId, TestComponentId),
g_mainManipulatorManagerId, AZStd::make_unique<NullHoverSelection>(),
TranslationManipulators::Dimensions::Three, ConfigureTranslationManipulatorAppearance3d);
AZ::EntityComponentIdPair(m_entityId, TestComponentId), aztf::g_mainManipulatorManagerId,
AZStd::make_unique<aztf::NullHoverSelection>(), aztf::TranslationManipulators::Dimensions::Three,
aztf::ConfigureTranslationManipulatorAppearance3d);
}
void EditorVertexSelectionFixture::PopulateVertices()
{
for (size_t vertIndex = 0; vertIndex < EditorVertexSelectionFixture::VertexCount; ++vertIndex)
{
InsertVertexAfter(
AZ::EntityComponentIdPair(m_entityId, TestComponentId), 0, AZ::Vector3::CreateZero());
AzToolsFramework::InsertVertexAfter(AZ::EntityComponentIdPair(m_entityId, TestComponentId), 0, AZ::Vector3::CreateZero());
}
}
void EditorVertexSelectionFixture::ClearVertices()
{
for (size_t vertIndex = 0; vertIndex < EditorVertexSelectionFixture::VertexCount; ++vertIndex)
{
SafeRemoveVertex<AZ::Vector3>(
AZ::EntityComponentIdPair(m_entityId, TestComponentId), 0);
AzToolsFramework::SafeRemoveVertex<AZ::Vector3>(AZ::EntityComponentIdPair(m_entityId, TestComponentId), 0);
}
}
@@ -188,7 +234,7 @@ namespace UnitTest
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// When
// just provide a placeholder mouse interaction event in this case
m_vertexSelection.SnapVerticesToTerrain(ViewportInteraction::MouseInteractionEvent{});
m_vertexSelection.SnapVerticesToSurface(AzToolsFramework::ViewportInteraction::MouseInteractionEvent{});
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -197,8 +243,7 @@ namespace UnitTest
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
using EditorVertexSelectionManipulatorFixture =
IndirectCallManipulatorViewportInteractionFixtureMixin<EditorVertexSelectionFixture>;
using EditorVertexSelectionManipulatorFixture = IndirectCallManipulatorViewportInteractionFixtureMixin<EditorVertexSelectionFixture>;
TEST_F(EditorVertexSelectionManipulatorFixture, CannotDeleteAllVertices)
{
@@ -206,25 +251,23 @@ namespace UnitTest
const auto entityComponentIdPair = AZ::EntityComponentIdPair(m_entityId, TestComponentId);
const float horizontalPositions[] = {-1.5f, -0.5f, 0.5f, 1.5f};
for (size_t vertIndex = 0; vertIndex < std::size(horizontalPositions); ++vertIndex)
const float horizontalPositions[] = { -1.5f, -0.5f, 0.5f, 1.5f };
for (size_t vertIndex = 0; vertIndex < AZStd::size(horizontalPositions); ++vertIndex)
{
InsertVertexAfter(
entityComponentIdPair, vertIndex, AZ::Vector3(horizontalPositions[vertIndex], 5.0f, 0.0f));
AzToolsFramework::InsertVertexAfter(entityComponentIdPair, vertIndex, AZ::Vector3(horizontalPositions[vertIndex], 5.0f, 0.0f));
}
// rebuild the vertex selection after adding the new verts
// rebuild the vertex selection after adding the new vertices
RecreateVertexSelection();
// build a vector of the vertex positions in screen space
AZStd::vector<AzFramework::ScreenPoint> vertexScreenPositions;
for (size_t vertIndex = 0; vertIndex < std::size(horizontalPositions); ++vertIndex)
for (size_t vertIndex = 0; vertIndex < AZStd::size(horizontalPositions); ++vertIndex)
{
AZ::Vector3 localVertex;
AZ::Vector3 localVertex = AZ::Vector3::CreateZero();
bool found = false;
AZ::FixedVerticesRequestBus<AZ::Vector3>::EventResult(
found, m_entityId, &AZ::FixedVerticesRequestBus<AZ::Vector3>::Handler::GetVertex,
vertIndex, localVertex);
found, m_entityId, &AZ::FixedVerticesRequestBus<AZ::Vector3>::Handler::GetVertex, vertIndex, localVertex);
if (found)
{
@@ -267,9 +310,9 @@ namespace UnitTest
const auto entityComponentIdPair = AZ::EntityComponentIdPair(m_entityId, TestComponentId);
// add a single vertex (in front of the camera)
InsertVertexAfter(entityComponentIdPair, 0, AZ::Vector3::CreateAxisY(5.0f));
AzToolsFramework::InsertVertexAfter(entityComponentIdPair, 0, AZ::Vector3::CreateAxisY(5.0f));
// rebuild the vertex selection after adding the new verts
// rebuild the vertex selection after adding the new vertices
RecreateVertexSelection();
AzFramework::ScreenPoint vertexScreenPosition;
@@ -300,4 +343,132 @@ namespace UnitTest
// deleting the last vertex through a manipulator is disallowed - size should remain the same
EXPECT_THAT(vertexCountAfter, Eq(1));
}
static AZ::EntityId CreateEntityForVertexIntersectionPlacement(EditorVertexSelectionManipulatorFixture& fixture)
{
auto* app = fixture.GetApplication();
app->RegisterComponentDescriptor(BoundsTestComponent::CreateDescriptor());
app->RegisterComponentDescriptor(RenderGeometryIntersectionTestComponent::CreateDescriptor());
AZ::Entity* entityGround = nullptr;
AZ::EntityId entityIdGround = CreateDefaultEditorEntity("EntityGround", &entityGround);
entityGround->Deactivate();
auto ground = entityGround->CreateComponent<RenderGeometryIntersectionTestComponent>();
entityGround->Activate();
ground->m_localBounds = AZ::Aabb::CreateFromMinMax(AZ::Vector3(-10.0f, -10.0f, -0.5f), AZ::Vector3(10.0f, 10.0f, 0.5f));
return entityIdGround;
}
static AZStd::vector<AzFramework::ScreenPoint> SetupVertices(
const AZ::EntityId entityId, EditorVertexSelectionManipulatorFixture& fixture)
{
const auto entityComponentIdPair = AZ::EntityComponentIdPair(entityId, TestComponentId);
const float horizontalPositions[] = { -3.0f, -1.0f, 1.0f, 3.0f };
for (size_t vertIndex = 0; vertIndex < AZStd::size(horizontalPositions); ++vertIndex)
{
AzToolsFramework::InsertVertexAfter(entityComponentIdPair, vertIndex, AZ::Vector3(horizontalPositions[vertIndex], 0.0f, 0.0f));
}
// rebuild the vertex selection after adding the new vertices
fixture.RecreateVertexSelection();
// build a vector of the vertex positions in screen space
AZStd::vector<AzFramework::ScreenPoint> vertexScreenPositions;
for (size_t vertIndex = 0; vertIndex < AZStd::size(horizontalPositions); ++vertIndex)
{
AZ::Vector3 localVertex;
bool found = false;
AZ::FixedVerticesRequestBus<AZ::Vector3>::EventResult(
found, entityId, &AZ::FixedVerticesRequestBus<AZ::Vector3>::Handler::GetVertex, vertIndex, localVertex);
if (found)
{
const AZ::Vector3 worldVertex = AzToolsFramework::GetWorldTransform(entityId).TransformPoint(localVertex);
vertexScreenPositions.push_back(AzFramework::WorldToScreen(worldVertex, fixture.m_cameraState));
}
}
return vertexScreenPositions;
}
AzToolsFramework::ViewportInteraction::MouseInteractionEvent BuildMiddleMouseDownEvent(
const AzFramework::ScreenPoint& screenPosition, const AzFramework::ViewportId viewportId)
{
AzToolsFramework::ViewportInteraction::MousePick mousePick;
mousePick.m_screenCoordinates = screenPosition;
AzToolsFramework::ViewportInteraction::MouseInteraction mouseInteraction;
mouseInteraction.m_interactionId.m_cameraId = AZ::EntityId();
mouseInteraction.m_interactionId.m_viewportId = viewportId;
mouseInteraction.m_mouseButtons =
AzToolsFramework::ViewportInteraction::MouseButtonsFromButton(AzToolsFramework::ViewportInteraction::MouseButton::Middle);
mouseInteraction.m_mousePick = mousePick;
mouseInteraction.m_keyboardModifiers = AzToolsFramework::ViewportInteraction::KeyboardModifiers(
static_cast<AZ::u32>(AzToolsFramework::ViewportInteraction::KeyboardModifier::Shift) |
static_cast<AZ::u32>(AzToolsFramework::ViewportInteraction::KeyboardModifier::Ctrl));
return AzToolsFramework::ViewportInteraction::MouseInteractionEvent(
mouseInteraction, AzToolsFramework::ViewportInteraction::MouseEvent::Down, /*captured=*/false);
}
TEST_F(EditorVertexSelectionManipulatorFixture, VertexPlacedWhereIntersectionPointIsFoundWithCustomReferenceSpace)
{
const AZ::EntityId entityIdGround = CreateEntityForVertexIntersectionPlacement(*this);
// position ground
AzToolsFramework::SetWorldTransform(
entityIdGround,
AZ::Transform::CreateFromMatrix3x3AndTranslation(
AZ::Matrix3x3::CreateRotationX(AZ::DegToRad(-20.0f)) * AZ::Matrix3x3::CreateRotationY(AZ::DegToRad(-40.0f)) *
AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(60.0f)),
AZ::Vector3(14.0f, -6.0f, 5.0f)));
// camera (go to position format) - 12.00, 18.00, 16.00, -38.00, -175.00
m_cameraState.m_viewportSize = AZ::Vector2(1280.0f, 720.0f);
AzFramework::SetCameraTransform(
m_cameraState,
AZ::Transform::CreateFromMatrix3x3AndTranslation(
AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(-175.0f)) * AZ::Matrix3x3::CreateRotationX(AZ::DegToRad(-38.0f)),
AZ::Vector3(12.0f, 18.0f, 16.0f)));
// create orientated and scaled transform for vertex selection entity transform
auto vertexSelectionTransform = AZ::Transform::CreateFromMatrix3x3AndTranslation(
AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(45.0f)), AZ::Vector3(14.0f, 7.0f, 5.0f));
vertexSelectionTransform.MultiplyByUniformScale(3.0f);
// set the initial starting position of the vertex selection
AzToolsFramework::SetWorldTransform(m_entityId, vertexSelectionTransform);
auto vertexScreenPositions = SetupVertices(m_entityId, *this);
// press and drag the mouse (starting where the surface manipulator is)
// select each vertex (by holding ctrl)
m_actionDispatcher->CameraState(m_cameraState)->MousePosition(vertexScreenPositions[0])->MouseLButtonDown()->MouseLButtonUp();
const auto finalPositionWorld = AZ::Vector3(14.3573294f, -8.94695091f, 7.08627319f);
// calculate the position in screen space of the final position of the entity
const auto finalPositionScreen = AzFramework::WorldToScreen(finalPositionWorld, m_cameraState);
auto middleMouseDownEvent =
BuildMiddleMouseDownEvent(finalPositionScreen, m_viewportManipulatorInteraction->GetViewportInteraction().GetViewportId());
// explicitly handle mouse event in vertex selection instance
m_vertexSelection.HandleMouse(middleMouseDownEvent);
// read back the position of the vertex now
AZ::Vector3 localVertex = AZ::Vector3::CreateZero();
bool found = false;
AZ::FixedVerticesRequestBus<AZ::Vector3>::EventResult(
found, m_entityId, &AZ::FixedVerticesRequestBus<AZ::Vector3>::Handler::GetVertex, 0, localVertex);
// transform to world space
const AZ::Vector3 worldVertex = vertexSelectionTransform.TransformPoint(localVertex);
EXPECT_THAT(found, ::testing::IsTrue());
// ensure final world positions match
EXPECT_THAT(worldVertex, IsCloseTolerance(finalPositionWorld, 0.01f));
}
} // namespace UnitTest
@@ -0,0 +1,76 @@
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <AzFramework/Viewport/ViewportScreen.h>
#include <AzManipulatorTestFramework/AzManipulatorTestFrameworkTestHelpers.h>
#include <AzManipulatorTestFramework/AzManipulatorTestFrameworkUtils.h>
#include <AzToolsFramework/Entity/EditorEntityHelpers.h>
#include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
#include <AzToolsFramework/Viewport/ViewportMessages.h>
#include <AzToolsFramework/ViewportSelection/EditorSelectionUtil.h>
#include <Tests/BoundsTestComponent.h>
namespace UnitTest
{
class IndirectCallViewportInteractionIntersectionFixture : public ToolsApplicationFixture
{
public:
void SetUpEditorFixtureImpl() override
{
auto* app = GetApplication();
// register a simple component implementing BoundsRequestBus and EditorComponentSelectionRequestsBus
app->RegisterComponentDescriptor(BoundsTestComponent::CreateDescriptor());
// register a component implementing RenderGeometry::IntersectionRequestBus
app->RegisterComponentDescriptor(RenderGeometryIntersectionTestComponent::CreateDescriptor());
AZ::Entity* entityGround = nullptr;
m_entityIdGround = CreateDefaultEditorEntity("EntityGround", &entityGround);
entityGround->Deactivate();
auto ground = entityGround->CreateComponent<RenderGeometryIntersectionTestComponent>();
entityGround->Activate();
ground->m_localBounds = AZ::Aabb::CreateFromMinMax(AZ::Vector3(-10.0f, -10.0f, -0.5f), AZ::Vector3(10.0f, 10.0f, 0.5f));
}
AZ::EntityId m_entityIdGround;
};
using IndirectCallManipulatorViewportInteractionIntersectionFixture =
IndirectCallManipulatorViewportInteractionFixtureMixin<IndirectCallViewportInteractionIntersectionFixture>;
TEST_F(IndirectCallManipulatorViewportInteractionIntersectionFixture, FindClosestPickIntersectionReturnsExpectedSurfacePoint)
{
// camera - 21.00, 8.00, 11.00, -22.00, 150.00
m_cameraState.m_viewportSize = AZ::Vector2(1280.0f, 720.0f);
AzFramework::SetCameraTransform(
m_cameraState,
AZ::Transform::CreateFromMatrix3x3AndTranslation(
AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(150.0f)) * AZ::Matrix3x3::CreateRotationX(AZ::DegToRad(-22.0f)),
AZ::Vector3(21.0f, 8.0f, 11.0f)));
m_actionDispatcher->CameraState(m_cameraState);
AzToolsFramework::SetWorldTransform(
m_entityIdGround,
AZ::Transform::CreateFromMatrix3x3AndTranslation(
AZ::Matrix3x3::CreateRotationY(AZ::DegToRad(40.0f)) * AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(60.0f)),
AZ::Vector3(14.0f, -6.0f, 5.0f)));
// expected world position (value taken from editor scenario)
const auto expectedWorldPosition = AZ::Vector3(13.606657f, -2.6753534f, 5.9827675f);
const auto screenPosition = AzFramework::WorldToScreen(expectedWorldPosition, m_cameraState);
// perform ray intersection against mesh
const auto worldIntersectionPoint = AzToolsFramework::FindClosestPickIntersection(
m_viewportManipulatorInteraction->GetViewportInteraction().GetViewportId(), screenPosition,
AzToolsFramework::EditorPickRayLength, AzToolsFramework::GetDefaultEntityPlacementDistance());
EXPECT_THAT(worldIntersectionPoint, IsCloseTolerance(expectedWorldPosition, 0.01f));
}
} // namespace UnitTest
@@ -136,5 +136,6 @@ set(FILES
Viewport/ViewportUiDisplayTests.cpp
Viewport/ViewportUiManagerTests.cpp
Viewport/ViewportUiWidgetManagerTests.cpp
Viewport/ViewportInteractionTests.cpp
Visibility/EditorVisibilityTests.cpp
)