Hi, I’m trying to work with this but I’m finding some issues when applying the recreated morph targets…
USkeletalMesh *FinalMesh = NewObject<USkeletalMesh>(this, USkeletalMesh::StaticClass(), FName("MergedMesh"), RF_Transient);
TArray<FSkelMeshMergeSectionMapping> SectionMappings;
FSkeletalMeshMerge Merger(FinalMesh, MeshArray, SectionMappings, 0);
//Merger.GenerateLODModel<int>(0);
const bool MergeStatus = Merger.DoMerge();
check(MergeStatus == true);
SetSkeletalMesh(FinalMesh);
//We need to add the number of vertices per LOD
//so we can recreate the morph target on the newly merged mesh
for(int i = 0; i < MeshArray.Num(); ++i)
{
for(int j = 0; j < MeshArray[i]->MorphTargets.Num(); ++j)
{
if(MeshArray[i]->MorphTargets[j]->HasDataForLOD(0) == true)
{
int32 NumDeltas = 0;
MeshArray[i]->MorphTargets[j]->GetMorphTargetDelta(0, NumDeltas);
NumVerticesPerLOD[0] += NumDeltas;
UE_LOG(LogTemp, Log, TEXT("This morph target has this many deltas : %d"), NumDeltas);
}
}
}
for(int i = 0; i < MeshArray.Num(); ++i)
{
for(int j = 0; j < MeshArray[i]->MorphTargets.Num(); ++j)
{
if(MeshArray[i]->MorphTargets[j]->HasDataForLOD(0) == true)
{
UMorphTarget* FinalMeshMorphTarget = NewObject<UMorphTarget>(this, UMorphTarget::StaticClass());
TArray<FMorphTargetDelta> NewMorphTargetDeltas;
int32 NumDeltas = 0;
FMorphTargetDelta *MorphTargetDeltaArray = MeshArray[i]->MorphTargets[j]->GetMorphTargetDelta(0, NumDeltas);
for(int z = 0; z < NumDeltas; ++z)
{
FMorphTargetDelta NewTargetDelta(MorphTargetDeltaArray[z]);
NewTargetDelta.SourceIdx += NumVerticesPerLOD[0];
NewMorphTargetDeltas.Add(NewTargetDelta);
}
#if WITH_EDITOR
TArray<FSkelMeshSection> Sections;
for (int w = 0; w < MeshArray[i]->GetImportedModel()->LODModels[0].Sections.Num(); ++w)
{
Sections.Add(MeshArray[i]->GetImportedModel()->LODModels[0].Sections[w]);
Sections.Last().BaseIndex += NumVerticesPerLOD[0];
}
FinalMeshMorphTarget->PopulateDeltas(NewMorphTargetDeltas, 0, Sections);
#endif
FinalMeshMorphTarget->BaseSkelMesh = FinalMesh;
FinalMesh->MorphTargets.Add(FinalMeshMorphTarget);
FinalMesh->MorphTargetIndexMap.Add(TPair<FName, int32>(FName("NewMorphTarget"), 0));
//UE_LOG(LogTemp, Log, TEXT("The source idx of the new morph target is : %d | The old one was : %d"), NewMorphTarget.SourceIdx, MorphTargetInfo->SourceIdx);
}
}
}
NumVerticesPerLOD[0] = 0;
I think the issue is with the vertex buffers. I know I’m missing something but can’t quite figure out what yet. Any help would be appreciated! I’m on UE4 4.24