13UVREPhysicalAnimationComponent::UVREPhysicalAnimationComponent(
const FObjectInitializer& ObjectInitializer)
56 FTransform BoneTransform;
58 if (BoneIndex > 0 && BoneIndex != ParentBoneIndex)
60 BoneTransform = RefSkel.GetRefBonePose()[BoneIndex];
62 FMeshBoneInfo BoneInfo = RefSkel.GetRefBoneInfo()[BoneIndex];
63 if (BoneInfo.ParentIndex != 0 && BoneInfo.ParentIndex != ParentBoneIndex)
75 FTransform BoneTransform;
77 if (SkeleMesh && !BoneName.IsNone() && !ParentBoneName.IsNone())
80 FReferenceSkeleton RefSkel;
81 RefSkel = SkeleMesh->SkeletalMesh->GetRefSkeleton();
91 TArray<FWeldedBoneDriverData> OriginalData;
101 if (!SkeleMesh || !SkeleMesh->Bodies.Num())
107 UPhysicsAsset* PhysAsset = SkeleMesh ? SkeleMesh->GetPhysicsAsset() :
nullptr;
108 if (PhysAsset && SkeleMesh->SkeletalMesh)
114 int32 ParentBodyIdx = PhysAsset->FindBodyIndex(BaseWeldedBoneDriverName);
116 if (FBodyInstance* ParentBody = (ParentBodyIdx == INDEX_NONE ?
nullptr : SkeleMesh->Bodies[ParentBodyIdx]))
119 FPhysicsActorHandle& ActorHandle = ParentBody->WeldParent ? ParentBody->WeldParent->GetPhysicsActorHandle() : ParentBody->GetPhysicsActorHandle();
121 if (FPhysicsInterface::IsValid(ActorHandle) )
123 FPhysicsCommand::ExecuteWrite(ActorHandle, [&](FPhysicsActorHandle&
Actor)
126 PhysicsInterfaceTypes::FInlineShapeArray Shapes;
127 FPhysicsInterface::GetAllShapes_AssumedLocked(
Actor, Shapes);
129 for (FPhysicsShapeHandle& Shape : Shapes)
131 if (ParentBody->WeldParent)
133 const FBodyInstance* OriginalBI = ParentBody->WeldParent->GetOriginalBodyInstance(Shape);
135 if (OriginalBI != ParentBody)
142 FKShapeElem* ShapeElem = FChaosUserData::Get<FKShapeElem>(FPhysicsInterface::GetUserData(Shape));
143#elif PHYSICS_INTERFACE_PHYSX
144 FKShapeElem* ShapeElem = FPhysxUserData::Get<FKShapeElem>(FPhysicsInterface::GetUserData(Shape));
148 FName TargetBoneName = ShapeElem->GetName();
149 int32 BoneIdx = SkeleMesh->GetBoneIndex(TargetBoneName);
151 if (BoneIdx != INDEX_NONE)
154 DriverData.
BoneName = TargetBoneName;
157 if (bReInit && OriginalData.Num() - 1 >=
BoneDriverMap.Num())
163 FTransform BoneTransform = FTransform::Identity;
164 if (SkeleMesh->GetBoneIndex(TargetBoneName) != INDEX_NONE)
174 DriverData.
RelativeTransform = FPhysicsInterface::GetLocalTransform(Shape) * BoneTransform;
184 ParentBody->SleepFamily = ESleepFamily::Custom;
186 float SleepEnergyThresh = FPhysicsInterface::GetSleepEnergyThreshold_AssumesLocked(
Actor);
187 SleepEnergyThresh *= ParentBody->GetSleepThresholdMultiplier();
188 FPhysicsInterface::SetSleepEnergyThreshold_AssumesLocked(
Actor, SleepEnergyThresh);
219 if (!SkeleMesh || !SkeleMesh->Bodies.Num())
222 UPhysicsAsset* PhysAsset = SkeleMesh ? SkeleMesh->GetPhysicsAsset() :
nullptr;
223 if(PhysAsset && SkeleMesh->SkeletalMesh)
227 int32 ParentBodyIdx = PhysAsset->FindBodyIndex(BaseWeldedBoneDriverName);
229 if (FBodyInstance* ParentBody = (ParentBodyIdx == INDEX_NONE ?
nullptr : SkeleMesh->Bodies[ParentBodyIdx]))
235 FPhysicsActorHandle& ActorHandle = ParentBody->WeldParent ? ParentBody->WeldParent->GetPhysicsActorHandle() : ParentBody->GetPhysicsActorHandle();
237 if (FPhysicsInterface::IsValid(ActorHandle) )
240 bool bModifiedBody =
false;
241 FPhysicsCommand::ExecuteWrite(ActorHandle, [&](FPhysicsActorHandle&
Actor)
243 PhysicsInterfaceTypes::FInlineShapeArray Shapes;
244 FPhysicsInterface::GetAllShapes_AssumedLocked(
Actor, Shapes);
246 FTransform GlobalPose = FPhysicsInterface::GetGlobalPose_AssumesLocked(ActorHandle).Inverse();
248 for (FPhysicsShapeHandle& Shape : Shapes)
251 if (ParentBody->WeldParent)
253 const FBodyInstance* OriginalBI = ParentBody->WeldParent->GetOriginalBodyInstance(Shape);
255 if (OriginalBI != ParentBody)
264 bModifiedBody =
true;
266 FTransform Trans = SkeleMesh->GetSocketTransform(WeldedData->BoneName, ERelativeTransformSpace::RTS_World);
270 FTransform GlobalTransform = WeldedData->RelativeTransform * Trans;
271 FTransform RelativeTM = GlobalTransform * GlobalPose;
273 if (!WeldedData->LastLocal.Equals(RelativeTM))
275 FPhysicsInterface::SetLocalTransform(Shape, RelativeTM);
276 WeldedData->LastLocal = RelativeTM;