I have FleshAsset which is embedded inside a skeletal mesh component . My target is to deform the mesh by some velocity when the user clicks on the Asset on the viewport . For that I take the mouse cooordinates and find the FleshAsset under it . I also find the Vertices that overlap the impact point .I applied deformations on the vertices but they are not visible on the viewport . Following is my code snippet
USkeletalMeshComponent pComponent = Cast(HitResult.GetComponent());
if (!pComponent)
return;
AActor FleshOwner = pComponent->GetOwner();
if (!FleshOwner)
{
return ;
}TArray<UFleshComponent*> Components;
FleshOwner->GetComponents(UFleshComponent::StaticClass(), Components);
for (UActorComponent* Component : Components)
{
UFleshComponent* FleshComponent = Cast(Component);
FleshComponent->bAllowAnyoneToDestroyMe = true;
FleshComponent->bHasPerInstanceHitProxies = true;
FleshComponent->bRayTracingFarField = true;
FleshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndProbe);
if (FleshComponent && FleshComponent->IsAttachedTo(pComponent))
{
UFleshAsset* fleshAsset = const_cast<UFleshAsset*>(FleshComponent->GetRestCollection());
UDeformableTetrahedralComponent *pDefTetComp = Cast(FleshComponent);if (!pDefTetComp) { UE_LOG(LogTemp, Log, TEXT("DeformableTetrahedralComponent is Empty")); } else { pDefTetComp->RegisterComponent(); } UStaticMesh* StaticMesh = fleshAsset->StaticMesh.Get(); UBodySetup* BodySetup = StaticMesh->GetBodySetup(); if (BodySetup) { BodySetup->Modify(); BodySetup->InvalidatePhysicsData(); BodySetup->MarkPackageDirty(); FTransform ComponentTransform = FleshComponent->GetComponentTransform(); FleshComponent->SetSimulatePhysics(true); FKAggregateGeom& AggGeom = BodySetup->AggGeom; for (int32 i = 0; i < AggGeom.ConvexElems.Num(); ++i) { FKConvexElem& ConvexElem = AggGeom.ConvexElems[i]; TArray<FVector> & tVertices = ConvexElem.VertexData; TArray<int32> Indices = ConvexElem.IndexData; for (int32 Index = 0; Index < Indices.Num(); Index += 3) { if (Index + 2 < Indices.Num()) { FVector Vertex1 = ComponentTransform.TransformPosition(tVertices[Indices[Index]]); FVector Vertex2 = ComponentTransform.TransformPosition(tVertices[Indices[Index + 1]]); FVector Vertex3 = ComponentTransform.TransformPosition(tVertices[Indices[Index + 2]]); UE_LOG(LogTemp, Log, TEXT("Face vertices: %s, %s, %s"), *Vertex1.ToString(), *Vertex2.ToString(), *Vertex3.ToString()); if (PointInTriangle(HitResult.ImpactPoint, Vertex1, Vertex2, Vertex3)) { UE_LOG(LogTemp, Log, TEXT("Impact point lies on face with vertices: %s, %s, %s"), *Vertex1.ToString(), *Vertex2.ToString(), *Vertex3.ToString()); float DeformStrength = 100.0f; FVector DeformDirection = (Vertex1 - HitResult.ImpactPoint).GetSafeNormal(); Vertex1 += DeformDirection * DeformStrength; DeformDirection = (Vertex2 - HitResult.ImpactPoint).GetSafeNormal(); Vertex2 += DeformDirection * DeformStrength; DeformDirection = (Vertex3 - HitResult.ImpactPoint).GetSafeNormal(); Vertex3 += DeformDirection * DeformStrength; // Update the vertices back to local space tVertices[Indices[Index]] = ComponentTransform.InverseTransformPosition(Vertex1); tVertices[Indices[Index + 1]] = ComponentTransform.InverseTransformPosition(Vertex2); tVertices[Indices[Index + 2]] = ComponentTransform.InverseTransformPosition(Vertex3); pDefTetComp->AddVelocityChangeImpulseAtLocation(DeformDirection, HitResult.ImpactPoint); } } } } ConvexElem.ComputeChaosConvexIndices(true); ConvexElem.SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); BodySetup->CreatePhysicsMeshes(); FleshComponent->RecreatePhysicsState(); FleshComponent->MarkRenderStateDirty(); StaticMesh->Build(); }