Files
UnrealEngineUWP/Engine/Plugins/Runtime/MeshModelingToolset/Source/ModelingComponents/Private/Physics/PhysicsDataCollection.cpp
semion piskarev 45f0108aac MeshModelingTools: combine the volume component tool targets into one tool target. Make that tool target implement the IPhysicsDataSource interface. Fix bug in PhysicsDataCollection.cpp that caused crash in PhysicsTools::InitializePhysicsToolObjectPropertySet later.
#rb Ryan.Schmidt
#rnx
#jira none
#preflight 61a53b812e4ffe189820dc16

#ROBOMERGE-AUTHOR: semion.piskarev
#ROBOMERGE-SOURCE: CL 18315604 in //UE5/Release-5.0/... via CL 18315949
#ROBOMERGE-BOT: STARSHIP (Release-Engine-Staging -> Release-Engine-Test) (v895-18170469)

[CL 18316335 by semion piskarev in ue5-release-engine-test branch]
2021-11-29 16:34:52 -05:00

133 lines
3.3 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "Physics/PhysicsDataCollection.h"
#include "Physics/CollisionGeometryConversion.h"
#include "Engine/StaticMesh.h"
#include "Components/StaticMeshComponent.h"
#include "Components/BrushComponent.h"
#include "Components/DynamicMeshComponent.h"
#include "PhysicsEngine/BodySetup.h"
using namespace UE::Geometry;
void FPhysicsDataCollection::InitializeFromComponent(const UActorComponent* Component, bool bInitializeAggGeom)
{
SourceComponent = Component;
BodySetup = nullptr;
if ( const UStaticMeshComponent* StaticMeshComponent = Cast<UStaticMeshComponent>(Component) )
{
if (UStaticMesh* StaticMesh = StaticMeshComponent->GetStaticMesh())
{
SourceStaticMesh = StaticMesh;
BodySetup = StaticMesh->GetBodySetup();
}
}
else if (const UBrushComponent* BrushComponent = Cast<UBrushComponent>(Component))
{
BodySetup = BrushComponent->BrushBodySetup;
}
else if (const UDynamicMeshComponent* DynamicMeshComponent = Cast<UDynamicMeshComponent>(Component))
{
BodySetup = DynamicMeshComponent->GetBodySetup();
}
ExternalScale3D = FVector(1.f, 1.f, 1.f);
if (bInitializeAggGeom && BodySetup != nullptr)
{
AggGeom = BodySetup->AggGeom;
UE::Geometry::GetShapeSet(AggGeom, Geometry);
}
}
void FPhysicsDataCollection::InitializeFromStaticMesh(const UStaticMesh* StaticMesh, bool bInitializeAggGeom)
{
if (ensure(StaticMesh))
{
SourceStaticMesh = StaticMesh;
BodySetup = StaticMesh->GetBodySetup();
ExternalScale3D = FVector(1.f, 1.f, 1.f);
if (bInitializeAggGeom)
{
AggGeom = BodySetup->AggGeom;
UE::Geometry::GetShapeSet(AggGeom, Geometry);
}
}
}
void FPhysicsDataCollection::InitializeFromExisting(const FPhysicsDataCollection& Other)
{
SourceComponent = Other.SourceComponent;
SourceStaticMesh = Other.SourceStaticMesh;
BodySetup = Other.BodySetup;
ExternalScale3D = Other.ExternalScale3D;
}
void FPhysicsDataCollection::CopyGeometryFromExisting(const FPhysicsDataCollection& Other)
{
Geometry = Other.Geometry;
AggGeom = Other.AggGeom;
}
void FPhysicsDataCollection::ClearAggregate()
{
AggGeom = FKAggregateGeom();
}
void FPhysicsDataCollection::CopyGeometryToAggregate()
{
for (FBoxShape3d& BoxGeom : Geometry.Boxes)
{
FKBoxElem Element;
UE::Geometry::GetFKElement(BoxGeom.Box, Element);
AggGeom.BoxElems.Add(Element);
}
for (FSphereShape3d& SphereGeom : Geometry.Spheres)
{
FKSphereElem Element;
UE::Geometry::GetFKElement(SphereGeom.Sphere, Element);
AggGeom.SphereElems.Add(Element);
}
for (UE::Geometry::FCapsuleShape3d& CapsuleGeom : Geometry.Capsules)
{
FKSphylElem Element;
UE::Geometry::GetFKElement(CapsuleGeom.Capsule, Element);
AggGeom.SphylElems.Add(Element);
}
for (FConvexShape3d& ConvexGeom : Geometry.Convexes)
{
FKConvexElem Element;
UE::Geometry::GetFKElement(ConvexGeom.Mesh, Element);
#if !WITH_CHAOS
// Chaos will compute the IndexData itself on the call to FKConvexElem::UpdateElemBox() in ::GetFKElement() above.
// PhysX will not, so initialize that data with the mesh triangles.
// (This code should go into ::GetFKElement but cannot because it needs to be added in a hotfix)
for (FIndex3i Triangle : ConvexGeom.Mesh.TrianglesItr())
{
Element.IndexData.Add(Triangle.A);
Element.IndexData.Add(Triangle.B);
Element.IndexData.Add(Triangle.C);
}
#endif
AggGeom.ConvexElems.Add(Element);
}
}