Render Colliders (#5434)

* Render Colliders

Signed-off-by: rhhong <rhhong@amazon.com>

* CR feedback

Signed-off-by: rhhong <rhhong@amazon.com>
This commit is contained in:
Roman
2021-11-09 13:42:56 -08:00
committed by GitHub
parent e6a1f8ed0a
commit aa229976f3
23 changed files with 655 additions and 72 deletions
@@ -8,6 +8,7 @@
#include <AzCore/std/algorithm.h>
#include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
#include <AzFramework/Entity/EntityDebugDisplayBus.h>
#include <EMotionFX/CommandSystem/Source/SimulatedObjectCommands.h>
#include <EMotionFX/Source/Actor.h>
#include <EMotionFX/Source/ActorInstance.h>
@@ -18,6 +19,7 @@
#include <EMotionFX/Tools/EMotionStudio/EMStudioSDK/Source/RenderPlugin/RenderOptions.h>
#include <EMotionFX/Tools/EMotionStudio/EMStudioSDK/Source/RenderPlugin/RenderPlugin.h>
#include <EMotionFX/Tools/EMotionStudio/EMStudioSDK/Source/RenderPlugin/RenderViewWidget.h>
#include <EMotionFX/Tools/EMotionStudio/EMStudioSDK/Source/RenderPlugin/ViewportPluginBus.h>
#include <Editor/ColliderContainerWidget.h>
#include <Editor/ColliderHelpers.h>
#include <Editor/Plugins/SimulatedObject/SimulatedJointWidget.h>
@@ -25,6 +27,7 @@
#include <Editor/ReselectingTreeView.h>
#include <Editor/SimulatedObjectHelpers.h>
#include <Editor/SkeletonModel.h>
#include <Integration/Rendering/RenderActorSettings.h>
#include <MCore/Source/AzCoreConversions.h>
#include <QLabel>
#include <QPushButton>
@@ -330,6 +333,11 @@ namespace EMotionFX
const Actor* actor = selectedRowIndices[0].data(SkeletonModel::ROLE_ACTOR_POINTER).value<Actor*>();
const SimulatedObjectSetup* simulatedObjectSetup = actor->GetSimulatedObjectSetup().get();
if (!simulatedObjectSetup)
{
AZ_Assert(false, "Expected a simulated object setup on the actor.");
return;
}
AZStd::unordered_set<const SimulatedObject*> addToCandidates;
for (const QModelIndex& index : selectedRowIndices)
@@ -477,7 +485,7 @@ namespace EMotionFX
// -------------------------------------------------- Rendering -------------------------------------------------------------
void SimulatedObjectWidget::Render(EMStudio::RenderPlugin* renderPlugin, RenderInfo* renderInfo)
void SimulatedObjectWidget::LegacyRender(EMStudio::RenderPlugin* renderPlugin, RenderInfo* renderInfo)
{
if (!m_actor || !m_actorInstance)
{
@@ -501,7 +509,92 @@ namespace EMotionFX
ActorInstance* actorInstance = GetActorManager().GetActorInstance(actorInstanceIndex);
const Actor* actor = actorInstance->GetActor();
const SimulatedObjectSetup* setup = actor->GetSimulatedObjectSetup().get();
AZ_Assert(setup, "Expected a simulated object setup on the actor instance.");
if (!setup)
{
AZ_Assert(false, "Expected a simulated object setup on the actor instance.");
return;
}
const size_t objectCount = setup->GetNumSimulatedObjects();
for (size_t objectIndex = 0; objectIndex < objectCount; ++objectIndex)
{
const SimulatedObject* object = setup->GetSimulatedObject(objectIndex);
const size_t simulatedJointCount = object->GetNumSimulatedJoints();
for (size_t simulatedJointIndex = 0; simulatedJointIndex < simulatedJointCount; ++simulatedJointIndex)
{
const SimulatedJoint* simulatedJoint = object->GetSimulatedJoint(simulatedJointIndex);
const size_t skeletonJointIndex = simulatedJoint->GetSkeletonJointIndex();
if (selectedJointIndices.find(skeletonJointIndex) != selectedJointIndices.end())
{
LegacyRenderJointRadius(simulatedJoint, actorInstance, AZ::Color(1.0f, 0.0f, 1.0f, 1.0f));
}
}
}
}
}
const bool renderColliders = activeViewWidget->GetRenderFlag(EMStudio::RenderViewWidget::RENDER_SIMULATEDOBJECT_COLLIDERS);
if (renderColliders)
{
const EMStudio::RenderOptions* renderOptions = renderPlugin->GetRenderOptions();
ColliderContainerWidget::LegacyRenderColliders(PhysicsSetup::SimulatedObjectCollider,
renderOptions->GetSimulatedObjectColliderColor(),
renderOptions->GetSelectedSimulatedObjectColliderColor(),
renderPlugin,
renderInfo);
}
}
void SimulatedObjectWidget::LegacyRenderJointRadius(const SimulatedJoint* joint, ActorInstance* actorInstance, const AZ::Color& color)
{
#ifndef EMFX_SCALE_DISABLED
const float scale = actorInstance->GetWorldSpaceTransform().m_scale.GetX();
#else
const float scale = 1.0f;
#endif
const float radius = joint->GetCollisionRadius() * scale;
if (radius <= AZ::Constants::FloatEpsilon)
{
return;
}
AZ_Assert(joint->GetSkeletonJointIndex() != InvalidIndex, "Expected skeletal joint index to be valid.");
const EMotionFX::Transform jointTransform =
actorInstance->GetTransformData()->GetCurrentPose()->GetWorldSpaceTransform(joint->GetSkeletonJointIndex());
DebugDraw& debugDraw = GetDebugDraw();
DebugDraw::ActorInstanceData* drawData = debugDraw.GetActorInstanceData(actorInstance);
drawData->Lock();
drawData->DrawWireframeSphere(jointTransform.m_position, radius, color, jointTransform.m_rotation, 12, 12);
drawData->Unlock();
}
void SimulatedObjectWidget::Render(EMotionFX::ActorRenderFlagBitset renderFlags)
{
if (!m_actor || !m_actorInstance)
{
return;
}
const AZ::Render::RenderActorSettings& settings = EMotionFX::GetRenderActorSettings();
const bool renderSimulatedJoints = renderFlags[RENDER_SIMULATEJOINTS];
const AZStd::unordered_set<size_t>& selectedJointIndices = EMStudio::GetManager()->GetSelectedJointIndices();
if (renderSimulatedJoints && !selectedJointIndices.empty())
{
// Render the joint radius.
const size_t actorInstanceCount = GetActorManager().GetNumActorInstances();
for (size_t actorInstanceIndex = 0; actorInstanceIndex < actorInstanceCount; ++actorInstanceIndex)
{
ActorInstance* actorInstance = GetActorManager().GetActorInstance(actorInstanceIndex);
const Actor* actor = actorInstance->GetActor();
const SimulatedObjectSetup* setup = actor->GetSimulatedObjectSetup().get();
if (!setup)
{
AZ_Assert(false, "Expected a simulated object setup on the actor instance.");
return;
}
const size_t objectCount = setup->GetNumSimulatedObjects();
for (size_t objectIndex = 0; objectIndex < objectCount; ++objectIndex)
{
@@ -520,25 +613,21 @@ namespace EMotionFX
}
}
const bool renderColliders = activeViewWidget->GetRenderFlag(EMStudio::RenderViewWidget::RENDER_SIMULATEDOBJECT_COLLIDERS);
const bool renderColliders = renderFlags[RENDER_SIMULATEDOBJECT_COLLIDERS];
if (renderColliders)
{
const EMStudio::RenderOptions* renderOptions = renderPlugin->GetRenderOptions();
ColliderContainerWidget::RenderColliders(PhysicsSetup::SimulatedObjectCollider,
renderOptions->GetSimulatedObjectColliderColor(),
renderOptions->GetSelectedSimulatedObjectColliderColor(),
renderPlugin,
renderInfo);
settings.m_simulatedObjectColliderColor, settings.m_selectedSimulatedObjectColliderColor);
}
}
void SimulatedObjectWidget::RenderJointRadius(const SimulatedJoint* joint, ActorInstance* actorInstance, const AZ::Color& color)
void SimulatedObjectWidget::RenderJointRadius(const SimulatedJoint* joint, ActorInstance* actorInstance, const AZ::Color& color)
{
#ifndef EMFX_SCALE_DISABLED
const float scale = actorInstance->GetWorldSpaceTransform().m_scale.GetX();
#else
const float scale = 1.0f;
#endif
#ifndef EMFX_SCALE_DISABLED
const float scale = actorInstance->GetWorldSpaceTransform().m_scale.GetX();
#else
const float scale = 1.0f;
#endif
const float radius = joint->GetCollisionRadius() * scale;
if (radius <= AZ::Constants::FloatEpsilon)
@@ -547,12 +636,21 @@ namespace EMotionFX
}
AZ_Assert(joint->GetSkeletonJointIndex() != InvalidIndex, "Expected skeletal joint index to be valid.");
const EMotionFX::Transform jointTransform = actorInstance->GetTransformData()->GetCurrentPose()->GetWorldSpaceTransform(joint->GetSkeletonJointIndex());
const EMotionFX::Transform jointTransform =
actorInstance->GetTransformData()->GetCurrentPose()->GetWorldSpaceTransform(joint->GetSkeletonJointIndex());
DebugDraw& debugDraw = GetDebugDraw();
DebugDraw::ActorInstanceData* drawData = debugDraw.GetActorInstanceData(actorInstance);
drawData->Lock();
drawData->DrawWireframeSphere(jointTransform.m_position, radius, color, jointTransform.m_rotation, 12, 12);
drawData->Unlock();
AZ::s32 viewportId = -1;
EMStudio::ViewportPluginRequestBus::BroadcastResult(viewportId, &EMStudio::ViewportPluginRequestBus::Events::GetViewportId);
AzFramework::DebugDisplayRequestBus::BusPtr debugDisplayBus;
AzFramework::DebugDisplayRequestBus::Bind(debugDisplayBus, viewportId);
AzFramework::DebugDisplayRequests* debugDisplay = nullptr;
debugDisplay = AzFramework::DebugDisplayRequestBus::FindFirstHandler(debugDisplayBus);
if (!debugDisplay)
{
return;
}
debugDisplay->SetColor(color);
debugDisplay->DrawWireSphere(jointTransform.m_position, radius);
}
} // namespace EMotionFX