From 62eb05dffd995bafe913802dcafe06f1b5aa1f7d Mon Sep 17 00:00:00 2001 From: benoit deschenes Date: Tue, 25 Jan 2022 18:15:12 -0500 Subject: [PATCH] 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] --- .../DatasmithRhinoMeshExporter.cs | 49 ++++++++++++------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/Engine/Source/Programs/Enterprise/Datasmith/DatasmithRhinoExporter/Private/ElementExporters/DatasmithRhinoMeshExporter.cs b/Engine/Source/Programs/Enterprise/Datasmith/DatasmithRhinoExporter/Private/ElementExporters/DatasmithRhinoMeshExporter.cs index 4cdf42dc3439..eaceb553b2a6 100644 --- a/Engine/Source/Programs/Enterprise/Datasmith/DatasmithRhinoExporter/Private/ElementExporters/DatasmithRhinoMeshExporter.cs +++ b/Engine/Source/Programs/Enterprise/Datasmith/DatasmithRhinoExporter/Private/ElementExporters/DatasmithRhinoMeshExporter.cs @@ -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 MeshSections = MeshInfo.RhinoMeshes; List UniqueMaterialInfo = new List(); - 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); + } + } } } \ No newline at end of file