Messing UVs while converting from skeletalMesh to StaticMesh

Hello,

(to moderators : I am not sure this is the best forum to post this, but as this involves some c++ code, I put that here, feel free to move it)

I am using a small piece of code, based on the ConvertMeshesToStaticMesh utility code, to dynamically convert from a SkeletalMesh to a StaticMesh.
Things are going OK for position and tangents, but it looks like I am missing something for UVs :

Once converted, I get bad UVs such as :
BadUVs.png](filedata/fetch?id=1784904&d=1594040069)

While original skeletalMesh shows the right ones :
goodUVs.png](filedata/fetch?id=1784905&d=1594040086)
https://forums.unrealengine.com/core/image/gif;base64

I tried a lot of things. In the piece of code above, I am using a LODSection of skeletalMesh, from a skeletalMesh, to convert it to a static mesh (one per lod).
All the commented lines have been tried and lead to the same UVs value, There is probably an abvious thing I am missing, but even by comparing with UE other use cases of WedgeTexCoords,
I did not find my mistake.

Any help would be appreciated !

Best regards

Sebastien


const int32 NumWedges = SkelMeshSection.NumTriangles * 3;
const uint32 NumTexCoords = FMath::Min(LODData.StaticVertexBuffers.StaticMeshVertexBuffer.GetNumTexCoords(), (uint32)MAX_MESH_TEXTURE_COORDS);
for (uint32 iTexCoordIndex = 0; iTexCoordIndex < NumTexCoords; iTexCoordIndex++)
{
RawMesh.WedgeTexCoords[iTexCoordIndex].Reserve(NumWedges);
}

for (int32 WedgeIndex = 0; WedgeIndex < NumWedges; WedgeIndex++)
{
const int32 VertexIndexForWedge = IndexBuffer.Get(SkelMeshSection.BaseIndex + WedgeIndex);

…]

//RawMesh.WedgeTexCoords[0].Add(FVector2D(SkinnedVertex.U, SkinnedVertex.V));
//RawMesh.WedgeTexCoords[0].Add(InSkeletalMeshComp->GetVertexUV(VertexIndexForWedge, 0));

for (uint32 iTexCoordIndex = 0; iTexCoordIndex < NumTexCoords; iTexCoordIndex++)
{
RawMesh.WedgeTexCoords[iTexCoordIndex].Add(InSkeletalMeshComp->GetVertexUV(VertexIndexForWedge, iTexCoordIndex));
//RawMesh.WedgeTexCoords[iTexCoordIndex].Add(LODData.StaticVertexBuffers.StaticMeshVertexBuffer.GetVertexUV(VertexIndexForWedge, iTexCoordIndex));
}

…]
}

Some additional information :

In Maya, here is the UV of the shield for example :
mayaUVs.jpg](filedata/fetch?id=1784969&d=1594049314)
Expecting values from 0-0.7 in U and 0-0.5 in V.
I can see this value range when reading the skeletalMesh data, and when building my RawMesh, and again when adding a source model to build my static mesh.
I can also see the correct range value in the resulting StaticMesh SourceModels FMeshDescription, as vertex instance attribute set “TextureCoordinate”

But finally, inspecting the resulting staticMesh in UE, these are the displayed UVs ???

](filedata/fetch?id=1784970&d=1594049322)

Little up on this issue (if allowed?), this is a real blocker for my dev.

For reference, the issue was the lightmap uv index being recomputed and written over the last (and only) mesh UV set. SO check your FStaticMeshSourceModel build settings, and look for :
bGenerateLightmapUVs = true; // depending on your usage
//SrcLightmapIndex = 0;
DstLightmapIndex = firstFreeUVTexCoordIndex; // must be first free UV texture coordinate index