I am building a Dynamic mesh at runtime from a procedural mesh data. But the UV’s don’t seem to work. Here is the code:
bool ProceduralMeshToDynamicMesh(UProceduralMeshComponent* ProcMesh, UDynamicMeshComponent * OutputDynamicMesh)
{
if (!ProcMesh || !OutputDynamicMesh) return false;
FDynamicMesh3 NewMesh;
NewMesh.EnableTriangleGroups();
//NewMesh.EnableVertexUVs(FVector2f());
NewMesh.EnableVertexColors(FVector3f());
NewMesh.EnableVertexNormals(FVector3f());
NewMesh.EnableAttributes();
NewMesh.Attributes()->SetNumUVLayers(4);
TMap<int, int> VertToUVID0, VertToUVID1, VertToUVID2, VertToUVID3;
int TotalNumTriangles = 0, TotalNumVerts = 0;
for (int32 SectionIndex = 0; SectionIndex < ProcMesh->GetNumSections(); SectionIndex++)
{
TotalNumTriangles += ProcMesh->GetProcMeshSection(SectionIndex)->ProcIndexBuffer.Num();
TotalNumVerts += ProcMesh->GetProcMeshSection(SectionIndex)->ProcVertexBuffer.Num();
}
TotalNumTriangles /= 3;
int BaseIndex = 0;
int BaseVertIndex = 0;
for (int32 SectionIndex = 0; SectionIndex < ProcMesh->GetNumSections(); SectionIndex++)
{
FProcMeshSection* Section = ProcMesh->GetProcMeshSection(SectionIndex);
const int NumSectionVerts = Section->ProcVertexBuffer.Num();
if(SectionIndex > 0)
NewMesh.AllocateTriangleGroup();
TArray<FProcMeshTangent> Tangents;
for (int i = 0; i < NumSectionVerts; ++i)
{
const int VertID = NewMesh.AppendVertex(Section->ProcVertexBuffer[i].Position);
NewMesh.SetVertexColor(VertID, FVector3f(Section->ProcVertexBuffer[i].Color));
NewMesh.SetVertexNormal(VertID, FVector3f(Section->ProcVertexBuffer[i].Normal));
VertToUVID0.Add(VertID, NewMesh.Attributes()->GetUVLayer(0)->AppendElement(FVector2f(Section->ProcVertexBuffer[i].UV0)));
VertToUVID1.Add(VertID, NewMesh.Attributes()->GetUVLayer(1)->AppendElement(FVector2f(Section->ProcVertexBuffer[i].UV1)));
VertToUVID2.Add(VertID, NewMesh.Attributes()->GetUVLayer(2)->AppendElement(FVector2f(Section->ProcVertexBuffer[i].UV2)));
VertToUVID3.Add(VertID, NewMesh.Attributes()->GetUVLayer(3)->AppendElement(FVector2f(Section->ProcVertexBuffer[i].UV3)));
}
for (int i = 0; i < Section->ProcIndexBuffer.Num(); i += 3)
{
const int TriID = NewMesh.AppendTriangle(Section->ProcIndexBuffer[i] + BaseVertIndex, Section->ProcIndexBuffer[i + 1] + BaseVertIndex, Section->ProcIndexBuffer[i + 2] + BaseVertIndex, SectionIndex);
const FIndex3i& Tri = NewMesh.GetTriangle(TriID);
FIndex3i UVTri(VertToUVID0[Tri.A], VertToUVID0[Tri.B], VertToUVID0[Tri.C]);
NewMesh.Attributes()->GetUVLayer(0)->SetTriangle(TriID, UVTri);
UVTri = FIndex3i(VertToUVID1[Tri.A], VertToUVID1[Tri.B], VertToUVID1[Tri.C]);
NewMesh.Attributes()->GetUVLayer(1)->SetTriangle(TriID, UVTri);
UVTri = FIndex3i(VertToUVID2[Tri.A], VertToUVID2[Tri.B], VertToUVID2[Tri.C]);
NewMesh.Attributes()->GetUVLayer(2)->SetTriangle(TriID, UVTri);
UVTri = FIndex3i(VertToUVID3[Tri.A], VertToUVID3[Tri.B], VertToUVID3[Tri.C]);
NewMesh.Attributes()->GetUVLayer(3)->SetTriangle(TriID, UVTri);
}
BaseIndex += NewMesh.MaxTriangleID() * 3;
BaseVertIndex += NumSectionVerts;
if (ProcMesh->GetMaterial(SectionIndex))
OutputDynamicMesh->SetMaterial(SectionIndex, ProcMesh->GetMaterial(SectionIndex));
}
OutputDynamicMesh->SetMesh(MoveTemp(NewMesh));
return true;
}
Am I missing something? I have also tried SetElement(VertID, UV) but that just crashes with out of bounds errors