Skip to content

Commit

Permalink
Add support for 8 uv channels (#1113)
Browse files Browse the repository at this point in the history
Extended the number of UV channels from 2 to 8.
  • Loading branch information
SergioRZMasson committed Mar 12, 2024
1 parent c31ed63 commit 9e01673
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 35 deletions.
99 changes: 65 additions & 34 deletions Maya/Exporter/BabylonExporter.Mesh.cs
Expand Up @@ -15,7 +15,7 @@ internal partial class BabylonExporter
private MStringArray allMayaInfluenceNames; // the joint names that influence the mesh (joint with 0 weight included)
private MDoubleArray allMayaInfluenceWeights; // the joint weights for the vertex (0 weight included)
private Dictionary<string, int> indexByNodeName = new Dictionary<string, int>(); // contains the node (joint and parents of the current skin) fullPathName and its index

/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -496,7 +496,7 @@ private BabylonNode ExportMesh(MDagPath mDagPath, BabylonScene babylonScene)

var uvSetNames = new MStringArray();
mFnMesh.getUVSetNames(uvSetNames);
bool[] isUVExportSuccess = new bool[Math.Min(uvSetNames.Count, 2)];
bool[] isUVExportSuccess = new bool[Math.Min(uvSetNames.Count, 8)];
for (int indexUVSet = 0; indexUVSet < isUVExportSuccess.Length; indexUVSet++)
{
isUVExportSuccess[indexUVSet] = true;
Expand Down Expand Up @@ -584,13 +584,36 @@ private BabylonNode ExportMesh(MDagPath mDagPath, BabylonScene babylonScene)
// UVs
if (uvSetNames.Count > 0 && isUVExportSuccess[0])
{

babylonMesh.uvs = vertices.SelectMany(v => v.UV).ToArray();
}
if (uvSetNames.Count > 1 && isUVExportSuccess[1])
{
babylonMesh.uvs2 = vertices.SelectMany(v => v.UV2).ToArray();
}
if (uvSetNames.Count > 2 && isUVExportSuccess[2])
{
babylonMesh.uvs3 = vertices.SelectMany(v => v.UV3).ToArray();
}
if (uvSetNames.Count > 3 && isUVExportSuccess[3])
{
babylonMesh.uvs4 = vertices.SelectMany(v => v.UV4).ToArray();
}
if (uvSetNames.Count > 4 && isUVExportSuccess[4])
{
babylonMesh.uvs5 = vertices.SelectMany(v => v.UV5).ToArray();
}
if (uvSetNames.Count > 5 && isUVExportSuccess[5])
{
babylonMesh.uvs6 = vertices.SelectMany(v => v.UV6).ToArray();
}
if (uvSetNames.Count > 6 && isUVExportSuccess[6])
{
babylonMesh.uvs7 = vertices.SelectMany(v => v.UV7).ToArray();
}
if (uvSetNames.Count > 7 && isUVExportSuccess[7])
{
babylonMesh.uvs8 = vertices.SelectMany(v => v.UV8).ToArray();
}

babylonMesh.subMeshes = subMeshes.ToArray();

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

}

/// <summary>
/// Extract vertex UV for the given UV channel.
/// </summary>
/// <param name="mFnMesh"></param>
/// <param name="polygonId">The polygon (face) to examine</param>
/// <param name="vertexIndexLocal">The face-relative (local) vertex id to examine</param>
/// <param name="indexUVSet">Index of the target UV set.</param>
/// <param name="uvSetNames"></param>
/// <param name="isUVExportSuccess"></param>
/// <returns></returns>
private float[] ExtractVertexUV(MFnMesh mFnMesh, int polygonId, int vertexIndexLocal, int indexUVSet, MStringArray uvSetNames,ref bool[] isUVExportSuccess)
{
if (uvSetNames.Count > indexUVSet && isUVExportSuccess[indexUVSet])
{
try
{
float u = 0, v = 0;
mFnMesh.getPolygonUV(polygonId, vertexIndexLocal, ref u, ref v, uvSetNames[indexUVSet]);
return new float[] { u, v };
}
catch
{
// An exception is raised when a vertex isn't mapped to an UV coordinate
isUVExportSuccess[indexUVSet] = false;
return null;
}
}

return null;
}

/// <summary>
/// Extract geometry (position, normal, UVs...) for a specific vertex
/// </summary>
Expand Down Expand Up @@ -907,37 +961,14 @@ private GlobalVertex ExtractVertex(MFnMesh mFnMesh, int polygonId, int vertexInd
}
}

// UV
int indexUVSet = 0;
if (uvSetNames.Count > indexUVSet && isUVExportSuccess[indexUVSet])
{
try
{
float u = 0, v = 0;
mFnMesh.getPolygonUV(polygonId, vertexIndexLocal, ref u, ref v, uvSetNames[indexUVSet]);
vertex.UV = new float[] { u, v };
}
catch
{
// An exception is raised when a vertex isn't mapped to an UV coordinate
isUVExportSuccess[indexUVSet] = false;
}
}
indexUVSet = 1;
if (uvSetNames.Count > indexUVSet && isUVExportSuccess[indexUVSet])
{
try
{
float u = 0, v = 0;
mFnMesh.getPolygonUV(polygonId, vertexIndexLocal, ref u, ref v, uvSetNames[indexUVSet]);
vertex.UV2 = new float[] { u, v };
}
catch
{
// An exception is raised when a vertex isn't mapped to an UV coordinate
isUVExportSuccess[indexUVSet] = false;
}
}
vertex.UV = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 0, uvSetNames, ref isUVExportSuccess); // UV
vertex.UV2 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 1, uvSetNames, ref isUVExportSuccess); // UV2
vertex.UV3 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 2, uvSetNames, ref isUVExportSuccess); // UV3
vertex.UV4 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 3, uvSetNames, ref isUVExportSuccess); // UV4
vertex.UV5 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 4, uvSetNames, ref isUVExportSuccess); // UV5
vertex.UV6 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 5, uvSetNames, ref isUVExportSuccess); // UV6
vertex.UV7 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 6, uvSetNames, ref isUVExportSuccess); // UV7
vertex.UV8 = ExtractVertexUV(mFnMesh, polygonId, vertexIndexLocal, 7, uvSetNames, ref isUVExportSuccess); // UV8

// skin
if (mFnSkinCluster != null)
Expand Down
18 changes: 18 additions & 0 deletions Maya/Exporter/GlobalVertex.cs
Expand Up @@ -11,6 +11,12 @@ public struct GlobalVertex
public float[] Tangent { get; set; } // Vec3
public float[] UV { get; set; } // Vec2
public float[] UV2 { get; set; } // Vec2
public float[] UV3 { get; set; } // Vec2
public float[] UV4 { get; set; } // Vec2
public float[] UV5 { get; set; } // Vec2
public float[] UV6 { get; set; } // Vec2
public float[] UV7 { get; set; } // Vec2
public float[] UV8 { get; set; } // Vec2
public int BonesIndices { get; set; }
public float[] Weights { get; set; } // Vec4
public int BonesIndicesExtra { get; set; }
Expand All @@ -26,6 +32,12 @@ public GlobalVertex(GlobalVertex other)
this.Tangent = other.Tangent;
this.UV = other.UV;
this.UV2 = other.UV2;
this.UV3 = other.UV3;
this.UV4 = other.UV4;
this.UV5 = other.UV5;
this.UV6 = other.UV6;
this.UV7 = other.UV7;
this.UV8 = other.UV8;
this.BonesIndices = other.BonesIndices;
this.Weights = other.Weights;
this.BonesIndicesExtra = other.BonesIndicesExtra;
Expand All @@ -49,6 +61,12 @@ public override bool Equals(object obj)
other.Normal.IsAlmostEqualTo(Normal, Tools.Epsilon) &
other.UV.IsAlmostEqualTo(UV, Tools.Epsilon) &&
other.UV2.IsAlmostEqualTo(UV2, Tools.Epsilon) &&
other.UV3.IsAlmostEqualTo(UV3, Tools.Epsilon) &&
other.UV4.IsAlmostEqualTo(UV4, Tools.Epsilon) &&
other.UV5.IsAlmostEqualTo(UV5, Tools.Epsilon) &&
other.UV6.IsAlmostEqualTo(UV6, Tools.Epsilon) &&
other.UV7.IsAlmostEqualTo(UV7, Tools.Epsilon) &&
other.UV8.IsAlmostEqualTo(UV8, Tools.Epsilon) &&
other.Weights.IsAlmostEqualTo(Weights, Tools.Epsilon) &&
other.WeightsExtra.IsAlmostEqualTo(WeightsExtra, Tools.Epsilon) &&
other.Color.IsAlmostEqualTo(Color, Tools.Epsilon) &&
Expand Down

0 comments on commit 9e01673

Please sign in to comment.