Merge pull request #71 from aws-lumberyard-dev/non-uniform-scale-visibility

Bug fixes for non-uniform scale.
Fixing visibility bounds for non-uniformly scaled polygon prism activation.
Fixing editor rigid body position for physx asset colliders with position offsets and NUS.
Fixing runtime subdivision level for asset colliders.
This commit is contained in:
greerdv
2021-04-16 11:32:50 +01:00
committed by GitHub
7 changed files with 29 additions and 15 deletions
@@ -142,6 +142,7 @@ namespace Physics
->Field("PhysicsAsset", &PhysicsAssetShapeConfiguration::m_asset)
->Field("AssetScale", &PhysicsAssetShapeConfiguration::m_assetScale)
->Field("UseMaterialsFromAsset", &PhysicsAssetShapeConfiguration::m_useMaterialsFromAsset)
->Field("SubdivisionLevel", &PhysicsAssetShapeConfiguration::m_subdivisionLevel)
;
if (auto editContext = serializeContext->GetEditContext())
@@ -141,6 +141,7 @@ namespace Physics
AZ::Data::Asset<AZ::Data::AssetData> m_asset{ AZ::Data::AssetLoadBehavior::PreLoad };
AZ::Vector3 m_assetScale = AZ::Vector3::CreateOne();
bool m_useMaterialsFromAsset = true;
AZ::u8 m_subdivisionLevel = 4; ///< The level of subdivision if a primitive shape is replaced with a convex mesh due to scaling.
};
class NativeShapeConfiguration : public ShapeConfiguration
@@ -217,6 +217,7 @@ namespace LmbrCentral
AZ::TransformBus::EventResult(m_currentTransform, entityId, &AZ::TransformBus::Events::GetWorldTM);
m_currentNonUniformScale = AZ::Vector3::CreateOne();
AZ::NonUniformScaleRequestBus::EventResult(m_currentNonUniformScale, m_entityId, &AZ::NonUniformScaleRequests::GetScale);
m_polygonPrism->SetNonUniformScale(m_currentNonUniformScale);
m_intersectionDataCache.InvalidateCache(InvalidateShapeCacheReason::ShapeChange);
AZ::TransformNotificationBus::Handler::BusConnect(entityId);
@@ -330,10 +330,8 @@ namespace PhysX
}
const bool hasNonUniformScale = (AZ::NonUniformScaleRequestBus::FindFirstHandler(GetEntityId()) != nullptr);
// the value for the subdivision level doesn't matter in the runtime, because any approximation of primitives will already have
// happened in the editor, so can pass an arbitrary value here
AZ::u8 subdivisionLevel = 0;
Utils::GetShapesFromAsset(physicsAssetConfiguration, componentColliderConfiguration, hasNonUniformScale, subdivisionLevel, m_shapes);
Utils::GetShapesFromAsset(physicsAssetConfiguration, componentColliderConfiguration, hasNonUniformScale,
physicsAssetConfiguration.m_subdivisionLevel, m_shapes);
return true;
}
@@ -514,6 +514,8 @@ namespace PhysX
break;
case Physics::ShapeType::PhysicsAsset:
colliderComponent = gameEntity->CreateComponent<MeshColliderComponent>();
m_shapeConfiguration.m_physicsAsset.m_configuration.m_subdivisionLevel = m_shapeConfiguration.m_subdivisionLevel;
colliderComponent->SetShapeConfigurationList({ AZStd::make_pair(sharedColliderConfig,
AZStd::make_shared<Physics::PhysicsAssetShapeConfiguration>(m_shapeConfiguration.m_physicsAsset.m_configuration)) });
@@ -561,6 +563,8 @@ namespace PhysX
void EditorColliderComponent::CreateStaticEditorCollider()
{
m_cachedAabbDirty = true;
// Don't create static rigid body in the editor if current entity components
// don't allow creation of runtime static rigid body component
if (!StaticRigidBodyUtils::CanCreateRuntimeComponent(*GetEntity()))
@@ -1014,11 +1018,17 @@ namespace PhysX
// PhysX::ColliderShapeBus
AZ::Aabb EditorColliderComponent::GetColliderShapeAabb()
{
return PhysX::Utils::GetColliderAabb(GetWorldTM()
, m_hasNonUniformScale
, m_shapeConfiguration.m_subdivisionLevel
, m_shapeConfiguration.GetCurrent()
, m_configuration);
if (m_cachedAabbDirty)
{
m_cachedAabb = PhysX::Utils::GetColliderAabb(GetWorldTM()
, m_hasNonUniformScale
, m_shapeConfiguration.m_subdivisionLevel
, m_shapeConfiguration.GetCurrent()
, m_configuration);
m_cachedAabbDirty = false;
}
return m_cachedAabb;
}
void EditorColliderComponent::UpdateShapeConfigurationScale()
@@ -261,6 +261,8 @@ namespace PhysX
bool m_hasNonUniformScale = false; //!< Whether there is a non-uniform scale component on this entity.
AZ::Vector3 m_cachedNonUniformScale = AZ::Vector3::CreateOne(); //!< Caches the current non-uniform scale.
mutable AZStd::optional<Physics::CookedMeshShapeConfiguration> m_scaledPrimitive; //!< Approximation for non-uniformly scaled primitive.
AZ::Aabb m_cachedAabb = AZ::Aabb::CreateNull(); //!< Cache the Aabb to avoid recalculating it.
bool m_cachedAabbDirty = true; //!< Track whether the cached Aabb needs to be recomputed.
AZ::ComponentDescriptor::StringWarningArray m_componentWarnings;
};
@@ -36,7 +36,7 @@ namespace PhysX
const bool hasNonUniformScaleComponent = (AZ::NonUniformScaleRequestBus::FindFirstHandler(entity->GetId()) != nullptr);
const AZStd::vector<EditorColliderComponent*> colliders = entity->FindComponents<EditorColliderComponent>();
const AZStd::vector<EditorColliderComponent*> colliders = entity->FindComponents<EditorColliderComponent>();
for (const EditorColliderComponent* collider : colliders)
{
const EditorProxyShapeConfig& shapeConfigurationProxy = collider->GetShapeConfiguration();
@@ -45,12 +45,14 @@ namespace PhysX
continue;
}
const Physics::ColliderConfiguration colliderConfiguration = collider->GetColliderConfigurationScaled();
const Physics::ColliderConfiguration colliderConfigurationScaled = collider->GetColliderConfigurationScaled();
const Physics::ColliderConfiguration colliderConfigurationUnscaled = collider->GetColliderConfiguration();
if (shapeConfigurationProxy.IsAssetConfig())
{
AZStd::vector<AZStd::shared_ptr<Physics::Shape>> shapes;
Utils::GetShapesFromAsset(shapeConfigurationProxy.m_physicsAsset.m_configuration,
colliderConfiguration, hasNonUniformScaleComponent, shapeConfigurationProxy.m_subdivisionLevel, shapes);
colliderConfigurationUnscaled, hasNonUniformScaleComponent, shapeConfigurationProxy.m_subdivisionLevel, shapes);
for (const auto& shape : shapes)
{
@@ -64,7 +66,7 @@ namespace PhysX
if (!hasNonUniformScaleComponent)
{
AZStd::shared_ptr<Physics::Shape> shape = AZ::Interface<Physics::System>::Get()->CreateShape(
colliderConfiguration, shapeConfiguration);
colliderConfigurationScaled, shapeConfiguration);
AZ_Assert(shape, "CreateEditorWorldRigidBody: Shape must not be null!");
if (shape)
{
@@ -73,7 +75,6 @@ namespace PhysX
}
else
{
const Physics::ColliderConfiguration colliderConfigurationUnscaled = collider->GetColliderConfiguration();
auto convexConfig = Utils::CreateConvexFromPrimitive(colliderConfigurationUnscaled, shapeConfiguration,
shapeConfigurationProxy.m_subdivisionLevel, shapeConfiguration.m_scale);
auto colliderConfigurationNoOffset = colliderConfigurationUnscaled;
@@ -377,7 +378,7 @@ namespace PhysX
configuration.m_kinematic = m_config.m_kinematic;
configuration.m_colliderAndShapeData = Internal::GetCollisionShapes(GetEntity());
if (auto* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get())
if (auto* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get())
{
m_rigidBodyHandle = sceneInterface->AddSimulatedBody(m_editorSceneHandle, &configuration);
m_editorBody = azdynamic_cast<AzPhysics::RigidBody*>(sceneInterface->GetSimulatedBodyFromHandle(m_editorSceneHandle, m_rigidBodyHandle));