Files
UnrealEngineUWP/Engine/Plugins/Runtime/MeshModelingToolset/Source/ModelingComponents/Private/Physics/PhysicsDataCollection.cpp
tyson brochu e78697a2c1 SetCollisionGeometryTool: add Mesh to Level Set / SDF conversion
#rb jimmy.andrews
#preflight 62952aa7e61254772f6b4b5e

[CL 20432875 by tyson brochu in ue5-main branch]
2022-05-30 16:48:48 -04:00

141 lines
3.6 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);
}
for (UE::Geometry::FLevelSetShape3d& LevelSetGeom : Geometry.LevelSets)
{
FKLevelSetElem Element;
UE::Geometry::GetFKElement(LevelSetGeom.GridTransform, LevelSetGeom.Grid, LevelSetGeom.CellSize, Element);
AggGeom.LevelSetElems.Emplace(MoveTemp(Element));
}
}