// Fill out your copyright notice in the Description page of Project Settings. #include "AnimNode_PlayBVH.h" #include "Animation/AnimInstanceProxy.h" #include #include #include #include #include #include "Misc/CoreMiscDefines.h" #include "Async/Async.h" #include "OPPOAPI.h" #include "Launch/Resources/Version.h" FAnimNode_PlayBVH::FAnimNode_PlayBVH() { } void FAnimNode_PlayBVH::Initialize_AnyThread(const FAnimationInitializeContext& Context) { //FAnimNode_Base::Initialize_AnyThread(Context); BasePose.Initialize(Context); OwningActor = Context.AnimInstanceProxy->GetSkelMeshComponent()->GetOwner(); FVector t(156.58,77.7433,-95.1845); /*FQuat q = FQuat(0.1f, 0.1f, -0.2f, -1.0f); FRotator ttt = q.Rotator(); FRotator ww(10, -30, 160); FQuat z = FQuat(ww); FQuat t = UnityCoordinate2UECoordinate(q.GetNormalized()); FRotator tt = t.Rotator();*/ /*TActorIterator actorItr = TActorIterator(OwningActor->GetWorld()); for (actorItr; actorItr; ++actorItr) { if (actorItr) { AOPPOWorkManager* pTmpManager = Cast(*actorItr); if (pTmpManager) { oppoManager = pTmpManager; break; } } }*/ if (BVHAsset != nullptr) { for (int32 i = BVHAsset->FrameDataOffset; i < BVHAsset->AnimData.Num(); i++) { TArray Words; TArray FrameBoneInfo; if (BVHAsset->AnimData[i].ParseIntoArray(Words, TEXT(" "), false) >= BVHAsset->ChannelNum) { for (int32 j = 0; j < BVHAsset->BoneTotalNumber; j++) { FBoneMapInfo tempBoneInfo; FString strName = BVHAsset->Name_Joint[j].ToString(); /*if(strName.Contains("Hand")) { int32 strIndex = strName.Find("Hand"); int32 temp = strName.Len() - strIndex; if (temp > 4) continue; } */ tempBoneInfo.BoneName = BVHAsset->Name_Joint[j]; /*float Xposition = 0; float Yposition = 0; float Zposition = 0; float Zrotation = 0; float Xrotation = 0; float Yrotation = 0;*/ if (j == 0) { tempBoneInfo.Xposition = FCString::Atof(*Words[0]); tempBoneInfo.Yposition = FCString::Atof(*Words[1]); tempBoneInfo.Zposition = FCString::Atof(*Words[2]); /*Zrotation = wrapAngle(FCString::Atof(*Words[j * 3 + 3])); Xrotation = wrapAngle(FCString::Atof(*Words[j * 3 + 4])); Yrotation = wrapAngle(FCString::Atof(*Words[j * 3 + 5]));*/ tempBoneInfo.Zrotation = FCString::Atof(*Words[j * 3 + 3]); tempBoneInfo.Xrotation = FCString::Atof(*Words[j * 3 + 4]); tempBoneInfo.Yrotation = FCString::Atof(*Words[j * 3 + 5]); tempBoneInfo.TranslationMode = VF_Additive; //tempBoneInfo.bvhPos = FVector(Xposition, Zposition, Yposition); tempBoneInfo.bvhPos = FVector(tempBoneInfo.Xposition, tempBoneInfo.Zposition, tempBoneInfo.Yposition); // Unity } else { tempBoneInfo.Zrotation = FCString::Atof(*Words[j * 3 + 3]); tempBoneInfo.Xrotation = FCString::Atof(*Words[j * 3 + 4]); tempBoneInfo.Yrotation = FCString::Atof(*Words[j * 3 + 5]); tempBoneInfo.TranslationMode = VF_Ignore; } /*FQuat qx(FVector(1, 0, 0), FMath::DegreesToRadians(-Xrotation)); FQuat qz(FVector(0, 0, 1), FMath::DegreesToRadians(-Yrotation)); FQuat qy(FVector(0, 1, 0), FMath::DegreesToRadians(-Zrotation)); FQuat qu = qy * qz * qx; */ //tempBoneInfo.bvhRot = qu; FrameBoneInfo.Add(tempBoneInfo); } BoneInfoList.Add(i - BVHAsset->FrameDataOffset, FrameBoneInfo); } } } } void FAnimNode_PlayBVH::CacheBones_AnyThread(const FAnimationCacheBonesContext& Context) { BasePose.CacheBones(Context); } void FAnimNode_PlayBVH::Update_AnyThread(const FAnimationUpdateContext& Context) { BasePose.Update(Context); if (isCanPlay) { if (BoneInfoList.Num() > 0) { FrameData.Empty(); FrameData = BoneInfoList[CurrentFrame]; CurrentFrame++; if (CurrentFrame >= BoneInfoList.Num() - 1) { if (isLoop) { CurrentFrame = 0; } else { CurrentFrame = 0; isCanPlay = false; } } } else { isCanPlay = false; } } } //void FAnimNode_PlayBVH::EvaluateComponentSpace_AnyThread(FComponentSpacePoseContext& Output) //{ // BasePose.EvaluateComponentSpace(Output); // // Output.ResetToRefPose(); // check(Output.AnimInstanceProxy->GetSkeleton() != nullptr); // // const FBoneContainer& BoneContainer = Output.Pose.GetPose().GetBoneContainer(); // FTransform ComponentTransform = Output.AnimInstanceProxy->GetComponentTransform(); // // int32 index = BoneContainer.GetPoseBoneIndexForBoneName("LeftArm"); // if (index != -1) // { // FCompactPoseBoneIndex CompactPoseBoneToModify(index); // FTransform NewBoneTM = Output.Pose.GetComponentSpaceTransform(CompactPoseBoneToModify); // // FAnimationRuntime::ConvertCSTransformToBoneSpace(ComponentTransform, Output.Pose, NewBoneTM, CompactPoseBoneToModify, BCS_ComponentSpace); // // FQuat qx(FVector(1, 0, 0), FMath::DegreesToRadians(xRot)); // FQuat qz(FVector(0, 0, 1), FMath::DegreesToRadians(yRot)); // FQuat qy(FVector(0, 1, 0), -FMath::DegreesToRadians(zRot)); // // FQuat qu = qy * qz * qx; // NewBoneTM.SetRotation(qu*NewBoneTM.GetRotation()); // // //Convert back to Component Space. // FAnimationRuntime::ConvertBoneSpaceTransformToCS(ComponentTransform, Output.Pose, NewBoneTM, CompactPoseBoneToModify, BCS_ComponentSpace); // Output.Pose.SetComponentSpaceTransform(CompactPoseBoneToModify, NewBoneTM); // // } //} void FAnimNode_PlayBVH::EvaluateComponentSpace_AnyThread(FComponentSpacePoseContext& Output) { BasePose.EvaluateComponentSpace(Output); Output.ResetToRefPose(); check(Output.AnimInstanceProxy->GetSkeleton() != nullptr); const FBoneContainer& BoneContainer = Output.Pose.GetPose().GetBoneContainer(); FTransform ComponentTransform = Output.AnimInstanceProxy->GetComponentTransform(); for (int32 i = 0; i < FrameData.Num(); i++) { GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("BoneName: %s"), *FrameData[i].BoneName.ToString())); int32 index = BoneContainer.GetPoseBoneIndexForBoneName(FrameData[i].BoneName); if (index != -1) { FCompactPoseBoneIndex CompactPoseBoneToModify(index); FTransform NewBoneTM = Output.Pose.GetComponentSpaceTransform(CompactPoseBoneToModify); //Convert to Bone Space. if (FrameData[i].TranslationMode == VF_Additive) { //Convert to Bone Space. FAnimationRuntime::ConvertCSTransformToBoneSpace(ComponentTransform, Output.Pose, NewBoneTM, CompactPoseBoneToModify, BCS_ComponentSpace); NewBoneTM.SetTranslation(FrameData[i].bvhPos); //Convert back to Component Space. FAnimationRuntime::ConvertBoneSpaceTransformToCS(ComponentTransform, Output.Pose, NewBoneTM, CompactPoseBoneToModify, BCS_ComponentSpace); } FAnimationRuntime::ConvertCSTransformToBoneSpace(ComponentTransform, Output.Pose, NewBoneTM, CompactPoseBoneToModify, BCS_ComponentSpace); FQuat qx(FVector(1, 0, 0), -FMath::DegreesToRadians(FrameData[i].Xrotation)); FQuat qz(FVector(0, 0, 1), -FMath::DegreesToRadians(FrameData[i].Yrotation)); FQuat qy(FVector(0, 1, 0), -FMath::DegreesToRadians(FrameData[i].Zrotation)); FQuat qu = qy * qz * qx; /*FQuat rotation = FQuat::Identity; rotation *= FQuat(FVector::ForwardVector, -FMath::DegreesToRadians(FrameData[i].Xrotation)); rotation *= FQuat(FVector::UpVector, -FMath::DegreesToRadians(FrameData[i].Zrotation)); rotation *= FQuat(FVector::RightVector, FMath::DegreesToRadians(FrameData[i].Yrotation));*/ NewBoneTM.SetRotation(qu * NewBoneTM.GetRotation()); FAnimationRuntime::ConvertBoneSpaceTransformToCS(ComponentTransform, Output.Pose, NewBoneTM, CompactPoseBoneToModify, BCS_ComponentSpace); Output.Pose.SetComponentSpaceTransform(CompactPoseBoneToModify, NewBoneTM); } } } //void FAnimNode_PlayBVH::EvaluateComponentSpace_AnyThread(FComponentSpacePoseContext& Output) //{ // BasePose.EvaluateComponentSpace(Output); // // Output.ResetToRefPose(); // check(Output.AnimInstanceProxy->GetSkeleton() != nullptr); // USkeleton *skeleton = Output.AnimInstanceProxy->GetSkeleton(); // // // const FBoneContainer& BoneContainer = Output.Pose.GetPose().GetBoneContainer(); // FTransform ComponentTransform = Output.AnimInstanceProxy->GetComponentTransform(); // for (int32 i = 0; i < FrameData.Num(); i++) // { // GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("BoneName: %s"), *FrameData[i].BoneName.ToString())); // int32 index = BoneContainer.GetPoseBoneIndexForBoneName(FrameData[i].BoneName); // // if (index != -1) // { // // FCompactPoseBoneIndex CompactPoseBoneToModify(index); // FTransform NewBoneTM = Output.Pose.GetLocalSpaceTransform(CompactPoseBoneToModify); // ttt = NewBoneTM.GetRotation().Rotator(); // //Convert to Bone Space. // if (FrameData[i].TranslationMode == VF_Additive) // { // //Convert to Bone Space. // //FAnimationRuntime::ConvertCSTransformToBoneSpace(ComponentTransform, Output.Pose, NewBoneTM, CompactPoseBoneToModify, BCS_ComponentSpace); // //NewBoneTM.SetTranslation(FrameData[i].bvhPos); // //Convert back to Component Space. // //FAnimationRuntime::ConvertBoneSpaceTransformToCS(ComponentTransform, Output.Pose, NewBoneTM, CompactPoseBoneToModify, BCS_ComponentSpace); // // } // // FRotator bvhRotator(-FrameData[i].Yrotation, -FrameData[i].Zrotation, FrameData[i].Xrotation); // NewBoneTM.SetRotation(NewBoneTM.GetRotation()* FQuat(bvhRotator)); // Output.Pose.SetComponentSpaceTransform(CompactPoseBoneToModify, NewBoneTM); // /*FCompactPoseBoneIndex ParentIndex = Output.Pose.GetPose().GetParentBoneIndex(CompactPoseBoneToModify); // if (ParentIndex != INDEX_NONE) // { // const FTransform& ParentTM = Output.Pose.GetComponentSpaceTransform(ParentIndex); // FTransform NewTransform = NewBoneTM * ParentTM; // Output.Pose.SetComponentSpaceTransform(CompactPoseBoneToModify, NewTransform); // } // else // { // Output.Pose.SetComponentSpaceTransform(CompactPoseBoneToModify, NewBoneTM); // }*/ // //FAnimationRuntime::ConvertCSTransformToBoneSpace(ComponentTransform, Output.Pose, NewBoneTM, CompactPoseBoneToModify, BCS_BoneSpace); // //FVector VecOld = NewBoneTM.GetRotation().Euler(); // /*FVector VecNew(FrameData[i].Xrotation + VecOld.X, // -FrameData[i].Zrotation + VecOld.Y, // FrameData[i].Yrotation + VecOld.Z);*/ // // //FRotator bvhRotator(FrameData[i].Xrotation, -FrameData[i].Yrotation, -FrameData[i].Zrotation); // // // //NewBoneTM.SetRotation(FQuat(bvhRotator)* NewBoneTM.GetRotation()); // // //NewBoneTM.SetRotation(qu * NewBoneTM.GetRotation()); // //Convert back to Component Space. // //FAnimationRuntime::ConvertBoneSpaceTransformToCS(ComponentTransform, Output.Pose, NewBoneTM, CompactPoseBoneToModify, BCS_BoneSpace); // // //Output.Pose.SetComponentSpaceTransform(CompactPoseBoneToModify, NewBoneTM); // } // } //} void FAnimNode_PlayBVH::GatherDebugData(FNodeDebugData& DebugData) { Super::GatherDebugData(DebugData); } //void FAnimNode_PlayBVH::PreUpdate(const UAnimInstance* InAnimInstance) //{ // Super::PreUpdate(InAnimInstance); // //} void FAnimNode_PlayBVH::OnInitializeAnimInstance(const FAnimInstanceProxy* InProxy, const UAnimInstance* InAnimInstance) { Super::OnInitializeAnimInstance(InProxy, InAnimInstance); }