Skip to content

Commit 9e01673

Browse files
Add support for 8 uv channels (#1113)
Extended the number of UV channels from 2 to 8.
1 parent c31ed63 commit 9e01673

File tree

8 files changed

+274
-35
lines changed

8 files changed

+274
-35
lines changed

Maya/Exporter/BabylonExporter.Mesh.cs

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal partial class BabylonExporter
1515
private MStringArray allMayaInfluenceNames; // the joint names that influence the mesh (joint with 0 weight included)
1616
private MDoubleArray allMayaInfluenceWeights; // the joint weights for the vertex (0 weight included)
1717
private Dictionary<string, int> indexByNodeName = new Dictionary<string, int>(); // contains the node (joint and parents of the current skin) fullPathName and its index
18-
18+
1919
/// <summary>
2020
///
2121
/// </summary>
@@ -496,7 +496,7 @@ private BabylonNode ExportMesh(MDagPath mDagPath, BabylonScene babylonScene)
496496

497497
var uvSetNames = new MStringArray();
498498
mFnMesh.getUVSetNames(uvSetNames);
499-
bool[] isUVExportSuccess = new bool[Math.Min(uvSetNames.Count, 2)];
499+
bool[] isUVExportSuccess = new bool[Math.Min(uvSetNames.Count, 8)];
500500
for (int indexUVSet = 0; indexUVSet < isUVExportSuccess.Length; indexUVSet++)
501501
{
502502
isUVExportSuccess[indexUVSet] = true;
@@ -584,13 +584,36 @@ private BabylonNode ExportMesh(MDagPath mDagPath, BabylonScene babylonScene)
584584
// UVs
585585
if (uvSetNames.Count > 0 && isUVExportSuccess[0])
586586
{
587-
588587
babylonMesh.uvs = vertices.SelectMany(v => v.UV).ToArray();
589588
}
590589
if (uvSetNames.Count > 1 && isUVExportSuccess[1])
591590
{
592591
babylonMesh.uvs2 = vertices.SelectMany(v => v.UV2).ToArray();
593592
}
593+
if (uvSetNames.Count > 2 && isUVExportSuccess[2])
594+
{
595+
babylonMesh.uvs3 = vertices.SelectMany(v => v.UV3).ToArray();
596+
}
597+
if (uvSetNames.Count > 3 && isUVExportSuccess[3])
598+
{
599+
babylonMesh.uvs4 = vertices.SelectMany(v => v.UV4).ToArray();
600+
}
601+
if (uvSetNames.Count > 4 && isUVExportSuccess[4])
602+
{
603+
babylonMesh.uvs5 = vertices.SelectMany(v => v.UV5).ToArray();
604+
}
605+
if (uvSetNames.Count > 5 && isUVExportSuccess[5])
606+
{
607+
babylonMesh.uvs6 = vertices.SelectMany(v => v.UV6).ToArray();
608+
}
609+
if (uvSetNames.Count > 6 && isUVExportSuccess[6])
610+
{
611+
babylonMesh.uvs7 = vertices.SelectMany(v => v.UV7).ToArray();
612+
}
613+
if (uvSetNames.Count > 7 && isUVExportSuccess[7])
614+
{
615+
babylonMesh.uvs8 = vertices.SelectMany(v => v.UV8).ToArray();
616+
}
594617

595618
babylonMesh.subMeshes = subMeshes.ToArray();
596619

@@ -824,6 +847,37 @@ private void ExtractGeometry(BabylonMesh babylonMesh, MFnMesh mFnMesh, List<Glob
824847

825848
}
826849

850+
/// <summary>
851+
/// Extract vertex UV for the given UV channel.
852+
/// </summary>
853+
/// <param name="mFnMesh"></param>
854+
/// <param name="polygonId">The polygon (face) to examine</param>
855+
/// <param name="vertexIndexLocal">The face-relative (local) vertex id to examine</param>
856+
/// <param name="indexUVSet">Index of the target UV set.</param>
857+
/// <param name="uvSetNames"></param>
858+
/// <param name="isUVExportSuccess"></param>
859+
/// <returns></returns>
860+
private float[] ExtractVertexUV(MFnMesh mFnMesh, int polygonId, int vertexIndexLocal, int indexUVSet, MStringArray uvSetNames,ref bool[] isUVExportSuccess)
861+
{
862+
if (uvSetNames.Count > indexUVSet && isUVExportSuccess[indexUVSet])
863+
{
864+
try
865+
{
866+
float u = 0, v = 0;
867+
mFnMesh.getPolygonUV(polygonId, vertexIndexLocal, ref u, ref v, uvSetNames[indexUVSet]);
868+
return new float[] { u, v };
869+
}
870+
catch
871+
{
872+
// An exception is raised when a vertex isn't mapped to an UV coordinate
873+
isUVExportSuccess[indexUVSet] = false;
874+
return null;
875+
}
876+
}
877+
878+
return null;
879+
}
880+
827881
/// <summary>
828882
/// Extract geometry (position, normal, UVs...) for a specific vertex
829883
/// </summary>
@@ -907,37 +961,14 @@ private GlobalVertex ExtractVertex(MFnMesh mFnMesh, int polygonId, int vertexInd
907961
}
908962
}
909963

910-
// UV
911-
int indexUVSet = 0;
912-
if (uvSetNames.Count > indexUVSet && isUVExportSuccess[indexUVSet])
913-
{
914-
try
915-
{
916-
float u = 0, v = 0;
917-
mFnMesh.getPolygonUV(polygonId, vertexIndexLocal, ref u, ref v, uvSetNames[indexUVSet]);
918-
vertex.UV = new float[] { u, v };
919-
}
920-
catch
921-
{
922-
// An exception is raised when a vertex isn't mapped to an UV coordinate
923-
isUVExportSuccess[indexUVSet] = false;
924-
}
925-
}
926-
indexUVSet = 1;
927-
if (uvSetNames.Count > indexUVSet && isUVExportSuccess[indexUVSet])
928-
{
929-
try
930-
{
931-
float u = 0, v = 0;
932-
mFnMesh.getPolygonUV(polygonId, vertexIndexLocal, ref u, ref v, uvSetNames[indexUVSet]);
933-
vertex.UV2 = new float[] { u, v };
934-
}
935-
catch
936-
{
937-
// An exception is raised when a vertex isn't mapped to an UV coordinate
938-
isUVExportSuccess[indexUVSet] = false;
939-
}
940-
}
964+
vertex.UV = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 0, uvSetNames, ref isUVExportSuccess); // UV
965+
vertex.UV2 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 1, uvSetNames, ref isUVExportSuccess); // UV2
966+
vertex.UV3 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 2, uvSetNames, ref isUVExportSuccess); // UV3
967+
vertex.UV4 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 3, uvSetNames, ref isUVExportSuccess); // UV4
968+
vertex.UV5 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 4, uvSetNames, ref isUVExportSuccess); // UV5
969+
vertex.UV6 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 5, uvSetNames, ref isUVExportSuccess); // UV6
970+
vertex.UV7 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 6, uvSetNames, ref isUVExportSuccess); // UV7
971+
vertex.UV8 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 7, uvSetNames, ref isUVExportSuccess); // UV8
941972

942973
// skin
943974
if (mFnSkinCluster != null)

Maya/Exporter/GlobalVertex.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public struct GlobalVertex
1111
public float[] Tangent { get; set; } // Vec3
1212
public float[] UV { get; set; } // Vec2
1313
public float[] UV2 { get; set; } // Vec2
14+
public float[] UV3 { get; set; } // Vec2
15+
public float[] UV4 { get; set; } // Vec2
16+
public float[] UV5 { get; set; } // Vec2
17+
public float[] UV6 { get; set; } // Vec2
18+
public float[] UV7 { get; set; } // Vec2
19+
public float[] UV8 { get; set; } // Vec2
1420
public int BonesIndices { get; set; }
1521
public float[] Weights { get; set; } // Vec4
1622
public int BonesIndicesExtra { get; set; }
@@ -26,6 +32,12 @@ public GlobalVertex(GlobalVertex other)
2632
this.Tangent = other.Tangent;
2733
this.UV = other.UV;
2834
this.UV2 = other.UV2;
35+
this.UV3 = other.UV3;
36+
this.UV4 = other.UV4;
37+
this.UV5 = other.UV5;
38+
this.UV6 = other.UV6;
39+
this.UV7 = other.UV7;
40+
this.UV8 = other.UV8;
2941
this.BonesIndices = other.BonesIndices;
3042
this.Weights = other.Weights;
3143
this.BonesIndicesExtra = other.BonesIndicesExtra;
@@ -49,6 +61,12 @@ public override bool Equals(object obj)
4961
other.Normal.IsAlmostEqualTo(Normal, Tools.Epsilon) &
5062
other.UV.IsAlmostEqualTo(UV, Tools.Epsilon) &&
5163
other.UV2.IsAlmostEqualTo(UV2, Tools.Epsilon) &&
64+
other.UV3.IsAlmostEqualTo(UV3, Tools.Epsilon) &&
65+
other.UV4.IsAlmostEqualTo(UV4, Tools.Epsilon) &&
66+
other.UV5.IsAlmostEqualTo(UV5, Tools.Epsilon) &&
67+
other.UV6.IsAlmostEqualTo(UV6, Tools.Epsilon) &&
68+
other.UV7.IsAlmostEqualTo(UV7, Tools.Epsilon) &&
69+
other.UV8.IsAlmostEqualTo(UV8, Tools.Epsilon) &&
5270
other.Weights.IsAlmostEqualTo(Weights, Tools.Epsilon) &&
5371
other.WeightsExtra.IsAlmostEqualTo(WeightsExtra, Tools.Epsilon) &&
5472
other.Color.IsAlmostEqualTo(Color, Tools.Epsilon) &&

0 commit comments

Comments
 (0)