Fixing no UV channel exported when no customization is present

#jira UE-140210
#rb JeanLuc.Corenthin
#preflight 61f07e88be0f0e0a621773c8

#ROBOMERGE-AUTHOR: benoit.deschenes
#ROBOMERGE-SOURCE: CL 18729647 in //UE5/Release-5.0/... via CL 18729705 via CL 18729800
#ROBOMERGE-BOT: UE5 (Release-Engine-Test -> Main) (v903-18687472)

[CL 18729856 by benoit deschenes in ue5-main branch]
This commit is contained in:
benoit deschenes
2022-01-25 18:15:12 -05:00
parent c583201636
commit 62eb05dffd

View File

@@ -93,9 +93,10 @@ namespace DatasmithRhino.ElementExporters
int VertexIndexOffset = 0;
int FaceIndexOffset = 0;
int UVIndexOffset = 0;
int NumberOfUVChannels = System.Math.Max(1, MeshInfo.TextureMappings.Count);
List<Mesh> MeshSections = MeshInfo.RhinoMeshes;
List<DatasmithMaterialInfo> UniqueMaterialInfo = new List<DatasmithMaterialInfo>();
InitializeDatasmithMesh(DatasmithMesh, MeshSections, MeshInfo.TextureMappings.Count);
InitializeDatasmithMesh(DatasmithMesh, MeshSections, NumberOfUVChannels);
for (int MeshIndex = 0; MeshIndex < MeshSections.Count; ++MeshIndex )
{
@@ -166,26 +167,30 @@ namespace DatasmithRhino.ElementExporters
}
}
for (int UVChannel = 0; UVChannel < MeshInfo.TextureMappings.Count; ++UVChannel)
if (MeshInfo.TextureMappings.Count > 0)
{
// Since we are offsetting the meshes before exporting them, we must apply the same correction to the UV transform.
Transform InverveOffsetTranform;
MeshInfo.OffsetTransform.TryGetInverse(out InverveOffsetTranform);
DatasmithTextureMappingData TextureMappingData = MeshInfo.TextureMappings[UVChannel];
Transform CorrectedTransform = InverveOffsetTranform * TextureMappingData.ObjectTransform;
// Rhino gives no guarantee on the state of the texture mapping in a given mesh.
// We must make sure that UV is set to the channel we are exporting.
const bool bLazyLoad = false;
RhinoMesh.SetTextureCoordinates(TextureMappingData.RhinoTextureMapping, CorrectedTransform, bLazyLoad);
// Add the UV coordinates for the triangles we just added.
for (int UVIndex = 0; UVIndex < RhinoMesh.TextureCoordinates.Count; ++UVIndex)
for (int UVChannel = 0; UVChannel < MeshInfo.TextureMappings.Count; ++UVChannel)
{
Point2f UV = RhinoMesh.TextureCoordinates[UVIndex];
DatasmithMesh.SetUV(UVChannel, UVIndex + UVIndexOffset, UV.X, 1 - UV.Y);
// Since we are offsetting the meshes before exporting them, we must apply the same correction to the UV transform.
Transform InverveOffsetTranform;
MeshInfo.OffsetTransform.TryGetInverse(out InverveOffsetTranform);
DatasmithTextureMappingData TextureMappingData = MeshInfo.TextureMappings[UVChannel];
Transform CorrectedTransform = InverveOffsetTranform * TextureMappingData.ObjectTransform;
// Rhino gives no guarantee on the state of the texture mapping in a given mesh.
// We must make sure that UV is set to the channel we are exporting.
const bool bLazyLoad = false;
RhinoMesh.SetTextureCoordinates(TextureMappingData.RhinoTextureMapping, CorrectedTransform, bLazyLoad);
// Add the UV coordinates to the current channel.
AddUVsToMesh(DatasmithMesh, RhinoMesh, UVChannel, UVIndexOffset);
}
}
else
{
// No custom TextureMapping, just export the current UV coordinate in channel 0.
AddUVsToMesh(DatasmithMesh, RhinoMesh, 0, UVIndexOffset);
}
VertexIndexOffset += RhinoMesh.Vertices.Count;
FaceIndexOffset += RhinoMesh.Faces.Count + RhinoMesh.Faces.QuadCount;
@@ -228,5 +233,15 @@ namespace DatasmithRhino.ElementExporters
Mesh.SetNormal(NormalIndex + 1, NormalB.X, NormalB.Y, NormalB.Z);
Mesh.SetNormal(NormalIndex + 2, NormalC.X, NormalC.Y, NormalC.Z);
}
private static void AddUVsToMesh(FDatasmithFacadeMesh DatasmithMesh, Mesh RhinoMesh, int UVChannel, int UVIndexOffset)
{
// Add the UV coordinates for the triangles we just added.
for (int UVIndex = 0; UVIndex < RhinoMesh.TextureCoordinates.Count; ++UVIndex)
{
Point2f UV = RhinoMesh.TextureCoordinates[UVIndex];
DatasmithMesh.SetUV(UVChannel, UVIndex + UVIndexOffset, UV.X, 1 - UV.Y);
}
}
}
}