添加 Source/FLESH/Private/SoftBodyVisceraComponent.cpp

This commit is contained in:
2025-04-21 18:02:41 +08:00
parent 37cc1a148d
commit da4725d45b

View File

@@ -0,0 +1,141 @@
#include "SoftBodyVisceraComponent.h"
#include "Engine/SkeletalMesh.h"
#include "PhysicsEngine/PhysicsAsset.h"
#include "PhysicsEngine/BodySetup.h"
USoftBodyVisceraComponent::USoftBodyVisceraComponent()
{
// Set default values
PrimaryComponentTick.bCanEverTick = true;
bWantsInitializeComponent = true;
// Create tools
SetupPhysicsTools();
}
void USoftBodyVisceraComponent::BeginPlay()
{
Super::BeginPlay();
// Initialize physics if not already initialized
if (SoftBodyPhysicsTool && !SoftBodyPhysicsTool->IsValidLowLevel())
{
SetupPhysicsTools();
}
}
void USoftBodyVisceraComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// Additional tick logic can be added here
}
bool USoftBodyVisceraComponent::InitializeSoftBodyPhysics(const FSoftBodySimulationSettings& SimulationSettings)
{
if (!SoftBodyPhysicsTool)
{
SetupPhysicsTools();
}
if (SoftBodyPhysicsTool)
{
return SoftBodyPhysicsTool->InitializeSoftBodyPhysics(this, SimulationSettings);
}
return false;
}
bool USoftBodyVisceraComponent::AddAnchorPoint(const FSoftBodyAnchorPoint& AnchorPoint)
{
if (SoftBodyPhysicsTool)
{
return SoftBodyPhysicsTool->AddAnchorPoint(AnchorPoint);
}
return false;
}
bool USoftBodyVisceraComponent::AddLineChain(const FSoftBodyLineChain& LineChain)
{
if (SoftBodyPhysicsTool)
{
return SoftBodyPhysicsTool->AddLineChain(LineChain);
}
return false;
}
bool USoftBodyVisceraComponent::AddTetrahedron(const FSoftBodyTetrahedron& Tetrahedron)
{
if (SoftBodyPhysicsTool)
{
return SoftBodyPhysicsTool->AddTetrahedron(Tetrahedron);
}
return false;
}
bool USoftBodyVisceraComponent::AddPlaneConstraint(const FSoftBodyPlaneConstraint& PlaneConstraint)
{
if (SoftBodyPhysicsTool)
{
return SoftBodyPhysicsTool->AddPlaneConstraint(PlaneConstraint);
}
return false;
}
void USoftBodyVisceraComponent::UpdateSimulationSettings(const FSoftBodySimulationSettings& SimulationSettings)
{
if (SoftBodyPhysicsTool)
{
SoftBodyPhysicsTool->UpdateSimulationSettings(SimulationSettings);
}
}
void USoftBodyVisceraComponent::SetSimulationEnabled(bool bEnable)
{
if (SoftBodyPhysicsTool)
{
SoftBodyPhysicsTool->SetSimulationEnabled(bEnable);
}
}
bool USoftBodyVisceraComponent::PerformCut(const FCutPlane& CutPlane, ECapMeshMethod CapMethod)
{
if (!BooleanCutTool)
{
SetupPhysicsTools();
}
if (BooleanCutTool && GetSkeletalMeshAsset())
{
// Perform the cut operation
TArray<USkeletalMesh*> CutResults = BooleanCutTool->CutSkeletalMesh(GetSkeletalMeshAsset(), CutPlane, NAME_None, true);
if (CutResults.Num() > 0 && CutResults[0])
{
// Update the mesh with the cut result
SetSkeletalMesh(CutResults[0]);
return true;
}
}
return false;
}
void USoftBodyVisceraComponent::SetupPhysicsTools()
{
// Create the soft body physics tool if it doesn't exist
if (!SoftBodyPhysicsTool)
{
SoftBodyPhysicsTool = NewObject<USoftBodyPhysicsTool>(this, TEXT("SoftBodyPhysicsTool"));
}
// Create the boolean cut tool if it doesn't exist
if (!BooleanCutTool)
{
BooleanCutTool = NewObject<UBooleanCutTool>(this, TEXT("BooleanCutTool"));
}
}